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