Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "xfs_log.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 21 | #include "xfs_inum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "xfs_sb.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 23 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_dir.h" |
| 25 | #include "xfs_dir2.h" |
| 26 | #include "xfs_trans.h" |
| 27 | #include "xfs_dmapi.h" |
| 28 | #include "xfs_mount.h" |
| 29 | #include "xfs_bmap_btree.h" |
| 30 | #include "xfs_alloc_btree.h" |
| 31 | #include "xfs_ialloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "xfs_dir_sf.h" |
| 33 | #include "xfs_dir2_sf.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 34 | #include "xfs_attr_sf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "xfs_dinode.h" |
| 36 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 37 | #include "xfs_alloc.h" |
| 38 | #include "xfs_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include "xfs_error.h" |
| 40 | #include "xfs_rw.h" |
| 41 | #include "xfs_iomap.h" |
| 42 | #include <linux/mpage.h> |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 43 | #include <linux/pagevec.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <linux/writeback.h> |
| 45 | |
| 46 | STATIC void xfs_count_page_state(struct page *, int *, int *, int *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 48 | #if defined(XFS_RW_TRACE) |
| 49 | void |
| 50 | xfs_page_trace( |
| 51 | int tag, |
| 52 | struct inode *inode, |
| 53 | struct page *page, |
| 54 | int mask) |
| 55 | { |
| 56 | xfs_inode_t *ip; |
| 57 | bhv_desc_t *bdp; |
| 58 | vnode_t *vp = LINVFS_GET_VP(inode); |
| 59 | loff_t isize = i_size_read(inode); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 60 | loff_t offset = page_offset(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | int delalloc = -1, unmapped = -1, unwritten = -1; |
| 62 | |
| 63 | if (page_has_buffers(page)) |
| 64 | xfs_count_page_state(page, &delalloc, &unmapped, &unwritten); |
| 65 | |
| 66 | bdp = vn_bhv_lookup(VN_BHV_HEAD(vp), &xfs_vnodeops); |
| 67 | ip = XFS_BHVTOI(bdp); |
| 68 | if (!ip->i_rwtrace) |
| 69 | return; |
| 70 | |
| 71 | ktrace_enter(ip->i_rwtrace, |
| 72 | (void *)((unsigned long)tag), |
| 73 | (void *)ip, |
| 74 | (void *)inode, |
| 75 | (void *)page, |
| 76 | (void *)((unsigned long)mask), |
| 77 | (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)), |
| 78 | (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)), |
| 79 | (void *)((unsigned long)((isize >> 32) & 0xffffffff)), |
| 80 | (void *)((unsigned long)(isize & 0xffffffff)), |
| 81 | (void *)((unsigned long)((offset >> 32) & 0xffffffff)), |
| 82 | (void *)((unsigned long)(offset & 0xffffffff)), |
| 83 | (void *)((unsigned long)delalloc), |
| 84 | (void *)((unsigned long)unmapped), |
| 85 | (void *)((unsigned long)unwritten), |
| 86 | (void *)NULL, |
| 87 | (void *)NULL); |
| 88 | } |
| 89 | #else |
| 90 | #define xfs_page_trace(tag, inode, page, mask) |
| 91 | #endif |
| 92 | |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 93 | /* |
| 94 | * Schedule IO completion handling on a xfsdatad if this was |
| 95 | * the final hold on this ioend. |
| 96 | */ |
| 97 | STATIC void |
| 98 | xfs_finish_ioend( |
| 99 | xfs_ioend_t *ioend) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 101 | if (atomic_dec_and_test(&ioend->io_remaining)) |
| 102 | queue_work(xfsdatad_workqueue, &ioend->io_work); |
| 103 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 105 | /* |
| 106 | * We're now finished for good with this ioend structure. |
| 107 | * Update the page state via the associated buffer_heads, |
| 108 | * release holds on the inode and bio, and finally free |
| 109 | * up memory. Do not use the ioend after this. |
| 110 | */ |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 111 | STATIC void |
| 112 | xfs_destroy_ioend( |
| 113 | xfs_ioend_t *ioend) |
| 114 | { |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 115 | struct buffer_head *bh, *next; |
| 116 | |
| 117 | for (bh = ioend->io_buffer_head; bh; bh = next) { |
| 118 | next = bh->b_private; |
| 119 | bh->b_end_io(bh, ioend->io_uptodate); |
| 120 | } |
| 121 | |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 122 | vn_iowake(ioend->io_vnode); |
| 123 | mempool_free(ioend, xfs_ioend_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /* |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 127 | * Buffered IO write completion for delayed allocate extents. |
| 128 | * TODO: Update ondisk isize now that we know the file data |
| 129 | * has been flushed (i.e. the notorious "NULL file" problem). |
| 130 | */ |
| 131 | STATIC void |
| 132 | xfs_end_bio_delalloc( |
| 133 | void *data) |
| 134 | { |
| 135 | xfs_ioend_t *ioend = data; |
| 136 | |
| 137 | xfs_destroy_ioend(ioend); |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Buffered IO write completion for regular, written extents. |
| 142 | */ |
| 143 | STATIC void |
| 144 | xfs_end_bio_written( |
| 145 | void *data) |
| 146 | { |
| 147 | xfs_ioend_t *ioend = data; |
| 148 | |
| 149 | xfs_destroy_ioend(ioend); |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * IO write completion for unwritten extents. |
| 154 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | * Issue transactions to convert a buffer range from unwritten |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 156 | * to written extents. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | */ |
| 158 | STATIC void |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 159 | xfs_end_bio_unwritten( |
| 160 | void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | { |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 162 | xfs_ioend_t *ioend = data; |
| 163 | vnode_t *vp = ioend->io_vnode; |
| 164 | xfs_off_t offset = ioend->io_offset; |
| 165 | size_t size = ioend->io_size; |
| 166 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 168 | if (ioend->io_uptodate) |
| 169 | VOP_BMAP(vp, offset, size, BMAPI_UNWRITTEN, NULL, NULL, error); |
| 170 | xfs_destroy_ioend(ioend); |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * Allocate and initialise an IO completion structure. |
| 175 | * We need to track unwritten extent write completion here initially. |
| 176 | * We'll need to extend this for updating the ondisk inode size later |
| 177 | * (vs. incore size). |
| 178 | */ |
| 179 | STATIC xfs_ioend_t * |
| 180 | xfs_alloc_ioend( |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 181 | struct inode *inode, |
| 182 | unsigned int type) |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 183 | { |
| 184 | xfs_ioend_t *ioend; |
| 185 | |
| 186 | ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS); |
| 187 | |
| 188 | /* |
| 189 | * Set the count to 1 initially, which will prevent an I/O |
| 190 | * completion callback from happening before we have started |
| 191 | * all the I/O from calling the completion routine too early. |
| 192 | */ |
| 193 | atomic_set(&ioend->io_remaining, 1); |
| 194 | ioend->io_uptodate = 1; /* cleared if any I/O fails */ |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 195 | ioend->io_list = NULL; |
| 196 | ioend->io_type = type; |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 197 | ioend->io_vnode = LINVFS_GET_VP(inode); |
Christoph Hellwig | c1a073b | 2005-09-05 08:23:35 +1000 | [diff] [blame] | 198 | ioend->io_buffer_head = NULL; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 199 | ioend->io_buffer_tail = NULL; |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 200 | atomic_inc(&ioend->io_vnode->v_iocount); |
| 201 | ioend->io_offset = 0; |
| 202 | ioend->io_size = 0; |
| 203 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 204 | if (type == IOMAP_UNWRITTEN) |
| 205 | INIT_WORK(&ioend->io_work, xfs_end_bio_unwritten, ioend); |
| 206 | else if (type == IOMAP_DELAY) |
| 207 | INIT_WORK(&ioend->io_work, xfs_end_bio_delalloc, ioend); |
| 208 | else |
| 209 | INIT_WORK(&ioend->io_work, xfs_end_bio_written, ioend); |
Christoph Hellwig | 0829c36 | 2005-09-02 16:58:49 +1000 | [diff] [blame] | 210 | |
| 211 | return ioend; |
| 212 | } |
| 213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | STATIC int |
| 215 | xfs_map_blocks( |
| 216 | struct inode *inode, |
| 217 | loff_t offset, |
| 218 | ssize_t count, |
| 219 | xfs_iomap_t *mapp, |
| 220 | int flags) |
| 221 | { |
| 222 | vnode_t *vp = LINVFS_GET_VP(inode); |
| 223 | int error, nmaps = 1; |
| 224 | |
| 225 | VOP_BMAP(vp, offset, count, flags, mapp, &nmaps, error); |
| 226 | if (!error && (flags & (BMAPI_WRITE|BMAPI_ALLOCATE))) |
| 227 | VMODIFY(vp); |
| 228 | return -error; |
| 229 | } |
| 230 | |
Christoph Hellwig | 1defeac | 2006-01-11 20:48:33 +1100 | [diff] [blame] | 231 | STATIC inline int |
| 232 | xfs_iomap_valid( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | xfs_iomap_t *iomapp, |
Christoph Hellwig | 1defeac | 2006-01-11 20:48:33 +1100 | [diff] [blame] | 234 | loff_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | { |
Christoph Hellwig | 1defeac | 2006-01-11 20:48:33 +1100 | [diff] [blame] | 236 | return offset >= iomapp->iomap_offset && |
| 237 | offset < iomapp->iomap_offset + iomapp->iomap_bsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 240 | /* |
| 241 | * BIO completion handler for buffered IO. |
| 242 | */ |
| 243 | STATIC int |
| 244 | xfs_end_bio( |
| 245 | struct bio *bio, |
| 246 | unsigned int bytes_done, |
| 247 | int error) |
| 248 | { |
| 249 | xfs_ioend_t *ioend = bio->bi_private; |
| 250 | |
| 251 | if (bio->bi_size) |
| 252 | return 1; |
| 253 | |
| 254 | ASSERT(ioend); |
| 255 | ASSERT(atomic_read(&bio->bi_cnt) >= 1); |
| 256 | |
| 257 | /* Toss bio and pass work off to an xfsdatad thread */ |
| 258 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) |
| 259 | ioend->io_uptodate = 0; |
| 260 | bio->bi_private = NULL; |
| 261 | bio->bi_end_io = NULL; |
| 262 | |
| 263 | bio_put(bio); |
| 264 | xfs_finish_ioend(ioend); |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | STATIC void |
| 269 | xfs_submit_ioend_bio( |
| 270 | xfs_ioend_t *ioend, |
| 271 | struct bio *bio) |
| 272 | { |
| 273 | atomic_inc(&ioend->io_remaining); |
| 274 | |
| 275 | bio->bi_private = ioend; |
| 276 | bio->bi_end_io = xfs_end_bio; |
| 277 | |
| 278 | submit_bio(WRITE, bio); |
| 279 | ASSERT(!bio_flagged(bio, BIO_EOPNOTSUPP)); |
| 280 | bio_put(bio); |
| 281 | } |
| 282 | |
| 283 | STATIC struct bio * |
| 284 | xfs_alloc_ioend_bio( |
| 285 | struct buffer_head *bh) |
| 286 | { |
| 287 | struct bio *bio; |
| 288 | int nvecs = bio_get_nr_vecs(bh->b_bdev); |
| 289 | |
| 290 | do { |
| 291 | bio = bio_alloc(GFP_NOIO, nvecs); |
| 292 | nvecs >>= 1; |
| 293 | } while (!bio); |
| 294 | |
| 295 | ASSERT(bio->bi_private == NULL); |
| 296 | bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9); |
| 297 | bio->bi_bdev = bh->b_bdev; |
| 298 | bio_get(bio); |
| 299 | return bio; |
| 300 | } |
| 301 | |
| 302 | STATIC void |
| 303 | xfs_start_buffer_writeback( |
| 304 | struct buffer_head *bh) |
| 305 | { |
| 306 | ASSERT(buffer_mapped(bh)); |
| 307 | ASSERT(buffer_locked(bh)); |
| 308 | ASSERT(!buffer_delay(bh)); |
| 309 | ASSERT(!buffer_unwritten(bh)); |
| 310 | |
| 311 | mark_buffer_async_write(bh); |
| 312 | set_buffer_uptodate(bh); |
| 313 | clear_buffer_dirty(bh); |
| 314 | } |
| 315 | |
| 316 | STATIC void |
| 317 | xfs_start_page_writeback( |
| 318 | struct page *page, |
| 319 | struct writeback_control *wbc, |
| 320 | int clear_dirty, |
| 321 | int buffers) |
| 322 | { |
| 323 | ASSERT(PageLocked(page)); |
| 324 | ASSERT(!PageWriteback(page)); |
| 325 | set_page_writeback(page); |
| 326 | if (clear_dirty) |
| 327 | clear_page_dirty(page); |
| 328 | unlock_page(page); |
| 329 | if (!buffers) { |
| 330 | end_page_writeback(page); |
| 331 | wbc->pages_skipped++; /* We didn't write this page */ |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | static inline int bio_add_buffer(struct bio *bio, struct buffer_head *bh) |
| 336 | { |
| 337 | return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh)); |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * Submit all of the bios for all of the ioends we have saved up, |
| 342 | * covering the initial writepage page and also any probed pages. |
| 343 | */ |
| 344 | STATIC void |
| 345 | xfs_submit_ioend( |
| 346 | xfs_ioend_t *ioend) |
| 347 | { |
| 348 | xfs_ioend_t *next; |
| 349 | struct buffer_head *bh; |
| 350 | struct bio *bio; |
| 351 | sector_t lastblock = 0; |
| 352 | |
| 353 | do { |
| 354 | next = ioend->io_list; |
| 355 | bio = NULL; |
| 356 | |
| 357 | for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) { |
| 358 | xfs_start_buffer_writeback(bh); |
| 359 | |
| 360 | if (!bio) { |
| 361 | retry: |
| 362 | bio = xfs_alloc_ioend_bio(bh); |
| 363 | } else if (bh->b_blocknr != lastblock + 1) { |
| 364 | xfs_submit_ioend_bio(ioend, bio); |
| 365 | goto retry; |
| 366 | } |
| 367 | |
| 368 | if (bio_add_buffer(bio, bh) != bh->b_size) { |
| 369 | xfs_submit_ioend_bio(ioend, bio); |
| 370 | goto retry; |
| 371 | } |
| 372 | |
| 373 | lastblock = bh->b_blocknr; |
| 374 | } |
| 375 | if (bio) |
| 376 | xfs_submit_ioend_bio(ioend, bio); |
| 377 | xfs_finish_ioend(ioend); |
| 378 | } while ((ioend = next) != NULL); |
| 379 | } |
| 380 | |
| 381 | /* |
| 382 | * Cancel submission of all buffer_heads so far in this endio. |
| 383 | * Toss the endio too. Only ever called for the initial page |
| 384 | * in a writepage request, so only ever one page. |
| 385 | */ |
| 386 | STATIC void |
| 387 | xfs_cancel_ioend( |
| 388 | xfs_ioend_t *ioend) |
| 389 | { |
| 390 | xfs_ioend_t *next; |
| 391 | struct buffer_head *bh, *next_bh; |
| 392 | |
| 393 | do { |
| 394 | next = ioend->io_list; |
| 395 | bh = ioend->io_buffer_head; |
| 396 | do { |
| 397 | next_bh = bh->b_private; |
| 398 | clear_buffer_async_write(bh); |
| 399 | unlock_buffer(bh); |
| 400 | } while ((bh = next_bh) != NULL); |
| 401 | |
| 402 | vn_iowake(ioend->io_vnode); |
| 403 | mempool_free(ioend, xfs_ioend_pool); |
| 404 | } while ((ioend = next) != NULL); |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | * Test to see if we've been building up a completion structure for |
| 409 | * earlier buffers -- if so, we try to append to this ioend if we |
| 410 | * can, otherwise we finish off any current ioend and start another. |
| 411 | * Return true if we've finished the given ioend. |
| 412 | */ |
| 413 | STATIC void |
| 414 | xfs_add_to_ioend( |
| 415 | struct inode *inode, |
| 416 | struct buffer_head *bh, |
Christoph Hellwig | 7336cea | 2006-01-11 20:49:16 +1100 | [diff] [blame] | 417 | xfs_off_t offset, |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 418 | unsigned int type, |
| 419 | xfs_ioend_t **result, |
| 420 | int need_ioend) |
| 421 | { |
| 422 | xfs_ioend_t *ioend = *result; |
| 423 | |
| 424 | if (!ioend || need_ioend || type != ioend->io_type) { |
| 425 | xfs_ioend_t *previous = *result; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 426 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 427 | ioend = xfs_alloc_ioend(inode, type); |
| 428 | ioend->io_offset = offset; |
| 429 | ioend->io_buffer_head = bh; |
| 430 | ioend->io_buffer_tail = bh; |
| 431 | if (previous) |
| 432 | previous->io_list = ioend; |
| 433 | *result = ioend; |
| 434 | } else { |
| 435 | ioend->io_buffer_tail->b_private = bh; |
| 436 | ioend->io_buffer_tail = bh; |
| 437 | } |
| 438 | |
| 439 | bh->b_private = NULL; |
| 440 | ioend->io_size += bh->b_size; |
| 441 | } |
| 442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | STATIC void |
| 444 | xfs_map_at_offset( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | struct buffer_head *bh, |
Christoph Hellwig | 1defeac | 2006-01-11 20:48:33 +1100 | [diff] [blame] | 446 | loff_t offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | int block_bits, |
Christoph Hellwig | 1defeac | 2006-01-11 20:48:33 +1100 | [diff] [blame] | 448 | xfs_iomap_t *iomapp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | { |
| 450 | xfs_daddr_t bn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | int sector_shift; |
| 452 | |
| 453 | ASSERT(!(iomapp->iomap_flags & IOMAP_HOLE)); |
| 454 | ASSERT(!(iomapp->iomap_flags & IOMAP_DELAY)); |
| 455 | ASSERT(iomapp->iomap_bn != IOMAP_DADDR_NULL); |
| 456 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | sector_shift = block_bits - BBSHIFT; |
Christoph Hellwig | 1defeac | 2006-01-11 20:48:33 +1100 | [diff] [blame] | 458 | bn = (iomapp->iomap_bn >> sector_shift) + |
| 459 | ((offset - iomapp->iomap_offset) >> block_bits); |
| 460 | |
| 461 | ASSERT(bn || (iomapp->iomap_flags & IOMAP_REALTIME)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | ASSERT((bn << sector_shift) >= iomapp->iomap_bn); |
| 463 | |
| 464 | lock_buffer(bh); |
| 465 | bh->b_blocknr = bn; |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 466 | bh->b_bdev = iomapp->iomap_target->bt_bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | set_buffer_mapped(bh); |
| 468 | clear_buffer_delay(bh); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 469 | clear_buffer_unwritten(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | /* |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 473 | * Look for a page at index that is suitable for clustering. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | */ |
| 475 | STATIC unsigned int |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 476 | xfs_probe_page( |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 477 | struct page *page, |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 478 | unsigned int pg_offset, |
| 479 | int mapped) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | int ret = 0; |
| 482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | if (PageWriteback(page)) |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 484 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
| 486 | if (page->mapping && PageDirty(page)) { |
| 487 | if (page_has_buffers(page)) { |
| 488 | struct buffer_head *bh, *head; |
| 489 | |
| 490 | bh = head = page_buffers(page); |
| 491 | do { |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 492 | if (!buffer_uptodate(bh)) |
| 493 | break; |
| 494 | if (mapped != buffer_mapped(bh)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | break; |
| 496 | ret += bh->b_size; |
| 497 | if (ret >= pg_offset) |
| 498 | break; |
| 499 | } while ((bh = bh->b_this_page) != head); |
| 500 | } else |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 501 | ret = mapped ? 0 : PAGE_CACHE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | return ret; |
| 505 | } |
| 506 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 507 | STATIC size_t |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 508 | xfs_probe_cluster( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | struct inode *inode, |
| 510 | struct page *startpage, |
| 511 | struct buffer_head *bh, |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 512 | struct buffer_head *head, |
| 513 | int mapped) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | { |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 515 | struct pagevec pvec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | pgoff_t tindex, tlast, tloff; |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 517 | size_t total = 0; |
| 518 | int done = 0, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | |
| 520 | /* First sum forwards in this page */ |
| 521 | do { |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 522 | if (mapped != buffer_mapped(bh)) |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 523 | return total; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | total += bh->b_size; |
| 525 | } while ((bh = bh->b_this_page) != head); |
| 526 | |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 527 | /* if we reached the end of the page, sum forwards in following pages */ |
| 528 | tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT; |
| 529 | tindex = startpage->index + 1; |
| 530 | |
| 531 | /* Prune this back to avoid pathological behavior */ |
| 532 | tloff = min(tlast, startpage->index + 64); |
| 533 | |
| 534 | pagevec_init(&pvec, 0); |
| 535 | while (!done && tindex <= tloff) { |
| 536 | unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1); |
| 537 | |
| 538 | if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len)) |
| 539 | break; |
| 540 | |
| 541 | for (i = 0; i < pagevec_count(&pvec); i++) { |
| 542 | struct page *page = pvec.pages[i]; |
| 543 | size_t pg_offset, len = 0; |
| 544 | |
| 545 | if (tindex == tlast) { |
| 546 | pg_offset = |
| 547 | i_size_read(inode) & (PAGE_CACHE_SIZE - 1); |
Christoph Hellwig | 1defeac | 2006-01-11 20:48:33 +1100 | [diff] [blame] | 548 | if (!pg_offset) { |
| 549 | done = 1; |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 550 | break; |
Christoph Hellwig | 1defeac | 2006-01-11 20:48:33 +1100 | [diff] [blame] | 551 | } |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 552 | } else |
| 553 | pg_offset = PAGE_CACHE_SIZE; |
| 554 | |
| 555 | if (page->index == tindex && !TestSetPageLocked(page)) { |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 556 | len = xfs_probe_page(page, pg_offset, mapped); |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 557 | unlock_page(page); |
| 558 | } |
| 559 | |
| 560 | if (!len) { |
| 561 | done = 1; |
| 562 | break; |
| 563 | } |
| 564 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | total += len; |
Christoph Hellwig | 1defeac | 2006-01-11 20:48:33 +1100 | [diff] [blame] | 566 | tindex++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | } |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 568 | |
| 569 | pagevec_release(&pvec); |
| 570 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | } |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 572 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | return total; |
| 574 | } |
| 575 | |
| 576 | /* |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 577 | * Test if a given page is suitable for writing as part of an unwritten |
| 578 | * or delayed allocate extent. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | */ |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 580 | STATIC int |
| 581 | xfs_is_delayed_page( |
| 582 | struct page *page, |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 583 | unsigned int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | if (PageWriteback(page)) |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 586 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | |
| 588 | if (page->mapping && page_has_buffers(page)) { |
| 589 | struct buffer_head *bh, *head; |
| 590 | int acceptable = 0; |
| 591 | |
| 592 | bh = head = page_buffers(page); |
| 593 | do { |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 594 | if (buffer_unwritten(bh)) |
| 595 | acceptable = (type == IOMAP_UNWRITTEN); |
| 596 | else if (buffer_delay(bh)) |
| 597 | acceptable = (type == IOMAP_DELAY); |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 598 | else if (buffer_mapped(bh)) |
| 599 | acceptable = (type == 0); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 600 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | } while ((bh = bh->b_this_page) != head); |
| 603 | |
| 604 | if (acceptable) |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 605 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | } |
| 607 | |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 608 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | } |
| 610 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | /* |
| 612 | * Allocate & map buffers for page given the extent map. Write it out. |
| 613 | * except for the original page of a writepage, this is called on |
| 614 | * delalloc/unwritten pages only, for the original page it is possible |
| 615 | * that the page has no mapping at all. |
| 616 | */ |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 617 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | xfs_convert_page( |
| 619 | struct inode *inode, |
| 620 | struct page *page, |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 621 | loff_t tindex, |
Christoph Hellwig | 1defeac | 2006-01-11 20:48:33 +1100 | [diff] [blame] | 622 | xfs_iomap_t *mp, |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 623 | xfs_ioend_t **ioendp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | struct writeback_control *wbc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | int startio, |
| 626 | int all_bh) |
| 627 | { |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 628 | struct buffer_head *bh, *head; |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 629 | xfs_off_t end_offset; |
| 630 | unsigned long p_offset; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 631 | unsigned int type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | int bbits = inode->i_blkbits; |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 633 | int len, page_dirty; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 634 | int count = 0, done = 0, uptodate = 1; |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 635 | xfs_off_t offset = page_offset(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 637 | if (page->index != tindex) |
| 638 | goto fail; |
| 639 | if (TestSetPageLocked(page)) |
| 640 | goto fail; |
| 641 | if (PageWriteback(page)) |
| 642 | goto fail_unlock_page; |
| 643 | if (page->mapping != inode->i_mapping) |
| 644 | goto fail_unlock_page; |
| 645 | if (!xfs_is_delayed_page(page, (*ioendp)->io_type)) |
| 646 | goto fail_unlock_page; |
| 647 | |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 648 | /* |
| 649 | * page_dirty is initially a count of buffers on the page before |
| 650 | * EOF and is decrememted as we move each into a cleanable state. |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 651 | * |
| 652 | * Derivation: |
| 653 | * |
| 654 | * End offset is the highest offset that this page should represent. |
| 655 | * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1)) |
| 656 | * will evaluate non-zero and be less than PAGE_CACHE_SIZE and |
| 657 | * hence give us the correct page_dirty count. On any other page, |
| 658 | * it will be zero and in that case we need page_dirty to be the |
| 659 | * count of buffers on the page. |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 660 | */ |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 661 | end_offset = min_t(unsigned long long, |
| 662 | (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, |
| 663 | i_size_read(inode)); |
| 664 | |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 665 | len = 1 << inode->i_blkbits; |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 666 | p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1), |
| 667 | PAGE_CACHE_SIZE); |
| 668 | p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE; |
| 669 | page_dirty = p_offset / len; |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 670 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | bh = head = page_buffers(page); |
| 672 | do { |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 673 | if (offset >= end_offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | break; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 675 | if (!buffer_uptodate(bh)) |
| 676 | uptodate = 0; |
| 677 | if (!(PageUptodate(page) || buffer_uptodate(bh))) { |
| 678 | done = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | continue; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 680 | } |
| 681 | |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 682 | if (buffer_unwritten(bh) || buffer_delay(bh)) { |
| 683 | if (buffer_unwritten(bh)) |
| 684 | type = IOMAP_UNWRITTEN; |
| 685 | else |
| 686 | type = IOMAP_DELAY; |
| 687 | |
| 688 | if (!xfs_iomap_valid(mp, offset)) { |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 689 | done = 1; |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 690 | continue; |
| 691 | } |
| 692 | |
| 693 | ASSERT(!(mp->iomap_flags & IOMAP_HOLE)); |
| 694 | ASSERT(!(mp->iomap_flags & IOMAP_DELAY)); |
| 695 | |
| 696 | xfs_map_at_offset(bh, offset, bbits, mp); |
| 697 | if (startio) { |
Christoph Hellwig | 7336cea | 2006-01-11 20:49:16 +1100 | [diff] [blame] | 698 | xfs_add_to_ioend(inode, bh, offset, |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 699 | type, ioendp, done); |
| 700 | } else { |
| 701 | set_buffer_dirty(bh); |
| 702 | unlock_buffer(bh); |
| 703 | mark_buffer_dirty(bh); |
| 704 | } |
| 705 | page_dirty--; |
| 706 | count++; |
| 707 | } else { |
| 708 | type = 0; |
| 709 | if (buffer_mapped(bh) && all_bh && startio) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | lock_buffer(bh); |
Christoph Hellwig | 7336cea | 2006-01-11 20:49:16 +1100 | [diff] [blame] | 711 | xfs_add_to_ioend(inode, bh, offset, |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 712 | type, ioendp, done); |
| 713 | count++; |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 714 | page_dirty--; |
Christoph Hellwig | 9260dc6 | 2006-01-11 20:48:47 +1100 | [diff] [blame] | 715 | } else { |
| 716 | done = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | } |
Christoph Hellwig | 7336cea | 2006-01-11 20:49:16 +1100 | [diff] [blame] | 719 | } while (offset += len, (bh = bh->b_this_page) != head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 721 | if (uptodate && bh == head) |
| 722 | SetPageUptodate(page); |
| 723 | |
| 724 | if (startio) { |
Christoph Hellwig | f5e596b | 2006-01-11 20:49:42 +1100 | [diff] [blame^] | 725 | if (count) { |
| 726 | struct backing_dev_info *bdi; |
| 727 | |
| 728 | bdi = inode->i_mapping->backing_dev_info; |
| 729 | if (bdi_write_congested(bdi)) { |
| 730 | wbc->encountered_congestion = 1; |
| 731 | done = 1; |
| 732 | } else if (--wbc->nr_to_write <= 0) { |
| 733 | done = 1; |
| 734 | } |
| 735 | } |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 736 | xfs_start_page_writeback(page, wbc, !page_dirty, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | } |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 738 | |
| 739 | return done; |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 740 | fail_unlock_page: |
| 741 | unlock_page(page); |
| 742 | fail: |
| 743 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | /* |
| 747 | * Convert & write out a cluster of pages in the same extent as defined |
| 748 | * by mp and following the start page. |
| 749 | */ |
| 750 | STATIC void |
| 751 | xfs_cluster_write( |
| 752 | struct inode *inode, |
| 753 | pgoff_t tindex, |
| 754 | xfs_iomap_t *iomapp, |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 755 | xfs_ioend_t **ioendp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | struct writeback_control *wbc, |
| 757 | int startio, |
| 758 | int all_bh, |
| 759 | pgoff_t tlast) |
| 760 | { |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 761 | struct pagevec pvec; |
| 762 | int done = 0, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 764 | pagevec_init(&pvec, 0); |
| 765 | while (!done && tindex <= tlast) { |
| 766 | unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1); |
| 767 | |
| 768 | if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | break; |
Christoph Hellwig | 10ce444 | 2006-01-11 20:48:14 +1100 | [diff] [blame] | 770 | |
| 771 | for (i = 0; i < pagevec_count(&pvec); i++) { |
| 772 | done = xfs_convert_page(inode, pvec.pages[i], tindex++, |
| 773 | iomapp, ioendp, wbc, startio, all_bh); |
| 774 | if (done) |
| 775 | break; |
| 776 | } |
| 777 | |
| 778 | pagevec_release(&pvec); |
| 779 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | |
| 783 | /* |
| 784 | * Calling this without startio set means we are being asked to make a dirty |
| 785 | * page ready for freeing it's buffers. When called with startio set then |
| 786 | * we are coming from writepage. |
| 787 | * |
| 788 | * When called with startio set it is important that we write the WHOLE |
| 789 | * page if possible. |
| 790 | * The bh->b_state's cannot know if any of the blocks or which block for |
| 791 | * that matter are dirty due to mmap writes, and therefore bh uptodate is |
| 792 | * only vaild if the page itself isn't completely uptodate. Some layers |
| 793 | * may clear the page dirty flag prior to calling write page, under the |
| 794 | * assumption the entire page will be written out; by not writing out the |
| 795 | * whole page the page can be reused before all valid dirty data is |
| 796 | * written out. Note: in the case of a page that has been dirty'd by |
| 797 | * mapwrite and but partially setup by block_prepare_write the |
| 798 | * bh->b_states's will not agree and only ones setup by BPW/BCW will have |
| 799 | * valid state, thus the whole page must be written out thing. |
| 800 | */ |
| 801 | |
| 802 | STATIC int |
| 803 | xfs_page_state_convert( |
| 804 | struct inode *inode, |
| 805 | struct page *page, |
| 806 | struct writeback_control *wbc, |
| 807 | int startio, |
| 808 | int unmapped) /* also implies page uptodate */ |
| 809 | { |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 810 | struct buffer_head *bh, *head; |
Christoph Hellwig | 1defeac | 2006-01-11 20:48:33 +1100 | [diff] [blame] | 811 | xfs_iomap_t iomap; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 812 | xfs_ioend_t *ioend = NULL, *iohead = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | loff_t offset; |
| 814 | unsigned long p_offset = 0; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 815 | unsigned int type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | __uint64_t end_offset; |
| 817 | pgoff_t end_index, last_index, tlast; |
Christoph Hellwig | d5cb48a | 2006-01-11 20:49:02 +1100 | [diff] [blame] | 818 | ssize_t size, len; |
| 819 | int flags, err, iomap_valid = 0, uptodate = 1; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 820 | int page_dirty, count = 0, trylock_flag = 0; |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 821 | int all_bh = unmapped; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | |
Daniel Moore | 3ba0815 | 2005-05-05 13:31:34 -0700 | [diff] [blame] | 823 | /* wait for other IO threads? */ |
Christoph Hellwig | f5e596b | 2006-01-11 20:49:42 +1100 | [diff] [blame^] | 824 | if (startio && (wbc->sync_mode == WB_SYNC_NONE && wbc->nonblocking)) |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 825 | trylock_flag |= BMAPI_TRYLOCK; |
Daniel Moore | 3ba0815 | 2005-05-05 13:31:34 -0700 | [diff] [blame] | 826 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | /* Is this page beyond the end of the file? */ |
| 828 | offset = i_size_read(inode); |
| 829 | end_index = offset >> PAGE_CACHE_SHIFT; |
| 830 | last_index = (offset - 1) >> PAGE_CACHE_SHIFT; |
| 831 | if (page->index >= end_index) { |
| 832 | if ((page->index >= end_index + 1) || |
| 833 | !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) { |
Nathan Scott | 19d5bcf | 2005-11-02 15:14:09 +1100 | [diff] [blame] | 834 | if (startio) |
| 835 | unlock_page(page); |
| 836 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | /* |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 841 | * page_dirty is initially a count of buffers on the page before |
| 842 | * EOF and is decrememted as we move each into a cleanable state. |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 843 | * |
| 844 | * Derivation: |
| 845 | * |
| 846 | * End offset is the highest offset that this page should represent. |
| 847 | * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1)) |
| 848 | * will evaluate non-zero and be less than PAGE_CACHE_SIZE and |
| 849 | * hence give us the correct page_dirty count. On any other page, |
| 850 | * it will be zero and in that case we need page_dirty to be the |
| 851 | * count of buffers on the page. |
| 852 | */ |
| 853 | end_offset = min_t(unsigned long long, |
| 854 | (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, offset); |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 855 | len = 1 << inode->i_blkbits; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 856 | p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1), |
| 857 | PAGE_CACHE_SIZE); |
| 858 | p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE; |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 859 | page_dirty = p_offset / len; |
| 860 | |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 861 | bh = head = page_buffers(page); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 862 | offset = page_offset(page); |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 863 | flags = -1; |
| 864 | type = 0; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 865 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 866 | /* TODO: cleanup count and page_dirty */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | |
| 868 | do { |
| 869 | if (offset >= end_offset) |
| 870 | break; |
| 871 | if (!buffer_uptodate(bh)) |
| 872 | uptodate = 0; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 873 | if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio) { |
Christoph Hellwig | 1defeac | 2006-01-11 20:48:33 +1100 | [diff] [blame] | 874 | /* |
| 875 | * the iomap is actually still valid, but the ioend |
| 876 | * isn't. shouldn't happen too often. |
| 877 | */ |
| 878 | iomap_valid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | continue; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 880 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | |
Christoph Hellwig | 1defeac | 2006-01-11 20:48:33 +1100 | [diff] [blame] | 882 | if (iomap_valid) |
| 883 | iomap_valid = xfs_iomap_valid(&iomap, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
| 885 | /* |
| 886 | * First case, map an unwritten extent and prepare for |
| 887 | * extent state conversion transaction on completion. |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 888 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | * Second case, allocate space for a delalloc buffer. |
| 890 | * We can return EAGAIN here in the release page case. |
Christoph Hellwig | d5cb48a | 2006-01-11 20:49:02 +1100 | [diff] [blame] | 891 | * |
| 892 | * Third case, an unmapped buffer was found, and we are |
| 893 | * in a path where we need to write the whole page out. |
| 894 | */ |
| 895 | if (buffer_unwritten(bh) || buffer_delay(bh) || |
| 896 | ((buffer_uptodate(bh) || PageUptodate(page)) && |
| 897 | !buffer_mapped(bh) && (unmapped || startio))) { |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 898 | /* |
| 899 | * Make sure we don't use a read-only iomap |
| 900 | */ |
| 901 | if (flags == BMAPI_READ) |
| 902 | iomap_valid = 0; |
| 903 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 904 | if (buffer_unwritten(bh)) { |
| 905 | type = IOMAP_UNWRITTEN; |
| 906 | flags = BMAPI_WRITE|BMAPI_IGNSTATE; |
Christoph Hellwig | d5cb48a | 2006-01-11 20:49:02 +1100 | [diff] [blame] | 907 | } else if (buffer_delay(bh)) { |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 908 | type = IOMAP_DELAY; |
| 909 | flags = BMAPI_ALLOCATE; |
| 910 | if (!startio) |
| 911 | flags |= trylock_flag; |
Christoph Hellwig | d5cb48a | 2006-01-11 20:49:02 +1100 | [diff] [blame] | 912 | } else { |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 913 | type = IOMAP_NEW; |
Christoph Hellwig | d5cb48a | 2006-01-11 20:49:02 +1100 | [diff] [blame] | 914 | flags = BMAPI_WRITE|BMAPI_MMAP; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 915 | } |
| 916 | |
Christoph Hellwig | 1defeac | 2006-01-11 20:48:33 +1100 | [diff] [blame] | 917 | if (!iomap_valid) { |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 918 | if (type == IOMAP_NEW) { |
| 919 | size = xfs_probe_cluster(inode, |
| 920 | page, bh, head, 0); |
Christoph Hellwig | d5cb48a | 2006-01-11 20:49:02 +1100 | [diff] [blame] | 921 | } else { |
| 922 | size = len; |
| 923 | } |
| 924 | |
| 925 | err = xfs_map_blocks(inode, offset, size, |
| 926 | &iomap, flags); |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 927 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | goto error; |
Christoph Hellwig | 1defeac | 2006-01-11 20:48:33 +1100 | [diff] [blame] | 929 | iomap_valid = xfs_iomap_valid(&iomap, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | } |
Christoph Hellwig | 1defeac | 2006-01-11 20:48:33 +1100 | [diff] [blame] | 931 | if (iomap_valid) { |
| 932 | xfs_map_at_offset(bh, offset, |
| 933 | inode->i_blkbits, &iomap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | if (startio) { |
Christoph Hellwig | 7336cea | 2006-01-11 20:49:16 +1100 | [diff] [blame] | 935 | xfs_add_to_ioend(inode, bh, offset, |
Christoph Hellwig | 1defeac | 2006-01-11 20:48:33 +1100 | [diff] [blame] | 936 | type, &ioend, |
| 937 | !iomap_valid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | } else { |
| 939 | set_buffer_dirty(bh); |
| 940 | unlock_buffer(bh); |
| 941 | mark_buffer_dirty(bh); |
| 942 | } |
| 943 | page_dirty--; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 944 | count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | } |
Christoph Hellwig | d5cb48a | 2006-01-11 20:49:02 +1100 | [diff] [blame] | 946 | } else if (buffer_uptodate(bh) && startio) { |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 947 | /* |
| 948 | * we got here because the buffer is already mapped. |
| 949 | * That means it must already have extents allocated |
| 950 | * underneath it. Map the extent by reading it. |
| 951 | */ |
| 952 | if (!iomap_valid || type != 0) { |
| 953 | flags = BMAPI_READ; |
| 954 | size = xfs_probe_cluster(inode, page, bh, |
| 955 | head, 1); |
| 956 | err = xfs_map_blocks(inode, offset, size, |
| 957 | &iomap, flags); |
| 958 | if (err) |
| 959 | goto error; |
| 960 | iomap_valid = xfs_iomap_valid(&iomap, offset); |
| 961 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 963 | type = 0; |
Christoph Hellwig | d5cb48a | 2006-01-11 20:49:02 +1100 | [diff] [blame] | 964 | if (!test_and_set_bit(BH_Lock, &bh->b_state)) { |
| 965 | ASSERT(buffer_mapped(bh)); |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 966 | if (iomap_valid) |
| 967 | all_bh = 1; |
Christoph Hellwig | 7336cea | 2006-01-11 20:49:16 +1100 | [diff] [blame] | 968 | xfs_add_to_ioend(inode, bh, offset, type, |
Christoph Hellwig | d5cb48a | 2006-01-11 20:49:02 +1100 | [diff] [blame] | 969 | &ioend, !iomap_valid); |
| 970 | page_dirty--; |
| 971 | count++; |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 972 | } else { |
Christoph Hellwig | 1defeac | 2006-01-11 20:48:33 +1100 | [diff] [blame] | 973 | iomap_valid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | } |
Christoph Hellwig | d5cb48a | 2006-01-11 20:49:02 +1100 | [diff] [blame] | 975 | } else if ((buffer_uptodate(bh) || PageUptodate(page)) && |
| 976 | (unmapped || startio)) { |
| 977 | iomap_valid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | } |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 979 | |
| 980 | if (!iohead) |
| 981 | iohead = ioend; |
| 982 | |
| 983 | } while (offset += len, ((bh = bh->b_this_page) != head)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | |
| 985 | if (uptodate && bh == head) |
| 986 | SetPageUptodate(page); |
| 987 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 988 | if (startio) |
| 989 | xfs_start_page_writeback(page, wbc, 1, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | |
Christoph Hellwig | 1defeac | 2006-01-11 20:48:33 +1100 | [diff] [blame] | 991 | if (ioend && iomap_valid) { |
| 992 | offset = (iomap.iomap_offset + iomap.iomap_bsize - 1) >> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | PAGE_CACHE_SHIFT; |
Nathan Scott | 775bf6c | 2005-05-05 13:33:01 -0700 | [diff] [blame] | 994 | tlast = min_t(pgoff_t, offset, last_index); |
Christoph Hellwig | 1defeac | 2006-01-11 20:48:33 +1100 | [diff] [blame] | 995 | xfs_cluster_write(inode, page->index + 1, &iomap, &ioend, |
Christoph Hellwig | 6c4fe19 | 2006-01-11 20:49:28 +1100 | [diff] [blame] | 996 | wbc, startio, all_bh, tlast); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | } |
| 998 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 999 | if (iohead) |
| 1000 | xfs_submit_ioend(iohead); |
| 1001 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | return page_dirty; |
| 1003 | |
| 1004 | error: |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1005 | if (iohead) |
| 1006 | xfs_cancel_ioend(iohead); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | |
| 1008 | /* |
| 1009 | * If it's delalloc and we have nowhere to put it, |
| 1010 | * throw it away, unless the lower layers told |
| 1011 | * us to try again. |
| 1012 | */ |
| 1013 | if (err != -EAGAIN) { |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1014 | if (!unmapped) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | block_invalidatepage(page, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | ClearPageUptodate(page); |
| 1017 | } |
| 1018 | return err; |
| 1019 | } |
| 1020 | |
| 1021 | STATIC int |
| 1022 | __linvfs_get_block( |
| 1023 | struct inode *inode, |
| 1024 | sector_t iblock, |
| 1025 | unsigned long blocks, |
| 1026 | struct buffer_head *bh_result, |
| 1027 | int create, |
| 1028 | int direct, |
| 1029 | bmapi_flags_t flags) |
| 1030 | { |
| 1031 | vnode_t *vp = LINVFS_GET_VP(inode); |
| 1032 | xfs_iomap_t iomap; |
Nathan Scott | fdc7ed7 | 2005-11-02 15:13:13 +1100 | [diff] [blame] | 1033 | xfs_off_t offset; |
| 1034 | ssize_t size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | int retpbbm = 1; |
| 1036 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | |
Nathan Scott | fdc7ed7 | 2005-11-02 15:13:13 +1100 | [diff] [blame] | 1038 | offset = (xfs_off_t)iblock << inode->i_blkbits; |
Nathan Scott | a465639 | 2005-11-25 16:41:57 +1100 | [diff] [blame] | 1039 | if (blocks) |
| 1040 | size = (ssize_t) min_t(xfs_off_t, LONG_MAX, |
| 1041 | (xfs_off_t)blocks << inode->i_blkbits); |
| 1042 | else |
| 1043 | size = 1 << inode->i_blkbits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | |
| 1045 | VOP_BMAP(vp, offset, size, |
| 1046 | create ? flags : BMAPI_READ, &iomap, &retpbbm, error); |
| 1047 | if (error) |
| 1048 | return -error; |
| 1049 | |
| 1050 | if (retpbbm == 0) |
| 1051 | return 0; |
| 1052 | |
| 1053 | if (iomap.iomap_bn != IOMAP_DADDR_NULL) { |
Nathan Scott | fdc7ed7 | 2005-11-02 15:13:13 +1100 | [diff] [blame] | 1054 | xfs_daddr_t bn; |
| 1055 | xfs_off_t delta; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | |
| 1057 | /* For unwritten extents do not report a disk address on |
| 1058 | * the read case (treat as if we're reading into a hole). |
| 1059 | */ |
| 1060 | if (create || !(iomap.iomap_flags & IOMAP_UNWRITTEN)) { |
| 1061 | delta = offset - iomap.iomap_offset; |
| 1062 | delta >>= inode->i_blkbits; |
| 1063 | |
| 1064 | bn = iomap.iomap_bn >> (inode->i_blkbits - BBSHIFT); |
| 1065 | bn += delta; |
| 1066 | BUG_ON(!bn && !(iomap.iomap_flags & IOMAP_REALTIME)); |
| 1067 | bh_result->b_blocknr = bn; |
| 1068 | set_buffer_mapped(bh_result); |
| 1069 | } |
| 1070 | if (create && (iomap.iomap_flags & IOMAP_UNWRITTEN)) { |
| 1071 | if (direct) |
| 1072 | bh_result->b_private = inode; |
| 1073 | set_buffer_unwritten(bh_result); |
| 1074 | set_buffer_delay(bh_result); |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | /* If this is a realtime file, data might be on a new device */ |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1079 | bh_result->b_bdev = iomap.iomap_target->bt_bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | |
| 1081 | /* If we previously allocated a block out beyond eof and |
| 1082 | * we are now coming back to use it then we will need to |
| 1083 | * flag it as new even if it has a disk address. |
| 1084 | */ |
| 1085 | if (create && |
| 1086 | ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) || |
Nathan Scott | fdc7ed7 | 2005-11-02 15:13:13 +1100 | [diff] [blame] | 1087 | (offset >= i_size_read(inode)) || (iomap.iomap_flags & IOMAP_NEW))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | set_buffer_new(bh_result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | |
| 1090 | if (iomap.iomap_flags & IOMAP_DELAY) { |
| 1091 | BUG_ON(direct); |
| 1092 | if (create) { |
| 1093 | set_buffer_uptodate(bh_result); |
| 1094 | set_buffer_mapped(bh_result); |
| 1095 | set_buffer_delay(bh_result); |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | if (blocks) { |
Nathan Scott | fdc7ed7 | 2005-11-02 15:13:13 +1100 | [diff] [blame] | 1100 | ASSERT(iomap.iomap_bsize - iomap.iomap_delta > 0); |
| 1101 | offset = min_t(xfs_off_t, |
| 1102 | iomap.iomap_bsize - iomap.iomap_delta, |
Nathan Scott | a465639 | 2005-11-25 16:41:57 +1100 | [diff] [blame] | 1103 | (xfs_off_t)blocks << inode->i_blkbits); |
Nathan Scott | fdc7ed7 | 2005-11-02 15:13:13 +1100 | [diff] [blame] | 1104 | bh_result->b_size = (u32) min_t(xfs_off_t, UINT_MAX, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | return 0; |
| 1108 | } |
| 1109 | |
| 1110 | int |
| 1111 | linvfs_get_block( |
| 1112 | struct inode *inode, |
| 1113 | sector_t iblock, |
| 1114 | struct buffer_head *bh_result, |
| 1115 | int create) |
| 1116 | { |
| 1117 | return __linvfs_get_block(inode, iblock, 0, bh_result, |
| 1118 | create, 0, BMAPI_WRITE); |
| 1119 | } |
| 1120 | |
| 1121 | STATIC int |
| 1122 | linvfs_get_blocks_direct( |
| 1123 | struct inode *inode, |
| 1124 | sector_t iblock, |
| 1125 | unsigned long max_blocks, |
| 1126 | struct buffer_head *bh_result, |
| 1127 | int create) |
| 1128 | { |
| 1129 | return __linvfs_get_block(inode, iblock, max_blocks, bh_result, |
| 1130 | create, 1, BMAPI_WRITE|BMAPI_DIRECT); |
| 1131 | } |
| 1132 | |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1133 | STATIC void |
| 1134 | linvfs_end_io_direct( |
| 1135 | struct kiocb *iocb, |
| 1136 | loff_t offset, |
| 1137 | ssize_t size, |
| 1138 | void *private) |
| 1139 | { |
| 1140 | xfs_ioend_t *ioend = iocb->private; |
| 1141 | |
| 1142 | /* |
| 1143 | * Non-NULL private data means we need to issue a transaction to |
| 1144 | * convert a range from unwritten to written extents. This needs |
| 1145 | * to happen from process contect but aio+dio I/O completion |
| 1146 | * happens from irq context so we need to defer it to a workqueue. |
| 1147 | * This is not nessecary for synchronous direct I/O, but we do |
| 1148 | * it anyway to keep the code uniform and simpler. |
| 1149 | * |
| 1150 | * The core direct I/O code might be changed to always call the |
| 1151 | * completion handler in the future, in which case all this can |
| 1152 | * go away. |
| 1153 | */ |
| 1154 | if (private && size > 0) { |
| 1155 | ioend->io_offset = offset; |
| 1156 | ioend->io_size = size; |
| 1157 | xfs_finish_ioend(ioend); |
| 1158 | } else { |
| 1159 | ASSERT(size >= 0); |
| 1160 | xfs_destroy_ioend(ioend); |
| 1161 | } |
| 1162 | |
| 1163 | /* |
| 1164 | * blockdev_direct_IO can return an error even afer the I/O |
| 1165 | * completion handler was called. Thus we need to protect |
| 1166 | * against double-freeing. |
| 1167 | */ |
| 1168 | iocb->private = NULL; |
| 1169 | } |
| 1170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | STATIC ssize_t |
| 1172 | linvfs_direct_IO( |
| 1173 | int rw, |
| 1174 | struct kiocb *iocb, |
| 1175 | const struct iovec *iov, |
| 1176 | loff_t offset, |
| 1177 | unsigned long nr_segs) |
| 1178 | { |
| 1179 | struct file *file = iocb->ki_filp; |
| 1180 | struct inode *inode = file->f_mapping->host; |
| 1181 | vnode_t *vp = LINVFS_GET_VP(inode); |
| 1182 | xfs_iomap_t iomap; |
| 1183 | int maps = 1; |
| 1184 | int error; |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1185 | ssize_t ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | |
| 1187 | VOP_BMAP(vp, offset, 0, BMAPI_DEVICE, &iomap, &maps, error); |
| 1188 | if (error) |
| 1189 | return -error; |
| 1190 | |
Christoph Hellwig | f6d6d4f | 2006-01-11 15:40:13 +1100 | [diff] [blame] | 1191 | iocb->private = xfs_alloc_ioend(inode, IOMAP_UNWRITTEN); |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1192 | |
| 1193 | ret = blockdev_direct_IO_own_locking(rw, iocb, inode, |
Nathan Scott | ce8e922 | 2006-01-11 15:39:08 +1100 | [diff] [blame] | 1194 | iomap.iomap_target->bt_bdev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | iov, offset, nr_segs, |
| 1196 | linvfs_get_blocks_direct, |
Christoph Hellwig | f097386 | 2005-09-05 08:22:52 +1000 | [diff] [blame] | 1197 | linvfs_end_io_direct); |
| 1198 | |
| 1199 | if (unlikely(ret <= 0 && iocb->private)) |
| 1200 | xfs_destroy_ioend(iocb->private); |
| 1201 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | |
| 1205 | STATIC sector_t |
| 1206 | linvfs_bmap( |
| 1207 | struct address_space *mapping, |
| 1208 | sector_t block) |
| 1209 | { |
| 1210 | struct inode *inode = (struct inode *)mapping->host; |
| 1211 | vnode_t *vp = LINVFS_GET_VP(inode); |
| 1212 | int error; |
| 1213 | |
| 1214 | vn_trace_entry(vp, "linvfs_bmap", (inst_t *)__return_address); |
| 1215 | |
| 1216 | VOP_RWLOCK(vp, VRWLOCK_READ); |
| 1217 | VOP_FLUSH_PAGES(vp, (xfs_off_t)0, -1, 0, FI_REMAPF, error); |
| 1218 | VOP_RWUNLOCK(vp, VRWLOCK_READ); |
| 1219 | return generic_block_bmap(mapping, block, linvfs_get_block); |
| 1220 | } |
| 1221 | |
| 1222 | STATIC int |
| 1223 | linvfs_readpage( |
| 1224 | struct file *unused, |
| 1225 | struct page *page) |
| 1226 | { |
| 1227 | return mpage_readpage(page, linvfs_get_block); |
| 1228 | } |
| 1229 | |
| 1230 | STATIC int |
| 1231 | linvfs_readpages( |
| 1232 | struct file *unused, |
| 1233 | struct address_space *mapping, |
| 1234 | struct list_head *pages, |
| 1235 | unsigned nr_pages) |
| 1236 | { |
| 1237 | return mpage_readpages(mapping, pages, nr_pages, linvfs_get_block); |
| 1238 | } |
| 1239 | |
| 1240 | STATIC void |
| 1241 | xfs_count_page_state( |
| 1242 | struct page *page, |
| 1243 | int *delalloc, |
| 1244 | int *unmapped, |
| 1245 | int *unwritten) |
| 1246 | { |
| 1247 | struct buffer_head *bh, *head; |
| 1248 | |
| 1249 | *delalloc = *unmapped = *unwritten = 0; |
| 1250 | |
| 1251 | bh = head = page_buffers(page); |
| 1252 | do { |
| 1253 | if (buffer_uptodate(bh) && !buffer_mapped(bh)) |
| 1254 | (*unmapped) = 1; |
| 1255 | else if (buffer_unwritten(bh) && !buffer_delay(bh)) |
| 1256 | clear_buffer_unwritten(bh); |
| 1257 | else if (buffer_unwritten(bh)) |
| 1258 | (*unwritten) = 1; |
| 1259 | else if (buffer_delay(bh)) |
| 1260 | (*delalloc) = 1; |
| 1261 | } while ((bh = bh->b_this_page) != head); |
| 1262 | } |
| 1263 | |
| 1264 | |
| 1265 | /* |
| 1266 | * writepage: Called from one of two places: |
| 1267 | * |
| 1268 | * 1. we are flushing a delalloc buffer head. |
| 1269 | * |
| 1270 | * 2. we are writing out a dirty page. Typically the page dirty |
| 1271 | * state is cleared before we get here. In this case is it |
| 1272 | * conceivable we have no buffer heads. |
| 1273 | * |
| 1274 | * For delalloc space on the page we need to allocate space and |
| 1275 | * flush it. For unmapped buffer heads on the page we should |
| 1276 | * allocate space if the page is uptodate. For any other dirty |
| 1277 | * buffer heads on the page we should flush them. |
| 1278 | * |
| 1279 | * If we detect that a transaction would be required to flush |
| 1280 | * the page, we have to check the process flags first, if we |
| 1281 | * are already in a transaction or disk I/O during allocations |
| 1282 | * is off, we need to fail the writepage and redirty the page. |
| 1283 | */ |
| 1284 | |
| 1285 | STATIC int |
| 1286 | linvfs_writepage( |
| 1287 | struct page *page, |
| 1288 | struct writeback_control *wbc) |
| 1289 | { |
| 1290 | int error; |
| 1291 | int need_trans; |
| 1292 | int delalloc, unmapped, unwritten; |
| 1293 | struct inode *inode = page->mapping->host; |
| 1294 | |
| 1295 | xfs_page_trace(XFS_WRITEPAGE_ENTER, inode, page, 0); |
| 1296 | |
| 1297 | /* |
| 1298 | * We need a transaction if: |
| 1299 | * 1. There are delalloc buffers on the page |
| 1300 | * 2. The page is uptodate and we have unmapped buffers |
| 1301 | * 3. The page is uptodate and we have no buffers |
| 1302 | * 4. There are unwritten buffers on the page |
| 1303 | */ |
| 1304 | |
| 1305 | if (!page_has_buffers(page)) { |
| 1306 | unmapped = 1; |
| 1307 | need_trans = 1; |
| 1308 | } else { |
| 1309 | xfs_count_page_state(page, &delalloc, &unmapped, &unwritten); |
| 1310 | if (!PageUptodate(page)) |
| 1311 | unmapped = 0; |
| 1312 | need_trans = delalloc + unmapped + unwritten; |
| 1313 | } |
| 1314 | |
| 1315 | /* |
| 1316 | * If we need a transaction and the process flags say |
| 1317 | * we are already in a transaction, or no IO is allowed |
| 1318 | * then mark the page dirty again and leave the page |
| 1319 | * as is. |
| 1320 | */ |
| 1321 | if (PFLAGS_TEST_FSTRANS() && need_trans) |
| 1322 | goto out_fail; |
| 1323 | |
| 1324 | /* |
| 1325 | * Delay hooking up buffer heads until we have |
| 1326 | * made our go/no-go decision. |
| 1327 | */ |
| 1328 | if (!page_has_buffers(page)) |
| 1329 | create_empty_buffers(page, 1 << inode->i_blkbits, 0); |
| 1330 | |
| 1331 | /* |
| 1332 | * Convert delayed allocate, unwritten or unmapped space |
| 1333 | * to real space and flush out to disk. |
| 1334 | */ |
| 1335 | error = xfs_page_state_convert(inode, page, wbc, 1, unmapped); |
| 1336 | if (error == -EAGAIN) |
| 1337 | goto out_fail; |
| 1338 | if (unlikely(error < 0)) |
| 1339 | goto out_unlock; |
| 1340 | |
| 1341 | return 0; |
| 1342 | |
| 1343 | out_fail: |
| 1344 | redirty_page_for_writepage(wbc, page); |
| 1345 | unlock_page(page); |
| 1346 | return 0; |
| 1347 | out_unlock: |
| 1348 | unlock_page(page); |
| 1349 | return error; |
| 1350 | } |
| 1351 | |
Nathan Scott | bcec2b7 | 2005-09-02 16:40:17 +1000 | [diff] [blame] | 1352 | STATIC int |
| 1353 | linvfs_invalidate_page( |
| 1354 | struct page *page, |
| 1355 | unsigned long offset) |
| 1356 | { |
| 1357 | xfs_page_trace(XFS_INVALIDPAGE_ENTER, |
| 1358 | page->mapping->host, page, offset); |
| 1359 | return block_invalidatepage(page, offset); |
| 1360 | } |
| 1361 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | /* |
| 1363 | * Called to move a page into cleanable state - and from there |
| 1364 | * to be released. Possibly the page is already clean. We always |
| 1365 | * have buffer heads in this call. |
| 1366 | * |
| 1367 | * Returns 0 if the page is ok to release, 1 otherwise. |
| 1368 | * |
| 1369 | * Possible scenarios are: |
| 1370 | * |
| 1371 | * 1. We are being called to release a page which has been written |
| 1372 | * to via regular I/O. buffer heads will be dirty and possibly |
| 1373 | * delalloc. If no delalloc buffer heads in this case then we |
| 1374 | * can just return zero. |
| 1375 | * |
| 1376 | * 2. We are called to release a page which has been written via |
| 1377 | * mmap, all we need to do is ensure there is no delalloc |
| 1378 | * state in the buffer heads, if not we can let the caller |
| 1379 | * free them and we should come back later via writepage. |
| 1380 | */ |
| 1381 | STATIC int |
| 1382 | linvfs_release_page( |
| 1383 | struct page *page, |
Al Viro | 27496a8 | 2005-10-21 03:20:48 -0400 | [diff] [blame] | 1384 | gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | { |
| 1386 | struct inode *inode = page->mapping->host; |
| 1387 | int dirty, delalloc, unmapped, unwritten; |
| 1388 | struct writeback_control wbc = { |
| 1389 | .sync_mode = WB_SYNC_ALL, |
| 1390 | .nr_to_write = 1, |
| 1391 | }; |
| 1392 | |
| 1393 | xfs_page_trace(XFS_RELEASEPAGE_ENTER, inode, page, gfp_mask); |
| 1394 | |
| 1395 | xfs_count_page_state(page, &delalloc, &unmapped, &unwritten); |
| 1396 | if (!delalloc && !unwritten) |
| 1397 | goto free_buffers; |
| 1398 | |
| 1399 | if (!(gfp_mask & __GFP_FS)) |
| 1400 | return 0; |
| 1401 | |
| 1402 | /* If we are already inside a transaction or the thread cannot |
| 1403 | * do I/O, we cannot release this page. |
| 1404 | */ |
| 1405 | if (PFLAGS_TEST_FSTRANS()) |
| 1406 | return 0; |
| 1407 | |
| 1408 | /* |
| 1409 | * Convert delalloc space to real space, do not flush the |
| 1410 | * data out to disk, that will be done by the caller. |
| 1411 | * Never need to allocate space here - we will always |
| 1412 | * come back to writepage in that case. |
| 1413 | */ |
| 1414 | dirty = xfs_page_state_convert(inode, page, &wbc, 0, 0); |
| 1415 | if (dirty == 0 && !unwritten) |
| 1416 | goto free_buffers; |
| 1417 | return 0; |
| 1418 | |
| 1419 | free_buffers: |
| 1420 | return try_to_free_buffers(page); |
| 1421 | } |
| 1422 | |
| 1423 | STATIC int |
| 1424 | linvfs_prepare_write( |
| 1425 | struct file *file, |
| 1426 | struct page *page, |
| 1427 | unsigned int from, |
| 1428 | unsigned int to) |
| 1429 | { |
| 1430 | return block_prepare_write(page, from, to, linvfs_get_block); |
| 1431 | } |
| 1432 | |
| 1433 | struct address_space_operations linvfs_aops = { |
| 1434 | .readpage = linvfs_readpage, |
| 1435 | .readpages = linvfs_readpages, |
| 1436 | .writepage = linvfs_writepage, |
| 1437 | .sync_page = block_sync_page, |
| 1438 | .releasepage = linvfs_release_page, |
Nathan Scott | bcec2b7 | 2005-09-02 16:40:17 +1000 | [diff] [blame] | 1439 | .invalidatepage = linvfs_invalidate_page, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | .prepare_write = linvfs_prepare_write, |
| 1441 | .commit_write = generic_commit_write, |
| 1442 | .bmap = linvfs_bmap, |
| 1443 | .direct_IO = linvfs_direct_IO, |
| 1444 | }; |