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