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