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