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