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