Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000-2004 Silicon Graphics, Inc. All Rights Reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of version 2 of the GNU General Public License as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it would be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 11 | * |
| 12 | * Further, this software is distributed without any warranty that it is |
| 13 | * free of the rightful claim of any third person regarding infringement |
| 14 | * or the like. Any license provided herein, whether implied or |
| 15 | * otherwise, applies only to this software file. Patent licenses, if |
| 16 | * any, provided herein do not apply to combinations of this program with |
| 17 | * other software, or any other product whatsoever. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write the Free Software Foundation, Inc., 59 |
| 21 | * Temple Place - Suite 330, Boston MA 02111-1307, USA. |
| 22 | * |
| 23 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 24 | * Mountain View, CA 94043, or: |
| 25 | * |
| 26 | * http://www.sgi.com |
| 27 | * |
| 28 | * For further information regarding this notice, see: |
| 29 | * |
| 30 | * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/ |
| 31 | */ |
| 32 | |
| 33 | #include "xfs.h" |
| 34 | |
| 35 | #include "xfs_fs.h" |
| 36 | #include "xfs_inum.h" |
| 37 | #include "xfs_log.h" |
| 38 | #include "xfs_trans.h" |
| 39 | #include "xfs_sb.h" |
| 40 | #include "xfs_ag.h" |
| 41 | #include "xfs_dir.h" |
| 42 | #include "xfs_dir2.h" |
| 43 | #include "xfs_alloc.h" |
| 44 | #include "xfs_dmapi.h" |
| 45 | #include "xfs_quota.h" |
| 46 | #include "xfs_mount.h" |
| 47 | #include "xfs_alloc_btree.h" |
| 48 | #include "xfs_bmap_btree.h" |
| 49 | #include "xfs_ialloc_btree.h" |
| 50 | #include "xfs_btree.h" |
| 51 | #include "xfs_ialloc.h" |
| 52 | #include "xfs_attr_sf.h" |
| 53 | #include "xfs_dir_sf.h" |
| 54 | #include "xfs_dir2_sf.h" |
| 55 | #include "xfs_dinode.h" |
| 56 | #include "xfs_inode.h" |
| 57 | #include "xfs_bmap.h" |
| 58 | #include "xfs_bit.h" |
| 59 | #include "xfs_rtalloc.h" |
| 60 | #include "xfs_error.h" |
| 61 | #include "xfs_itable.h" |
| 62 | #include "xfs_rw.h" |
| 63 | #include "xfs_acl.h" |
| 64 | #include "xfs_cap.h" |
| 65 | #include "xfs_mac.h" |
| 66 | #include "xfs_attr.h" |
| 67 | #include "xfs_buf_item.h" |
| 68 | #include "xfs_trans_space.h" |
| 69 | #include "xfs_utils.h" |
| 70 | #include "xfs_iomap.h" |
| 71 | |
| 72 | #if defined(XFS_RW_TRACE) |
| 73 | void |
| 74 | xfs_iomap_enter_trace( |
| 75 | int tag, |
| 76 | xfs_iocore_t *io, |
| 77 | xfs_off_t offset, |
| 78 | ssize_t count) |
| 79 | { |
| 80 | xfs_inode_t *ip = XFS_IO_INODE(io); |
| 81 | |
| 82 | if (!ip->i_rwtrace) |
| 83 | return; |
| 84 | |
| 85 | ktrace_enter(ip->i_rwtrace, |
| 86 | (void *)((unsigned long)tag), |
| 87 | (void *)ip, |
| 88 | (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)), |
| 89 | (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)), |
| 90 | (void *)((unsigned long)((offset >> 32) & 0xffffffff)), |
| 91 | (void *)((unsigned long)(offset & 0xffffffff)), |
| 92 | (void *)((unsigned long)count), |
| 93 | (void *)((unsigned long)((io->io_new_size >> 32) & 0xffffffff)), |
| 94 | (void *)((unsigned long)(io->io_new_size & 0xffffffff)), |
| 95 | (void *)NULL, |
| 96 | (void *)NULL, |
| 97 | (void *)NULL, |
| 98 | (void *)NULL, |
| 99 | (void *)NULL, |
| 100 | (void *)NULL, |
| 101 | (void *)NULL); |
| 102 | } |
| 103 | |
| 104 | void |
| 105 | xfs_iomap_map_trace( |
| 106 | int tag, |
| 107 | xfs_iocore_t *io, |
| 108 | xfs_off_t offset, |
| 109 | ssize_t count, |
| 110 | xfs_iomap_t *iomapp, |
| 111 | xfs_bmbt_irec_t *imapp, |
| 112 | int flags) |
| 113 | { |
| 114 | xfs_inode_t *ip = XFS_IO_INODE(io); |
| 115 | |
| 116 | if (!ip->i_rwtrace) |
| 117 | return; |
| 118 | |
| 119 | ktrace_enter(ip->i_rwtrace, |
| 120 | (void *)((unsigned long)tag), |
| 121 | (void *)ip, |
| 122 | (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)), |
| 123 | (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)), |
| 124 | (void *)((unsigned long)((offset >> 32) & 0xffffffff)), |
| 125 | (void *)((unsigned long)(offset & 0xffffffff)), |
| 126 | (void *)((unsigned long)count), |
| 127 | (void *)((unsigned long)flags), |
| 128 | (void *)((unsigned long)((iomapp->iomap_offset >> 32) & 0xffffffff)), |
| 129 | (void *)((unsigned long)(iomapp->iomap_offset & 0xffffffff)), |
| 130 | (void *)((unsigned long)(iomapp->iomap_delta)), |
| 131 | (void *)((unsigned long)(iomapp->iomap_bsize)), |
| 132 | (void *)((unsigned long)(iomapp->iomap_bn)), |
| 133 | (void *)(__psint_t)(imapp->br_startoff), |
| 134 | (void *)((unsigned long)(imapp->br_blockcount)), |
| 135 | (void *)(__psint_t)(imapp->br_startblock)); |
| 136 | } |
| 137 | #else |
| 138 | #define xfs_iomap_enter_trace(tag, io, offset, count) |
| 139 | #define xfs_iomap_map_trace(tag, io, offset, count, iomapp, imapp, flags) |
| 140 | #endif |
| 141 | |
| 142 | #define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \ |
| 143 | << mp->m_writeio_log) |
| 144 | #define XFS_STRAT_WRITE_IMAPS 2 |
| 145 | #define XFS_WRITE_IMAPS XFS_BMAP_MAX_NMAP |
| 146 | |
| 147 | STATIC int |
| 148 | xfs_imap_to_bmap( |
| 149 | xfs_iocore_t *io, |
| 150 | xfs_off_t offset, |
| 151 | xfs_bmbt_irec_t *imap, |
| 152 | xfs_iomap_t *iomapp, |
| 153 | int imaps, /* Number of imap entries */ |
| 154 | int iomaps, /* Number of iomap entries */ |
| 155 | int flags) |
| 156 | { |
| 157 | xfs_mount_t *mp; |
| 158 | xfs_fsize_t nisize; |
| 159 | int pbm; |
| 160 | xfs_fsblock_t start_block; |
| 161 | |
| 162 | mp = io->io_mount; |
| 163 | nisize = XFS_SIZE(mp, io); |
| 164 | if (io->io_new_size > nisize) |
| 165 | nisize = io->io_new_size; |
| 166 | |
| 167 | for (pbm = 0; imaps && pbm < iomaps; imaps--, iomapp++, imap++, pbm++) { |
| 168 | iomapp->iomap_offset = XFS_FSB_TO_B(mp, imap->br_startoff); |
| 169 | iomapp->iomap_delta = offset - iomapp->iomap_offset; |
| 170 | iomapp->iomap_bsize = XFS_FSB_TO_B(mp, imap->br_blockcount); |
| 171 | iomapp->iomap_flags = flags; |
| 172 | |
| 173 | if (io->io_flags & XFS_IOCORE_RT) { |
| 174 | iomapp->iomap_flags |= IOMAP_REALTIME; |
| 175 | iomapp->iomap_target = mp->m_rtdev_targp; |
| 176 | } else { |
| 177 | iomapp->iomap_target = mp->m_ddev_targp; |
| 178 | } |
| 179 | start_block = imap->br_startblock; |
| 180 | if (start_block == HOLESTARTBLOCK) { |
| 181 | iomapp->iomap_bn = IOMAP_DADDR_NULL; |
| 182 | iomapp->iomap_flags |= IOMAP_HOLE; |
| 183 | } else if (start_block == DELAYSTARTBLOCK) { |
| 184 | iomapp->iomap_bn = IOMAP_DADDR_NULL; |
| 185 | iomapp->iomap_flags |= IOMAP_DELAY; |
| 186 | } else { |
| 187 | iomapp->iomap_bn = XFS_FSB_TO_DB_IO(io, start_block); |
| 188 | if (ISUNWRITTEN(imap)) |
| 189 | iomapp->iomap_flags |= IOMAP_UNWRITTEN; |
| 190 | } |
| 191 | |
| 192 | if ((iomapp->iomap_offset + iomapp->iomap_bsize) >= nisize) { |
| 193 | iomapp->iomap_flags |= IOMAP_EOF; |
| 194 | } |
| 195 | |
| 196 | offset += iomapp->iomap_bsize - iomapp->iomap_delta; |
| 197 | } |
| 198 | return pbm; /* Return the number filled */ |
| 199 | } |
| 200 | |
| 201 | int |
| 202 | xfs_iomap( |
| 203 | xfs_iocore_t *io, |
| 204 | xfs_off_t offset, |
| 205 | ssize_t count, |
| 206 | int flags, |
| 207 | xfs_iomap_t *iomapp, |
| 208 | int *niomaps) |
| 209 | { |
| 210 | xfs_mount_t *mp = io->io_mount; |
| 211 | xfs_fileoff_t offset_fsb, end_fsb; |
| 212 | int error = 0; |
| 213 | int lockmode = 0; |
| 214 | xfs_bmbt_irec_t imap; |
| 215 | int nimaps = 1; |
| 216 | int bmapi_flags = 0; |
| 217 | int iomap_flags = 0; |
| 218 | |
| 219 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 220 | return XFS_ERROR(EIO); |
| 221 | |
| 222 | switch (flags & |
| 223 | (BMAPI_READ | BMAPI_WRITE | BMAPI_ALLOCATE | |
| 224 | BMAPI_UNWRITTEN | BMAPI_DEVICE)) { |
| 225 | case BMAPI_READ: |
| 226 | xfs_iomap_enter_trace(XFS_IOMAP_READ_ENTER, io, offset, count); |
| 227 | lockmode = XFS_LCK_MAP_SHARED(mp, io); |
| 228 | bmapi_flags = XFS_BMAPI_ENTIRE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | break; |
| 230 | case BMAPI_WRITE: |
| 231 | xfs_iomap_enter_trace(XFS_IOMAP_WRITE_ENTER, io, offset, count); |
| 232 | lockmode = XFS_ILOCK_EXCL|XFS_EXTSIZE_WR; |
Nathan Scott | c31e887 | 2005-09-05 10:06:55 +1000 | [diff] [blame] | 233 | if (flags & BMAPI_IGNSTATE) |
| 234 | bmapi_flags |= XFS_BMAPI_IGSTATE|XFS_BMAPI_ENTIRE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | XFS_ILOCK(mp, io, lockmode); |
| 236 | break; |
| 237 | case BMAPI_ALLOCATE: |
| 238 | xfs_iomap_enter_trace(XFS_IOMAP_ALLOC_ENTER, io, offset, count); |
| 239 | lockmode = XFS_ILOCK_SHARED|XFS_EXTSIZE_RD; |
| 240 | bmapi_flags = XFS_BMAPI_ENTIRE; |
| 241 | /* Attempt non-blocking lock */ |
| 242 | if (flags & BMAPI_TRYLOCK) { |
| 243 | if (!XFS_ILOCK_NOWAIT(mp, io, lockmode)) |
| 244 | return XFS_ERROR(EAGAIN); |
| 245 | } else { |
| 246 | XFS_ILOCK(mp, io, lockmode); |
| 247 | } |
| 248 | break; |
| 249 | case BMAPI_UNWRITTEN: |
| 250 | goto phase2; |
| 251 | case BMAPI_DEVICE: |
| 252 | lockmode = XFS_LCK_MAP_SHARED(mp, io); |
| 253 | iomapp->iomap_target = io->io_flags & XFS_IOCORE_RT ? |
| 254 | mp->m_rtdev_targp : mp->m_ddev_targp; |
| 255 | error = 0; |
| 256 | *niomaps = 1; |
| 257 | goto out; |
| 258 | default: |
| 259 | BUG(); |
| 260 | } |
| 261 | |
| 262 | ASSERT(offset <= mp->m_maxioffset); |
| 263 | if ((xfs_fsize_t)offset + count > mp->m_maxioffset) |
| 264 | count = mp->m_maxioffset - offset; |
| 265 | end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count); |
| 266 | offset_fsb = XFS_B_TO_FSBT(mp, offset); |
| 267 | |
| 268 | error = XFS_BMAPI(mp, NULL, io, offset_fsb, |
| 269 | (xfs_filblks_t)(end_fsb - offset_fsb), |
| 270 | bmapi_flags, NULL, 0, &imap, |
| 271 | &nimaps, NULL); |
| 272 | |
| 273 | if (error) |
| 274 | goto out; |
| 275 | |
| 276 | phase2: |
| 277 | switch (flags & (BMAPI_WRITE|BMAPI_ALLOCATE|BMAPI_UNWRITTEN)) { |
| 278 | case BMAPI_WRITE: |
| 279 | /* If we found an extent, return it */ |
Russell Cattelan | 68d1498 | 2005-05-06 06:42:22 -0700 | [diff] [blame] | 280 | if (nimaps && |
| 281 | (imap.br_startblock != HOLESTARTBLOCK) && |
| 282 | (imap.br_startblock != DELAYSTARTBLOCK)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, io, |
| 284 | offset, count, iomapp, &imap, flags); |
| 285 | break; |
| 286 | } |
| 287 | |
| 288 | if (flags & (BMAPI_DIRECT|BMAPI_MMAP)) { |
| 289 | error = XFS_IOMAP_WRITE_DIRECT(mp, io, offset, |
| 290 | count, flags, &imap, &nimaps, nimaps); |
| 291 | } else { |
| 292 | error = XFS_IOMAP_WRITE_DELAY(mp, io, offset, count, |
| 293 | flags, &imap, &nimaps); |
| 294 | } |
| 295 | if (!error) { |
| 296 | xfs_iomap_map_trace(XFS_IOMAP_ALLOC_MAP, io, |
| 297 | offset, count, iomapp, &imap, flags); |
| 298 | } |
| 299 | iomap_flags = IOMAP_NEW; |
| 300 | break; |
| 301 | case BMAPI_ALLOCATE: |
| 302 | /* If we found an extent, return it */ |
| 303 | XFS_IUNLOCK(mp, io, lockmode); |
| 304 | lockmode = 0; |
| 305 | |
| 306 | if (nimaps && !ISNULLSTARTBLOCK(imap.br_startblock)) { |
| 307 | xfs_iomap_map_trace(XFS_IOMAP_WRITE_MAP, io, |
| 308 | offset, count, iomapp, &imap, flags); |
| 309 | break; |
| 310 | } |
| 311 | |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 312 | error = XFS_IOMAP_WRITE_ALLOCATE(mp, io, offset, count, |
| 313 | &imap, &nimaps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | break; |
| 315 | case BMAPI_UNWRITTEN: |
| 316 | lockmode = 0; |
| 317 | error = XFS_IOMAP_WRITE_UNWRITTEN(mp, io, offset, count); |
| 318 | nimaps = 0; |
| 319 | break; |
| 320 | } |
| 321 | |
| 322 | if (nimaps) { |
| 323 | *niomaps = xfs_imap_to_bmap(io, offset, &imap, |
| 324 | iomapp, nimaps, *niomaps, iomap_flags); |
| 325 | } else if (niomaps) { |
| 326 | *niomaps = 0; |
| 327 | } |
| 328 | |
| 329 | out: |
| 330 | if (lockmode) |
| 331 | XFS_IUNLOCK(mp, io, lockmode); |
| 332 | return XFS_ERROR(error); |
| 333 | } |
| 334 | |
| 335 | STATIC int |
| 336 | xfs_flush_space( |
| 337 | xfs_inode_t *ip, |
| 338 | int *fsynced, |
| 339 | int *ioflags) |
| 340 | { |
| 341 | switch (*fsynced) { |
| 342 | case 0: |
| 343 | if (ip->i_delayed_blks) { |
| 344 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 345 | xfs_flush_inode(ip); |
| 346 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 347 | *fsynced = 1; |
| 348 | } else { |
| 349 | *ioflags |= BMAPI_SYNC; |
| 350 | *fsynced = 2; |
| 351 | } |
| 352 | return 0; |
| 353 | case 1: |
| 354 | *fsynced = 2; |
| 355 | *ioflags |= BMAPI_SYNC; |
| 356 | return 0; |
| 357 | case 2: |
| 358 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 359 | xfs_flush_device(ip); |
| 360 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 361 | *fsynced = 3; |
| 362 | return 0; |
| 363 | } |
| 364 | return 1; |
| 365 | } |
| 366 | |
| 367 | int |
| 368 | xfs_iomap_write_direct( |
| 369 | xfs_inode_t *ip, |
Nathan Scott | f403b7f | 2005-05-05 13:33:40 -0700 | [diff] [blame] | 370 | xfs_off_t offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | size_t count, |
| 372 | int flags, |
| 373 | xfs_bmbt_irec_t *ret_imap, |
| 374 | int *nmaps, |
| 375 | int found) |
| 376 | { |
| 377 | xfs_mount_t *mp = ip->i_mount; |
| 378 | xfs_iocore_t *io = &ip->i_iocore; |
| 379 | xfs_fileoff_t offset_fsb; |
| 380 | xfs_fileoff_t last_fsb; |
| 381 | xfs_filblks_t count_fsb; |
| 382 | xfs_fsize_t isize; |
| 383 | xfs_fsblock_t firstfsb; |
| 384 | int nimaps, maps; |
| 385 | int error; |
| 386 | int bmapi_flag; |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 387 | int quota_flag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | int rt; |
| 389 | xfs_trans_t *tp; |
| 390 | xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS], *imapp; |
| 391 | xfs_bmap_free_t free_list; |
| 392 | int aeof; |
Nathan Scott | d52b44d | 2005-09-02 16:41:32 +1000 | [diff] [blame] | 393 | xfs_filblks_t qblocks, resblks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | int committed; |
Nathan Scott | d52b44d | 2005-09-02 16:41:32 +1000 | [diff] [blame] | 395 | int resrtextents; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | |
| 397 | /* |
| 398 | * Make sure that the dquots are there. This doesn't hold |
| 399 | * the ilock across a disk read. |
| 400 | */ |
| 401 | error = XFS_QM_DQATTACH(ip->i_mount, ip, XFS_QMOPT_ILOCKED); |
| 402 | if (error) |
| 403 | return XFS_ERROR(error); |
| 404 | |
| 405 | maps = min(XFS_WRITE_IMAPS, *nmaps); |
| 406 | nimaps = maps; |
| 407 | |
| 408 | isize = ip->i_d.di_size; |
| 409 | aeof = (offset + count) > isize; |
| 410 | |
| 411 | if (io->io_new_size > isize) |
| 412 | isize = io->io_new_size; |
| 413 | |
| 414 | offset_fsb = XFS_B_TO_FSBT(mp, offset); |
| 415 | last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count))); |
| 416 | count_fsb = last_fsb - offset_fsb; |
| 417 | if (found && (ret_imap->br_startblock == HOLESTARTBLOCK)) { |
| 418 | xfs_fileoff_t map_last_fsb; |
| 419 | |
| 420 | map_last_fsb = ret_imap->br_blockcount + ret_imap->br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | if (map_last_fsb < last_fsb) { |
| 422 | last_fsb = map_last_fsb; |
| 423 | count_fsb = last_fsb - offset_fsb; |
| 424 | } |
| 425 | ASSERT(count_fsb > 0); |
| 426 | } |
| 427 | |
| 428 | /* |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 429 | * Determine if reserving space on the data or realtime partition. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | */ |
| 431 | if ((rt = XFS_IS_REALTIME_INODE(ip))) { |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 432 | xfs_extlen_t extsz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 434 | if (!(extsz = ip->i_d.di_extsize)) |
| 435 | extsz = mp->m_sb.sb_rextsize; |
Nathan Scott | d52b44d | 2005-09-02 16:41:32 +1000 | [diff] [blame] | 436 | resrtextents = qblocks = (count_fsb + extsz - 1); |
| 437 | do_div(resrtextents, mp->m_sb.sb_rextsize); |
| 438 | resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0); |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 439 | quota_flag = XFS_QMOPT_RES_RTBLKS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | } else { |
Nathan Scott | d52b44d | 2005-09-02 16:41:32 +1000 | [diff] [blame] | 441 | resrtextents = 0; |
| 442 | resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, count_fsb); |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 443 | quota_flag = XFS_QMOPT_RES_REGBLKS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /* |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 447 | * Allocate and setup the transaction |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | */ |
| 449 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 450 | tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | error = xfs_trans_reserve(tp, resblks, |
Nathan Scott | d52b44d | 2005-09-02 16:41:32 +1000 | [diff] [blame] | 452 | XFS_WRITE_LOG_RES(mp), resrtextents, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | XFS_TRANS_PERM_LOG_RES, |
| 454 | XFS_WRITE_LOG_COUNT); |
| 455 | |
| 456 | /* |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 457 | * Check for running out of space, note: need lock to return |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | */ |
| 459 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | xfs_trans_cancel(tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | if (error) |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 463 | goto error_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 465 | if (XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | error = (EDQUOT); |
| 467 | goto error1; |
| 468 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | |
| 470 | bmapi_flag = XFS_BMAPI_WRITE; |
| 471 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 472 | xfs_trans_ihold(tp, ip); |
| 473 | |
| 474 | if (!(flags & BMAPI_MMAP) && (offset < ip->i_d.di_size || rt)) |
| 475 | bmapi_flag |= XFS_BMAPI_PREALLOC; |
| 476 | |
| 477 | /* |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 478 | * Issue the bmapi() call to allocate the blocks |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | */ |
| 480 | XFS_BMAP_INIT(&free_list, &firstfsb); |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 481 | nimaps = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | imapp = &imap[0]; |
| 483 | error = xfs_bmapi(tp, ip, offset_fsb, count_fsb, |
| 484 | bmapi_flag, &firstfsb, 0, imapp, &nimaps, &free_list); |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 485 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | |
| 488 | /* |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 489 | * Complete the transaction |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | error = xfs_bmap_finish(&tp, &free_list, firstfsb, &committed); |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 492 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL); |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 495 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | goto error_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 498 | /* |
| 499 | * Copy any maps to caller's array and return any error. |
| 500 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | if (nimaps == 0) { |
| 502 | error = (ENOSPC); |
| 503 | goto error_out; |
| 504 | } |
| 505 | |
| 506 | *ret_imap = imap[0]; |
| 507 | *nmaps = 1; |
| 508 | if ( !(io->io_flags & XFS_IOCORE_RT) && !ret_imap->br_startblock) { |
| 509 | cmn_err(CE_PANIC,"Access to block zero: fs <%s> inode: %lld " |
| 510 | "start_block : %llx start_off : %llx blkcnt : %llx " |
| 511 | "extent-state : %x \n", |
| 512 | (ip->i_mount)->m_fsname, |
| 513 | (long long)ip->i_ino, |
| 514 | ret_imap->br_startblock, ret_imap->br_startoff, |
| 515 | ret_imap->br_blockcount,ret_imap->br_state); |
| 516 | } |
| 517 | return 0; |
| 518 | |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 519 | error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | xfs_bmap_cancel(&free_list); |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 521 | XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 523 | error1: /* Just cancel transaction */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); |
| 525 | *nmaps = 0; /* nothing set-up here */ |
| 526 | |
| 527 | error_out: |
| 528 | return XFS_ERROR(error); |
| 529 | } |
| 530 | |
| 531 | int |
| 532 | xfs_iomap_write_delay( |
| 533 | xfs_inode_t *ip, |
Nathan Scott | f403b7f | 2005-05-05 13:33:40 -0700 | [diff] [blame] | 534 | xfs_off_t offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | size_t count, |
| 536 | int ioflag, |
| 537 | xfs_bmbt_irec_t *ret_imap, |
| 538 | int *nmaps) |
| 539 | { |
| 540 | xfs_mount_t *mp = ip->i_mount; |
| 541 | xfs_iocore_t *io = &ip->i_iocore; |
| 542 | xfs_fileoff_t offset_fsb; |
| 543 | xfs_fileoff_t last_fsb; |
| 544 | xfs_fsize_t isize; |
| 545 | xfs_fsblock_t firstblock; |
| 546 | int nimaps; |
| 547 | int error; |
| 548 | xfs_bmbt_irec_t imap[XFS_WRITE_IMAPS]; |
| 549 | int aeof; |
| 550 | int fsynced = 0; |
| 551 | |
| 552 | ASSERT(ismrlocked(&ip->i_lock, MR_UPDATE) != 0); |
| 553 | |
| 554 | /* |
| 555 | * Make sure that the dquots are there. This doesn't hold |
| 556 | * the ilock across a disk read. |
| 557 | */ |
| 558 | |
| 559 | error = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED); |
| 560 | if (error) |
| 561 | return XFS_ERROR(error); |
| 562 | |
| 563 | retry: |
| 564 | isize = ip->i_d.di_size; |
| 565 | if (io->io_new_size > isize) { |
| 566 | isize = io->io_new_size; |
| 567 | } |
| 568 | |
| 569 | aeof = 0; |
| 570 | offset_fsb = XFS_B_TO_FSBT(mp, offset); |
| 571 | last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count))); |
| 572 | /* |
| 573 | * If the caller is doing a write at the end of the file, |
| 574 | * then extend the allocation (and the buffer used for the write) |
| 575 | * out to the file system's write iosize. We clean up any extra |
| 576 | * space left over when the file is closed in xfs_inactive(). |
| 577 | * |
| 578 | * For sync writes, we are flushing delayed allocate space to |
| 579 | * try to make additional space available for allocation near |
| 580 | * the filesystem full boundary - preallocation hurts in that |
| 581 | * situation, of course. |
| 582 | */ |
| 583 | if (!(ioflag & BMAPI_SYNC) && ((offset + count) > ip->i_d.di_size)) { |
| 584 | xfs_off_t aligned_offset; |
| 585 | xfs_filblks_t count_fsb; |
| 586 | unsigned int iosize; |
| 587 | xfs_fileoff_t ioalign; |
| 588 | int n; |
| 589 | xfs_fileoff_t start_fsb; |
| 590 | |
| 591 | /* |
| 592 | * If there are any real blocks past eof, then don't |
| 593 | * do any speculative allocation. |
| 594 | */ |
| 595 | start_fsb = XFS_B_TO_FSBT(mp, |
| 596 | ((xfs_ufsize_t)(offset + count - 1))); |
| 597 | count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp)); |
| 598 | while (count_fsb > 0) { |
| 599 | nimaps = XFS_WRITE_IMAPS; |
| 600 | error = XFS_BMAPI(mp, NULL, io, start_fsb, count_fsb, |
| 601 | 0, &firstblock, 0, imap, &nimaps, NULL); |
| 602 | if (error) { |
| 603 | return error; |
| 604 | } |
| 605 | for (n = 0; n < nimaps; n++) { |
| 606 | if ( !(io->io_flags & XFS_IOCORE_RT) && |
| 607 | !imap[n].br_startblock) { |
| 608 | cmn_err(CE_PANIC,"Access to block " |
| 609 | "zero: fs <%s> inode: %lld " |
| 610 | "start_block : %llx start_off " |
| 611 | ": %llx blkcnt : %llx " |
| 612 | "extent-state : %x \n", |
| 613 | (ip->i_mount)->m_fsname, |
| 614 | (long long)ip->i_ino, |
| 615 | imap[n].br_startblock, |
| 616 | imap[n].br_startoff, |
| 617 | imap[n].br_blockcount, |
| 618 | imap[n].br_state); |
| 619 | } |
| 620 | if ((imap[n].br_startblock != HOLESTARTBLOCK) && |
| 621 | (imap[n].br_startblock != DELAYSTARTBLOCK)) { |
| 622 | goto write_map; |
| 623 | } |
| 624 | start_fsb += imap[n].br_blockcount; |
| 625 | count_fsb -= imap[n].br_blockcount; |
| 626 | } |
| 627 | } |
| 628 | iosize = mp->m_writeio_blocks; |
| 629 | aligned_offset = XFS_WRITEIO_ALIGN(mp, (offset + count - 1)); |
| 630 | ioalign = XFS_B_TO_FSBT(mp, aligned_offset); |
| 631 | last_fsb = ioalign + iosize; |
| 632 | aeof = 1; |
| 633 | } |
| 634 | write_map: |
| 635 | nimaps = XFS_WRITE_IMAPS; |
| 636 | firstblock = NULLFSBLOCK; |
| 637 | |
| 638 | /* |
| 639 | * If mounted with the "-o swalloc" option, roundup the allocation |
| 640 | * request to a stripe width boundary if the file size is >= |
| 641 | * stripe width and we are allocating past the allocation eof. |
| 642 | */ |
| 643 | if (!(io->io_flags & XFS_IOCORE_RT) && mp->m_swidth |
| 644 | && (mp->m_flags & XFS_MOUNT_SWALLOC) |
| 645 | && (isize >= XFS_FSB_TO_B(mp, mp->m_swidth)) && aeof) { |
| 646 | int eof; |
| 647 | xfs_fileoff_t new_last_fsb; |
| 648 | |
| 649 | new_last_fsb = roundup_64(last_fsb, mp->m_swidth); |
| 650 | error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof); |
| 651 | if (error) { |
| 652 | return error; |
| 653 | } |
| 654 | if (eof) { |
| 655 | last_fsb = new_last_fsb; |
| 656 | } |
| 657 | /* |
| 658 | * Roundup the allocation request to a stripe unit (m_dalign) boundary |
| 659 | * if the file size is >= stripe unit size, and we are allocating past |
| 660 | * the allocation eof. |
| 661 | */ |
| 662 | } else if (!(io->io_flags & XFS_IOCORE_RT) && mp->m_dalign && |
| 663 | (isize >= XFS_FSB_TO_B(mp, mp->m_dalign)) && aeof) { |
| 664 | int eof; |
| 665 | xfs_fileoff_t new_last_fsb; |
| 666 | new_last_fsb = roundup_64(last_fsb, mp->m_dalign); |
| 667 | error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof); |
| 668 | if (error) { |
| 669 | return error; |
| 670 | } |
| 671 | if (eof) { |
| 672 | last_fsb = new_last_fsb; |
| 673 | } |
| 674 | /* |
| 675 | * Round up the allocation request to a real-time extent boundary |
| 676 | * if the file is on the real-time subvolume. |
| 677 | */ |
| 678 | } else if (io->io_flags & XFS_IOCORE_RT && aeof) { |
| 679 | int eof; |
| 680 | xfs_fileoff_t new_last_fsb; |
| 681 | |
| 682 | new_last_fsb = roundup_64(last_fsb, mp->m_sb.sb_rextsize); |
| 683 | error = XFS_BMAP_EOF(mp, io, new_last_fsb, XFS_DATA_FORK, &eof); |
| 684 | if (error) { |
| 685 | return error; |
| 686 | } |
| 687 | if (eof) |
| 688 | last_fsb = new_last_fsb; |
| 689 | } |
| 690 | error = xfs_bmapi(NULL, ip, offset_fsb, |
| 691 | (xfs_filblks_t)(last_fsb - offset_fsb), |
| 692 | XFS_BMAPI_DELAY | XFS_BMAPI_WRITE | |
| 693 | XFS_BMAPI_ENTIRE, &firstblock, 1, imap, |
| 694 | &nimaps, NULL); |
| 695 | /* |
| 696 | * This can be EDQUOT, if nimaps == 0 |
| 697 | */ |
| 698 | if (error && (error != ENOSPC)) { |
| 699 | return XFS_ERROR(error); |
| 700 | } |
| 701 | /* |
| 702 | * If bmapi returned us nothing, and if we didn't get back EDQUOT, |
| 703 | * then we must have run out of space. |
| 704 | */ |
| 705 | if (nimaps == 0) { |
| 706 | xfs_iomap_enter_trace(XFS_IOMAP_WRITE_NOSPACE, |
| 707 | io, offset, count); |
| 708 | if (xfs_flush_space(ip, &fsynced, &ioflag)) |
| 709 | return XFS_ERROR(ENOSPC); |
| 710 | |
| 711 | error = 0; |
| 712 | goto retry; |
| 713 | } |
| 714 | |
| 715 | *ret_imap = imap[0]; |
| 716 | *nmaps = 1; |
| 717 | if ( !(io->io_flags & XFS_IOCORE_RT) && !ret_imap->br_startblock) { |
| 718 | cmn_err(CE_PANIC,"Access to block zero: fs <%s> inode: %lld " |
| 719 | "start_block : %llx start_off : %llx blkcnt : %llx " |
| 720 | "extent-state : %x \n", |
| 721 | (ip->i_mount)->m_fsname, |
| 722 | (long long)ip->i_ino, |
| 723 | ret_imap->br_startblock, ret_imap->br_startoff, |
| 724 | ret_imap->br_blockcount,ret_imap->br_state); |
| 725 | } |
| 726 | return 0; |
| 727 | } |
| 728 | |
| 729 | /* |
| 730 | * Pass in a delayed allocate extent, convert it to real extents; |
| 731 | * return to the caller the extent we create which maps on top of |
| 732 | * the originating callers request. |
| 733 | * |
| 734 | * Called without a lock on the inode. |
| 735 | */ |
| 736 | int |
| 737 | xfs_iomap_write_allocate( |
| 738 | xfs_inode_t *ip, |
Nathan Scott | f403b7f | 2005-05-05 13:33:40 -0700 | [diff] [blame] | 739 | xfs_off_t offset, |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 740 | size_t count, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | xfs_bmbt_irec_t *map, |
| 742 | int *retmap) |
| 743 | { |
| 744 | xfs_mount_t *mp = ip->i_mount; |
| 745 | xfs_iocore_t *io = &ip->i_iocore; |
| 746 | xfs_fileoff_t offset_fsb, last_block; |
| 747 | xfs_fileoff_t end_fsb, map_start_fsb; |
| 748 | xfs_fsblock_t first_block; |
| 749 | xfs_bmap_free_t free_list; |
| 750 | xfs_filblks_t count_fsb; |
| 751 | xfs_bmbt_irec_t imap[XFS_STRAT_WRITE_IMAPS]; |
| 752 | xfs_trans_t *tp; |
| 753 | int i, nimaps, committed; |
| 754 | int error = 0; |
| 755 | int nres; |
| 756 | |
| 757 | *retmap = 0; |
| 758 | |
| 759 | /* |
| 760 | * Make sure that the dquots are there. |
| 761 | */ |
| 762 | if ((error = XFS_QM_DQATTACH(mp, ip, 0))) |
| 763 | return XFS_ERROR(error); |
| 764 | |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 765 | offset_fsb = XFS_B_TO_FSBT(mp, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | count_fsb = map->br_blockcount; |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 767 | map_start_fsb = map->br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | |
| 769 | XFS_STATS_ADD(xs_xstrat_bytes, XFS_FSB_TO_B(mp, count_fsb)); |
| 770 | |
| 771 | while (count_fsb != 0) { |
| 772 | /* |
| 773 | * Set up a transaction with which to allocate the |
| 774 | * backing store for the file. Do allocations in a |
| 775 | * loop until we get some space in the range we are |
| 776 | * interested in. The other space that might be allocated |
| 777 | * is in the delayed allocation extent on which we sit |
| 778 | * but before our buffer starts. |
| 779 | */ |
| 780 | |
| 781 | nimaps = 0; |
| 782 | while (nimaps == 0) { |
| 783 | tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE); |
| 784 | nres = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK); |
| 785 | error = xfs_trans_reserve(tp, nres, |
| 786 | XFS_WRITE_LOG_RES(mp), |
| 787 | 0, XFS_TRANS_PERM_LOG_RES, |
| 788 | XFS_WRITE_LOG_COUNT); |
| 789 | if (error == ENOSPC) { |
| 790 | error = xfs_trans_reserve(tp, 0, |
| 791 | XFS_WRITE_LOG_RES(mp), |
| 792 | 0, |
| 793 | XFS_TRANS_PERM_LOG_RES, |
| 794 | XFS_WRITE_LOG_COUNT); |
| 795 | } |
| 796 | if (error) { |
| 797 | xfs_trans_cancel(tp, 0); |
| 798 | return XFS_ERROR(error); |
| 799 | } |
| 800 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 801 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 802 | xfs_trans_ihold(tp, ip); |
| 803 | |
| 804 | XFS_BMAP_INIT(&free_list, &first_block); |
| 805 | |
| 806 | nimaps = XFS_STRAT_WRITE_IMAPS; |
| 807 | /* |
| 808 | * Ensure we don't go beyond eof - it is possible |
| 809 | * the extents changed since we did the read call, |
| 810 | * we dropped the ilock in the interim. |
| 811 | */ |
| 812 | |
| 813 | end_fsb = XFS_B_TO_FSB(mp, ip->i_d.di_size); |
| 814 | xfs_bmap_last_offset(NULL, ip, &last_block, |
| 815 | XFS_DATA_FORK); |
| 816 | last_block = XFS_FILEOFF_MAX(last_block, end_fsb); |
| 817 | if ((map_start_fsb + count_fsb) > last_block) { |
| 818 | count_fsb = last_block - map_start_fsb; |
| 819 | if (count_fsb == 0) { |
| 820 | error = EAGAIN; |
| 821 | goto trans_cancel; |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | /* Go get the actual blocks */ |
| 826 | error = xfs_bmapi(tp, ip, map_start_fsb, count_fsb, |
| 827 | XFS_BMAPI_WRITE, &first_block, 1, |
| 828 | imap, &nimaps, &free_list); |
| 829 | if (error) |
| 830 | goto trans_cancel; |
| 831 | |
| 832 | error = xfs_bmap_finish(&tp, &free_list, |
| 833 | first_block, &committed); |
| 834 | if (error) |
| 835 | goto trans_cancel; |
| 836 | |
| 837 | error = xfs_trans_commit(tp, |
| 838 | XFS_TRANS_RELEASE_LOG_RES, NULL); |
| 839 | if (error) |
| 840 | goto error0; |
| 841 | |
| 842 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 843 | } |
| 844 | |
| 845 | /* |
| 846 | * See if we were able to allocate an extent that |
| 847 | * covers at least part of the callers request |
| 848 | */ |
| 849 | |
| 850 | for (i = 0; i < nimaps; i++) { |
| 851 | if ( !(io->io_flags & XFS_IOCORE_RT) && |
| 852 | !imap[i].br_startblock) { |
| 853 | cmn_err(CE_PANIC,"Access to block zero: " |
| 854 | "fs <%s> inode: %lld " |
| 855 | "start_block : %llx start_off : %llx " |
| 856 | "blkcnt : %llx extent-state : %x \n", |
| 857 | (ip->i_mount)->m_fsname, |
| 858 | (long long)ip->i_ino, |
| 859 | imap[i].br_startblock, |
| 860 | imap[i].br_startoff, |
| 861 | imap[i].br_blockcount,imap[i].br_state); |
| 862 | } |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 863 | if ((offset_fsb >= imap[i].br_startoff) && |
| 864 | (offset_fsb < (imap[i].br_startoff + |
| 865 | imap[i].br_blockcount))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | *map = imap[i]; |
| 867 | *retmap = 1; |
| 868 | XFS_STATS_INC(xs_xstrat_quick); |
| 869 | return 0; |
| 870 | } |
| 871 | count_fsb -= imap[i].br_blockcount; |
| 872 | } |
| 873 | |
| 874 | /* So far we have not mapped the requested part of the |
| 875 | * file, just surrounding data, try again. |
| 876 | */ |
| 877 | nimaps--; |
Nathan Scott | 24e17b5 | 2005-05-05 13:33:20 -0700 | [diff] [blame] | 878 | map_start_fsb = imap[nimaps].br_startoff + |
| 879 | imap[nimaps].br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | trans_cancel: |
| 883 | xfs_bmap_cancel(&free_list); |
| 884 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT); |
| 885 | error0: |
| 886 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 887 | return XFS_ERROR(error); |
| 888 | } |
| 889 | |
| 890 | int |
| 891 | xfs_iomap_write_unwritten( |
| 892 | xfs_inode_t *ip, |
Nathan Scott | f403b7f | 2005-05-05 13:33:40 -0700 | [diff] [blame] | 893 | xfs_off_t offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | size_t count) |
| 895 | { |
| 896 | xfs_mount_t *mp = ip->i_mount; |
| 897 | xfs_iocore_t *io = &ip->i_iocore; |
| 898 | xfs_trans_t *tp; |
| 899 | xfs_fileoff_t offset_fsb; |
| 900 | xfs_filblks_t count_fsb; |
| 901 | xfs_filblks_t numblks_fsb; |
| 902 | xfs_bmbt_irec_t imap; |
| 903 | int committed; |
| 904 | int error; |
| 905 | int nres; |
| 906 | int nimaps; |
| 907 | xfs_fsblock_t firstfsb; |
| 908 | xfs_bmap_free_t free_list; |
| 909 | |
| 910 | xfs_iomap_enter_trace(XFS_IOMAP_UNWRITTEN, |
| 911 | &ip->i_iocore, offset, count); |
| 912 | |
| 913 | offset_fsb = XFS_B_TO_FSBT(mp, offset); |
| 914 | count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count); |
| 915 | count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb); |
| 916 | |
| 917 | do { |
| 918 | nres = XFS_DIOSTRAT_SPACE_RES(mp, 0); |
| 919 | |
| 920 | /* |
| 921 | * set up a transaction to convert the range of extents |
| 922 | * from unwritten to real. Do allocations in a loop until |
| 923 | * we have covered the range passed in. |
| 924 | */ |
| 925 | |
| 926 | tp = xfs_trans_alloc(mp, XFS_TRANS_STRAT_WRITE); |
| 927 | error = xfs_trans_reserve(tp, nres, |
| 928 | XFS_WRITE_LOG_RES(mp), 0, |
| 929 | XFS_TRANS_PERM_LOG_RES, |
| 930 | XFS_WRITE_LOG_COUNT); |
| 931 | if (error) { |
| 932 | xfs_trans_cancel(tp, 0); |
| 933 | goto error0; |
| 934 | } |
| 935 | |
| 936 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 937 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 938 | xfs_trans_ihold(tp, ip); |
| 939 | |
| 940 | /* |
| 941 | * Modify the unwritten extent state of the buffer. |
| 942 | */ |
| 943 | XFS_BMAP_INIT(&free_list, &firstfsb); |
| 944 | nimaps = 1; |
| 945 | error = xfs_bmapi(tp, ip, offset_fsb, count_fsb, |
| 946 | XFS_BMAPI_WRITE, &firstfsb, |
| 947 | 1, &imap, &nimaps, &free_list); |
| 948 | if (error) |
| 949 | goto error_on_bmapi_transaction; |
| 950 | |
| 951 | error = xfs_bmap_finish(&(tp), &(free_list), |
| 952 | firstfsb, &committed); |
| 953 | if (error) |
| 954 | goto error_on_bmapi_transaction; |
| 955 | |
| 956 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL); |
| 957 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 958 | if (error) |
| 959 | goto error0; |
| 960 | |
| 961 | if ( !(io->io_flags & XFS_IOCORE_RT) && !imap.br_startblock) { |
| 962 | cmn_err(CE_PANIC,"Access to block zero: fs <%s> " |
| 963 | "inode: %lld start_block : %llx start_off : " |
| 964 | "%llx blkcnt : %llx extent-state : %x \n", |
| 965 | (ip->i_mount)->m_fsname, |
| 966 | (long long)ip->i_ino, |
| 967 | imap.br_startblock,imap.br_startoff, |
| 968 | imap.br_blockcount,imap.br_state); |
| 969 | } |
| 970 | |
| 971 | if ((numblks_fsb = imap.br_blockcount) == 0) { |
| 972 | /* |
| 973 | * The numblks_fsb value should always get |
| 974 | * smaller, otherwise the loop is stuck. |
| 975 | */ |
| 976 | ASSERT(imap.br_blockcount); |
| 977 | break; |
| 978 | } |
| 979 | offset_fsb += numblks_fsb; |
| 980 | count_fsb -= numblks_fsb; |
| 981 | } while (count_fsb > 0); |
| 982 | |
| 983 | return 0; |
| 984 | |
| 985 | error_on_bmapi_transaction: |
| 986 | xfs_bmap_cancel(&free_list); |
| 987 | xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT)); |
| 988 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 989 | error0: |
| 990 | return XFS_ERROR(error); |
| 991 | } |