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