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