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