| 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" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_fs.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "xfs_types.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 21 | #include "xfs_bit.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "xfs_log.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 23 | #include "xfs_inum.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_trans.h" | 
 | 25 | #include "xfs_sb.h" | 
 | 26 | #include "xfs_ag.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "xfs_dir2.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 28 | #include "xfs_da_btree.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_dir2_sf.h" | 
 | 33 | #include "xfs_dinode.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_inode.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 35 | #include "xfs_btree.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 36 | #include "xfs_mount.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include "xfs_itable.h" | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 38 | #include "xfs_dir2_data.h" | 
 | 39 | #include "xfs_dir2_leaf.h" | 
 | 40 | #include "xfs_dir2_block.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 41 | #include "xfs_inode_item.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include "xfs_extfree_item.h" | 
 | 43 | #include "xfs_alloc.h" | 
 | 44 | #include "xfs_bmap.h" | 
 | 45 | #include "xfs_rtalloc.h" | 
 | 46 | #include "xfs_error.h" | 
| Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 47 | #include "xfs_attr_leaf.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include "xfs_rw.h" | 
 | 49 | #include "xfs_quota.h" | 
 | 50 | #include "xfs_trans_space.h" | 
 | 51 | #include "xfs_buf_item.h" | 
| David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 52 | #include "xfs_filestream.h" | 
| Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 53 | #include "xfs_vnodeops.h" | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 54 | #include "xfs_trace.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 |  | 
 | 56 |  | 
 | 57 | #ifdef DEBUG | 
 | 58 | STATIC void | 
 | 59 | xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork); | 
 | 60 | #endif | 
 | 61 |  | 
 | 62 | kmem_zone_t		*xfs_bmap_free_item_zone; | 
 | 63 |  | 
 | 64 | /* | 
 | 65 |  * Prototypes for internal bmap routines. | 
 | 66 |  */ | 
 | 67 |  | 
 | 68 |  | 
 | 69 | /* | 
 | 70 |  * Called from xfs_bmap_add_attrfork to handle extents format files. | 
 | 71 |  */ | 
 | 72 | STATIC int					/* error */ | 
 | 73 | xfs_bmap_add_attrfork_extents( | 
 | 74 | 	xfs_trans_t		*tp,		/* transaction pointer */ | 
 | 75 | 	xfs_inode_t		*ip,		/* incore inode pointer */ | 
 | 76 | 	xfs_fsblock_t		*firstblock,	/* first block allocated */ | 
 | 77 | 	xfs_bmap_free_t		*flist,		/* blocks to free at commit */ | 
 | 78 | 	int			*flags);	/* inode logging flags */ | 
 | 79 |  | 
 | 80 | /* | 
 | 81 |  * Called from xfs_bmap_add_attrfork to handle local format files. | 
 | 82 |  */ | 
 | 83 | STATIC int					/* error */ | 
 | 84 | xfs_bmap_add_attrfork_local( | 
 | 85 | 	xfs_trans_t		*tp,		/* transaction pointer */ | 
 | 86 | 	xfs_inode_t		*ip,		/* incore inode pointer */ | 
 | 87 | 	xfs_fsblock_t		*firstblock,	/* first block allocated */ | 
 | 88 | 	xfs_bmap_free_t		*flist,		/* blocks to free at commit */ | 
 | 89 | 	int			*flags);	/* inode logging flags */ | 
 | 90 |  | 
 | 91 | /* | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 92 |  * Called by xfs_bmapi to update file extent records and the btree | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 |  * after allocating space (or doing a delayed allocation). | 
 | 94 |  */ | 
 | 95 | STATIC int				/* error */ | 
 | 96 | xfs_bmap_add_extent( | 
 | 97 | 	xfs_inode_t		*ip,	/* incore inode pointer */ | 
 | 98 | 	xfs_extnum_t		idx,	/* extent number to update/insert */ | 
 | 99 | 	xfs_btree_cur_t		**curp,	/* if *curp is null, not a btree */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 100 | 	xfs_bmbt_irec_t		*new,	/* new data to add to file extents */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | 	xfs_fsblock_t		*first,	/* pointer to firstblock variable */ | 
 | 102 | 	xfs_bmap_free_t		*flist,	/* list of extents to be freed */ | 
 | 103 | 	int			*logflagsp, /* inode logging flags */ | 
 | 104 | 	int			whichfork, /* data or attr fork */ | 
 | 105 | 	int			rsvd);	/* OK to allocate reserved blocks */ | 
 | 106 |  | 
 | 107 | /* | 
 | 108 |  * Called by xfs_bmap_add_extent to handle cases converting a delayed | 
 | 109 |  * allocation to a real allocation. | 
 | 110 |  */ | 
 | 111 | STATIC int				/* error */ | 
 | 112 | xfs_bmap_add_extent_delay_real( | 
 | 113 | 	xfs_inode_t		*ip,	/* incore inode pointer */ | 
 | 114 | 	xfs_extnum_t		idx,	/* extent number to update/insert */ | 
 | 115 | 	xfs_btree_cur_t		**curp,	/* if *curp is null, not a btree */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 116 | 	xfs_bmbt_irec_t		*new,	/* new data to add to file extents */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | 	xfs_filblks_t		*dnew,	/* new delayed-alloc indirect blocks */ | 
 | 118 | 	xfs_fsblock_t		*first,	/* pointer to firstblock variable */ | 
 | 119 | 	xfs_bmap_free_t		*flist,	/* list of extents to be freed */ | 
 | 120 | 	int			*logflagsp, /* inode logging flags */ | 
 | 121 | 	int			rsvd);	/* OK to allocate reserved blocks */ | 
 | 122 |  | 
 | 123 | /* | 
 | 124 |  * Called by xfs_bmap_add_extent to handle cases converting a hole | 
 | 125 |  * to a delayed allocation. | 
 | 126 |  */ | 
 | 127 | STATIC int				/* error */ | 
 | 128 | xfs_bmap_add_extent_hole_delay( | 
 | 129 | 	xfs_inode_t		*ip,	/* incore inode pointer */ | 
 | 130 | 	xfs_extnum_t		idx,	/* extent number to update/insert */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 131 | 	xfs_bmbt_irec_t		*new,	/* new data to add to file extents */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | 	int			*logflagsp,/* inode logging flags */ | 
 | 133 | 	int			rsvd);	/* OK to allocate reserved blocks */ | 
 | 134 |  | 
 | 135 | /* | 
 | 136 |  * Called by xfs_bmap_add_extent to handle cases converting a hole | 
 | 137 |  * to a real allocation. | 
 | 138 |  */ | 
 | 139 | STATIC int				/* error */ | 
 | 140 | xfs_bmap_add_extent_hole_real( | 
 | 141 | 	xfs_inode_t		*ip,	/* incore inode pointer */ | 
 | 142 | 	xfs_extnum_t		idx,	/* extent number to update/insert */ | 
 | 143 | 	xfs_btree_cur_t		*cur,	/* if null, not a btree */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 144 | 	xfs_bmbt_irec_t		*new,	/* new data to add to file extents */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | 	int			*logflagsp, /* inode logging flags */ | 
 | 146 | 	int			whichfork); /* data or attr fork */ | 
 | 147 |  | 
 | 148 | /* | 
 | 149 |  * Called by xfs_bmap_add_extent to handle cases converting an unwritten | 
 | 150 |  * allocation to a real allocation or vice versa. | 
 | 151 |  */ | 
 | 152 | STATIC int				/* error */ | 
 | 153 | xfs_bmap_add_extent_unwritten_real( | 
 | 154 | 	xfs_inode_t		*ip,	/* incore inode pointer */ | 
 | 155 | 	xfs_extnum_t		idx,	/* extent number to update/insert */ | 
 | 156 | 	xfs_btree_cur_t		**curp,	/* if *curp is null, not a btree */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 157 | 	xfs_bmbt_irec_t		*new,	/* new data to add to file extents */ | 
| Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 158 | 	int			*logflagsp); /* inode logging flags */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 |  | 
 | 160 | /* | 
 | 161 |  * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. | 
 | 162 |  * It figures out where to ask the underlying allocator to put the new extent. | 
 | 163 |  */ | 
 | 164 | STATIC int				/* error */ | 
 | 165 | xfs_bmap_alloc( | 
 | 166 | 	xfs_bmalloca_t		*ap);	/* bmap alloc argument struct */ | 
 | 167 |  | 
 | 168 | /* | 
 | 169 |  * Transform a btree format file with only one leaf node, where the | 
 | 170 |  * extents list will fit in the inode, into an extents format file. | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 171 |  * Since the file extents are already in-core, all we have to do is | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 |  * give up the space for the btree root and pitch the leaf block. | 
 | 173 |  */ | 
 | 174 | STATIC int				/* error */ | 
 | 175 | xfs_bmap_btree_to_extents( | 
 | 176 | 	xfs_trans_t		*tp,	/* transaction pointer */ | 
 | 177 | 	xfs_inode_t		*ip,	/* incore inode pointer */ | 
 | 178 | 	xfs_btree_cur_t		*cur,	/* btree cursor */ | 
 | 179 | 	int			*logflagsp, /* inode logging flags */ | 
 | 180 | 	int			whichfork); /* data or attr fork */ | 
 | 181 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | /* | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 183 |  * Called by xfs_bmapi to update file extent records and the btree | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 |  * after removing space (or undoing a delayed allocation). | 
 | 185 |  */ | 
 | 186 | STATIC int				/* error */ | 
 | 187 | xfs_bmap_del_extent( | 
 | 188 | 	xfs_inode_t		*ip,	/* incore inode pointer */ | 
 | 189 | 	xfs_trans_t		*tp,	/* current trans pointer */ | 
 | 190 | 	xfs_extnum_t		idx,	/* extent number to update/insert */ | 
 | 191 | 	xfs_bmap_free_t		*flist,	/* list of extents to be freed */ | 
 | 192 | 	xfs_btree_cur_t		*cur,	/* if null, not a btree */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 193 | 	xfs_bmbt_irec_t		*new,	/* new data to add to file extents */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | 	int			*logflagsp,/* inode logging flags */ | 
 | 195 | 	int			whichfork, /* data or attr fork */ | 
 | 196 | 	int			rsvd);	 /* OK to allocate reserved blocks */ | 
 | 197 |  | 
 | 198 | /* | 
 | 199 |  * Remove the entry "free" from the free item list.  Prev points to the | 
 | 200 |  * previous entry, unless "free" is the head of the list. | 
 | 201 |  */ | 
 | 202 | STATIC void | 
 | 203 | xfs_bmap_del_free( | 
 | 204 | 	xfs_bmap_free_t		*flist,	/* free item list header */ | 
 | 205 | 	xfs_bmap_free_item_t	*prev,	/* previous item on list, if any */ | 
 | 206 | 	xfs_bmap_free_item_t	*free);	/* list item to be freed */ | 
 | 207 |  | 
 | 208 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 |  * Convert an extents-format file into a btree-format file. | 
 | 210 |  * The new file will have a root block (in the inode) and a single child block. | 
 | 211 |  */ | 
 | 212 | STATIC int					/* error */ | 
 | 213 | xfs_bmap_extents_to_btree( | 
 | 214 | 	xfs_trans_t		*tp,		/* transaction pointer */ | 
 | 215 | 	xfs_inode_t		*ip,		/* incore inode pointer */ | 
 | 216 | 	xfs_fsblock_t		*firstblock,	/* first-block-allocated */ | 
 | 217 | 	xfs_bmap_free_t		*flist,		/* blocks freed in xaction */ | 
 | 218 | 	xfs_btree_cur_t		**curp,		/* cursor returned to caller */ | 
 | 219 | 	int			wasdel,		/* converting a delayed alloc */ | 
 | 220 | 	int			*logflagsp,	/* inode logging flags */ | 
 | 221 | 	int			whichfork);	/* data or attr fork */ | 
 | 222 |  | 
 | 223 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 |  * Convert a local file to an extents file. | 
 | 225 |  * This code is sort of bogus, since the file data needs to get | 
 | 226 |  * logged so it won't be lost.  The bmap-level manipulations are ok, though. | 
 | 227 |  */ | 
 | 228 | STATIC int				/* error */ | 
 | 229 | xfs_bmap_local_to_extents( | 
 | 230 | 	xfs_trans_t	*tp,		/* transaction pointer */ | 
 | 231 | 	xfs_inode_t	*ip,		/* incore inode pointer */ | 
 | 232 | 	xfs_fsblock_t	*firstblock,	/* first block allocated in xaction */ | 
 | 233 | 	xfs_extlen_t	total,		/* total blocks needed by transaction */ | 
 | 234 | 	int		*logflagsp,	/* inode logging flags */ | 
 | 235 | 	int		whichfork);	/* data or attr fork */ | 
 | 236 |  | 
 | 237 | /* | 
 | 238 |  * Search the extents list for the inode, for the extent containing bno. | 
 | 239 |  * If bno lies in a hole, point to the next entry.  If bno lies past eof, | 
 | 240 |  * *eofp will be set, and *prevp will contain the last entry (null if none). | 
 | 241 |  * Else, *lastxp will be set to the index of the found | 
 | 242 |  * entry; *gotp will contain the entry. | 
 | 243 |  */ | 
| Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 244 | STATIC xfs_bmbt_rec_host_t *		/* pointer to found extent entry */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | xfs_bmap_search_extents( | 
 | 246 | 	xfs_inode_t	*ip,		/* incore inode pointer */ | 
 | 247 | 	xfs_fileoff_t	bno,		/* block number searched for */ | 
 | 248 | 	int		whichfork,	/* data or attr fork */ | 
 | 249 | 	int		*eofp,		/* out: end of file found */ | 
 | 250 | 	xfs_extnum_t	*lastxp,	/* out: last extent index */ | 
 | 251 | 	xfs_bmbt_irec_t	*gotp,		/* out: extent entry found */ | 
 | 252 | 	xfs_bmbt_irec_t	*prevp);	/* out: previous extent entry found */ | 
 | 253 |  | 
| Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 254 | /* | 
 | 255 |  * Check the last inode extent to determine whether this allocation will result | 
 | 256 |  * in blocks being allocated at the end of the file. When we allocate new data | 
 | 257 |  * blocks at the end of the file which do not start at the previous data block, | 
 | 258 |  * we will try to align the new blocks at stripe unit boundaries. | 
 | 259 |  */ | 
 | 260 | STATIC int				/* error */ | 
 | 261 | xfs_bmap_isaeof( | 
 | 262 | 	xfs_inode_t	*ip,		/* incore inode pointer */ | 
 | 263 | 	xfs_fileoff_t   off,		/* file offset in fsblocks */ | 
 | 264 | 	int             whichfork,	/* data or attribute fork */ | 
 | 265 | 	char		*aeof);		/* return value */ | 
 | 266 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | /* | 
 | 268 |  * Compute the worst-case number of indirect blocks that will be used | 
 | 269 |  * for ip's delayed extent of length "len". | 
 | 270 |  */ | 
 | 271 | STATIC xfs_filblks_t | 
 | 272 | xfs_bmap_worst_indlen( | 
 | 273 | 	xfs_inode_t		*ip,	/* incore inode pointer */ | 
 | 274 | 	xfs_filblks_t		len);	/* delayed extent length */ | 
 | 275 |  | 
 | 276 | #ifdef DEBUG | 
 | 277 | /* | 
 | 278 |  * Perform various validation checks on the values being returned | 
 | 279 |  * from xfs_bmapi(). | 
 | 280 |  */ | 
 | 281 | STATIC void | 
 | 282 | xfs_bmap_validate_ret( | 
 | 283 | 	xfs_fileoff_t		bno, | 
 | 284 | 	xfs_filblks_t		len, | 
 | 285 | 	int			flags, | 
 | 286 | 	xfs_bmbt_irec_t		*mval, | 
 | 287 | 	int			nmap, | 
 | 288 | 	int			ret_nmap); | 
 | 289 | #else | 
 | 290 | #define	xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) | 
 | 291 | #endif /* DEBUG */ | 
 | 292 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | STATIC int | 
 | 294 | xfs_bmap_count_tree( | 
 | 295 | 	xfs_mount_t     *mp, | 
 | 296 | 	xfs_trans_t     *tp, | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 297 | 	xfs_ifork_t	*ifp, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | 	xfs_fsblock_t   blockno, | 
 | 299 | 	int             levelin, | 
 | 300 | 	int		*count); | 
 | 301 |  | 
| Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 302 | STATIC void | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | xfs_bmap_count_leaves( | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 304 | 	xfs_ifork_t		*ifp, | 
 | 305 | 	xfs_extnum_t		idx, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | 	int			numrecs, | 
 | 307 | 	int			*count); | 
 | 308 |  | 
| Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 309 | STATIC void | 
| Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 310 | xfs_bmap_disk_count_leaves( | 
| Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 311 | 	struct xfs_mount	*mp, | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 312 | 	struct xfs_btree_block	*block, | 
| Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 313 | 	int			numrecs, | 
 | 314 | 	int			*count); | 
 | 315 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | /* | 
 | 317 |  * Bmap internal routines. | 
 | 318 |  */ | 
 | 319 |  | 
| Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 320 | STATIC int				/* error */ | 
 | 321 | xfs_bmbt_lookup_eq( | 
 | 322 | 	struct xfs_btree_cur	*cur, | 
 | 323 | 	xfs_fileoff_t		off, | 
 | 324 | 	xfs_fsblock_t		bno, | 
 | 325 | 	xfs_filblks_t		len, | 
 | 326 | 	int			*stat)	/* success/failure */ | 
 | 327 | { | 
 | 328 | 	cur->bc_rec.b.br_startoff = off; | 
 | 329 | 	cur->bc_rec.b.br_startblock = bno; | 
 | 330 | 	cur->bc_rec.b.br_blockcount = len; | 
 | 331 | 	return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); | 
 | 332 | } | 
 | 333 |  | 
 | 334 | STATIC int				/* error */ | 
 | 335 | xfs_bmbt_lookup_ge( | 
 | 336 | 	struct xfs_btree_cur	*cur, | 
 | 337 | 	xfs_fileoff_t		off, | 
 | 338 | 	xfs_fsblock_t		bno, | 
 | 339 | 	xfs_filblks_t		len, | 
 | 340 | 	int			*stat)	/* success/failure */ | 
 | 341 | { | 
 | 342 | 	cur->bc_rec.b.br_startoff = off; | 
 | 343 | 	cur->bc_rec.b.br_startblock = bno; | 
 | 344 | 	cur->bc_rec.b.br_blockcount = len; | 
 | 345 | 	return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat); | 
 | 346 | } | 
 | 347 |  | 
| Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 348 | /* | 
 | 349 | * Update the record referred to by cur to the value given | 
 | 350 |  * by [off, bno, len, state]. | 
 | 351 |  * This either works (return 0) or gets an EFSCORRUPTED error. | 
 | 352 |  */ | 
 | 353 | STATIC int | 
 | 354 | xfs_bmbt_update( | 
 | 355 | 	struct xfs_btree_cur	*cur, | 
 | 356 | 	xfs_fileoff_t		off, | 
 | 357 | 	xfs_fsblock_t		bno, | 
 | 358 | 	xfs_filblks_t		len, | 
 | 359 | 	xfs_exntst_t		state) | 
 | 360 | { | 
 | 361 | 	union xfs_btree_rec	rec; | 
 | 362 |  | 
 | 363 | 	xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state); | 
 | 364 | 	return xfs_btree_update(cur, &rec); | 
 | 365 | } | 
| Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 366 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | /* | 
 | 368 |  * Called from xfs_bmap_add_attrfork to handle btree format files. | 
 | 369 |  */ | 
 | 370 | STATIC int					/* error */ | 
 | 371 | xfs_bmap_add_attrfork_btree( | 
 | 372 | 	xfs_trans_t		*tp,		/* transaction pointer */ | 
 | 373 | 	xfs_inode_t		*ip,		/* incore inode pointer */ | 
 | 374 | 	xfs_fsblock_t		*firstblock,	/* first block allocated */ | 
 | 375 | 	xfs_bmap_free_t		*flist,		/* blocks to free at commit */ | 
 | 376 | 	int			*flags)		/* inode logging flags */ | 
 | 377 | { | 
 | 378 | 	xfs_btree_cur_t		*cur;		/* btree cursor */ | 
 | 379 | 	int			error;		/* error return value */ | 
 | 380 | 	xfs_mount_t		*mp;		/* file system mount struct */ | 
 | 381 | 	int			stat;		/* newroot status */ | 
 | 382 |  | 
 | 383 | 	mp = ip->i_mount; | 
 | 384 | 	if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) | 
 | 385 | 		*flags |= XFS_ILOG_DBROOT; | 
 | 386 | 	else { | 
| Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 387 | 		cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | 		cur->bc_private.b.flist = flist; | 
 | 389 | 		cur->bc_private.b.firstblock = *firstblock; | 
 | 390 | 		if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat))) | 
 | 391 | 			goto error0; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 392 | 		/* must be at least one entry */ | 
 | 393 | 		XFS_WANT_CORRUPTED_GOTO(stat == 1, error0); | 
| Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 394 | 		if ((error = xfs_btree_new_iroot(cur, flags, &stat))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | 			goto error0; | 
 | 396 | 		if (stat == 0) { | 
 | 397 | 			xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | 
 | 398 | 			return XFS_ERROR(ENOSPC); | 
 | 399 | 		} | 
 | 400 | 		*firstblock = cur->bc_private.b.firstblock; | 
 | 401 | 		cur->bc_private.b.allocated = 0; | 
 | 402 | 		xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | 
 | 403 | 	} | 
 | 404 | 	return 0; | 
 | 405 | error0: | 
 | 406 | 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | 
 | 407 | 	return error; | 
 | 408 | } | 
 | 409 |  | 
 | 410 | /* | 
 | 411 |  * Called from xfs_bmap_add_attrfork to handle extents format files. | 
 | 412 |  */ | 
 | 413 | STATIC int					/* error */ | 
 | 414 | xfs_bmap_add_attrfork_extents( | 
 | 415 | 	xfs_trans_t		*tp,		/* transaction pointer */ | 
 | 416 | 	xfs_inode_t		*ip,		/* incore inode pointer */ | 
 | 417 | 	xfs_fsblock_t		*firstblock,	/* first block allocated */ | 
 | 418 | 	xfs_bmap_free_t		*flist,		/* blocks to free at commit */ | 
 | 419 | 	int			*flags)		/* inode logging flags */ | 
 | 420 | { | 
 | 421 | 	xfs_btree_cur_t		*cur;		/* bmap btree cursor */ | 
 | 422 | 	int			error;		/* error return value */ | 
 | 423 |  | 
 | 424 | 	if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip)) | 
 | 425 | 		return 0; | 
 | 426 | 	cur = NULL; | 
 | 427 | 	error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0, | 
 | 428 | 		flags, XFS_DATA_FORK); | 
 | 429 | 	if (cur) { | 
 | 430 | 		cur->bc_private.b.allocated = 0; | 
 | 431 | 		xfs_btree_del_cursor(cur, | 
 | 432 | 			error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); | 
 | 433 | 	} | 
 | 434 | 	return error; | 
 | 435 | } | 
 | 436 |  | 
 | 437 | /* | 
 | 438 |  * Called from xfs_bmap_add_attrfork to handle local format files. | 
 | 439 |  */ | 
 | 440 | STATIC int					/* error */ | 
 | 441 | xfs_bmap_add_attrfork_local( | 
 | 442 | 	xfs_trans_t		*tp,		/* transaction pointer */ | 
 | 443 | 	xfs_inode_t		*ip,		/* incore inode pointer */ | 
 | 444 | 	xfs_fsblock_t		*firstblock,	/* first block allocated */ | 
 | 445 | 	xfs_bmap_free_t		*flist,		/* blocks to free at commit */ | 
 | 446 | 	int			*flags)		/* inode logging flags */ | 
 | 447 | { | 
 | 448 | 	xfs_da_args_t		dargs;		/* args for dir/attr code */ | 
 | 449 | 	int			error;		/* error return value */ | 
 | 450 | 	xfs_mount_t		*mp;		/* mount structure pointer */ | 
 | 451 |  | 
 | 452 | 	if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) | 
 | 453 | 		return 0; | 
 | 454 | 	if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) { | 
 | 455 | 		mp = ip->i_mount; | 
 | 456 | 		memset(&dargs, 0, sizeof(dargs)); | 
 | 457 | 		dargs.dp = ip; | 
 | 458 | 		dargs.firstblock = firstblock; | 
 | 459 | 		dargs.flist = flist; | 
 | 460 | 		dargs.total = mp->m_dirblkfsbs; | 
 | 461 | 		dargs.whichfork = XFS_DATA_FORK; | 
 | 462 | 		dargs.trans = tp; | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 463 | 		error = xfs_dir2_sf_to_block(&dargs); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | 	} else | 
 | 465 | 		error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags, | 
 | 466 | 			XFS_DATA_FORK); | 
 | 467 | 	return error; | 
 | 468 | } | 
 | 469 |  | 
 | 470 | /* | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 471 |  * Called by xfs_bmapi to update file extent records and the btree | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 |  * after allocating space (or doing a delayed allocation). | 
 | 473 |  */ | 
 | 474 | STATIC int				/* error */ | 
 | 475 | xfs_bmap_add_extent( | 
 | 476 | 	xfs_inode_t		*ip,	/* incore inode pointer */ | 
 | 477 | 	xfs_extnum_t		idx,	/* extent number to update/insert */ | 
 | 478 | 	xfs_btree_cur_t		**curp,	/* if *curp is null, not a btree */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 479 | 	xfs_bmbt_irec_t		*new,	/* new data to add to file extents */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | 	xfs_fsblock_t		*first,	/* pointer to firstblock variable */ | 
 | 481 | 	xfs_bmap_free_t		*flist,	/* list of extents to be freed */ | 
 | 482 | 	int			*logflagsp, /* inode logging flags */ | 
 | 483 | 	int			whichfork, /* data or attr fork */ | 
 | 484 | 	int			rsvd)	/* OK to use reserved data blocks */ | 
 | 485 | { | 
 | 486 | 	xfs_btree_cur_t		*cur;	/* btree cursor or null */ | 
 | 487 | 	xfs_filblks_t		da_new; /* new count del alloc blocks used */ | 
 | 488 | 	xfs_filblks_t		da_old; /* old count del alloc blocks used */ | 
 | 489 | 	int			error;	/* error return value */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | 	xfs_ifork_t		*ifp;	/* inode fork ptr */ | 
 | 491 | 	int			logflags; /* returned value */ | 
 | 492 | 	xfs_extnum_t		nextents; /* number of extents in file now */ | 
 | 493 |  | 
 | 494 | 	XFS_STATS_INC(xs_add_exlist); | 
 | 495 | 	cur = *curp; | 
 | 496 | 	ifp = XFS_IFORK_PTR(ip, whichfork); | 
 | 497 | 	nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); | 
 | 498 | 	ASSERT(idx <= nextents); | 
 | 499 | 	da_old = da_new = 0; | 
 | 500 | 	error = 0; | 
 | 501 | 	/* | 
 | 502 | 	 * This is the first extent added to a new/empty file. | 
 | 503 | 	 * Special case this one, so other routines get to assume there are | 
 | 504 | 	 * already extents in the list. | 
 | 505 | 	 */ | 
 | 506 | 	if (nextents == 0) { | 
| Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 507 | 		xfs_iext_insert(ip, 0, 1, new, | 
 | 508 | 				whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0); | 
 | 509 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | 		ASSERT(cur == NULL); | 
 | 511 | 		ifp->if_lastex = 0; | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 512 | 		if (!isnullstartblock(new->br_startblock)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | 			XFS_IFORK_NEXT_SET(ip, whichfork, 1); | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 514 | 			logflags = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | 		} else | 
 | 516 | 			logflags = 0; | 
 | 517 | 	} | 
 | 518 | 	/* | 
 | 519 | 	 * Any kind of new delayed allocation goes here. | 
 | 520 | 	 */ | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 521 | 	else if (isnullstartblock(new->br_startblock)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | 		if (cur) | 
 | 523 | 			ASSERT((cur->bc_private.b.flags & | 
 | 524 | 				XFS_BTCUR_BPRV_WASDEL) == 0); | 
| Eric Sandeen | e9ed9d2 | 2007-05-08 13:48:56 +1000 | [diff] [blame] | 525 | 		if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new, | 
| Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 526 | 				&logflags, rsvd))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | 			goto done; | 
 | 528 | 	} | 
 | 529 | 	/* | 
 | 530 | 	 * Real allocation off the end of the file. | 
 | 531 | 	 */ | 
 | 532 | 	else if (idx == nextents) { | 
 | 533 | 		if (cur) | 
 | 534 | 			ASSERT((cur->bc_private.b.flags & | 
 | 535 | 				XFS_BTCUR_BPRV_WASDEL) == 0); | 
 | 536 | 		if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new, | 
| Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 537 | 				&logflags, whichfork))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | 			goto done; | 
 | 539 | 	} else { | 
 | 540 | 		xfs_bmbt_irec_t	prev;	/* old extent at offset idx */ | 
 | 541 |  | 
 | 542 | 		/* | 
 | 543 | 		 * Get the record referred to by idx. | 
 | 544 | 		 */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 545 | 		xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | 		/* | 
 | 547 | 		 * If it's a real allocation record, and the new allocation ends | 
 | 548 | 		 * after the start of the referred to record, then we're filling | 
 | 549 | 		 * in a delayed or unwritten allocation with a real one, or | 
 | 550 | 		 * converting real back to unwritten. | 
 | 551 | 		 */ | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 552 | 		if (!isnullstartblock(new->br_startblock) && | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | 		    new->br_startoff + new->br_blockcount > prev.br_startoff) { | 
 | 554 | 			if (prev.br_state != XFS_EXT_UNWRITTEN && | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 555 | 			    isnullstartblock(prev.br_startblock)) { | 
 | 556 | 				da_old = startblockval(prev.br_startblock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | 				if (cur) | 
 | 558 | 					ASSERT(cur->bc_private.b.flags & | 
 | 559 | 						XFS_BTCUR_BPRV_WASDEL); | 
 | 560 | 				if ((error = xfs_bmap_add_extent_delay_real(ip, | 
 | 561 | 					idx, &cur, new, &da_new, first, flist, | 
| Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 562 | 					&logflags, rsvd))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | 					goto done; | 
 | 564 | 			} else if (new->br_state == XFS_EXT_NORM) { | 
 | 565 | 				ASSERT(new->br_state == XFS_EXT_NORM); | 
 | 566 | 				if ((error = xfs_bmap_add_extent_unwritten_real( | 
| Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 567 | 					ip, idx, &cur, new, &logflags))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | 					goto done; | 
 | 569 | 			} else { | 
 | 570 | 				ASSERT(new->br_state == XFS_EXT_UNWRITTEN); | 
 | 571 | 				if ((error = xfs_bmap_add_extent_unwritten_real( | 
| Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 572 | 					ip, idx, &cur, new, &logflags))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | 					goto done; | 
 | 574 | 			} | 
 | 575 | 			ASSERT(*curp == cur || *curp == NULL); | 
 | 576 | 		} | 
 | 577 | 		/* | 
 | 578 | 		 * Otherwise we're filling in a hole with an allocation. | 
 | 579 | 		 */ | 
 | 580 | 		else { | 
 | 581 | 			if (cur) | 
 | 582 | 				ASSERT((cur->bc_private.b.flags & | 
 | 583 | 					XFS_BTCUR_BPRV_WASDEL) == 0); | 
 | 584 | 			if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, | 
| Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 585 | 					new, &logflags, whichfork))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | 				goto done; | 
 | 587 | 		} | 
 | 588 | 	} | 
 | 589 |  | 
 | 590 | 	ASSERT(*curp == cur || *curp == NULL); | 
 | 591 | 	/* | 
 | 592 | 	 * Convert to a btree if necessary. | 
 | 593 | 	 */ | 
 | 594 | 	if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && | 
 | 595 | 	    XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) { | 
 | 596 | 		int	tmp_logflags;	/* partial log flag return val */ | 
 | 597 |  | 
 | 598 | 		ASSERT(cur == NULL); | 
 | 599 | 		error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first, | 
 | 600 | 			flist, &cur, da_old > 0, &tmp_logflags, whichfork); | 
 | 601 | 		logflags |= tmp_logflags; | 
 | 602 | 		if (error) | 
 | 603 | 			goto done; | 
 | 604 | 	} | 
 | 605 | 	/* | 
 | 606 | 	 * Adjust for changes in reserved delayed indirect blocks. | 
 | 607 | 	 * Nothing to do for disk quotas here. | 
 | 608 | 	 */ | 
 | 609 | 	if (da_old || da_new) { | 
 | 610 | 		xfs_filblks_t	nblks; | 
 | 611 |  | 
 | 612 | 		nblks = da_new; | 
 | 613 | 		if (cur) | 
 | 614 | 			nblks += cur->bc_private.b.allocated; | 
 | 615 | 		ASSERT(nblks <= da_old); | 
 | 616 | 		if (nblks < da_old) | 
 | 617 | 			xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, | 
| David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 618 | 				(int64_t)(da_old - nblks), rsvd); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | 	} | 
 | 620 | 	/* | 
 | 621 | 	 * Clear out the allocated field, done with it now in any case. | 
 | 622 | 	 */ | 
 | 623 | 	if (cur) { | 
 | 624 | 		cur->bc_private.b.allocated = 0; | 
 | 625 | 		*curp = cur; | 
 | 626 | 	} | 
 | 627 | done: | 
 | 628 | #ifdef DEBUG | 
 | 629 | 	if (!error) | 
 | 630 | 		xfs_bmap_check_leaf_extents(*curp, ip, whichfork); | 
 | 631 | #endif | 
 | 632 | 	*logflagsp = logflags; | 
 | 633 | 	return error; | 
 | 634 | } | 
 | 635 |  | 
 | 636 | /* | 
 | 637 |  * Called by xfs_bmap_add_extent to handle cases converting a delayed | 
 | 638 |  * allocation to a real allocation. | 
 | 639 |  */ | 
 | 640 | STATIC int				/* error */ | 
 | 641 | xfs_bmap_add_extent_delay_real( | 
 | 642 | 	xfs_inode_t		*ip,	/* incore inode pointer */ | 
 | 643 | 	xfs_extnum_t		idx,	/* extent number to update/insert */ | 
 | 644 | 	xfs_btree_cur_t		**curp,	/* if *curp is null, not a btree */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 645 | 	xfs_bmbt_irec_t		*new,	/* new data to add to file extents */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | 	xfs_filblks_t		*dnew,	/* new delayed-alloc indirect blocks */ | 
 | 647 | 	xfs_fsblock_t		*first,	/* pointer to firstblock variable */ | 
 | 648 | 	xfs_bmap_free_t		*flist,	/* list of extents to be freed */ | 
 | 649 | 	int			*logflagsp, /* inode logging flags */ | 
 | 650 | 	int			rsvd)	/* OK to use reserved data block allocation */ | 
 | 651 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | 	xfs_btree_cur_t		*cur;	/* btree cursor */ | 
 | 653 | 	int			diff;	/* temp value */ | 
| Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 654 | 	xfs_bmbt_rec_host_t	*ep;	/* extent entry for idx */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | 	int			error;	/* error return value */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | 	int			i;	/* temp state */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 657 | 	xfs_ifork_t		*ifp;	/* inode fork pointer */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | 	xfs_fileoff_t		new_endoff;	/* end offset of new entry */ | 
 | 659 | 	xfs_bmbt_irec_t		r[3];	/* neighbor extent entries */ | 
 | 660 | 					/* left is 0, right is 1, prev is 2 */ | 
 | 661 | 	int			rval=0;	/* return value (logging flags) */ | 
 | 662 | 	int			state = 0;/* state bits, accessed thru macros */ | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 663 | 	xfs_filblks_t		temp=0;	/* value for dnew calculations */ | 
 | 664 | 	xfs_filblks_t		temp2=0;/* value for dnew calculations */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | 	int			tmp_rval;	/* partial logging flags */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 |  | 
 | 667 | #define	LEFT		r[0] | 
 | 668 | #define	RIGHT		r[1] | 
 | 669 | #define	PREV		r[2] | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 |  | 
 | 671 | 	/* | 
 | 672 | 	 * Set up a bunch of variables to make the tests simpler. | 
 | 673 | 	 */ | 
 | 674 | 	cur = *curp; | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 675 | 	ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); | 
 | 676 | 	ep = xfs_iext_get_ext(ifp, idx); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | 	xfs_bmbt_get_all(ep, &PREV); | 
 | 678 | 	new_endoff = new->br_startoff + new->br_blockcount; | 
 | 679 | 	ASSERT(PREV.br_startoff <= new->br_startoff); | 
 | 680 | 	ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 681 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | 	/* | 
 | 683 | 	 * Set flags determining what part of the previous delayed allocation | 
 | 684 | 	 * extent is being replaced by a real allocation. | 
 | 685 | 	 */ | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 686 | 	if (PREV.br_startoff == new->br_startoff) | 
 | 687 | 		state |= BMAP_LEFT_FILLING; | 
 | 688 | 	if (PREV.br_startoff + PREV.br_blockcount == new_endoff) | 
 | 689 | 		state |= BMAP_RIGHT_FILLING; | 
 | 690 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | 	/* | 
 | 692 | 	 * Check and set flags if this segment has a left neighbor. | 
 | 693 | 	 * Don't set contiguous if the combined extent would be too large. | 
 | 694 | 	 */ | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 695 | 	if (idx > 0) { | 
 | 696 | 		state |= BMAP_LEFT_VALID; | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 697 | 		xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT); | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 698 |  | 
 | 699 | 		if (isnullstartblock(LEFT.br_startblock)) | 
 | 700 | 			state |= BMAP_LEFT_DELAY; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | 	} | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 702 |  | 
 | 703 | 	if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && | 
 | 704 | 	    LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && | 
 | 705 | 	    LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && | 
 | 706 | 	    LEFT.br_state == new->br_state && | 
 | 707 | 	    LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) | 
 | 708 | 		state |= BMAP_LEFT_CONTIG; | 
 | 709 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | 	/* | 
 | 711 | 	 * Check and set flags if this segment has a right neighbor. | 
 | 712 | 	 * Don't set contiguous if the combined extent would be too large. | 
 | 713 | 	 * Also check for all-three-contiguous being too large. | 
 | 714 | 	 */ | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 715 | 	if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) { | 
 | 716 | 		state |= BMAP_RIGHT_VALID; | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 717 | 		xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT); | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 718 |  | 
 | 719 | 		if (isnullstartblock(RIGHT.br_startblock)) | 
 | 720 | 			state |= BMAP_RIGHT_DELAY; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | 	} | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 722 |  | 
 | 723 | 	if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && | 
 | 724 | 	    new_endoff == RIGHT.br_startoff && | 
 | 725 | 	    new->br_startblock + new->br_blockcount == RIGHT.br_startblock && | 
 | 726 | 	    new->br_state == RIGHT.br_state && | 
 | 727 | 	    new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && | 
 | 728 | 	    ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | | 
 | 729 | 		       BMAP_RIGHT_FILLING)) != | 
 | 730 | 		      (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | | 
 | 731 | 		       BMAP_RIGHT_FILLING) || | 
 | 732 | 	     LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount | 
 | 733 | 			<= MAXEXTLEN)) | 
 | 734 | 		state |= BMAP_RIGHT_CONTIG; | 
 | 735 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | 	error = 0; | 
 | 737 | 	/* | 
 | 738 | 	 * Switch out based on the FILLING and CONTIG state bits. | 
 | 739 | 	 */ | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 740 | 	switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | | 
 | 741 | 			 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { | 
 | 742 | 	case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | | 
 | 743 | 	     BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | 		/* | 
 | 745 | 		 * Filling in all of a previously delayed allocation extent. | 
 | 746 | 		 * The left and right neighbors are both contiguous with new. | 
 | 747 | 		 */ | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 748 | 		trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 749 | 		xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | 			LEFT.br_blockcount + PREV.br_blockcount + | 
 | 751 | 			RIGHT.br_blockcount); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 752 | 		trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); | 
 | 753 |  | 
| Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 754 | 		xfs_iext_remove(ip, idx, 2, state); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | 		ip->i_df.if_lastex = idx - 1; | 
 | 756 | 		ip->i_d.di_nextents--; | 
 | 757 | 		if (cur == NULL) | 
 | 758 | 			rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | 
 | 759 | 		else { | 
 | 760 | 			rval = XFS_ILOG_CORE; | 
 | 761 | 			if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, | 
 | 762 | 					RIGHT.br_startblock, | 
 | 763 | 					RIGHT.br_blockcount, &i))) | 
 | 764 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 765 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 766 | 			if ((error = xfs_btree_delete(cur, &i))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 768 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 769 | 			if ((error = xfs_btree_decrement(cur, 0, &i))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 771 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | 			if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, | 
 | 773 | 					LEFT.br_startblock, | 
 | 774 | 					LEFT.br_blockcount + | 
 | 775 | 					PREV.br_blockcount + | 
 | 776 | 					RIGHT.br_blockcount, LEFT.br_state))) | 
 | 777 | 				goto done; | 
 | 778 | 		} | 
 | 779 | 		*dnew = 0; | 
 | 780 | 		break; | 
 | 781 |  | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 782 | 	case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | 		/* | 
 | 784 | 		 * Filling in all of a previously delayed allocation extent. | 
 | 785 | 		 * The left neighbor is contiguous, the right is not. | 
 | 786 | 		 */ | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 787 | 		trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 788 | 		xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | 			LEFT.br_blockcount + PREV.br_blockcount); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 790 | 		trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); | 
 | 791 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | 		ip->i_df.if_lastex = idx - 1; | 
| Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 793 | 		xfs_iext_remove(ip, idx, 1, state); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | 		if (cur == NULL) | 
 | 795 | 			rval = XFS_ILOG_DEXT; | 
 | 796 | 		else { | 
 | 797 | 			rval = 0; | 
 | 798 | 			if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff, | 
 | 799 | 					LEFT.br_startblock, LEFT.br_blockcount, | 
 | 800 | 					&i))) | 
 | 801 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 802 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | 			if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, | 
 | 804 | 					LEFT.br_startblock, | 
 | 805 | 					LEFT.br_blockcount + | 
 | 806 | 					PREV.br_blockcount, LEFT.br_state))) | 
 | 807 | 				goto done; | 
 | 808 | 		} | 
 | 809 | 		*dnew = 0; | 
 | 810 | 		break; | 
 | 811 |  | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 812 | 	case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | 		/* | 
 | 814 | 		 * Filling in all of a previously delayed allocation extent. | 
 | 815 | 		 * The right neighbor is contiguous, the left is not. | 
 | 816 | 		 */ | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 817 | 		trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | 		xfs_bmbt_set_startblock(ep, new->br_startblock); | 
 | 819 | 		xfs_bmbt_set_blockcount(ep, | 
 | 820 | 			PREV.br_blockcount + RIGHT.br_blockcount); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 821 | 		trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); | 
 | 822 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | 		ip->i_df.if_lastex = idx; | 
| Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 824 | 		xfs_iext_remove(ip, idx + 1, 1, state); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | 		if (cur == NULL) | 
 | 826 | 			rval = XFS_ILOG_DEXT; | 
 | 827 | 		else { | 
 | 828 | 			rval = 0; | 
 | 829 | 			if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, | 
 | 830 | 					RIGHT.br_startblock, | 
 | 831 | 					RIGHT.br_blockcount, &i))) | 
 | 832 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 833 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | 			if ((error = xfs_bmbt_update(cur, PREV.br_startoff, | 
 | 835 | 					new->br_startblock, | 
 | 836 | 					PREV.br_blockcount + | 
 | 837 | 					RIGHT.br_blockcount, PREV.br_state))) | 
 | 838 | 				goto done; | 
 | 839 | 		} | 
 | 840 | 		*dnew = 0; | 
 | 841 | 		break; | 
 | 842 |  | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 843 | 	case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | 		/* | 
 | 845 | 		 * Filling in all of a previously delayed allocation extent. | 
 | 846 | 		 * Neither the left nor right neighbors are contiguous with | 
 | 847 | 		 * the new one. | 
 | 848 | 		 */ | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 849 | 		trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | 		xfs_bmbt_set_startblock(ep, new->br_startblock); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 851 | 		trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); | 
 | 852 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | 		ip->i_df.if_lastex = idx; | 
 | 854 | 		ip->i_d.di_nextents++; | 
 | 855 | 		if (cur == NULL) | 
 | 856 | 			rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | 
 | 857 | 		else { | 
 | 858 | 			rval = XFS_ILOG_CORE; | 
 | 859 | 			if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, | 
 | 860 | 					new->br_startblock, new->br_blockcount, | 
 | 861 | 					&i))) | 
 | 862 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 863 | 			XFS_WANT_CORRUPTED_GOTO(i == 0, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | 			cur->bc_rec.b.br_state = XFS_EXT_NORM; | 
| Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 865 | 			if ((error = xfs_btree_insert(cur, &i))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 867 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | 		} | 
 | 869 | 		*dnew = 0; | 
 | 870 | 		break; | 
 | 871 |  | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 872 | 	case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | 		/* | 
 | 874 | 		 * Filling in the first part of a previous delayed allocation. | 
 | 875 | 		 * The left neighbor is contiguous. | 
 | 876 | 		 */ | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 877 | 		trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 878 | 		xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | 			LEFT.br_blockcount + new->br_blockcount); | 
 | 880 | 		xfs_bmbt_set_startoff(ep, | 
 | 881 | 			PREV.br_startoff + new->br_blockcount); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 882 | 		trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); | 
 | 883 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | 		temp = PREV.br_blockcount - new->br_blockcount; | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 885 | 		trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | 		xfs_bmbt_set_blockcount(ep, temp); | 
 | 887 | 		ip->i_df.if_lastex = idx - 1; | 
 | 888 | 		if (cur == NULL) | 
 | 889 | 			rval = XFS_ILOG_DEXT; | 
 | 890 | 		else { | 
 | 891 | 			rval = 0; | 
 | 892 | 			if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff, | 
 | 893 | 					LEFT.br_startblock, LEFT.br_blockcount, | 
 | 894 | 					&i))) | 
 | 895 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 896 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | 			if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, | 
 | 898 | 					LEFT.br_startblock, | 
 | 899 | 					LEFT.br_blockcount + | 
 | 900 | 					new->br_blockcount, | 
 | 901 | 					LEFT.br_state))) | 
 | 902 | 				goto done; | 
 | 903 | 		} | 
 | 904 | 		temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 905 | 			startblockval(PREV.br_startblock)); | 
 | 906 | 		xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 907 | 		trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | 		*dnew = temp; | 
 | 909 | 		break; | 
 | 910 |  | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 911 | 	case BMAP_LEFT_FILLING: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | 		/* | 
 | 913 | 		 * Filling in the first part of a previous delayed allocation. | 
 | 914 | 		 * The left neighbor is not contiguous. | 
 | 915 | 		 */ | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 916 | 		trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | 		xfs_bmbt_set_startoff(ep, new_endoff); | 
 | 918 | 		temp = PREV.br_blockcount - new->br_blockcount; | 
 | 919 | 		xfs_bmbt_set_blockcount(ep, temp); | 
| Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 920 | 		xfs_iext_insert(ip, idx, 1, new, state); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | 		ip->i_df.if_lastex = idx; | 
 | 922 | 		ip->i_d.di_nextents++; | 
 | 923 | 		if (cur == NULL) | 
 | 924 | 			rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | 
 | 925 | 		else { | 
 | 926 | 			rval = XFS_ILOG_CORE; | 
 | 927 | 			if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, | 
 | 928 | 					new->br_startblock, new->br_blockcount, | 
 | 929 | 					&i))) | 
 | 930 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 931 | 			XFS_WANT_CORRUPTED_GOTO(i == 0, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | 			cur->bc_rec.b.br_state = XFS_EXT_NORM; | 
| Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 933 | 			if ((error = xfs_btree_insert(cur, &i))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 935 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | 		} | 
 | 937 | 		if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && | 
 | 938 | 		    ip->i_d.di_nextents > ip->i_df.if_ext_max) { | 
 | 939 | 			error = xfs_bmap_extents_to_btree(ip->i_transp, ip, | 
 | 940 | 					first, flist, &cur, 1, &tmp_rval, | 
 | 941 | 					XFS_DATA_FORK); | 
 | 942 | 			rval |= tmp_rval; | 
 | 943 | 			if (error) | 
 | 944 | 				goto done; | 
 | 945 | 		} | 
 | 946 | 		temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 947 | 			startblockval(PREV.br_startblock) - | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | 			(cur ? cur->bc_private.b.allocated : 0)); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 949 | 		ep = xfs_iext_get_ext(ifp, idx + 1); | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 950 | 		xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 951 | 		trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | 		*dnew = temp; | 
 | 953 | 		break; | 
 | 954 |  | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 955 | 	case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | 		/* | 
 | 957 | 		 * Filling in the last part of a previous delayed allocation. | 
 | 958 | 		 * The right neighbor is contiguous with the new allocation. | 
 | 959 | 		 */ | 
 | 960 | 		temp = PREV.br_blockcount - new->br_blockcount; | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 961 | 		trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); | 
 | 962 | 		trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | 		xfs_bmbt_set_blockcount(ep, temp); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 964 | 		xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1), | 
 | 965 | 			new->br_startoff, new->br_startblock, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | 			new->br_blockcount + RIGHT.br_blockcount, | 
 | 967 | 			RIGHT.br_state); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 968 | 		trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | 		ip->i_df.if_lastex = idx + 1; | 
 | 970 | 		if (cur == NULL) | 
 | 971 | 			rval = XFS_ILOG_DEXT; | 
 | 972 | 		else { | 
 | 973 | 			rval = 0; | 
 | 974 | 			if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, | 
 | 975 | 					RIGHT.br_startblock, | 
 | 976 | 					RIGHT.br_blockcount, &i))) | 
 | 977 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 978 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | 			if ((error = xfs_bmbt_update(cur, new->br_startoff, | 
 | 980 | 					new->br_startblock, | 
 | 981 | 					new->br_blockcount + | 
 | 982 | 					RIGHT.br_blockcount, | 
 | 983 | 					RIGHT.br_state))) | 
 | 984 | 				goto done; | 
 | 985 | 		} | 
 | 986 | 		temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 987 | 			startblockval(PREV.br_startblock)); | 
 | 988 | 		xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 989 | 		trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | 		*dnew = temp; | 
 | 991 | 		break; | 
 | 992 |  | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 993 | 	case BMAP_RIGHT_FILLING: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | 		/* | 
 | 995 | 		 * Filling in the last part of a previous delayed allocation. | 
 | 996 | 		 * The right neighbor is not contiguous. | 
 | 997 | 		 */ | 
 | 998 | 		temp = PREV.br_blockcount - new->br_blockcount; | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 999 | 		trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | 		xfs_bmbt_set_blockcount(ep, temp); | 
| Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1001 | 		xfs_iext_insert(ip, idx + 1, 1, new, state); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | 		ip->i_df.if_lastex = idx + 1; | 
 | 1003 | 		ip->i_d.di_nextents++; | 
 | 1004 | 		if (cur == NULL) | 
 | 1005 | 			rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | 
 | 1006 | 		else { | 
 | 1007 | 			rval = XFS_ILOG_CORE; | 
 | 1008 | 			if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, | 
 | 1009 | 					new->br_startblock, new->br_blockcount, | 
 | 1010 | 					&i))) | 
 | 1011 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1012 | 			XFS_WANT_CORRUPTED_GOTO(i == 0, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | 			cur->bc_rec.b.br_state = XFS_EXT_NORM; | 
| Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1014 | 			if ((error = xfs_btree_insert(cur, &i))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1016 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | 		} | 
 | 1018 | 		if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && | 
 | 1019 | 		    ip->i_d.di_nextents > ip->i_df.if_ext_max) { | 
 | 1020 | 			error = xfs_bmap_extents_to_btree(ip->i_transp, ip, | 
 | 1021 | 				first, flist, &cur, 1, &tmp_rval, | 
 | 1022 | 				XFS_DATA_FORK); | 
 | 1023 | 			rval |= tmp_rval; | 
 | 1024 | 			if (error) | 
 | 1025 | 				goto done; | 
 | 1026 | 		} | 
 | 1027 | 		temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1028 | 			startblockval(PREV.br_startblock) - | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | 			(cur ? cur->bc_private.b.allocated : 0)); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1030 | 		ep = xfs_iext_get_ext(ifp, idx); | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1031 | 		xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1032 | 		trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | 		*dnew = temp; | 
 | 1034 | 		break; | 
 | 1035 |  | 
 | 1036 | 	case 0: | 
 | 1037 | 		/* | 
 | 1038 | 		 * Filling in the middle part of a previous delayed allocation. | 
 | 1039 | 		 * Contiguity is impossible here. | 
 | 1040 | 		 * This case is avoided almost all the time. | 
 | 1041 | 		 */ | 
 | 1042 | 		temp = new->br_startoff - PREV.br_startoff; | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1043 | 		trace_xfs_bmap_pre_update(ip, idx, 0, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | 		xfs_bmbt_set_blockcount(ep, temp); | 
 | 1045 | 		r[0] = *new; | 
| Lachlan McIlroy | d213371 | 2006-11-21 18:55:16 +1100 | [diff] [blame] | 1046 | 		r[1].br_state = PREV.br_state; | 
 | 1047 | 		r[1].br_startblock = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | 		r[1].br_startoff = new_endoff; | 
 | 1049 | 		temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff; | 
 | 1050 | 		r[1].br_blockcount = temp2; | 
| Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1051 | 		xfs_iext_insert(ip, idx + 1, 2, &r[0], state); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | 		ip->i_df.if_lastex = idx + 1; | 
 | 1053 | 		ip->i_d.di_nextents++; | 
 | 1054 | 		if (cur == NULL) | 
 | 1055 | 			rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | 
 | 1056 | 		else { | 
 | 1057 | 			rval = XFS_ILOG_CORE; | 
 | 1058 | 			if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, | 
 | 1059 | 					new->br_startblock, new->br_blockcount, | 
 | 1060 | 					&i))) | 
 | 1061 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1062 | 			XFS_WANT_CORRUPTED_GOTO(i == 0, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | 			cur->bc_rec.b.br_state = XFS_EXT_NORM; | 
| Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1064 | 			if ((error = xfs_btree_insert(cur, &i))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1066 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | 		} | 
 | 1068 | 		if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && | 
 | 1069 | 		    ip->i_d.di_nextents > ip->i_df.if_ext_max) { | 
 | 1070 | 			error = xfs_bmap_extents_to_btree(ip->i_transp, ip, | 
 | 1071 | 					first, flist, &cur, 1, &tmp_rval, | 
 | 1072 | 					XFS_DATA_FORK); | 
 | 1073 | 			rval |= tmp_rval; | 
 | 1074 | 			if (error) | 
 | 1075 | 				goto done; | 
 | 1076 | 		} | 
 | 1077 | 		temp = xfs_bmap_worst_indlen(ip, temp); | 
 | 1078 | 		temp2 = xfs_bmap_worst_indlen(ip, temp2); | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1079 | 		diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) - | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | 			(cur ? cur->bc_private.b.allocated : 0)); | 
 | 1081 | 		if (diff > 0 && | 
| David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 1082 | 		    xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | 			/* | 
 | 1084 | 			 * Ick gross gag me with a spoon. | 
 | 1085 | 			 */ | 
 | 1086 | 			ASSERT(0);	/* want to see if this ever happens! */ | 
 | 1087 | 			while (diff > 0) { | 
 | 1088 | 				if (temp) { | 
 | 1089 | 					temp--; | 
 | 1090 | 					diff--; | 
 | 1091 | 					if (!diff || | 
 | 1092 | 					    !xfs_mod_incore_sb(ip->i_mount, | 
| David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 1093 | 						    XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | 						break; | 
 | 1095 | 				} | 
 | 1096 | 				if (temp2) { | 
 | 1097 | 					temp2--; | 
 | 1098 | 					diff--; | 
 | 1099 | 					if (!diff || | 
 | 1100 | 					    !xfs_mod_incore_sb(ip->i_mount, | 
| David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 1101 | 						    XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | 						break; | 
 | 1103 | 				} | 
 | 1104 | 			} | 
 | 1105 | 		} | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1106 | 		ep = xfs_iext_get_ext(ifp, idx); | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1107 | 		xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1108 | 		trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); | 
 | 1109 | 		trace_xfs_bmap_pre_update(ip, idx + 2, state, _THIS_IP_); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1110 | 		xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2), | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1111 | 			nullstartblock((int)temp2)); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1112 | 		trace_xfs_bmap_post_update(ip, idx + 2, state, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | 		*dnew = temp + temp2; | 
 | 1114 | 		break; | 
 | 1115 |  | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1116 | 	case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: | 
 | 1117 | 	case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: | 
 | 1118 | 	case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: | 
 | 1119 | 	case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: | 
 | 1120 | 	case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: | 
 | 1121 | 	case BMAP_LEFT_CONTIG: | 
 | 1122 | 	case BMAP_RIGHT_CONTIG: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | 		/* | 
 | 1124 | 		 * These cases are all impossible. | 
 | 1125 | 		 */ | 
 | 1126 | 		ASSERT(0); | 
 | 1127 | 	} | 
 | 1128 | 	*curp = cur; | 
 | 1129 | done: | 
 | 1130 | 	*logflagsp = rval; | 
 | 1131 | 	return error; | 
 | 1132 | #undef	LEFT | 
 | 1133 | #undef	RIGHT | 
 | 1134 | #undef	PREV | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | } | 
 | 1136 |  | 
 | 1137 | /* | 
 | 1138 |  * Called by xfs_bmap_add_extent to handle cases converting an unwritten | 
 | 1139 |  * allocation to a real allocation or vice versa. | 
 | 1140 |  */ | 
 | 1141 | STATIC int				/* error */ | 
 | 1142 | xfs_bmap_add_extent_unwritten_real( | 
 | 1143 | 	xfs_inode_t		*ip,	/* incore inode pointer */ | 
 | 1144 | 	xfs_extnum_t		idx,	/* extent number to update/insert */ | 
 | 1145 | 	xfs_btree_cur_t		**curp,	/* if *curp is null, not a btree */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1146 | 	xfs_bmbt_irec_t		*new,	/* new data to add to file extents */ | 
| Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 1147 | 	int			*logflagsp) /* inode logging flags */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | 	xfs_btree_cur_t		*cur;	/* btree cursor */ | 
| Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 1150 | 	xfs_bmbt_rec_host_t	*ep;	/* extent entry for idx */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | 	int			error;	/* error return value */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | 	int			i;	/* temp state */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1153 | 	xfs_ifork_t		*ifp;	/* inode fork pointer */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | 	xfs_fileoff_t		new_endoff;	/* end offset of new entry */ | 
 | 1155 | 	xfs_exntst_t		newext;	/* new extent state */ | 
 | 1156 | 	xfs_exntst_t		oldext;	/* old extent state */ | 
 | 1157 | 	xfs_bmbt_irec_t		r[3];	/* neighbor extent entries */ | 
 | 1158 | 					/* left is 0, right is 1, prev is 2 */ | 
 | 1159 | 	int			rval=0;	/* return value (logging flags) */ | 
 | 1160 | 	int			state = 0;/* state bits, accessed thru macros */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 |  | 
 | 1162 | #define	LEFT		r[0] | 
 | 1163 | #define	RIGHT		r[1] | 
 | 1164 | #define	PREV		r[2] | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | 	/* | 
 | 1166 | 	 * Set up a bunch of variables to make the tests simpler. | 
 | 1167 | 	 */ | 
 | 1168 | 	error = 0; | 
 | 1169 | 	cur = *curp; | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1170 | 	ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); | 
 | 1171 | 	ep = xfs_iext_get_ext(ifp, idx); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | 	xfs_bmbt_get_all(ep, &PREV); | 
 | 1173 | 	newext = new->br_state; | 
 | 1174 | 	oldext = (newext == XFS_EXT_UNWRITTEN) ? | 
 | 1175 | 		XFS_EXT_NORM : XFS_EXT_UNWRITTEN; | 
 | 1176 | 	ASSERT(PREV.br_state == oldext); | 
 | 1177 | 	new_endoff = new->br_startoff + new->br_blockcount; | 
 | 1178 | 	ASSERT(PREV.br_startoff <= new->br_startoff); | 
 | 1179 | 	ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1180 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | 	/* | 
 | 1182 | 	 * Set flags determining what part of the previous oldext allocation | 
 | 1183 | 	 * extent is being replaced by a newext allocation. | 
 | 1184 | 	 */ | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1185 | 	if (PREV.br_startoff == new->br_startoff) | 
 | 1186 | 		state |= BMAP_LEFT_FILLING; | 
 | 1187 | 	if (PREV.br_startoff + PREV.br_blockcount == new_endoff) | 
 | 1188 | 		state |= BMAP_RIGHT_FILLING; | 
 | 1189 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | 	/* | 
 | 1191 | 	 * Check and set flags if this segment has a left neighbor. | 
 | 1192 | 	 * Don't set contiguous if the combined extent would be too large. | 
 | 1193 | 	 */ | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1194 | 	if (idx > 0) { | 
 | 1195 | 		state |= BMAP_LEFT_VALID; | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1196 | 		xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT); | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1197 |  | 
 | 1198 | 		if (isnullstartblock(LEFT.br_startblock)) | 
 | 1199 | 			state |= BMAP_LEFT_DELAY; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | 	} | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1201 |  | 
 | 1202 | 	if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && | 
 | 1203 | 	    LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && | 
 | 1204 | 	    LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && | 
 | 1205 | 	    LEFT.br_state == newext && | 
 | 1206 | 	    LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) | 
 | 1207 | 		state |= BMAP_LEFT_CONTIG; | 
 | 1208 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | 	/* | 
 | 1210 | 	 * Check and set flags if this segment has a right neighbor. | 
 | 1211 | 	 * Don't set contiguous if the combined extent would be too large. | 
 | 1212 | 	 * Also check for all-three-contiguous being too large. | 
 | 1213 | 	 */ | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1214 | 	if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) { | 
 | 1215 | 		state |= BMAP_RIGHT_VALID; | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1216 | 		xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT); | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1217 | 		if (isnullstartblock(RIGHT.br_startblock)) | 
 | 1218 | 			state |= BMAP_RIGHT_DELAY; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | 	} | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1220 |  | 
 | 1221 | 	if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && | 
 | 1222 | 	    new_endoff == RIGHT.br_startoff && | 
 | 1223 | 	    new->br_startblock + new->br_blockcount == RIGHT.br_startblock && | 
 | 1224 | 	    newext == RIGHT.br_state && | 
 | 1225 | 	    new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && | 
 | 1226 | 	    ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | | 
 | 1227 | 		       BMAP_RIGHT_FILLING)) != | 
 | 1228 | 		      (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | | 
 | 1229 | 		       BMAP_RIGHT_FILLING) || | 
 | 1230 | 	     LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount | 
 | 1231 | 			<= MAXEXTLEN)) | 
 | 1232 | 		state |= BMAP_RIGHT_CONTIG; | 
 | 1233 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | 	/* | 
 | 1235 | 	 * Switch out based on the FILLING and CONTIG state bits. | 
 | 1236 | 	 */ | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1237 | 	switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | | 
 | 1238 | 			 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { | 
 | 1239 | 	case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | | 
 | 1240 | 	     BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | 		/* | 
 | 1242 | 		 * Setting all of a previous oldext extent to newext. | 
 | 1243 | 		 * The left and right neighbors are both contiguous with new. | 
 | 1244 | 		 */ | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1245 | 		trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1246 | 		xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | 			LEFT.br_blockcount + PREV.br_blockcount + | 
 | 1248 | 			RIGHT.br_blockcount); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1249 | 		trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); | 
 | 1250 |  | 
| Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1251 | 		xfs_iext_remove(ip, idx, 2, state); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | 		ip->i_df.if_lastex = idx - 1; | 
 | 1253 | 		ip->i_d.di_nextents -= 2; | 
 | 1254 | 		if (cur == NULL) | 
 | 1255 | 			rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | 
 | 1256 | 		else { | 
 | 1257 | 			rval = XFS_ILOG_CORE; | 
 | 1258 | 			if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, | 
 | 1259 | 					RIGHT.br_startblock, | 
 | 1260 | 					RIGHT.br_blockcount, &i))) | 
 | 1261 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1262 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1263 | 			if ((error = xfs_btree_delete(cur, &i))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1265 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1266 | 			if ((error = xfs_btree_decrement(cur, 0, &i))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1268 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1269 | 			if ((error = xfs_btree_delete(cur, &i))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1271 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1272 | 			if ((error = xfs_btree_decrement(cur, 0, &i))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1274 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | 			if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, | 
 | 1276 | 				LEFT.br_startblock, | 
 | 1277 | 				LEFT.br_blockcount + PREV.br_blockcount + | 
 | 1278 | 				RIGHT.br_blockcount, LEFT.br_state))) | 
 | 1279 | 				goto done; | 
 | 1280 | 		} | 
 | 1281 | 		break; | 
 | 1282 |  | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1283 | 	case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | 		/* | 
 | 1285 | 		 * Setting all of a previous oldext extent to newext. | 
 | 1286 | 		 * The left neighbor is contiguous, the right is not. | 
 | 1287 | 		 */ | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1288 | 		trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1289 | 		xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | 			LEFT.br_blockcount + PREV.br_blockcount); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1291 | 		trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); | 
 | 1292 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | 		ip->i_df.if_lastex = idx - 1; | 
| Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1294 | 		xfs_iext_remove(ip, idx, 1, state); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | 		ip->i_d.di_nextents--; | 
 | 1296 | 		if (cur == NULL) | 
 | 1297 | 			rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | 
 | 1298 | 		else { | 
 | 1299 | 			rval = XFS_ILOG_CORE; | 
 | 1300 | 			if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, | 
 | 1301 | 					PREV.br_startblock, PREV.br_blockcount, | 
 | 1302 | 					&i))) | 
 | 1303 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1304 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1305 | 			if ((error = xfs_btree_delete(cur, &i))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1307 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1308 | 			if ((error = xfs_btree_decrement(cur, 0, &i))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1310 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | 			if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, | 
 | 1312 | 				LEFT.br_startblock, | 
 | 1313 | 				LEFT.br_blockcount + PREV.br_blockcount, | 
 | 1314 | 				LEFT.br_state))) | 
 | 1315 | 				goto done; | 
 | 1316 | 		} | 
 | 1317 | 		break; | 
 | 1318 |  | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1319 | 	case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | 		/* | 
 | 1321 | 		 * Setting all of a previous oldext extent to newext. | 
 | 1322 | 		 * The right neighbor is contiguous, the left is not. | 
 | 1323 | 		 */ | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1324 | 		trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | 		xfs_bmbt_set_blockcount(ep, | 
 | 1326 | 			PREV.br_blockcount + RIGHT.br_blockcount); | 
 | 1327 | 		xfs_bmbt_set_state(ep, newext); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1328 | 		trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | 		ip->i_df.if_lastex = idx; | 
| Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1330 | 		xfs_iext_remove(ip, idx + 1, 1, state); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | 		ip->i_d.di_nextents--; | 
 | 1332 | 		if (cur == NULL) | 
 | 1333 | 			rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | 
 | 1334 | 		else { | 
 | 1335 | 			rval = XFS_ILOG_CORE; | 
 | 1336 | 			if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, | 
 | 1337 | 					RIGHT.br_startblock, | 
 | 1338 | 					RIGHT.br_blockcount, &i))) | 
 | 1339 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1340 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1341 | 			if ((error = xfs_btree_delete(cur, &i))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1343 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1344 | 			if ((error = xfs_btree_decrement(cur, 0, &i))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1346 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | 			if ((error = xfs_bmbt_update(cur, new->br_startoff, | 
 | 1348 | 				new->br_startblock, | 
 | 1349 | 				new->br_blockcount + RIGHT.br_blockcount, | 
 | 1350 | 				newext))) | 
 | 1351 | 				goto done; | 
 | 1352 | 		} | 
 | 1353 | 		break; | 
 | 1354 |  | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1355 | 	case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | 		/* | 
 | 1357 | 		 * Setting all of a previous oldext extent to newext. | 
 | 1358 | 		 * Neither the left nor right neighbors are contiguous with | 
 | 1359 | 		 * the new one. | 
 | 1360 | 		 */ | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1361 | 		trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | 		xfs_bmbt_set_state(ep, newext); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1363 | 		trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); | 
 | 1364 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | 		ip->i_df.if_lastex = idx; | 
 | 1366 | 		if (cur == NULL) | 
 | 1367 | 			rval = XFS_ILOG_DEXT; | 
 | 1368 | 		else { | 
 | 1369 | 			rval = 0; | 
 | 1370 | 			if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, | 
 | 1371 | 					new->br_startblock, new->br_blockcount, | 
 | 1372 | 					&i))) | 
 | 1373 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1374 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | 			if ((error = xfs_bmbt_update(cur, new->br_startoff, | 
 | 1376 | 				new->br_startblock, new->br_blockcount, | 
 | 1377 | 				newext))) | 
 | 1378 | 				goto done; | 
 | 1379 | 		} | 
 | 1380 | 		break; | 
 | 1381 |  | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1382 | 	case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | 		/* | 
 | 1384 | 		 * Setting the first part of a previous oldext extent to newext. | 
 | 1385 | 		 * The left neighbor is contiguous. | 
 | 1386 | 		 */ | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1387 | 		trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1388 | 		xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | 			LEFT.br_blockcount + new->br_blockcount); | 
 | 1390 | 		xfs_bmbt_set_startoff(ep, | 
 | 1391 | 			PREV.br_startoff + new->br_blockcount); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1392 | 		trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); | 
 | 1393 |  | 
 | 1394 | 		trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | 		xfs_bmbt_set_startblock(ep, | 
 | 1396 | 			new->br_startblock + new->br_blockcount); | 
 | 1397 | 		xfs_bmbt_set_blockcount(ep, | 
 | 1398 | 			PREV.br_blockcount - new->br_blockcount); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1399 | 		trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); | 
 | 1400 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | 		ip->i_df.if_lastex = idx - 1; | 
 | 1402 | 		if (cur == NULL) | 
 | 1403 | 			rval = XFS_ILOG_DEXT; | 
 | 1404 | 		else { | 
 | 1405 | 			rval = 0; | 
 | 1406 | 			if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, | 
 | 1407 | 					PREV.br_startblock, PREV.br_blockcount, | 
 | 1408 | 					&i))) | 
 | 1409 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1410 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | 			if ((error = xfs_bmbt_update(cur, | 
 | 1412 | 				PREV.br_startoff + new->br_blockcount, | 
 | 1413 | 				PREV.br_startblock + new->br_blockcount, | 
 | 1414 | 				PREV.br_blockcount - new->br_blockcount, | 
 | 1415 | 				oldext))) | 
 | 1416 | 				goto done; | 
| Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1417 | 			if ((error = xfs_btree_decrement(cur, 0, &i))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | 				goto done; | 
 | 1419 | 			if (xfs_bmbt_update(cur, LEFT.br_startoff, | 
 | 1420 | 				LEFT.br_startblock, | 
 | 1421 | 				LEFT.br_blockcount + new->br_blockcount, | 
 | 1422 | 				LEFT.br_state)) | 
 | 1423 | 				goto done; | 
 | 1424 | 		} | 
 | 1425 | 		break; | 
 | 1426 |  | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1427 | 	case BMAP_LEFT_FILLING: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | 		/* | 
 | 1429 | 		 * Setting the first part of a previous oldext extent to newext. | 
 | 1430 | 		 * The left neighbor is not contiguous. | 
 | 1431 | 		 */ | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1432 | 		trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | 		ASSERT(ep && xfs_bmbt_get_state(ep) == oldext); | 
 | 1434 | 		xfs_bmbt_set_startoff(ep, new_endoff); | 
 | 1435 | 		xfs_bmbt_set_blockcount(ep, | 
 | 1436 | 			PREV.br_blockcount - new->br_blockcount); | 
 | 1437 | 		xfs_bmbt_set_startblock(ep, | 
 | 1438 | 			new->br_startblock + new->br_blockcount); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1439 | 		trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); | 
 | 1440 |  | 
| Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1441 | 		xfs_iext_insert(ip, idx, 1, new, state); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | 		ip->i_df.if_lastex = idx; | 
 | 1443 | 		ip->i_d.di_nextents++; | 
 | 1444 | 		if (cur == NULL) | 
 | 1445 | 			rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | 
 | 1446 | 		else { | 
 | 1447 | 			rval = XFS_ILOG_CORE; | 
 | 1448 | 			if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, | 
 | 1449 | 					PREV.br_startblock, PREV.br_blockcount, | 
 | 1450 | 					&i))) | 
 | 1451 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1452 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | 			if ((error = xfs_bmbt_update(cur, | 
 | 1454 | 				PREV.br_startoff + new->br_blockcount, | 
 | 1455 | 				PREV.br_startblock + new->br_blockcount, | 
 | 1456 | 				PREV.br_blockcount - new->br_blockcount, | 
 | 1457 | 				oldext))) | 
 | 1458 | 				goto done; | 
 | 1459 | 			cur->bc_rec.b = *new; | 
| Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1460 | 			if ((error = xfs_btree_insert(cur, &i))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1462 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | 		} | 
 | 1464 | 		break; | 
 | 1465 |  | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1466 | 	case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | 		/* | 
 | 1468 | 		 * Setting the last part of a previous oldext extent to newext. | 
 | 1469 | 		 * The right neighbor is contiguous with the new allocation. | 
 | 1470 | 		 */ | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1471 | 		trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); | 
 | 1472 | 		trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | 		xfs_bmbt_set_blockcount(ep, | 
 | 1474 | 			PREV.br_blockcount - new->br_blockcount); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1475 | 		trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1476 | 		xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1), | 
 | 1477 | 			new->br_startoff, new->br_startblock, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | 			new->br_blockcount + RIGHT.br_blockcount, newext); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1479 | 		trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_); | 
 | 1480 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | 		ip->i_df.if_lastex = idx + 1; | 
 | 1482 | 		if (cur == NULL) | 
 | 1483 | 			rval = XFS_ILOG_DEXT; | 
 | 1484 | 		else { | 
 | 1485 | 			rval = 0; | 
 | 1486 | 			if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, | 
 | 1487 | 					PREV.br_startblock, | 
 | 1488 | 					PREV.br_blockcount, &i))) | 
 | 1489 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1490 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | 			if ((error = xfs_bmbt_update(cur, PREV.br_startoff, | 
 | 1492 | 				PREV.br_startblock, | 
 | 1493 | 				PREV.br_blockcount - new->br_blockcount, | 
 | 1494 | 				oldext))) | 
 | 1495 | 				goto done; | 
| Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1496 | 			if ((error = xfs_btree_increment(cur, 0, &i))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | 				goto done; | 
 | 1498 | 			if ((error = xfs_bmbt_update(cur, new->br_startoff, | 
 | 1499 | 				new->br_startblock, | 
 | 1500 | 				new->br_blockcount + RIGHT.br_blockcount, | 
 | 1501 | 				newext))) | 
 | 1502 | 				goto done; | 
 | 1503 | 		} | 
 | 1504 | 		break; | 
 | 1505 |  | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1506 | 	case BMAP_RIGHT_FILLING: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | 		/* | 
 | 1508 | 		 * Setting the last part of a previous oldext extent to newext. | 
 | 1509 | 		 * The right neighbor is not contiguous. | 
 | 1510 | 		 */ | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1511 | 		trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | 		xfs_bmbt_set_blockcount(ep, | 
 | 1513 | 			PREV.br_blockcount - new->br_blockcount); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1514 | 		trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); | 
 | 1515 |  | 
| Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1516 | 		xfs_iext_insert(ip, idx + 1, 1, new, state); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | 		ip->i_df.if_lastex = idx + 1; | 
 | 1518 | 		ip->i_d.di_nextents++; | 
 | 1519 | 		if (cur == NULL) | 
 | 1520 | 			rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | 
 | 1521 | 		else { | 
 | 1522 | 			rval = XFS_ILOG_CORE; | 
 | 1523 | 			if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, | 
 | 1524 | 					PREV.br_startblock, PREV.br_blockcount, | 
 | 1525 | 					&i))) | 
 | 1526 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1527 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | 			if ((error = xfs_bmbt_update(cur, PREV.br_startoff, | 
 | 1529 | 				PREV.br_startblock, | 
 | 1530 | 				PREV.br_blockcount - new->br_blockcount, | 
 | 1531 | 				oldext))) | 
 | 1532 | 				goto done; | 
 | 1533 | 			if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, | 
 | 1534 | 					new->br_startblock, new->br_blockcount, | 
 | 1535 | 					&i))) | 
 | 1536 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1537 | 			XFS_WANT_CORRUPTED_GOTO(i == 0, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | 			cur->bc_rec.b.br_state = XFS_EXT_NORM; | 
| Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1539 | 			if ((error = xfs_btree_insert(cur, &i))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1541 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | 		} | 
 | 1543 | 		break; | 
 | 1544 |  | 
 | 1545 | 	case 0: | 
 | 1546 | 		/* | 
 | 1547 | 		 * Setting the middle part of a previous oldext extent to | 
 | 1548 | 		 * newext.  Contiguity is impossible here. | 
 | 1549 | 		 * One extent becomes three extents. | 
 | 1550 | 		 */ | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1551 | 		trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | 		xfs_bmbt_set_blockcount(ep, | 
 | 1553 | 			new->br_startoff - PREV.br_startoff); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1554 | 		trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); | 
 | 1555 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | 		r[0] = *new; | 
 | 1557 | 		r[1].br_startoff = new_endoff; | 
 | 1558 | 		r[1].br_blockcount = | 
 | 1559 | 			PREV.br_startoff + PREV.br_blockcount - new_endoff; | 
 | 1560 | 		r[1].br_startblock = new->br_startblock + new->br_blockcount; | 
 | 1561 | 		r[1].br_state = oldext; | 
| Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1562 | 		xfs_iext_insert(ip, idx + 1, 2, &r[0], state); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | 		ip->i_df.if_lastex = idx + 1; | 
 | 1564 | 		ip->i_d.di_nextents += 2; | 
 | 1565 | 		if (cur == NULL) | 
 | 1566 | 			rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | 
 | 1567 | 		else { | 
 | 1568 | 			rval = XFS_ILOG_CORE; | 
 | 1569 | 			if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, | 
 | 1570 | 					PREV.br_startblock, PREV.br_blockcount, | 
 | 1571 | 					&i))) | 
 | 1572 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1573 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | 			/* new right extent - oldext */ | 
 | 1575 | 			if ((error = xfs_bmbt_update(cur, r[1].br_startoff, | 
 | 1576 | 				r[1].br_startblock, r[1].br_blockcount, | 
 | 1577 | 				r[1].br_state))) | 
 | 1578 | 				goto done; | 
 | 1579 | 			/* new left extent - oldext */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | 			cur->bc_rec.b = PREV; | 
| Tim Shimmin | 6a617dd | 2008-07-18 17:13:04 +1000 | [diff] [blame] | 1581 | 			cur->bc_rec.b.br_blockcount = | 
 | 1582 | 				new->br_startoff - PREV.br_startoff; | 
| Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1583 | 			if ((error = xfs_btree_insert(cur, &i))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1585 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 1586 | 			/* | 
 | 1587 | 			 * Reset the cursor to the position of the new extent | 
 | 1588 | 			 * we are about to insert as we can't trust it after | 
 | 1589 | 			 * the previous insert. | 
 | 1590 | 			 */ | 
 | 1591 | 			if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, | 
 | 1592 | 					new->br_startblock, new->br_blockcount, | 
 | 1593 | 					&i))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | 				goto done; | 
| Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 1595 | 			XFS_WANT_CORRUPTED_GOTO(i == 0, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | 			/* new middle extent - newext */ | 
| Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 1597 | 			cur->bc_rec.b.br_state = new->br_state; | 
| Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1598 | 			if ((error = xfs_btree_insert(cur, &i))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1600 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | 		} | 
 | 1602 | 		break; | 
 | 1603 |  | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1604 | 	case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: | 
 | 1605 | 	case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: | 
 | 1606 | 	case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: | 
 | 1607 | 	case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: | 
 | 1608 | 	case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: | 
 | 1609 | 	case BMAP_LEFT_CONTIG: | 
 | 1610 | 	case BMAP_RIGHT_CONTIG: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | 		/* | 
 | 1612 | 		 * These cases are all impossible. | 
 | 1613 | 		 */ | 
 | 1614 | 		ASSERT(0); | 
 | 1615 | 	} | 
 | 1616 | 	*curp = cur; | 
 | 1617 | done: | 
 | 1618 | 	*logflagsp = rval; | 
 | 1619 | 	return error; | 
 | 1620 | #undef	LEFT | 
 | 1621 | #undef	RIGHT | 
 | 1622 | #undef	PREV | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | } | 
 | 1624 |  | 
 | 1625 | /* | 
 | 1626 |  * Called by xfs_bmap_add_extent to handle cases converting a hole | 
 | 1627 |  * to a delayed allocation. | 
 | 1628 |  */ | 
 | 1629 | /*ARGSUSED*/ | 
 | 1630 | STATIC int				/* error */ | 
 | 1631 | xfs_bmap_add_extent_hole_delay( | 
 | 1632 | 	xfs_inode_t		*ip,	/* incore inode pointer */ | 
 | 1633 | 	xfs_extnum_t		idx,	/* extent number to update/insert */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1634 | 	xfs_bmbt_irec_t		*new,	/* new data to add to file extents */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | 	int			*logflagsp, /* inode logging flags */ | 
 | 1636 | 	int			rsvd)		/* OK to allocate reserved blocks */ | 
 | 1637 | { | 
| Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 1638 | 	xfs_bmbt_rec_host_t	*ep;	/* extent record for idx */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1639 | 	xfs_ifork_t		*ifp;	/* inode fork pointer */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | 	xfs_bmbt_irec_t		left;	/* left neighbor extent entry */ | 
 | 1641 | 	xfs_filblks_t		newlen=0;	/* new indirect size */ | 
 | 1642 | 	xfs_filblks_t		oldlen=0;	/* old indirect size */ | 
 | 1643 | 	xfs_bmbt_irec_t		right;	/* right neighbor extent entry */ | 
 | 1644 | 	int			state;  /* state bits, accessed thru macros */ | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1645 | 	xfs_filblks_t		temp=0;	/* temp for indirect calculations */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 |  | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1647 | 	ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); | 
 | 1648 | 	ep = xfs_iext_get_ext(ifp, idx); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | 	state = 0; | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1650 | 	ASSERT(isnullstartblock(new->br_startblock)); | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1651 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | 	/* | 
 | 1653 | 	 * Check and set flags if this segment has a left neighbor | 
 | 1654 | 	 */ | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1655 | 	if (idx > 0) { | 
 | 1656 | 		state |= BMAP_LEFT_VALID; | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1657 | 		xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left); | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1658 |  | 
 | 1659 | 		if (isnullstartblock(left.br_startblock)) | 
 | 1660 | 			state |= BMAP_LEFT_DELAY; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | 	} | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1662 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | 	/* | 
 | 1664 | 	 * Check and set flags if the current (right) segment exists. | 
 | 1665 | 	 * If it doesn't exist, we're converting the hole at end-of-file. | 
 | 1666 | 	 */ | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1667 | 	if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) { | 
 | 1668 | 		state |= BMAP_RIGHT_VALID; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | 		xfs_bmbt_get_all(ep, &right); | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1670 |  | 
 | 1671 | 		if (isnullstartblock(right.br_startblock)) | 
 | 1672 | 			state |= BMAP_RIGHT_DELAY; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | 	} | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1674 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | 	/* | 
 | 1676 | 	 * Set contiguity flags on the left and right neighbors. | 
 | 1677 | 	 * Don't let extents get too large, even if the pieces are contiguous. | 
 | 1678 | 	 */ | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1679 | 	if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && | 
 | 1680 | 	    left.br_startoff + left.br_blockcount == new->br_startoff && | 
 | 1681 | 	    left.br_blockcount + new->br_blockcount <= MAXEXTLEN) | 
 | 1682 | 		state |= BMAP_LEFT_CONTIG; | 
 | 1683 |  | 
 | 1684 | 	if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && | 
 | 1685 | 	    new->br_startoff + new->br_blockcount == right.br_startoff && | 
 | 1686 | 	    new->br_blockcount + right.br_blockcount <= MAXEXTLEN && | 
 | 1687 | 	    (!(state & BMAP_LEFT_CONTIG) || | 
 | 1688 | 	     (left.br_blockcount + new->br_blockcount + | 
 | 1689 | 	      right.br_blockcount <= MAXEXTLEN))) | 
 | 1690 | 		state |= BMAP_RIGHT_CONTIG; | 
 | 1691 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | 	/* | 
 | 1693 | 	 * Switch out based on the contiguity flags. | 
 | 1694 | 	 */ | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1695 | 	switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { | 
 | 1696 | 	case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | 		/* | 
 | 1698 | 		 * New allocation is contiguous with delayed allocations | 
 | 1699 | 		 * on the left and on the right. | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1700 | 		 * Merge all three into a single extent record. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | 		 */ | 
 | 1702 | 		temp = left.br_blockcount + new->br_blockcount + | 
 | 1703 | 			right.br_blockcount; | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1704 |  | 
 | 1705 | 		trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1706 | 		xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp); | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1707 | 		oldlen = startblockval(left.br_startblock) + | 
 | 1708 | 			startblockval(new->br_startblock) + | 
 | 1709 | 			startblockval(right.br_startblock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | 		newlen = xfs_bmap_worst_indlen(ip, temp); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1711 | 		xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1), | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1712 | 			nullstartblock((int)newlen)); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1713 | 		trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); | 
 | 1714 |  | 
| Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1715 | 		xfs_iext_remove(ip, idx, 1, state); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | 		ip->i_df.if_lastex = idx - 1; | 
 | 1717 | 		break; | 
 | 1718 |  | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1719 | 	case BMAP_LEFT_CONTIG: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | 		/* | 
 | 1721 | 		 * New allocation is contiguous with a delayed allocation | 
 | 1722 | 		 * on the left. | 
 | 1723 | 		 * Merge the new allocation with the left neighbor. | 
 | 1724 | 		 */ | 
 | 1725 | 		temp = left.br_blockcount + new->br_blockcount; | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1726 | 		trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1727 | 		xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp); | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1728 | 		oldlen = startblockval(left.br_startblock) + | 
 | 1729 | 			startblockval(new->br_startblock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | 		newlen = xfs_bmap_worst_indlen(ip, temp); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1731 | 		xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1), | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1732 | 			nullstartblock((int)newlen)); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1733 | 		trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); | 
 | 1734 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | 		ip->i_df.if_lastex = idx - 1; | 
 | 1736 | 		break; | 
 | 1737 |  | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1738 | 	case BMAP_RIGHT_CONTIG: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | 		/* | 
 | 1740 | 		 * New allocation is contiguous with a delayed allocation | 
 | 1741 | 		 * on the right. | 
 | 1742 | 		 * Merge the new allocation with the right neighbor. | 
 | 1743 | 		 */ | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1744 | 		trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | 		temp = new->br_blockcount + right.br_blockcount; | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1746 | 		oldlen = startblockval(new->br_startblock) + | 
 | 1747 | 			startblockval(right.br_startblock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | 		newlen = xfs_bmap_worst_indlen(ip, temp); | 
 | 1749 | 		xfs_bmbt_set_allf(ep, new->br_startoff, | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1750 | 			nullstartblock((int)newlen), temp, right.br_state); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1751 | 		trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); | 
 | 1752 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | 		ip->i_df.if_lastex = idx; | 
 | 1754 | 		break; | 
 | 1755 |  | 
 | 1756 | 	case 0: | 
 | 1757 | 		/* | 
 | 1758 | 		 * New allocation is not contiguous with another | 
 | 1759 | 		 * delayed allocation. | 
 | 1760 | 		 * Insert a new entry. | 
 | 1761 | 		 */ | 
 | 1762 | 		oldlen = newlen = 0; | 
| Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1763 | 		xfs_iext_insert(ip, idx, 1, new, state); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | 		ip->i_df.if_lastex = idx; | 
 | 1765 | 		break; | 
 | 1766 | 	} | 
 | 1767 | 	if (oldlen != newlen) { | 
 | 1768 | 		ASSERT(oldlen > newlen); | 
 | 1769 | 		xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, | 
| David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 1770 | 			(int64_t)(oldlen - newlen), rsvd); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | 		/* | 
 | 1772 | 		 * Nothing to do for disk quota accounting here. | 
 | 1773 | 		 */ | 
 | 1774 | 	} | 
 | 1775 | 	*logflagsp = 0; | 
 | 1776 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1777 | } | 
 | 1778 |  | 
 | 1779 | /* | 
 | 1780 |  * Called by xfs_bmap_add_extent to handle cases converting a hole | 
 | 1781 |  * to a real allocation. | 
 | 1782 |  */ | 
 | 1783 | STATIC int				/* error */ | 
 | 1784 | xfs_bmap_add_extent_hole_real( | 
 | 1785 | 	xfs_inode_t		*ip,	/* incore inode pointer */ | 
 | 1786 | 	xfs_extnum_t		idx,	/* extent number to update/insert */ | 
 | 1787 | 	xfs_btree_cur_t		*cur,	/* if null, not a btree */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1788 | 	xfs_bmbt_irec_t		*new,	/* new data to add to file extents */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | 	int			*logflagsp, /* inode logging flags */ | 
 | 1790 | 	int			whichfork) /* data or attr fork */ | 
 | 1791 | { | 
| Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 1792 | 	xfs_bmbt_rec_host_t	*ep;	/* pointer to extent entry ins. point */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | 	int			error;	/* error return value */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | 	int			i;	/* temp state */ | 
 | 1795 | 	xfs_ifork_t		*ifp;	/* inode fork pointer */ | 
 | 1796 | 	xfs_bmbt_irec_t		left;	/* left neighbor extent entry */ | 
 | 1797 | 	xfs_bmbt_irec_t		right;	/* right neighbor extent entry */ | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1798 | 	int			rval=0;	/* return value (logging flags) */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | 	int			state;	/* state bits, accessed thru macros */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 |  | 
 | 1801 | 	ifp = XFS_IFORK_PTR(ip, whichfork); | 
 | 1802 | 	ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1803 | 	ep = xfs_iext_get_ext(ifp, idx); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | 	state = 0; | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1805 |  | 
| Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1806 | 	if (whichfork == XFS_ATTR_FORK) | 
 | 1807 | 		state |= BMAP_ATTRFORK; | 
 | 1808 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | 	/* | 
 | 1810 | 	 * Check and set flags if this segment has a left neighbor. | 
 | 1811 | 	 */ | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1812 | 	if (idx > 0) { | 
 | 1813 | 		state |= BMAP_LEFT_VALID; | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1814 | 		xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left); | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1815 | 		if (isnullstartblock(left.br_startblock)) | 
 | 1816 | 			state |= BMAP_LEFT_DELAY; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | 	} | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1818 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | 	/* | 
 | 1820 | 	 * Check and set flags if this segment has a current value. | 
 | 1821 | 	 * Not true if we're inserting into the "hole" at eof. | 
 | 1822 | 	 */ | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1823 | 	if (idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) { | 
 | 1824 | 		state |= BMAP_RIGHT_VALID; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | 		xfs_bmbt_get_all(ep, &right); | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1826 | 		if (isnullstartblock(right.br_startblock)) | 
 | 1827 | 			state |= BMAP_RIGHT_DELAY; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | 	} | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1829 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | 	/* | 
 | 1831 | 	 * We're inserting a real allocation between "left" and "right". | 
 | 1832 | 	 * Set the contiguity flags.  Don't let extents get too large. | 
 | 1833 | 	 */ | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1834 | 	if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && | 
 | 1835 | 	    left.br_startoff + left.br_blockcount == new->br_startoff && | 
 | 1836 | 	    left.br_startblock + left.br_blockcount == new->br_startblock && | 
 | 1837 | 	    left.br_state == new->br_state && | 
 | 1838 | 	    left.br_blockcount + new->br_blockcount <= MAXEXTLEN) | 
 | 1839 | 		state |= BMAP_LEFT_CONTIG; | 
 | 1840 |  | 
 | 1841 | 	if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && | 
 | 1842 | 	    new->br_startoff + new->br_blockcount == right.br_startoff && | 
 | 1843 | 	    new->br_startblock + new->br_blockcount == right.br_startblock && | 
 | 1844 | 	    new->br_state == right.br_state && | 
 | 1845 | 	    new->br_blockcount + right.br_blockcount <= MAXEXTLEN && | 
 | 1846 | 	    (!(state & BMAP_LEFT_CONTIG) || | 
 | 1847 | 	     left.br_blockcount + new->br_blockcount + | 
 | 1848 | 	     right.br_blockcount <= MAXEXTLEN)) | 
 | 1849 | 		state |= BMAP_RIGHT_CONTIG; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 |  | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1851 | 	error = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | 	/* | 
 | 1853 | 	 * Select which case we're in here, and implement it. | 
 | 1854 | 	 */ | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1855 | 	switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { | 
 | 1856 | 	case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | 		/* | 
 | 1858 | 		 * New allocation is contiguous with real allocations on the | 
 | 1859 | 		 * left and on the right. | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1860 | 		 * Merge all three into a single extent record. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | 		 */ | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1862 | 		trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1863 | 		xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | 			left.br_blockcount + new->br_blockcount + | 
 | 1865 | 			right.br_blockcount); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1866 | 		trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); | 
 | 1867 |  | 
| Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1868 | 		xfs_iext_remove(ip, idx, 1, state); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | 		ifp->if_lastex = idx - 1; | 
 | 1870 | 		XFS_IFORK_NEXT_SET(ip, whichfork, | 
 | 1871 | 			XFS_IFORK_NEXTENTS(ip, whichfork) - 1); | 
 | 1872 | 		if (cur == NULL) { | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1873 | 			rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1874 | 		} else { | 
 | 1875 | 			rval = XFS_ILOG_CORE; | 
 | 1876 | 			if ((error = xfs_bmbt_lookup_eq(cur, | 
 | 1877 | 					right.br_startoff, | 
 | 1878 | 					right.br_startblock, | 
 | 1879 | 					right.br_blockcount, &i))) | 
 | 1880 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1881 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1882 | 			if ((error = xfs_btree_delete(cur, &i))) | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1883 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1884 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1885 | 			if ((error = xfs_btree_decrement(cur, 0, &i))) | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1886 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1887 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1888 | 			if ((error = xfs_bmbt_update(cur, left.br_startoff, | 
 | 1889 | 					left.br_startblock, | 
 | 1890 | 					left.br_blockcount + | 
 | 1891 | 						new->br_blockcount + | 
 | 1892 | 						right.br_blockcount, | 
 | 1893 | 					left.br_state))) | 
 | 1894 | 				goto done; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | 		} | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1896 | 		break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 |  | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1898 | 	case BMAP_LEFT_CONTIG: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | 		/* | 
 | 1900 | 		 * New allocation is contiguous with a real allocation | 
 | 1901 | 		 * on the left. | 
 | 1902 | 		 * Merge the new allocation with the left neighbor. | 
 | 1903 | 		 */ | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1904 | 		trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1905 | 		xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | 			left.br_blockcount + new->br_blockcount); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1907 | 		trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); | 
 | 1908 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | 		ifp->if_lastex = idx - 1; | 
 | 1910 | 		if (cur == NULL) { | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1911 | 			rval = xfs_ilog_fext(whichfork); | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1912 | 		} else { | 
 | 1913 | 			rval = 0; | 
 | 1914 | 			if ((error = xfs_bmbt_lookup_eq(cur, | 
 | 1915 | 					left.br_startoff, | 
 | 1916 | 					left.br_startblock, | 
 | 1917 | 					left.br_blockcount, &i))) | 
 | 1918 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1919 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1920 | 			if ((error = xfs_bmbt_update(cur, left.br_startoff, | 
 | 1921 | 					left.br_startblock, | 
 | 1922 | 					left.br_blockcount + | 
 | 1923 | 						new->br_blockcount, | 
 | 1924 | 					left.br_state))) | 
 | 1925 | 				goto done; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 | 		} | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1927 | 		break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 |  | 
| Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1929 | 	case BMAP_RIGHT_CONTIG: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1930 | 		/* | 
 | 1931 | 		 * New allocation is contiguous with a real allocation | 
 | 1932 | 		 * on the right. | 
 | 1933 | 		 * Merge the new allocation with the right neighbor. | 
 | 1934 | 		 */ | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1935 | 		trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | 		xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock, | 
 | 1937 | 			new->br_blockcount + right.br_blockcount, | 
 | 1938 | 			right.br_state); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1939 | 		trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); | 
 | 1940 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | 		ifp->if_lastex = idx; | 
 | 1942 | 		if (cur == NULL) { | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1943 | 			rval = xfs_ilog_fext(whichfork); | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1944 | 		} else { | 
 | 1945 | 			rval = 0; | 
 | 1946 | 			if ((error = xfs_bmbt_lookup_eq(cur, | 
 | 1947 | 					right.br_startoff, | 
 | 1948 | 					right.br_startblock, | 
 | 1949 | 					right.br_blockcount, &i))) | 
 | 1950 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1951 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1952 | 			if ((error = xfs_bmbt_update(cur, new->br_startoff, | 
 | 1953 | 					new->br_startblock, | 
 | 1954 | 					new->br_blockcount + | 
 | 1955 | 						right.br_blockcount, | 
 | 1956 | 					right.br_state))) | 
 | 1957 | 				goto done; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | 		} | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1959 | 		break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 |  | 
 | 1961 | 	case 0: | 
 | 1962 | 		/* | 
 | 1963 | 		 * New allocation is not contiguous with another | 
 | 1964 | 		 * real allocation. | 
 | 1965 | 		 * Insert a new entry. | 
 | 1966 | 		 */ | 
| Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1967 | 		xfs_iext_insert(ip, idx, 1, new, state); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | 		ifp->if_lastex = idx; | 
 | 1969 | 		XFS_IFORK_NEXT_SET(ip, whichfork, | 
 | 1970 | 			XFS_IFORK_NEXTENTS(ip, whichfork) + 1); | 
 | 1971 | 		if (cur == NULL) { | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1972 | 			rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1973 | 		} else { | 
 | 1974 | 			rval = XFS_ILOG_CORE; | 
 | 1975 | 			if ((error = xfs_bmbt_lookup_eq(cur, | 
 | 1976 | 					new->br_startoff, | 
 | 1977 | 					new->br_startblock, | 
 | 1978 | 					new->br_blockcount, &i))) | 
 | 1979 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1980 | 			XFS_WANT_CORRUPTED_GOTO(i == 0, done); | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1981 | 			cur->bc_rec.b.br_state = new->br_state; | 
| Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1982 | 			if ((error = xfs_btree_insert(cur, &i))) | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1983 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1984 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | 		} | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1986 | 		break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | 	} | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1988 | done: | 
 | 1989 | 	*logflagsp = rval; | 
 | 1990 | 	return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | } | 
 | 1992 |  | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1993 | /* | 
 | 1994 |  * Adjust the size of the new extent based on di_extsize and rt extsize. | 
 | 1995 |  */ | 
 | 1996 | STATIC int | 
 | 1997 | xfs_bmap_extsize_align( | 
 | 1998 | 	xfs_mount_t	*mp, | 
 | 1999 | 	xfs_bmbt_irec_t	*gotp,		/* next extent pointer */ | 
 | 2000 | 	xfs_bmbt_irec_t	*prevp,		/* previous extent pointer */ | 
 | 2001 | 	xfs_extlen_t	extsz,		/* align to this extent size */ | 
 | 2002 | 	int		rt,		/* is this a realtime inode? */ | 
 | 2003 | 	int		eof,		/* is extent at end-of-file? */ | 
 | 2004 | 	int		delay,		/* creating delalloc extent? */ | 
 | 2005 | 	int		convert,	/* overwriting unwritten extent? */ | 
 | 2006 | 	xfs_fileoff_t	*offp,		/* in/out: aligned offset */ | 
 | 2007 | 	xfs_extlen_t	*lenp)		/* in/out: aligned length */ | 
 | 2008 | { | 
 | 2009 | 	xfs_fileoff_t	orig_off;	/* original offset */ | 
 | 2010 | 	xfs_extlen_t	orig_alen;	/* original length */ | 
 | 2011 | 	xfs_fileoff_t	orig_end;	/* original off+len */ | 
 | 2012 | 	xfs_fileoff_t	nexto;		/* next file offset */ | 
 | 2013 | 	xfs_fileoff_t	prevo;		/* previous file offset */ | 
 | 2014 | 	xfs_fileoff_t	align_off;	/* temp for offset */ | 
 | 2015 | 	xfs_extlen_t	align_alen;	/* temp for length */ | 
 | 2016 | 	xfs_extlen_t	temp;		/* temp for calculations */ | 
 | 2017 |  | 
 | 2018 | 	if (convert) | 
 | 2019 | 		return 0; | 
 | 2020 |  | 
 | 2021 | 	orig_off = align_off = *offp; | 
 | 2022 | 	orig_alen = align_alen = *lenp; | 
 | 2023 | 	orig_end = orig_off + orig_alen; | 
 | 2024 |  | 
 | 2025 | 	/* | 
 | 2026 | 	 * If this request overlaps an existing extent, then don't | 
 | 2027 | 	 * attempt to perform any additional alignment. | 
 | 2028 | 	 */ | 
 | 2029 | 	if (!delay && !eof && | 
 | 2030 | 	    (orig_off >= gotp->br_startoff) && | 
 | 2031 | 	    (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { | 
 | 2032 | 		return 0; | 
 | 2033 | 	} | 
 | 2034 |  | 
 | 2035 | 	/* | 
 | 2036 | 	 * If the file offset is unaligned vs. the extent size | 
 | 2037 | 	 * we need to align it.  This will be possible unless | 
 | 2038 | 	 * the file was previously written with a kernel that didn't | 
 | 2039 | 	 * perform this alignment, or if a truncate shot us in the | 
 | 2040 | 	 * foot. | 
 | 2041 | 	 */ | 
 | 2042 | 	temp = do_mod(orig_off, extsz); | 
 | 2043 | 	if (temp) { | 
 | 2044 | 		align_alen += temp; | 
 | 2045 | 		align_off -= temp; | 
 | 2046 | 	} | 
 | 2047 | 	/* | 
 | 2048 | 	 * Same adjustment for the end of the requested area. | 
 | 2049 | 	 */ | 
 | 2050 | 	if ((temp = (align_alen % extsz))) { | 
 | 2051 | 		align_alen += extsz - temp; | 
 | 2052 | 	} | 
 | 2053 | 	/* | 
 | 2054 | 	 * If the previous block overlaps with this proposed allocation | 
 | 2055 | 	 * then move the start forward without adjusting the length. | 
 | 2056 | 	 */ | 
 | 2057 | 	if (prevp->br_startoff != NULLFILEOFF) { | 
 | 2058 | 		if (prevp->br_startblock == HOLESTARTBLOCK) | 
 | 2059 | 			prevo = prevp->br_startoff; | 
 | 2060 | 		else | 
 | 2061 | 			prevo = prevp->br_startoff + prevp->br_blockcount; | 
 | 2062 | 	} else | 
 | 2063 | 		prevo = 0; | 
 | 2064 | 	if (align_off != orig_off && align_off < prevo) | 
 | 2065 | 		align_off = prevo; | 
 | 2066 | 	/* | 
 | 2067 | 	 * If the next block overlaps with this proposed allocation | 
 | 2068 | 	 * then move the start back without adjusting the length, | 
 | 2069 | 	 * but not before offset 0. | 
 | 2070 | 	 * This may of course make the start overlap previous block, | 
 | 2071 | 	 * and if we hit the offset 0 limit then the next block | 
 | 2072 | 	 * can still overlap too. | 
 | 2073 | 	 */ | 
 | 2074 | 	if (!eof && gotp->br_startoff != NULLFILEOFF) { | 
 | 2075 | 		if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || | 
 | 2076 | 		    (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) | 
 | 2077 | 			nexto = gotp->br_startoff + gotp->br_blockcount; | 
 | 2078 | 		else | 
 | 2079 | 			nexto = gotp->br_startoff; | 
 | 2080 | 	} else | 
 | 2081 | 		nexto = NULLFILEOFF; | 
 | 2082 | 	if (!eof && | 
 | 2083 | 	    align_off + align_alen != orig_end && | 
 | 2084 | 	    align_off + align_alen > nexto) | 
 | 2085 | 		align_off = nexto > align_alen ? nexto - align_alen : 0; | 
 | 2086 | 	/* | 
 | 2087 | 	 * If we're now overlapping the next or previous extent that | 
 | 2088 | 	 * means we can't fit an extsz piece in this hole.  Just move | 
 | 2089 | 	 * the start forward to the first valid spot and set | 
 | 2090 | 	 * the length so we hit the end. | 
 | 2091 | 	 */ | 
 | 2092 | 	if (align_off != orig_off && align_off < prevo) | 
 | 2093 | 		align_off = prevo; | 
 | 2094 | 	if (align_off + align_alen != orig_end && | 
 | 2095 | 	    align_off + align_alen > nexto && | 
 | 2096 | 	    nexto != NULLFILEOFF) { | 
 | 2097 | 		ASSERT(nexto > prevo); | 
 | 2098 | 		align_alen = nexto - align_off; | 
 | 2099 | 	} | 
 | 2100 |  | 
 | 2101 | 	/* | 
 | 2102 | 	 * If realtime, and the result isn't a multiple of the realtime | 
 | 2103 | 	 * extent size we need to remove blocks until it is. | 
 | 2104 | 	 */ | 
 | 2105 | 	if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { | 
 | 2106 | 		/* | 
 | 2107 | 		 * We're not covering the original request, or | 
 | 2108 | 		 * we won't be able to once we fix the length. | 
 | 2109 | 		 */ | 
 | 2110 | 		if (orig_off < align_off || | 
 | 2111 | 		    orig_end > align_off + align_alen || | 
 | 2112 | 		    align_alen - temp < orig_alen) | 
 | 2113 | 			return XFS_ERROR(EINVAL); | 
 | 2114 | 		/* | 
 | 2115 | 		 * Try to fix it by moving the start up. | 
 | 2116 | 		 */ | 
 | 2117 | 		if (align_off + temp <= orig_off) { | 
 | 2118 | 			align_alen -= temp; | 
 | 2119 | 			align_off += temp; | 
 | 2120 | 		} | 
 | 2121 | 		/* | 
 | 2122 | 		 * Try to fix it by moving the end in. | 
 | 2123 | 		 */ | 
 | 2124 | 		else if (align_off + align_alen - temp >= orig_end) | 
 | 2125 | 			align_alen -= temp; | 
 | 2126 | 		/* | 
 | 2127 | 		 * Set the start to the minimum then trim the length. | 
 | 2128 | 		 */ | 
 | 2129 | 		else { | 
 | 2130 | 			align_alen -= orig_off - align_off; | 
 | 2131 | 			align_off = orig_off; | 
 | 2132 | 			align_alen -= align_alen % mp->m_sb.sb_rextsize; | 
 | 2133 | 		} | 
 | 2134 | 		/* | 
 | 2135 | 		 * Result doesn't cover the request, fail it. | 
 | 2136 | 		 */ | 
 | 2137 | 		if (orig_off < align_off || orig_end > align_off + align_alen) | 
 | 2138 | 			return XFS_ERROR(EINVAL); | 
 | 2139 | 	} else { | 
 | 2140 | 		ASSERT(orig_off >= align_off); | 
 | 2141 | 		ASSERT(orig_end <= align_off + align_alen); | 
 | 2142 | 	} | 
 | 2143 |  | 
 | 2144 | #ifdef DEBUG | 
 | 2145 | 	if (!eof && gotp->br_startoff != NULLFILEOFF) | 
 | 2146 | 		ASSERT(align_off + align_alen <= gotp->br_startoff); | 
 | 2147 | 	if (prevp->br_startoff != NULLFILEOFF) | 
 | 2148 | 		ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); | 
 | 2149 | #endif | 
 | 2150 |  | 
 | 2151 | 	*lenp = align_alen; | 
 | 2152 | 	*offp = align_off; | 
 | 2153 | 	return 0; | 
 | 2154 | } | 
 | 2155 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | #define XFS_ALLOC_GAP_UNITS	4 | 
 | 2157 |  | 
| David Chinner | c2b1cba | 2008-04-10 12:21:40 +1000 | [diff] [blame] | 2158 | STATIC void | 
| Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2159 | xfs_bmap_adjacent( | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | 	xfs_bmalloca_t	*ap)		/* bmap alloc argument struct */ | 
 | 2161 | { | 
 | 2162 | 	xfs_fsblock_t	adjust;		/* adjustment to block numbers */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | 	xfs_agnumber_t	fb_agno;	/* ag number of ap->firstblock */ | 
 | 2164 | 	xfs_mount_t	*mp;		/* mount point structure */ | 
 | 2165 | 	int		nullfb;		/* true if ap->firstblock isn't set */ | 
 | 2166 | 	int		rt;		/* true if inode is realtime */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 |  | 
 | 2168 | #define	ISVALID(x,y)	\ | 
 | 2169 | 	(rt ? \ | 
 | 2170 | 		(x) < mp->m_sb.sb_rblocks : \ | 
 | 2171 | 		XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ | 
 | 2172 | 		XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ | 
 | 2173 | 		XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) | 
 | 2174 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | 	mp = ap->ip->i_mount; | 
 | 2176 | 	nullfb = ap->firstblock == NULLFSBLOCK; | 
 | 2177 | 	rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata; | 
 | 2178 | 	fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | 	/* | 
 | 2180 | 	 * If allocating at eof, and there's a previous real block, | 
| Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 2181 | 	 * try to use its last block as our starting point. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | 	 */ | 
 | 2183 | 	if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF && | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2184 | 	    !isnullstartblock(ap->prevp->br_startblock) && | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2185 | 	    ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount, | 
 | 2186 | 		    ap->prevp->br_startblock)) { | 
 | 2187 | 		ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount; | 
 | 2188 | 		/* | 
 | 2189 | 		 * Adjust for the gap between prevp and us. | 
 | 2190 | 		 */ | 
 | 2191 | 		adjust = ap->off - | 
 | 2192 | 			(ap->prevp->br_startoff + ap->prevp->br_blockcount); | 
 | 2193 | 		if (adjust && | 
 | 2194 | 		    ISVALID(ap->rval + adjust, ap->prevp->br_startblock)) | 
 | 2195 | 			ap->rval += adjust; | 
 | 2196 | 	} | 
 | 2197 | 	/* | 
 | 2198 | 	 * If not at eof, then compare the two neighbor blocks. | 
 | 2199 | 	 * Figure out whether either one gives us a good starting point, | 
 | 2200 | 	 * and pick the better one. | 
 | 2201 | 	 */ | 
 | 2202 | 	else if (!ap->eof) { | 
 | 2203 | 		xfs_fsblock_t	gotbno;		/* right side block number */ | 
 | 2204 | 		xfs_fsblock_t	gotdiff=0;	/* right side difference */ | 
 | 2205 | 		xfs_fsblock_t	prevbno;	/* left side block number */ | 
 | 2206 | 		xfs_fsblock_t	prevdiff=0;	/* left side difference */ | 
 | 2207 |  | 
 | 2208 | 		/* | 
 | 2209 | 		 * If there's a previous (left) block, select a requested | 
 | 2210 | 		 * start block based on it. | 
 | 2211 | 		 */ | 
 | 2212 | 		if (ap->prevp->br_startoff != NULLFILEOFF && | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2213 | 		    !isnullstartblock(ap->prevp->br_startblock) && | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2214 | 		    (prevbno = ap->prevp->br_startblock + | 
 | 2215 | 			       ap->prevp->br_blockcount) && | 
 | 2216 | 		    ISVALID(prevbno, ap->prevp->br_startblock)) { | 
 | 2217 | 			/* | 
 | 2218 | 			 * Calculate gap to end of previous block. | 
 | 2219 | 			 */ | 
 | 2220 | 			adjust = prevdiff = ap->off - | 
 | 2221 | 				(ap->prevp->br_startoff + | 
 | 2222 | 				 ap->prevp->br_blockcount); | 
 | 2223 | 			/* | 
 | 2224 | 			 * Figure the startblock based on the previous block's | 
 | 2225 | 			 * end and the gap size. | 
 | 2226 | 			 * Heuristic! | 
 | 2227 | 			 * If the gap is large relative to the piece we're | 
 | 2228 | 			 * allocating, or using it gives us an invalid block | 
 | 2229 | 			 * number, then just use the end of the previous block. | 
 | 2230 | 			 */ | 
 | 2231 | 			if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen && | 
 | 2232 | 			    ISVALID(prevbno + prevdiff, | 
 | 2233 | 				    ap->prevp->br_startblock)) | 
 | 2234 | 				prevbno += adjust; | 
 | 2235 | 			else | 
 | 2236 | 				prevdiff += adjust; | 
 | 2237 | 			/* | 
 | 2238 | 			 * If the firstblock forbids it, can't use it, | 
 | 2239 | 			 * must use default. | 
 | 2240 | 			 */ | 
 | 2241 | 			if (!rt && !nullfb && | 
 | 2242 | 			    XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) | 
 | 2243 | 				prevbno = NULLFSBLOCK; | 
 | 2244 | 		} | 
 | 2245 | 		/* | 
 | 2246 | 		 * No previous block or can't follow it, just default. | 
 | 2247 | 		 */ | 
 | 2248 | 		else | 
 | 2249 | 			prevbno = NULLFSBLOCK; | 
 | 2250 | 		/* | 
 | 2251 | 		 * If there's a following (right) block, select a requested | 
 | 2252 | 		 * start block based on it. | 
 | 2253 | 		 */ | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2254 | 		if (!isnullstartblock(ap->gotp->br_startblock)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | 			/* | 
 | 2256 | 			 * Calculate gap to start of next block. | 
 | 2257 | 			 */ | 
 | 2258 | 			adjust = gotdiff = ap->gotp->br_startoff - ap->off; | 
 | 2259 | 			/* | 
 | 2260 | 			 * Figure the startblock based on the next block's | 
 | 2261 | 			 * start and the gap size. | 
 | 2262 | 			 */ | 
 | 2263 | 			gotbno = ap->gotp->br_startblock; | 
 | 2264 | 			/* | 
 | 2265 | 			 * Heuristic! | 
 | 2266 | 			 * If the gap is large relative to the piece we're | 
 | 2267 | 			 * allocating, or using it gives us an invalid block | 
 | 2268 | 			 * number, then just use the start of the next block | 
 | 2269 | 			 * offset by our length. | 
 | 2270 | 			 */ | 
 | 2271 | 			if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen && | 
 | 2272 | 			    ISVALID(gotbno - gotdiff, gotbno)) | 
 | 2273 | 				gotbno -= adjust; | 
 | 2274 | 			else if (ISVALID(gotbno - ap->alen, gotbno)) { | 
 | 2275 | 				gotbno -= ap->alen; | 
 | 2276 | 				gotdiff += adjust - ap->alen; | 
 | 2277 | 			} else | 
 | 2278 | 				gotdiff += adjust; | 
 | 2279 | 			/* | 
 | 2280 | 			 * If the firstblock forbids it, can't use it, | 
 | 2281 | 			 * must use default. | 
 | 2282 | 			 */ | 
 | 2283 | 			if (!rt && !nullfb && | 
 | 2284 | 			    XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) | 
 | 2285 | 				gotbno = NULLFSBLOCK; | 
 | 2286 | 		} | 
 | 2287 | 		/* | 
 | 2288 | 		 * No next block, just default. | 
 | 2289 | 		 */ | 
 | 2290 | 		else | 
 | 2291 | 			gotbno = NULLFSBLOCK; | 
 | 2292 | 		/* | 
 | 2293 | 		 * If both valid, pick the better one, else the only good | 
 | 2294 | 		 * one, else ap->rval is already set (to 0 or the inode block). | 
 | 2295 | 		 */ | 
 | 2296 | 		if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) | 
 | 2297 | 			ap->rval = prevdiff <= gotdiff ? prevbno : gotbno; | 
 | 2298 | 		else if (prevbno != NULLFSBLOCK) | 
 | 2299 | 			ap->rval = prevbno; | 
 | 2300 | 		else if (gotbno != NULLFSBLOCK) | 
 | 2301 | 			ap->rval = gotbno; | 
 | 2302 | 	} | 
| Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2303 | #undef ISVALID | 
| Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2304 | } | 
 | 2305 |  | 
 | 2306 | STATIC int | 
 | 2307 | xfs_bmap_rtalloc( | 
 | 2308 | 	xfs_bmalloca_t	*ap)		/* bmap alloc argument struct */ | 
 | 2309 | { | 
 | 2310 | 	xfs_alloctype_t	atype = 0;	/* type for allocation routines */ | 
 | 2311 | 	int		error;		/* error return value */ | 
 | 2312 | 	xfs_mount_t	*mp;		/* mount point structure */ | 
 | 2313 | 	xfs_extlen_t	prod = 0;	/* product factor for allocators */ | 
 | 2314 | 	xfs_extlen_t	ralen = 0;	/* realtime allocation length */ | 
 | 2315 | 	xfs_extlen_t	align;		/* minimum allocation alignment */ | 
| Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2316 | 	xfs_rtblock_t	rtb; | 
 | 2317 |  | 
 | 2318 | 	mp = ap->ip->i_mount; | 
| David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 2319 | 	align = xfs_get_extsz_hint(ap->ip); | 
| Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2320 | 	prod = align / mp->m_sb.sb_rextsize; | 
 | 2321 | 	error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp, | 
 | 2322 | 					align, 1, ap->eof, 0, | 
 | 2323 | 					ap->conv, &ap->off, &ap->alen); | 
 | 2324 | 	if (error) | 
 | 2325 | 		return error; | 
 | 2326 | 	ASSERT(ap->alen); | 
 | 2327 | 	ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0); | 
 | 2328 |  | 
 | 2329 | 	/* | 
 | 2330 | 	 * If the offset & length are not perfectly aligned | 
 | 2331 | 	 * then kill prod, it will just get us in trouble. | 
 | 2332 | 	 */ | 
 | 2333 | 	if (do_mod(ap->off, align) || ap->alen % align) | 
 | 2334 | 		prod = 1; | 
 | 2335 | 	/* | 
 | 2336 | 	 * Set ralen to be the actual requested length in rtextents. | 
 | 2337 | 	 */ | 
 | 2338 | 	ralen = ap->alen / mp->m_sb.sb_rextsize; | 
 | 2339 | 	/* | 
 | 2340 | 	 * If the old value was close enough to MAXEXTLEN that | 
 | 2341 | 	 * we rounded up to it, cut it back so it's valid again. | 
 | 2342 | 	 * Note that if it's a really large request (bigger than | 
 | 2343 | 	 * MAXEXTLEN), we don't hear about that number, and can't | 
 | 2344 | 	 * adjust the starting point to match it. | 
 | 2345 | 	 */ | 
 | 2346 | 	if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN) | 
 | 2347 | 		ralen = MAXEXTLEN / mp->m_sb.sb_rextsize; | 
 | 2348 | 	/* | 
 | 2349 | 	 * If it's an allocation to an empty file at offset 0, | 
 | 2350 | 	 * pick an extent that will space things out in the rt area. | 
 | 2351 | 	 */ | 
 | 2352 | 	if (ap->eof && ap->off == 0) { | 
| Andrew Morton | 0892ccd | 2007-06-28 16:46:56 +1000 | [diff] [blame] | 2353 | 		xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */ | 
 | 2354 |  | 
| Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2355 | 		error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx); | 
 | 2356 | 		if (error) | 
 | 2357 | 			return error; | 
 | 2358 | 		ap->rval = rtx * mp->m_sb.sb_rextsize; | 
 | 2359 | 	} else { | 
 | 2360 | 		ap->rval = 0; | 
 | 2361 | 	} | 
 | 2362 |  | 
 | 2363 | 	xfs_bmap_adjacent(ap); | 
 | 2364 |  | 
 | 2365 | 	/* | 
 | 2366 | 	 * Realtime allocation, done through xfs_rtallocate_extent. | 
 | 2367 | 	 */ | 
 | 2368 | 	atype = ap->rval == 0 ?  XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO; | 
 | 2369 | 	do_div(ap->rval, mp->m_sb.sb_rextsize); | 
 | 2370 | 	rtb = ap->rval; | 
 | 2371 | 	ap->alen = ralen; | 
 | 2372 | 	if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen, | 
 | 2373 | 				&ralen, atype, ap->wasdel, prod, &rtb))) | 
 | 2374 | 		return error; | 
 | 2375 | 	if (rtb == NULLFSBLOCK && prod > 1 && | 
 | 2376 | 	    (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, | 
 | 2377 | 					   ap->alen, &ralen, atype, | 
 | 2378 | 					   ap->wasdel, 1, &rtb))) | 
 | 2379 | 		return error; | 
 | 2380 | 	ap->rval = rtb; | 
 | 2381 | 	if (ap->rval != NULLFSBLOCK) { | 
 | 2382 | 		ap->rval *= mp->m_sb.sb_rextsize; | 
 | 2383 | 		ralen *= mp->m_sb.sb_rextsize; | 
 | 2384 | 		ap->alen = ralen; | 
 | 2385 | 		ap->ip->i_d.di_nblocks += ralen; | 
 | 2386 | 		xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); | 
 | 2387 | 		if (ap->wasdel) | 
 | 2388 | 			ap->ip->i_delayed_blks -= ralen; | 
 | 2389 | 		/* | 
 | 2390 | 		 * Adjust the disk quota also. This was reserved | 
 | 2391 | 		 * earlier. | 
 | 2392 | 		 */ | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2393 | 		xfs_trans_mod_dquot_byino(ap->tp, ap->ip, | 
| Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2394 | 			ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT : | 
 | 2395 | 					XFS_TRANS_DQ_RTBCOUNT, (long) ralen); | 
 | 2396 | 	} else { | 
 | 2397 | 		ap->alen = 0; | 
 | 2398 | 	} | 
 | 2399 | 	return 0; | 
 | 2400 | } | 
 | 2401 |  | 
 | 2402 | STATIC int | 
| Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2403 | xfs_bmap_btalloc_nullfb( | 
 | 2404 | 	struct xfs_bmalloca	*ap, | 
 | 2405 | 	struct xfs_alloc_arg	*args, | 
 | 2406 | 	xfs_extlen_t		*blen) | 
 | 2407 | { | 
 | 2408 | 	struct xfs_mount	*mp = ap->ip->i_mount; | 
 | 2409 | 	struct xfs_perag	*pag; | 
 | 2410 | 	xfs_agnumber_t		ag, startag; | 
 | 2411 | 	int			notinit = 0; | 
 | 2412 | 	int			error; | 
 | 2413 |  | 
 | 2414 | 	if (ap->userdata && xfs_inode_is_filestream(ap->ip)) | 
 | 2415 | 		args->type = XFS_ALLOCTYPE_NEAR_BNO; | 
 | 2416 | 	else | 
 | 2417 | 		args->type = XFS_ALLOCTYPE_START_BNO; | 
 | 2418 | 	args->total = ap->total; | 
 | 2419 |  | 
 | 2420 | 	/* | 
 | 2421 | 	 * Search for an allocation group with a single extent large enough | 
 | 2422 | 	 * for the request.  If one isn't found, then adjust the minimum | 
 | 2423 | 	 * allocation size to the largest space found. | 
 | 2424 | 	 */ | 
 | 2425 | 	startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno); | 
 | 2426 | 	if (startag == NULLAGNUMBER) | 
 | 2427 | 		startag = ag = 0; | 
 | 2428 |  | 
 | 2429 | 	pag = xfs_perag_get(mp, ag); | 
 | 2430 | 	while (*blen < ap->alen) { | 
 | 2431 | 		if (!pag->pagf_init) { | 
 | 2432 | 			error = xfs_alloc_pagf_init(mp, args->tp, ag, | 
 | 2433 | 						    XFS_ALLOC_FLAG_TRYLOCK); | 
 | 2434 | 			if (error) { | 
 | 2435 | 				xfs_perag_put(pag); | 
 | 2436 | 				return error; | 
 | 2437 | 			} | 
 | 2438 | 		} | 
 | 2439 |  | 
 | 2440 | 		/* | 
 | 2441 | 		 * See xfs_alloc_fix_freelist... | 
 | 2442 | 		 */ | 
 | 2443 | 		if (pag->pagf_init) { | 
 | 2444 | 			xfs_extlen_t	longest; | 
 | 2445 | 			longest = xfs_alloc_longest_free_extent(mp, pag); | 
 | 2446 | 			if (*blen < longest) | 
 | 2447 | 				*blen = longest; | 
 | 2448 | 		} else | 
 | 2449 | 			notinit = 1; | 
 | 2450 |  | 
 | 2451 | 		if (xfs_inode_is_filestream(ap->ip)) { | 
 | 2452 | 			if (*blen >= ap->alen) | 
 | 2453 | 				break; | 
 | 2454 |  | 
 | 2455 | 			if (ap->userdata) { | 
 | 2456 | 				/* | 
 | 2457 | 				 * If startag is an invalid AG, we've | 
 | 2458 | 				 * come here once before and | 
 | 2459 | 				 * xfs_filestream_new_ag picked the | 
 | 2460 | 				 * best currently available. | 
 | 2461 | 				 * | 
 | 2462 | 				 * Don't continue looping, since we | 
 | 2463 | 				 * could loop forever. | 
 | 2464 | 				 */ | 
 | 2465 | 				if (startag == NULLAGNUMBER) | 
 | 2466 | 					break; | 
 | 2467 |  | 
 | 2468 | 				error = xfs_filestream_new_ag(ap, &ag); | 
 | 2469 | 				xfs_perag_put(pag); | 
 | 2470 | 				if (error) | 
 | 2471 | 					return error; | 
 | 2472 |  | 
 | 2473 | 				/* loop again to set 'blen'*/ | 
 | 2474 | 				startag = NULLAGNUMBER; | 
 | 2475 | 				pag = xfs_perag_get(mp, ag); | 
 | 2476 | 				continue; | 
 | 2477 | 			} | 
 | 2478 | 		} | 
 | 2479 | 		if (++ag == mp->m_sb.sb_agcount) | 
 | 2480 | 			ag = 0; | 
 | 2481 | 		if (ag == startag) | 
 | 2482 | 			break; | 
 | 2483 | 		xfs_perag_put(pag); | 
 | 2484 | 		pag = xfs_perag_get(mp, ag); | 
 | 2485 | 	} | 
 | 2486 | 	xfs_perag_put(pag); | 
 | 2487 |  | 
 | 2488 | 	/* | 
 | 2489 | 	 * Since the above loop did a BUF_TRYLOCK, it is | 
 | 2490 | 	 * possible that there is space for this request. | 
 | 2491 | 	 */ | 
 | 2492 | 	if (notinit || *blen < ap->minlen) | 
 | 2493 | 		args->minlen = ap->minlen; | 
 | 2494 | 	/* | 
 | 2495 | 	 * If the best seen length is less than the request | 
 | 2496 | 	 * length, use the best as the minimum. | 
 | 2497 | 	 */ | 
 | 2498 | 	else if (*blen < ap->alen) | 
 | 2499 | 		args->minlen = *blen; | 
 | 2500 | 	/* | 
 | 2501 | 	 * Otherwise we've seen an extent as big as alen, | 
 | 2502 | 	 * use that as the minimum. | 
 | 2503 | 	 */ | 
 | 2504 | 	else | 
 | 2505 | 		args->minlen = ap->alen; | 
 | 2506 |  | 
 | 2507 | 	/* | 
 | 2508 | 	 * set the failure fallback case to look in the selected | 
 | 2509 | 	 * AG as the stream may have moved. | 
 | 2510 | 	 */ | 
 | 2511 | 	if (xfs_inode_is_filestream(ap->ip)) | 
 | 2512 | 		ap->rval = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0); | 
 | 2513 |  | 
 | 2514 | 	return 0; | 
 | 2515 | } | 
 | 2516 |  | 
 | 2517 | STATIC int | 
| Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2518 | xfs_bmap_btalloc( | 
 | 2519 | 	xfs_bmalloca_t	*ap)		/* bmap alloc argument struct */ | 
 | 2520 | { | 
 | 2521 | 	xfs_mount_t	*mp;		/* mount point structure */ | 
 | 2522 | 	xfs_alloctype_t	atype = 0;	/* type for allocation routines */ | 
 | 2523 | 	xfs_extlen_t	align;		/* minimum allocation alignment */ | 
| Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2524 | 	xfs_agnumber_t	fb_agno;	/* ag number of ap->firstblock */ | 
| Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2525 | 	xfs_agnumber_t	ag; | 
| Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2526 | 	xfs_alloc_arg_t	args; | 
 | 2527 | 	xfs_extlen_t	blen; | 
| Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2528 | 	xfs_extlen_t	nextminlen = 0; | 
| Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2529 | 	int		nullfb;		/* true if ap->firstblock isn't set */ | 
 | 2530 | 	int		isaligned; | 
| Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2531 | 	int		tryagain; | 
 | 2532 | 	int		error; | 
 | 2533 |  | 
 | 2534 | 	mp = ap->ip->i_mount; | 
| David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 2535 | 	align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0; | 
| Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2536 | 	if (unlikely(align)) { | 
 | 2537 | 		error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp, | 
 | 2538 | 						align, 0, ap->eof, 0, ap->conv, | 
 | 2539 | 						&ap->off, &ap->alen); | 
 | 2540 | 		ASSERT(!error); | 
 | 2541 | 		ASSERT(ap->alen); | 
 | 2542 | 	} | 
 | 2543 | 	nullfb = ap->firstblock == NULLFSBLOCK; | 
 | 2544 | 	fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock); | 
| David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2545 | 	if (nullfb) { | 
 | 2546 | 		if (ap->userdata && xfs_inode_is_filestream(ap->ip)) { | 
 | 2547 | 			ag = xfs_filestream_lookup_ag(ap->ip); | 
 | 2548 | 			ag = (ag != NULLAGNUMBER) ? ag : 0; | 
 | 2549 | 			ap->rval = XFS_AGB_TO_FSB(mp, ag, 0); | 
 | 2550 | 		} else { | 
 | 2551 | 			ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino); | 
 | 2552 | 		} | 
 | 2553 | 	} else | 
| Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2554 | 		ap->rval = ap->firstblock; | 
 | 2555 |  | 
 | 2556 | 	xfs_bmap_adjacent(ap); | 
 | 2557 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2558 | 	/* | 
 | 2559 | 	 * If allowed, use ap->rval; otherwise must use firstblock since | 
 | 2560 | 	 * it's in the right allocation group. | 
 | 2561 | 	 */ | 
| Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2562 | 	if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2563 | 		; | 
 | 2564 | 	else | 
 | 2565 | 		ap->rval = ap->firstblock; | 
 | 2566 | 	/* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2567 | 	 * Normal allocation, done through xfs_alloc_vextent. | 
 | 2568 | 	 */ | 
| Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2569 | 	tryagain = isaligned = 0; | 
 | 2570 | 	args.tp = ap->tp; | 
 | 2571 | 	args.mp = mp; | 
 | 2572 | 	args.fsbno = ap->rval; | 
 | 2573 | 	args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks); | 
| Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 2574 | 	args.firstblock = ap->firstblock; | 
| Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2575 | 	blen = 0; | 
 | 2576 | 	if (nullfb) { | 
| Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2577 | 		error = xfs_bmap_btalloc_nullfb(ap, &args, &blen); | 
 | 2578 | 		if (error) | 
 | 2579 | 			return error; | 
| Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2580 | 	} else if (ap->low) { | 
| David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2581 | 		if (xfs_inode_is_filestream(ap->ip)) | 
 | 2582 | 			args.type = XFS_ALLOCTYPE_FIRST_AG; | 
 | 2583 | 		else | 
 | 2584 | 			args.type = XFS_ALLOCTYPE_START_BNO; | 
| Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2585 | 		args.total = args.minlen = ap->minlen; | 
 | 2586 | 	} else { | 
 | 2587 | 		args.type = XFS_ALLOCTYPE_NEAR_BNO; | 
 | 2588 | 		args.total = ap->total; | 
 | 2589 | 		args.minlen = ap->minlen; | 
 | 2590 | 	} | 
| David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 2591 | 	/* apply extent size hints if obtained earlier */ | 
 | 2592 | 	if (unlikely(align)) { | 
 | 2593 | 		args.prod = align; | 
| Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2594 | 		if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod))) | 
 | 2595 | 			args.mod = (xfs_extlen_t)(args.prod - args.mod); | 
| Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 2596 | 	} else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) { | 
| Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2597 | 		args.prod = 1; | 
 | 2598 | 		args.mod = 0; | 
 | 2599 | 	} else { | 
| Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 2600 | 		args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog; | 
| Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2601 | 		if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod)))) | 
 | 2602 | 			args.mod = (xfs_extlen_t)(args.prod - args.mod); | 
 | 2603 | 	} | 
 | 2604 | 	/* | 
 | 2605 | 	 * If we are not low on available data blocks, and the | 
 | 2606 | 	 * underlying logical volume manager is a stripe, and | 
 | 2607 | 	 * the file offset is zero then try to allocate data | 
 | 2608 | 	 * blocks on stripe unit boundary. | 
 | 2609 | 	 * NOTE: ap->aeof is only set if the allocation length | 
 | 2610 | 	 * is >= the stripe unit and the allocation offset is | 
 | 2611 | 	 * at the end of file. | 
 | 2612 | 	 */ | 
 | 2613 | 	if (!ap->low && ap->aeof) { | 
 | 2614 | 		if (!ap->off) { | 
 | 2615 | 			args.alignment = mp->m_dalign; | 
 | 2616 | 			atype = args.type; | 
 | 2617 | 			isaligned = 1; | 
 | 2618 | 			/* | 
 | 2619 | 			 * Adjust for alignment | 
 | 2620 | 			 */ | 
 | 2621 | 			if (blen > args.alignment && blen <= ap->alen) | 
 | 2622 | 				args.minlen = blen - args.alignment; | 
 | 2623 | 			args.minalignslop = 0; | 
 | 2624 | 		} else { | 
 | 2625 | 			/* | 
 | 2626 | 			 * First try an exact bno allocation. | 
 | 2627 | 			 * If it fails then do a near or start bno | 
 | 2628 | 			 * allocation with alignment turned on. | 
 | 2629 | 			 */ | 
 | 2630 | 			atype = args.type; | 
 | 2631 | 			tryagain = 1; | 
 | 2632 | 			args.type = XFS_ALLOCTYPE_THIS_BNO; | 
 | 2633 | 			args.alignment = 1; | 
 | 2634 | 			/* | 
 | 2635 | 			 * Compute the minlen+alignment for the | 
 | 2636 | 			 * next case.  Set slop so that the value | 
 | 2637 | 			 * of minlen+alignment+slop doesn't go up | 
 | 2638 | 			 * between the calls. | 
 | 2639 | 			 */ | 
 | 2640 | 			if (blen > mp->m_dalign && blen <= ap->alen) | 
 | 2641 | 				nextminlen = blen - mp->m_dalign; | 
 | 2642 | 			else | 
 | 2643 | 				nextminlen = args.minlen; | 
 | 2644 | 			if (nextminlen + mp->m_dalign > args.minlen + 1) | 
 | 2645 | 				args.minalignslop = | 
 | 2646 | 					nextminlen + mp->m_dalign - | 
 | 2647 | 					args.minlen - 1; | 
 | 2648 | 			else | 
 | 2649 | 				args.minalignslop = 0; | 
 | 2650 | 		} | 
 | 2651 | 	} else { | 
 | 2652 | 		args.alignment = 1; | 
 | 2653 | 		args.minalignslop = 0; | 
 | 2654 | 	} | 
 | 2655 | 	args.minleft = ap->minleft; | 
 | 2656 | 	args.wasdel = ap->wasdel; | 
 | 2657 | 	args.isfl = 0; | 
 | 2658 | 	args.userdata = ap->userdata; | 
 | 2659 | 	if ((error = xfs_alloc_vextent(&args))) | 
 | 2660 | 		return error; | 
 | 2661 | 	if (tryagain && args.fsbno == NULLFSBLOCK) { | 
 | 2662 | 		/* | 
 | 2663 | 		 * Exact allocation failed. Now try with alignment | 
 | 2664 | 		 * turned on. | 
 | 2665 | 		 */ | 
 | 2666 | 		args.type = atype; | 
 | 2667 | 		args.fsbno = ap->rval; | 
 | 2668 | 		args.alignment = mp->m_dalign; | 
 | 2669 | 		args.minlen = nextminlen; | 
 | 2670 | 		args.minalignslop = 0; | 
 | 2671 | 		isaligned = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2672 | 		if ((error = xfs_alloc_vextent(&args))) | 
 | 2673 | 			return error; | 
| Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2674 | 	} | 
 | 2675 | 	if (isaligned && args.fsbno == NULLFSBLOCK) { | 
 | 2676 | 		/* | 
 | 2677 | 		 * allocation failed, so turn off alignment and | 
 | 2678 | 		 * try again. | 
 | 2679 | 		 */ | 
 | 2680 | 		args.type = atype; | 
 | 2681 | 		args.fsbno = ap->rval; | 
 | 2682 | 		args.alignment = 0; | 
 | 2683 | 		if ((error = xfs_alloc_vextent(&args))) | 
 | 2684 | 			return error; | 
 | 2685 | 	} | 
 | 2686 | 	if (args.fsbno == NULLFSBLOCK && nullfb && | 
 | 2687 | 	    args.minlen > ap->minlen) { | 
 | 2688 | 		args.minlen = ap->minlen; | 
 | 2689 | 		args.type = XFS_ALLOCTYPE_START_BNO; | 
 | 2690 | 		args.fsbno = ap->rval; | 
 | 2691 | 		if ((error = xfs_alloc_vextent(&args))) | 
 | 2692 | 			return error; | 
 | 2693 | 	} | 
 | 2694 | 	if (args.fsbno == NULLFSBLOCK && nullfb) { | 
 | 2695 | 		args.fsbno = 0; | 
 | 2696 | 		args.type = XFS_ALLOCTYPE_FIRST_AG; | 
 | 2697 | 		args.total = ap->minlen; | 
 | 2698 | 		args.minleft = 0; | 
 | 2699 | 		if ((error = xfs_alloc_vextent(&args))) | 
 | 2700 | 			return error; | 
 | 2701 | 		ap->low = 1; | 
 | 2702 | 	} | 
 | 2703 | 	if (args.fsbno != NULLFSBLOCK) { | 
 | 2704 | 		ap->firstblock = ap->rval = args.fsbno; | 
 | 2705 | 		ASSERT(nullfb || fb_agno == args.agno || | 
 | 2706 | 		       (ap->low && fb_agno < args.agno)); | 
 | 2707 | 		ap->alen = args.len; | 
 | 2708 | 		ap->ip->i_d.di_nblocks += args.len; | 
 | 2709 | 		xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); | 
 | 2710 | 		if (ap->wasdel) | 
 | 2711 | 			ap->ip->i_delayed_blks -= args.len; | 
 | 2712 | 		/* | 
 | 2713 | 		 * Adjust the disk quota also. This was reserved | 
 | 2714 | 		 * earlier. | 
 | 2715 | 		 */ | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2716 | 		xfs_trans_mod_dquot_byino(ap->tp, ap->ip, | 
| Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2717 | 			ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : | 
 | 2718 | 					XFS_TRANS_DQ_BCOUNT, | 
 | 2719 | 			(long) args.len); | 
 | 2720 | 	} else { | 
 | 2721 | 		ap->rval = NULLFSBLOCK; | 
 | 2722 | 		ap->alen = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2723 | 	} | 
 | 2724 | 	return 0; | 
| Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2725 | } | 
 | 2726 |  | 
 | 2727 | /* | 
 | 2728 |  * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. | 
 | 2729 |  * It figures out where to ask the underlying allocator to put the new extent. | 
 | 2730 |  */ | 
 | 2731 | STATIC int | 
 | 2732 | xfs_bmap_alloc( | 
 | 2733 | 	xfs_bmalloca_t	*ap)		/* bmap alloc argument struct */ | 
 | 2734 | { | 
| Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 2735 | 	if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata) | 
| Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2736 | 		return xfs_bmap_rtalloc(ap); | 
 | 2737 | 	return xfs_bmap_btalloc(ap); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2738 | } | 
 | 2739 |  | 
 | 2740 | /* | 
 | 2741 |  * Transform a btree format file with only one leaf node, where the | 
 | 2742 |  * extents list will fit in the inode, into an extents format file. | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2743 |  * Since the file extents are already in-core, all we have to do is | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2744 |  * give up the space for the btree root and pitch the leaf block. | 
 | 2745 |  */ | 
 | 2746 | STATIC int				/* error */ | 
 | 2747 | xfs_bmap_btree_to_extents( | 
 | 2748 | 	xfs_trans_t		*tp,	/* transaction pointer */ | 
 | 2749 | 	xfs_inode_t		*ip,	/* incore inode pointer */ | 
 | 2750 | 	xfs_btree_cur_t		*cur,	/* btree cursor */ | 
 | 2751 | 	int			*logflagsp, /* inode logging flags */ | 
 | 2752 | 	int			whichfork)  /* data or attr fork */ | 
 | 2753 | { | 
 | 2754 | 	/* REFERENCED */ | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2755 | 	struct xfs_btree_block	*cblock;/* child btree block */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2756 | 	xfs_fsblock_t		cbno;	/* child block number */ | 
 | 2757 | 	xfs_buf_t		*cbp;	/* child block's buffer */ | 
 | 2758 | 	int			error;	/* error return value */ | 
 | 2759 | 	xfs_ifork_t		*ifp;	/* inode fork data */ | 
 | 2760 | 	xfs_mount_t		*mp;	/* mount point structure */ | 
| Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 2761 | 	__be64			*pp;	/* ptr to block address */ | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2762 | 	struct xfs_btree_block	*rblock;/* root btree block */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2763 |  | 
| Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 2764 | 	mp = ip->i_mount; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2765 | 	ifp = XFS_IFORK_PTR(ip, whichfork); | 
 | 2766 | 	ASSERT(ifp->if_flags & XFS_IFEXTENTS); | 
 | 2767 | 	ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); | 
 | 2768 | 	rblock = ifp->if_broot; | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 2769 | 	ASSERT(be16_to_cpu(rblock->bb_level) == 1); | 
 | 2770 | 	ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1); | 
| Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 2771 | 	ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1); | 
 | 2772 | 	pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes); | 
| Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 2773 | 	cbno = be64_to_cpu(*pp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2774 | 	*logflagsp = 0; | 
 | 2775 | #ifdef DEBUG | 
| Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 2776 | 	if ((error = xfs_btree_check_lptr(cur, cbno, 1))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2777 | 		return error; | 
 | 2778 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2779 | 	if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, | 
 | 2780 | 			XFS_BMAP_BTREE_REF))) | 
 | 2781 | 		return error; | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2782 | 	cblock = XFS_BUF_TO_BLOCK(cbp); | 
 | 2783 | 	if ((error = xfs_btree_check_block(cur, cblock, 0, cbp))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2784 | 		return error; | 
 | 2785 | 	xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp); | 
 | 2786 | 	ip->i_d.di_nblocks--; | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2787 | 	xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2788 | 	xfs_trans_binval(tp, cbp); | 
 | 2789 | 	if (cur->bc_bufs[0] == cbp) | 
 | 2790 | 		cur->bc_bufs[0] = NULL; | 
 | 2791 | 	xfs_iroot_realloc(ip, -1, whichfork); | 
 | 2792 | 	ASSERT(ifp->if_broot == NULL); | 
 | 2793 | 	ASSERT((ifp->if_flags & XFS_IFBROOT) == 0); | 
 | 2794 | 	XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2795 | 	*logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2796 | 	return 0; | 
 | 2797 | } | 
 | 2798 |  | 
 | 2799 | /* | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2800 |  * Called by xfs_bmapi to update file extent records and the btree | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2801 |  * after removing space (or undoing a delayed allocation). | 
 | 2802 |  */ | 
 | 2803 | STATIC int				/* error */ | 
 | 2804 | xfs_bmap_del_extent( | 
 | 2805 | 	xfs_inode_t		*ip,	/* incore inode pointer */ | 
 | 2806 | 	xfs_trans_t		*tp,	/* current transaction pointer */ | 
 | 2807 | 	xfs_extnum_t		idx,	/* extent number to update/delete */ | 
 | 2808 | 	xfs_bmap_free_t		*flist,	/* list of extents to be freed */ | 
 | 2809 | 	xfs_btree_cur_t		*cur,	/* if null, not a btree */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2810 | 	xfs_bmbt_irec_t		*del,	/* data to remove from extents */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2811 | 	int			*logflagsp, /* inode logging flags */ | 
 | 2812 | 	int			whichfork, /* data or attr fork */ | 
 | 2813 | 	int			rsvd)	/* OK to allocate reserved blocks */ | 
 | 2814 | { | 
 | 2815 | 	xfs_filblks_t		da_new;	/* new delay-alloc indirect blocks */ | 
 | 2816 | 	xfs_filblks_t		da_old;	/* old delay-alloc indirect blocks */ | 
 | 2817 | 	xfs_fsblock_t		del_endblock=0;	/* first block past del */ | 
 | 2818 | 	xfs_fileoff_t		del_endoff;	/* first offset past del */ | 
 | 2819 | 	int			delay;	/* current block is delayed allocated */ | 
 | 2820 | 	int			do_fx;	/* free extent at end of routine */ | 
| Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 2821 | 	xfs_bmbt_rec_host_t	*ep;	/* current extent entry pointer */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2822 | 	int			error;	/* error return value */ | 
 | 2823 | 	int			flags;	/* inode logging flags */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2824 | 	xfs_bmbt_irec_t		got;	/* current extent entry */ | 
 | 2825 | 	xfs_fileoff_t		got_endoff;	/* first offset past got */ | 
 | 2826 | 	int			i;	/* temp state */ | 
 | 2827 | 	xfs_ifork_t		*ifp;	/* inode fork pointer */ | 
 | 2828 | 	xfs_mount_t		*mp;	/* mount structure */ | 
 | 2829 | 	xfs_filblks_t		nblks;	/* quota/sb block count */ | 
 | 2830 | 	xfs_bmbt_irec_t		new;	/* new record to be inserted */ | 
 | 2831 | 	/* REFERENCED */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2832 | 	uint			qfield;	/* quota field to update */ | 
 | 2833 | 	xfs_filblks_t		temp;	/* for indirect length calculations */ | 
 | 2834 | 	xfs_filblks_t		temp2;	/* for indirect length calculations */ | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2835 | 	int			state = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2836 |  | 
 | 2837 | 	XFS_STATS_INC(xs_del_exlist); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2838 |  | 
 | 2839 | 	if (whichfork == XFS_ATTR_FORK) | 
 | 2840 | 		state |= BMAP_ATTRFORK; | 
 | 2841 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2842 | 	mp = ip->i_mount; | 
 | 2843 | 	ifp = XFS_IFORK_PTR(ip, whichfork); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2844 | 	ASSERT((idx >= 0) && (idx < ifp->if_bytes / | 
 | 2845 | 		(uint)sizeof(xfs_bmbt_rec_t))); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2846 | 	ASSERT(del->br_blockcount > 0); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2847 | 	ep = xfs_iext_get_ext(ifp, idx); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2848 | 	xfs_bmbt_get_all(ep, &got); | 
 | 2849 | 	ASSERT(got.br_startoff <= del->br_startoff); | 
 | 2850 | 	del_endoff = del->br_startoff + del->br_blockcount; | 
 | 2851 | 	got_endoff = got.br_startoff + got.br_blockcount; | 
 | 2852 | 	ASSERT(got_endoff >= del_endoff); | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2853 | 	delay = isnullstartblock(got.br_startblock); | 
 | 2854 | 	ASSERT(isnullstartblock(del->br_startblock) == delay); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2855 | 	flags = 0; | 
 | 2856 | 	qfield = 0; | 
 | 2857 | 	error = 0; | 
 | 2858 | 	/* | 
 | 2859 | 	 * If deleting a real allocation, must free up the disk space. | 
 | 2860 | 	 */ | 
 | 2861 | 	if (!delay) { | 
 | 2862 | 		flags = XFS_ILOG_CORE; | 
 | 2863 | 		/* | 
 | 2864 | 		 * Realtime allocation.  Free it and record di_nblocks update. | 
 | 2865 | 		 */ | 
| Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 2866 | 		if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2867 | 			xfs_fsblock_t	bno; | 
 | 2868 | 			xfs_filblks_t	len; | 
 | 2869 |  | 
 | 2870 | 			ASSERT(do_mod(del->br_blockcount, | 
 | 2871 | 				      mp->m_sb.sb_rextsize) == 0); | 
 | 2872 | 			ASSERT(do_mod(del->br_startblock, | 
 | 2873 | 				      mp->m_sb.sb_rextsize) == 0); | 
 | 2874 | 			bno = del->br_startblock; | 
 | 2875 | 			len = del->br_blockcount; | 
 | 2876 | 			do_div(bno, mp->m_sb.sb_rextsize); | 
 | 2877 | 			do_div(len, mp->m_sb.sb_rextsize); | 
 | 2878 | 			if ((error = xfs_rtfree_extent(ip->i_transp, bno, | 
 | 2879 | 					(xfs_extlen_t)len))) | 
 | 2880 | 				goto done; | 
 | 2881 | 			do_fx = 0; | 
 | 2882 | 			nblks = len * mp->m_sb.sb_rextsize; | 
 | 2883 | 			qfield = XFS_TRANS_DQ_RTBCOUNT; | 
 | 2884 | 		} | 
 | 2885 | 		/* | 
 | 2886 | 		 * Ordinary allocation. | 
 | 2887 | 		 */ | 
 | 2888 | 		else { | 
 | 2889 | 			do_fx = 1; | 
 | 2890 | 			nblks = del->br_blockcount; | 
 | 2891 | 			qfield = XFS_TRANS_DQ_BCOUNT; | 
 | 2892 | 		} | 
 | 2893 | 		/* | 
 | 2894 | 		 * Set up del_endblock and cur for later. | 
 | 2895 | 		 */ | 
 | 2896 | 		del_endblock = del->br_startblock + del->br_blockcount; | 
 | 2897 | 		if (cur) { | 
 | 2898 | 			if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff, | 
 | 2899 | 					got.br_startblock, got.br_blockcount, | 
 | 2900 | 					&i))) | 
 | 2901 | 				goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2902 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2903 | 		} | 
 | 2904 | 		da_old = da_new = 0; | 
 | 2905 | 	} else { | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2906 | 		da_old = startblockval(got.br_startblock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2907 | 		da_new = 0; | 
 | 2908 | 		nblks = 0; | 
 | 2909 | 		do_fx = 0; | 
 | 2910 | 	} | 
 | 2911 | 	/* | 
 | 2912 | 	 * Set flag value to use in switch statement. | 
 | 2913 | 	 * Left-contig is 2, right-contig is 1. | 
 | 2914 | 	 */ | 
 | 2915 | 	switch (((got.br_startoff == del->br_startoff) << 1) | | 
 | 2916 | 		(got_endoff == del_endoff)) { | 
 | 2917 | 	case 3: | 
 | 2918 | 		/* | 
 | 2919 | 		 * Matches the whole extent.  Delete the entry. | 
 | 2920 | 		 */ | 
| Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 2921 | 		xfs_iext_remove(ip, idx, 1, | 
 | 2922 | 				whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2923 | 		ifp->if_lastex = idx; | 
 | 2924 | 		if (delay) | 
 | 2925 | 			break; | 
 | 2926 | 		XFS_IFORK_NEXT_SET(ip, whichfork, | 
 | 2927 | 			XFS_IFORK_NEXTENTS(ip, whichfork) - 1); | 
 | 2928 | 		flags |= XFS_ILOG_CORE; | 
 | 2929 | 		if (!cur) { | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2930 | 			flags |= xfs_ilog_fext(whichfork); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2931 | 			break; | 
 | 2932 | 		} | 
| Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2933 | 		if ((error = xfs_btree_delete(cur, &i))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2934 | 			goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2935 | 		XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2936 | 		break; | 
 | 2937 |  | 
 | 2938 | 	case 2: | 
 | 2939 | 		/* | 
 | 2940 | 		 * Deleting the first part of the extent. | 
 | 2941 | 		 */ | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2942 | 		trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2943 | 		xfs_bmbt_set_startoff(ep, del_endoff); | 
 | 2944 | 		temp = got.br_blockcount - del->br_blockcount; | 
 | 2945 | 		xfs_bmbt_set_blockcount(ep, temp); | 
 | 2946 | 		ifp->if_lastex = idx; | 
 | 2947 | 		if (delay) { | 
 | 2948 | 			temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), | 
 | 2949 | 				da_old); | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2950 | 			xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2951 | 			trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2952 | 			da_new = temp; | 
 | 2953 | 			break; | 
 | 2954 | 		} | 
 | 2955 | 		xfs_bmbt_set_startblock(ep, del_endblock); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2956 | 		trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2957 | 		if (!cur) { | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2958 | 			flags |= xfs_ilog_fext(whichfork); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2959 | 			break; | 
 | 2960 | 		} | 
 | 2961 | 		if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock, | 
 | 2962 | 				got.br_blockcount - del->br_blockcount, | 
 | 2963 | 				got.br_state))) | 
 | 2964 | 			goto done; | 
 | 2965 | 		break; | 
 | 2966 |  | 
 | 2967 | 	case 1: | 
 | 2968 | 		/* | 
 | 2969 | 		 * Deleting the last part of the extent. | 
 | 2970 | 		 */ | 
 | 2971 | 		temp = got.br_blockcount - del->br_blockcount; | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2972 | 		trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2973 | 		xfs_bmbt_set_blockcount(ep, temp); | 
 | 2974 | 		ifp->if_lastex = idx; | 
 | 2975 | 		if (delay) { | 
 | 2976 | 			temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), | 
 | 2977 | 				da_old); | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2978 | 			xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2979 | 			trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2980 | 			da_new = temp; | 
 | 2981 | 			break; | 
 | 2982 | 		} | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2983 | 		trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2984 | 		if (!cur) { | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2985 | 			flags |= xfs_ilog_fext(whichfork); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2986 | 			break; | 
 | 2987 | 		} | 
 | 2988 | 		if ((error = xfs_bmbt_update(cur, got.br_startoff, | 
 | 2989 | 				got.br_startblock, | 
 | 2990 | 				got.br_blockcount - del->br_blockcount, | 
 | 2991 | 				got.br_state))) | 
 | 2992 | 			goto done; | 
 | 2993 | 		break; | 
 | 2994 |  | 
 | 2995 | 	case 0: | 
 | 2996 | 		/* | 
 | 2997 | 		 * Deleting the middle of the extent. | 
 | 2998 | 		 */ | 
 | 2999 | 		temp = del->br_startoff - got.br_startoff; | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3000 | 		trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3001 | 		xfs_bmbt_set_blockcount(ep, temp); | 
 | 3002 | 		new.br_startoff = del_endoff; | 
 | 3003 | 		temp2 = got_endoff - del_endoff; | 
 | 3004 | 		new.br_blockcount = temp2; | 
 | 3005 | 		new.br_state = got.br_state; | 
 | 3006 | 		if (!delay) { | 
 | 3007 | 			new.br_startblock = del_endblock; | 
 | 3008 | 			flags |= XFS_ILOG_CORE; | 
 | 3009 | 			if (cur) { | 
 | 3010 | 				if ((error = xfs_bmbt_update(cur, | 
 | 3011 | 						got.br_startoff, | 
 | 3012 | 						got.br_startblock, temp, | 
 | 3013 | 						got.br_state))) | 
 | 3014 | 					goto done; | 
| Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 3015 | 				if ((error = xfs_btree_increment(cur, 0, &i))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3016 | 					goto done; | 
 | 3017 | 				cur->bc_rec.b = new; | 
| Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3018 | 				error = xfs_btree_insert(cur, &i); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3019 | 				if (error && error != ENOSPC) | 
 | 3020 | 					goto done; | 
 | 3021 | 				/* | 
 | 3022 | 				 * If get no-space back from btree insert, | 
 | 3023 | 				 * it tried a split, and we have a zero | 
 | 3024 | 				 * block reservation. | 
 | 3025 | 				 * Fix up our state and return the error. | 
 | 3026 | 				 */ | 
 | 3027 | 				if (error == ENOSPC) { | 
 | 3028 | 					/* | 
 | 3029 | 					 * Reset the cursor, don't trust | 
 | 3030 | 					 * it after any insert operation. | 
 | 3031 | 					 */ | 
 | 3032 | 					if ((error = xfs_bmbt_lookup_eq(cur, | 
 | 3033 | 							got.br_startoff, | 
 | 3034 | 							got.br_startblock, | 
 | 3035 | 							temp, &i))) | 
 | 3036 | 						goto done; | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 3037 | 					XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3038 | 					/* | 
 | 3039 | 					 * Update the btree record back | 
 | 3040 | 					 * to the original value. | 
 | 3041 | 					 */ | 
 | 3042 | 					if ((error = xfs_bmbt_update(cur, | 
 | 3043 | 							got.br_startoff, | 
 | 3044 | 							got.br_startblock, | 
 | 3045 | 							got.br_blockcount, | 
 | 3046 | 							got.br_state))) | 
 | 3047 | 						goto done; | 
 | 3048 | 					/* | 
 | 3049 | 					 * Reset the extent record back | 
 | 3050 | 					 * to the original value. | 
 | 3051 | 					 */ | 
 | 3052 | 					xfs_bmbt_set_blockcount(ep, | 
 | 3053 | 						got.br_blockcount); | 
 | 3054 | 					flags = 0; | 
 | 3055 | 					error = XFS_ERROR(ENOSPC); | 
 | 3056 | 					goto done; | 
 | 3057 | 				} | 
| Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 3058 | 				XFS_WANT_CORRUPTED_GOTO(i == 1, done); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3059 | 			} else | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3060 | 				flags |= xfs_ilog_fext(whichfork); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3061 | 			XFS_IFORK_NEXT_SET(ip, whichfork, | 
 | 3062 | 				XFS_IFORK_NEXTENTS(ip, whichfork) + 1); | 
 | 3063 | 		} else { | 
 | 3064 | 			ASSERT(whichfork == XFS_DATA_FORK); | 
 | 3065 | 			temp = xfs_bmap_worst_indlen(ip, temp); | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3066 | 			xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3067 | 			temp2 = xfs_bmap_worst_indlen(ip, temp2); | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3068 | 			new.br_startblock = nullstartblock((int)temp2); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3069 | 			da_new = temp + temp2; | 
 | 3070 | 			while (da_new > da_old) { | 
 | 3071 | 				if (temp) { | 
 | 3072 | 					temp--; | 
 | 3073 | 					da_new--; | 
 | 3074 | 					xfs_bmbt_set_startblock(ep, | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3075 | 						nullstartblock((int)temp)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3076 | 				} | 
 | 3077 | 				if (da_new == da_old) | 
 | 3078 | 					break; | 
 | 3079 | 				if (temp2) { | 
 | 3080 | 					temp2--; | 
 | 3081 | 					da_new--; | 
 | 3082 | 					new.br_startblock = | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3083 | 						nullstartblock((int)temp2); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3084 | 				} | 
 | 3085 | 			} | 
 | 3086 | 		} | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3087 | 		trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); | 
 | 3088 | 		xfs_iext_insert(ip, idx + 1, 1, &new, state); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3089 | 		ifp->if_lastex = idx + 1; | 
 | 3090 | 		break; | 
 | 3091 | 	} | 
 | 3092 | 	/* | 
 | 3093 | 	 * If we need to, add to list of extents to delete. | 
 | 3094 | 	 */ | 
 | 3095 | 	if (do_fx) | 
 | 3096 | 		xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist, | 
 | 3097 | 			mp); | 
 | 3098 | 	/* | 
 | 3099 | 	 * Adjust inode # blocks in the file. | 
 | 3100 | 	 */ | 
 | 3101 | 	if (nblks) | 
 | 3102 | 		ip->i_d.di_nblocks -= nblks; | 
 | 3103 | 	/* | 
 | 3104 | 	 * Adjust quota data. | 
 | 3105 | 	 */ | 
 | 3106 | 	if (qfield) | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3107 | 		xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3108 |  | 
 | 3109 | 	/* | 
 | 3110 | 	 * Account for change in delayed indirect blocks. | 
 | 3111 | 	 * Nothing to do for disk quota accounting here. | 
 | 3112 | 	 */ | 
 | 3113 | 	ASSERT(da_old >= da_new); | 
 | 3114 | 	if (da_old > da_new) | 
| David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 3115 | 		xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int64_t)(da_old - da_new), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3116 | 			rsvd); | 
 | 3117 | done: | 
 | 3118 | 	*logflagsp = flags; | 
 | 3119 | 	return error; | 
 | 3120 | } | 
 | 3121 |  | 
 | 3122 | /* | 
 | 3123 |  * Remove the entry "free" from the free item list.  Prev points to the | 
 | 3124 |  * previous entry, unless "free" is the head of the list. | 
 | 3125 |  */ | 
 | 3126 | STATIC void | 
 | 3127 | xfs_bmap_del_free( | 
 | 3128 | 	xfs_bmap_free_t		*flist,	/* free item list header */ | 
 | 3129 | 	xfs_bmap_free_item_t	*prev,	/* previous item on list, if any */ | 
 | 3130 | 	xfs_bmap_free_item_t	*free)	/* list item to be freed */ | 
 | 3131 | { | 
 | 3132 | 	if (prev) | 
 | 3133 | 		prev->xbfi_next = free->xbfi_next; | 
 | 3134 | 	else | 
 | 3135 | 		flist->xbf_first = free->xbfi_next; | 
 | 3136 | 	flist->xbf_count--; | 
 | 3137 | 	kmem_zone_free(xfs_bmap_free_item_zone, free); | 
 | 3138 | } | 
 | 3139 |  | 
 | 3140 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3141 |  * Convert an extents-format file into a btree-format file. | 
 | 3142 |  * The new file will have a root block (in the inode) and a single child block. | 
 | 3143 |  */ | 
 | 3144 | STATIC int					/* error */ | 
 | 3145 | xfs_bmap_extents_to_btree( | 
 | 3146 | 	xfs_trans_t		*tp,		/* transaction pointer */ | 
 | 3147 | 	xfs_inode_t		*ip,		/* incore inode pointer */ | 
 | 3148 | 	xfs_fsblock_t		*firstblock,	/* first-block-allocated */ | 
 | 3149 | 	xfs_bmap_free_t		*flist,		/* blocks freed in xaction */ | 
 | 3150 | 	xfs_btree_cur_t		**curp,		/* cursor returned to caller */ | 
 | 3151 | 	int			wasdel,		/* converting a delayed alloc */ | 
 | 3152 | 	int			*logflagsp,	/* inode logging flags */ | 
 | 3153 | 	int			whichfork)	/* data or attr fork */ | 
 | 3154 | { | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3155 | 	struct xfs_btree_block	*ablock;	/* allocated (child) bt block */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3156 | 	xfs_buf_t		*abp;		/* buffer for ablock */ | 
 | 3157 | 	xfs_alloc_arg_t		args;		/* allocation arguments */ | 
 | 3158 | 	xfs_bmbt_rec_t		*arp;		/* child record pointer */ | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3159 | 	struct xfs_btree_block	*block;		/* btree root block */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3160 | 	xfs_btree_cur_t		*cur;		/* bmap btree cursor */ | 
| Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3161 | 	xfs_bmbt_rec_host_t	*ep;		/* extent record pointer */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3162 | 	int			error;		/* error return value */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3163 | 	xfs_extnum_t		i, cnt;		/* extent record index */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3164 | 	xfs_ifork_t		*ifp;		/* inode fork pointer */ | 
 | 3165 | 	xfs_bmbt_key_t		*kp;		/* root block key pointer */ | 
 | 3166 | 	xfs_mount_t		*mp;		/* mount structure */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3167 | 	xfs_extnum_t		nextents;	/* number of file extents */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3168 | 	xfs_bmbt_ptr_t		*pp;		/* root block address pointer */ | 
 | 3169 |  | 
 | 3170 | 	ifp = XFS_IFORK_PTR(ip, whichfork); | 
 | 3171 | 	ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS); | 
 | 3172 | 	ASSERT(ifp->if_ext_max == | 
 | 3173 | 	       XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); | 
 | 3174 | 	/* | 
 | 3175 | 	 * Make space in the inode incore. | 
 | 3176 | 	 */ | 
 | 3177 | 	xfs_iroot_realloc(ip, 1, whichfork); | 
 | 3178 | 	ifp->if_flags |= XFS_IFBROOT; | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3179 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3180 | 	/* | 
 | 3181 | 	 * Fill in the root. | 
 | 3182 | 	 */ | 
 | 3183 | 	block = ifp->if_broot; | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3184 | 	block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC); | 
 | 3185 | 	block->bb_level = cpu_to_be16(1); | 
 | 3186 | 	block->bb_numrecs = cpu_to_be16(1); | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3187 | 	block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO); | 
 | 3188 | 	block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO); | 
 | 3189 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3190 | 	/* | 
 | 3191 | 	 * Need a cursor.  Can't allocate until bb_level is filled in. | 
 | 3192 | 	 */ | 
 | 3193 | 	mp = ip->i_mount; | 
| Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 3194 | 	cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3195 | 	cur->bc_private.b.firstblock = *firstblock; | 
 | 3196 | 	cur->bc_private.b.flist = flist; | 
 | 3197 | 	cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; | 
 | 3198 | 	/* | 
 | 3199 | 	 * Convert to a btree with two levels, one record in root. | 
 | 3200 | 	 */ | 
 | 3201 | 	XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE); | 
 | 3202 | 	args.tp = tp; | 
 | 3203 | 	args.mp = mp; | 
| Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 3204 | 	args.firstblock = *firstblock; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3205 | 	if (*firstblock == NULLFSBLOCK) { | 
 | 3206 | 		args.type = XFS_ALLOCTYPE_START_BNO; | 
 | 3207 | 		args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); | 
 | 3208 | 	} else if (flist->xbf_low) { | 
 | 3209 | 		args.type = XFS_ALLOCTYPE_START_BNO; | 
 | 3210 | 		args.fsbno = *firstblock; | 
 | 3211 | 	} else { | 
 | 3212 | 		args.type = XFS_ALLOCTYPE_NEAR_BNO; | 
 | 3213 | 		args.fsbno = *firstblock; | 
 | 3214 | 	} | 
 | 3215 | 	args.minlen = args.maxlen = args.prod = 1; | 
 | 3216 | 	args.total = args.minleft = args.alignment = args.mod = args.isfl = | 
 | 3217 | 		args.minalignslop = 0; | 
 | 3218 | 	args.wasdel = wasdel; | 
 | 3219 | 	*logflagsp = 0; | 
 | 3220 | 	if ((error = xfs_alloc_vextent(&args))) { | 
 | 3221 | 		xfs_iroot_realloc(ip, -1, whichfork); | 
 | 3222 | 		xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | 
 | 3223 | 		return error; | 
 | 3224 | 	} | 
 | 3225 | 	/* | 
 | 3226 | 	 * Allocation can't fail, the space was reserved. | 
 | 3227 | 	 */ | 
 | 3228 | 	ASSERT(args.fsbno != NULLFSBLOCK); | 
 | 3229 | 	ASSERT(*firstblock == NULLFSBLOCK || | 
 | 3230 | 	       args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) || | 
 | 3231 | 	       (flist->xbf_low && | 
 | 3232 | 		args.agno > XFS_FSB_TO_AGNO(mp, *firstblock))); | 
 | 3233 | 	*firstblock = cur->bc_private.b.firstblock = args.fsbno; | 
 | 3234 | 	cur->bc_private.b.allocated++; | 
 | 3235 | 	ip->i_d.di_nblocks++; | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3236 | 	xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3237 | 	abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0); | 
 | 3238 | 	/* | 
 | 3239 | 	 * Fill in the child block. | 
 | 3240 | 	 */ | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3241 | 	ablock = XFS_BUF_TO_BLOCK(abp); | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3242 | 	ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3243 | 	ablock->bb_level = 0; | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3244 | 	ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO); | 
 | 3245 | 	ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO); | 
| Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3246 | 	arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3247 | 	nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3248 | 	for (cnt = i = 0; i < nextents; i++) { | 
 | 3249 | 		ep = xfs_iext_get_ext(ifp, i); | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3250 | 		if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) { | 
| Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 3251 | 			arp->l0 = cpu_to_be64(ep->l0); | 
 | 3252 | 			arp->l1 = cpu_to_be64(ep->l1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3253 | 			arp++; cnt++; | 
 | 3254 | 		} | 
 | 3255 | 	} | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3256 | 	ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork)); | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3257 | 	xfs_btree_set_numrecs(ablock, cnt); | 
 | 3258 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3259 | 	/* | 
 | 3260 | 	 * Fill in the root key and pointer. | 
 | 3261 | 	 */ | 
| Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3262 | 	kp = XFS_BMBT_KEY_ADDR(mp, block, 1); | 
 | 3263 | 	arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); | 
| Christoph Hellwig | 8801bb9 | 2006-09-28 10:58:17 +1000 | [diff] [blame] | 3264 | 	kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); | 
| Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3265 | 	pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur, | 
 | 3266 | 						be16_to_cpu(block->bb_level))); | 
| Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 3267 | 	*pp = cpu_to_be64(args.fsbno); | 
| Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3268 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3269 | 	/* | 
 | 3270 | 	 * Do all this logging at the end so that | 
 | 3271 | 	 * the root is at the right level. | 
 | 3272 | 	 */ | 
| Christoph Hellwig | fd6bcc5b | 2008-10-30 16:58:21 +1100 | [diff] [blame] | 3273 | 	xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); | 
 | 3274 | 	xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3275 | 	ASSERT(*curp == NULL); | 
 | 3276 | 	*curp = cur; | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3277 | 	*logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3278 | 	return 0; | 
 | 3279 | } | 
 | 3280 |  | 
 | 3281 | /* | 
| Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame] | 3282 |  * Calculate the default attribute fork offset for newly created inodes. | 
 | 3283 |  */ | 
 | 3284 | uint | 
 | 3285 | xfs_default_attroffset( | 
 | 3286 | 	struct xfs_inode	*ip) | 
 | 3287 | { | 
 | 3288 | 	struct xfs_mount	*mp = ip->i_mount; | 
 | 3289 | 	uint			offset; | 
 | 3290 |  | 
 | 3291 | 	if (mp->m_sb.sb_inodesize == 256) { | 
 | 3292 | 		offset = XFS_LITINO(mp) - | 
 | 3293 | 				XFS_BMDR_SPACE_CALC(MINABTPTRS); | 
 | 3294 | 	} else { | 
 | 3295 | 		offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS); | 
 | 3296 | 	} | 
 | 3297 |  | 
 | 3298 | 	ASSERT(offset < XFS_LITINO(mp)); | 
 | 3299 | 	return offset; | 
 | 3300 | } | 
 | 3301 |  | 
 | 3302 | /* | 
| Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3303 |  * Helper routine to reset inode di_forkoff field when switching | 
 | 3304 |  * attribute fork from local to extent format - we reset it where | 
 | 3305 |  * possible to make space available for inline data fork extents. | 
 | 3306 |  */ | 
 | 3307 | STATIC void | 
 | 3308 | xfs_bmap_forkoff_reset( | 
 | 3309 | 	xfs_mount_t	*mp, | 
 | 3310 | 	xfs_inode_t	*ip, | 
 | 3311 | 	int		whichfork) | 
 | 3312 | { | 
 | 3313 | 	if (whichfork == XFS_ATTR_FORK && | 
| Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame] | 3314 | 	    ip->i_d.di_format != XFS_DINODE_FMT_DEV && | 
 | 3315 | 	    ip->i_d.di_format != XFS_DINODE_FMT_UUID && | 
 | 3316 | 	    ip->i_d.di_format != XFS_DINODE_FMT_BTREE) { | 
 | 3317 | 		uint	dfl_forkoff = xfs_default_attroffset(ip) >> 3; | 
 | 3318 |  | 
 | 3319 | 		if (dfl_forkoff > ip->i_d.di_forkoff) { | 
 | 3320 | 			ip->i_d.di_forkoff = dfl_forkoff; | 
 | 3321 | 			ip->i_df.if_ext_max = | 
 | 3322 | 				XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t); | 
 | 3323 | 			ip->i_afp->if_ext_max = | 
 | 3324 | 				XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t); | 
 | 3325 | 		} | 
| Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3326 | 	} | 
 | 3327 | } | 
 | 3328 |  | 
 | 3329 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3330 |  * Convert a local file to an extents file. | 
 | 3331 |  * This code is out of bounds for data forks of regular files, | 
 | 3332 |  * since the file data needs to get logged so things will stay consistent. | 
 | 3333 |  * (The bmap-level manipulations are ok, though). | 
 | 3334 |  */ | 
 | 3335 | STATIC int				/* error */ | 
 | 3336 | xfs_bmap_local_to_extents( | 
 | 3337 | 	xfs_trans_t	*tp,		/* transaction pointer */ | 
 | 3338 | 	xfs_inode_t	*ip,		/* incore inode pointer */ | 
 | 3339 | 	xfs_fsblock_t	*firstblock,	/* first block allocated in xaction */ | 
 | 3340 | 	xfs_extlen_t	total,		/* total blocks needed by transaction */ | 
 | 3341 | 	int		*logflagsp,	/* inode logging flags */ | 
 | 3342 | 	int		whichfork)	/* data or attr fork */ | 
 | 3343 | { | 
 | 3344 | 	int		error;		/* error return value */ | 
 | 3345 | 	int		flags;		/* logging flags returned */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3346 | 	xfs_ifork_t	*ifp;		/* inode fork pointer */ | 
 | 3347 |  | 
 | 3348 | 	/* | 
 | 3349 | 	 * We don't want to deal with the case of keeping inode data inline yet. | 
 | 3350 | 	 * So sending the data fork of a regular inode is invalid. | 
 | 3351 | 	 */ | 
 | 3352 | 	ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG && | 
 | 3353 | 		 whichfork == XFS_DATA_FORK)); | 
 | 3354 | 	ifp = XFS_IFORK_PTR(ip, whichfork); | 
 | 3355 | 	ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); | 
 | 3356 | 	flags = 0; | 
 | 3357 | 	error = 0; | 
 | 3358 | 	if (ifp->if_bytes) { | 
 | 3359 | 		xfs_alloc_arg_t	args;	/* allocation arguments */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3360 | 		xfs_buf_t	*bp;	/* buffer for extent block */ | 
| Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3361 | 		xfs_bmbt_rec_host_t *ep;/* extent record pointer */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3362 |  | 
 | 3363 | 		args.tp = tp; | 
 | 3364 | 		args.mp = ip->i_mount; | 
| Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 3365 | 		args.firstblock = *firstblock; | 
| Mandy Kirkconnell | f020b67 | 2006-03-14 14:07:24 +1100 | [diff] [blame] | 3366 | 		ASSERT((ifp->if_flags & | 
 | 3367 | 			(XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3368 | 		/* | 
 | 3369 | 		 * Allocate a block.  We know we need only one, since the | 
 | 3370 | 		 * file currently fits in an inode. | 
 | 3371 | 		 */ | 
 | 3372 | 		if (*firstblock == NULLFSBLOCK) { | 
 | 3373 | 			args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); | 
 | 3374 | 			args.type = XFS_ALLOCTYPE_START_BNO; | 
 | 3375 | 		} else { | 
 | 3376 | 			args.fsbno = *firstblock; | 
 | 3377 | 			args.type = XFS_ALLOCTYPE_NEAR_BNO; | 
 | 3378 | 		} | 
 | 3379 | 		args.total = total; | 
 | 3380 | 		args.mod = args.minleft = args.alignment = args.wasdel = | 
 | 3381 | 			args.isfl = args.minalignslop = 0; | 
 | 3382 | 		args.minlen = args.maxlen = args.prod = 1; | 
 | 3383 | 		if ((error = xfs_alloc_vextent(&args))) | 
 | 3384 | 			goto done; | 
 | 3385 | 		/* | 
 | 3386 | 		 * Can't fail, the space was reserved. | 
 | 3387 | 		 */ | 
 | 3388 | 		ASSERT(args.fsbno != NULLFSBLOCK); | 
 | 3389 | 		ASSERT(args.len == 1); | 
 | 3390 | 		*firstblock = args.fsbno; | 
 | 3391 | 		bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0); | 
 | 3392 | 		memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data, | 
 | 3393 | 			ifp->if_bytes); | 
 | 3394 | 		xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1); | 
| Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3395 | 		xfs_bmap_forkoff_reset(args.mp, ip, whichfork); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3396 | 		xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3397 | 		xfs_iext_add(ifp, 0, 1); | 
 | 3398 | 		ep = xfs_iext_get_ext(ifp, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3399 | 		xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3400 | 		trace_xfs_bmap_post_update(ip, 0, | 
 | 3401 | 				whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0, | 
 | 3402 | 				_THIS_IP_); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3403 | 		XFS_IFORK_NEXT_SET(ip, whichfork, 1); | 
 | 3404 | 		ip->i_d.di_nblocks = 1; | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3405 | 		xfs_trans_mod_dquot_byino(tp, ip, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3406 | 			XFS_TRANS_DQ_BCOUNT, 1L); | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3407 | 		flags |= xfs_ilog_fext(whichfork); | 
| Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3408 | 	} else { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3409 | 		ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); | 
| Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3410 | 		xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork); | 
 | 3411 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3412 | 	ifp->if_flags &= ~XFS_IFINLINE; | 
 | 3413 | 	ifp->if_flags |= XFS_IFEXTENTS; | 
 | 3414 | 	XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); | 
 | 3415 | 	flags |= XFS_ILOG_CORE; | 
 | 3416 | done: | 
 | 3417 | 	*logflagsp = flags; | 
 | 3418 | 	return error; | 
 | 3419 | } | 
 | 3420 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3421 | /* | 
| Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3422 |  * Search the extent records for the entry containing block bno. | 
 | 3423 |  * If bno lies in a hole, point to the next entry.  If bno lies | 
 | 3424 |  * past eof, *eofp will be set, and *prevp will contain the last | 
 | 3425 |  * entry (null if none).  Else, *lastxp will be set to the index | 
 | 3426 |  * of the found entry; *gotp will contain the entry. | 
| Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3427 |  */ | 
| Eric Sandeen | d96f8f8 | 2009-07-02 00:09:33 -0500 | [diff] [blame] | 3428 | STATIC xfs_bmbt_rec_host_t *		/* pointer to found extent entry */ | 
| Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3429 | xfs_bmap_search_multi_extents( | 
 | 3430 | 	xfs_ifork_t	*ifp,		/* inode fork pointer */ | 
 | 3431 | 	xfs_fileoff_t	bno,		/* block number searched for */ | 
 | 3432 | 	int		*eofp,		/* out: end of file found */ | 
 | 3433 | 	xfs_extnum_t	*lastxp,	/* out: last extent index */ | 
 | 3434 | 	xfs_bmbt_irec_t	*gotp,		/* out: extent entry found */ | 
 | 3435 | 	xfs_bmbt_irec_t	*prevp)		/* out: previous extent entry found */ | 
 | 3436 | { | 
| Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3437 | 	xfs_bmbt_rec_host_t *ep;		/* extent record pointer */ | 
| Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3438 | 	xfs_extnum_t	lastx;		/* last extent index */ | 
| Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3439 |  | 
 | 3440 | 	/* | 
| Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3441 | 	 * Initialize the extent entry structure to catch access to | 
 | 3442 | 	 * uninitialized br_startblock field. | 
| Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3443 | 	 */ | 
| Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3444 | 	gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL; | 
 | 3445 | 	gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL; | 
 | 3446 | 	gotp->br_state = XFS_EXT_INVALID; | 
 | 3447 | #if XFS_BIG_BLKNOS | 
 | 3448 | 	gotp->br_startblock = 0xffffa5a5a5a5a5a5LL; | 
 | 3449 | #else | 
 | 3450 | 	gotp->br_startblock = 0xffffa5a5; | 
 | 3451 | #endif | 
 | 3452 | 	prevp->br_startoff = NULLFILEOFF; | 
 | 3453 |  | 
 | 3454 | 	ep = xfs_iext_bno_to_ext(ifp, bno, &lastx); | 
 | 3455 | 	if (lastx > 0) { | 
 | 3456 | 		xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp); | 
 | 3457 | 	} | 
 | 3458 | 	if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) { | 
 | 3459 | 		xfs_bmbt_get_all(ep, gotp); | 
 | 3460 | 		*eofp = 0; | 
| Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3461 | 	} else { | 
| Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3462 | 		if (lastx > 0) { | 
 | 3463 | 			*gotp = *prevp; | 
 | 3464 | 		} | 
 | 3465 | 		*eofp = 1; | 
 | 3466 | 		ep = NULL; | 
| Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3467 | 	} | 
| Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3468 | 	*lastxp = lastx; | 
| Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3469 | 	return ep; | 
 | 3470 | } | 
 | 3471 |  | 
 | 3472 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3473 |  * Search the extents list for the inode, for the extent containing bno. | 
 | 3474 |  * If bno lies in a hole, point to the next entry.  If bno lies past eof, | 
 | 3475 |  * *eofp will be set, and *prevp will contain the last entry (null if none). | 
 | 3476 |  * Else, *lastxp will be set to the index of the found | 
 | 3477 |  * entry; *gotp will contain the entry. | 
 | 3478 |  */ | 
| Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3479 | STATIC xfs_bmbt_rec_host_t *                 /* pointer to found extent entry */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3480 | xfs_bmap_search_extents( | 
 | 3481 | 	xfs_inode_t     *ip,            /* incore inode pointer */ | 
 | 3482 | 	xfs_fileoff_t   bno,            /* block number searched for */ | 
| Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3483 | 	int             fork,      	/* data or attr fork */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3484 | 	int             *eofp,          /* out: end of file found */ | 
 | 3485 | 	xfs_extnum_t    *lastxp,        /* out: last extent index */ | 
 | 3486 | 	xfs_bmbt_irec_t *gotp,          /* out: extent entry found */ | 
 | 3487 | 	xfs_bmbt_irec_t *prevp)         /* out: previous extent entry found */ | 
 | 3488 | { | 
 | 3489 | 	xfs_ifork_t	*ifp;		/* inode fork pointer */ | 
| Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3490 | 	xfs_bmbt_rec_host_t  *ep;            /* extent record pointer */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3491 |  | 
 | 3492 | 	XFS_STATS_INC(xs_look_exlist); | 
| Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3493 | 	ifp = XFS_IFORK_PTR(ip, fork); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3494 |  | 
| Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3495 | 	ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp); | 
 | 3496 |  | 
| Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3497 | 	if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) && | 
 | 3498 | 		     !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) { | 
 | 3499 | 		xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount, | 
 | 3500 | 				"Access to block zero in inode %llu " | 
 | 3501 | 				"start_block: %llx start_off: %llx " | 
 | 3502 | 				"blkcnt: %llx extent-state: %x lastx: %x\n", | 
 | 3503 | 			(unsigned long long)ip->i_ino, | 
| Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 3504 | 			(unsigned long long)gotp->br_startblock, | 
 | 3505 | 			(unsigned long long)gotp->br_startoff, | 
 | 3506 | 			(unsigned long long)gotp->br_blockcount, | 
| Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3507 | 			gotp->br_state, *lastxp); | 
 | 3508 | 		*lastxp = NULLEXTNUM; | 
 | 3509 | 		*eofp = 1; | 
 | 3510 | 		return NULL; | 
 | 3511 | 	} | 
 | 3512 | 	return ep; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3513 | } | 
 | 3514 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3515 | /* | 
 | 3516 |  * Compute the worst-case number of indirect blocks that will be used | 
 | 3517 |  * for ip's delayed extent of length "len". | 
 | 3518 |  */ | 
 | 3519 | STATIC xfs_filblks_t | 
 | 3520 | xfs_bmap_worst_indlen( | 
 | 3521 | 	xfs_inode_t	*ip,		/* incore inode pointer */ | 
 | 3522 | 	xfs_filblks_t	len)		/* delayed extent length */ | 
 | 3523 | { | 
 | 3524 | 	int		level;		/* btree level number */ | 
 | 3525 | 	int		maxrecs;	/* maximum record count at this level */ | 
 | 3526 | 	xfs_mount_t	*mp;		/* mount structure */ | 
 | 3527 | 	xfs_filblks_t	rval;		/* return value */ | 
 | 3528 |  | 
 | 3529 | 	mp = ip->i_mount; | 
 | 3530 | 	maxrecs = mp->m_bmap_dmxr[0]; | 
 | 3531 | 	for (level = 0, rval = 0; | 
 | 3532 | 	     level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK); | 
 | 3533 | 	     level++) { | 
 | 3534 | 		len += maxrecs - 1; | 
 | 3535 | 		do_div(len, maxrecs); | 
 | 3536 | 		rval += len; | 
 | 3537 | 		if (len == 1) | 
 | 3538 | 			return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) - | 
 | 3539 | 				level - 1; | 
 | 3540 | 		if (level == 0) | 
 | 3541 | 			maxrecs = mp->m_bmap_dmxr[1]; | 
 | 3542 | 	} | 
 | 3543 | 	return rval; | 
 | 3544 | } | 
 | 3545 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3546 | /* | 
 | 3547 |  * Convert inode from non-attributed to attributed. | 
 | 3548 |  * Must not be in a transaction, ip must not be locked. | 
 | 3549 |  */ | 
 | 3550 | int						/* error code */ | 
 | 3551 | xfs_bmap_add_attrfork( | 
 | 3552 | 	xfs_inode_t		*ip,		/* incore inode pointer */ | 
| Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3553 | 	int			size,		/* space new attribute needs */ | 
 | 3554 | 	int			rsvd)		/* xact may use reserved blks */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3555 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3556 | 	xfs_fsblock_t		firstblock;	/* 1st block/ag allocated */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3557 | 	xfs_bmap_free_t		flist;		/* freed extent records */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3558 | 	xfs_mount_t		*mp;		/* mount structure */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3559 | 	xfs_trans_t		*tp;		/* transaction pointer */ | 
| Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3560 | 	int			blks;		/* space reservation */ | 
 | 3561 | 	int			version = 1;	/* superblock attr version */ | 
 | 3562 | 	int			committed;	/* xaction was committed */ | 
 | 3563 | 	int			logflags;	/* logging flags */ | 
 | 3564 | 	int			error;		/* error return value */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3565 |  | 
| Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3566 | 	ASSERT(XFS_IFORK_Q(ip) == 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3567 | 	ASSERT(ip->i_df.if_ext_max == | 
 | 3568 | 	       XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t)); | 
| Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3569 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3570 | 	mp = ip->i_mount; | 
 | 3571 | 	ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); | 
 | 3572 | 	tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK); | 
 | 3573 | 	blks = XFS_ADDAFORK_SPACE_RES(mp); | 
 | 3574 | 	if (rsvd) | 
 | 3575 | 		tp->t_flags |= XFS_TRANS_RESERVE; | 
 | 3576 | 	if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0, | 
 | 3577 | 			XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT))) | 
 | 3578 | 		goto error0; | 
 | 3579 | 	xfs_ilock(ip, XFS_ILOCK_EXCL); | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3580 | 	error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ? | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3581 | 			XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : | 
 | 3582 | 			XFS_QMOPT_RES_REGBLKS); | 
 | 3583 | 	if (error) { | 
 | 3584 | 		xfs_iunlock(ip, XFS_ILOCK_EXCL); | 
 | 3585 | 		xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES); | 
 | 3586 | 		return error; | 
 | 3587 | 	} | 
 | 3588 | 	if (XFS_IFORK_Q(ip)) | 
 | 3589 | 		goto error1; | 
 | 3590 | 	if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { | 
 | 3591 | 		/* | 
 | 3592 | 		 * For inodes coming from pre-6.2 filesystems. | 
 | 3593 | 		 */ | 
 | 3594 | 		ASSERT(ip->i_d.di_aformat == 0); | 
 | 3595 | 		ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; | 
 | 3596 | 	} | 
 | 3597 | 	ASSERT(ip->i_d.di_anextents == 0); | 
| Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 3598 |  | 
 | 3599 | 	xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3600 | 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | 
| Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 3601 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3602 | 	switch (ip->i_d.di_format) { | 
 | 3603 | 	case XFS_DINODE_FMT_DEV: | 
 | 3604 | 		ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; | 
 | 3605 | 		break; | 
 | 3606 | 	case XFS_DINODE_FMT_UUID: | 
 | 3607 | 		ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3; | 
 | 3608 | 		break; | 
 | 3609 | 	case XFS_DINODE_FMT_LOCAL: | 
 | 3610 | 	case XFS_DINODE_FMT_EXTENTS: | 
 | 3611 | 	case XFS_DINODE_FMT_BTREE: | 
| Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3612 | 		ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); | 
 | 3613 | 		if (!ip->i_d.di_forkoff) | 
| Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame] | 3614 | 			ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3; | 
| Nathan Scott | 13059ff | 2006-01-11 15:32:01 +1100 | [diff] [blame] | 3615 | 		else if (mp->m_flags & XFS_MOUNT_ATTR2) | 
| Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3616 | 			version = 2; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3617 | 		break; | 
 | 3618 | 	default: | 
 | 3619 | 		ASSERT(0); | 
 | 3620 | 		error = XFS_ERROR(EINVAL); | 
 | 3621 | 		goto error1; | 
 | 3622 | 	} | 
 | 3623 | 	ip->i_df.if_ext_max = | 
 | 3624 | 		XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t); | 
 | 3625 | 	ASSERT(ip->i_afp == NULL); | 
 | 3626 | 	ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); | 
 | 3627 | 	ip->i_afp->if_ext_max = | 
 | 3628 | 		XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t); | 
 | 3629 | 	ip->i_afp->if_flags = XFS_IFEXTENTS; | 
 | 3630 | 	logflags = 0; | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3631 | 	xfs_bmap_init(&flist, &firstblock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3632 | 	switch (ip->i_d.di_format) { | 
 | 3633 | 	case XFS_DINODE_FMT_LOCAL: | 
 | 3634 | 		error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist, | 
 | 3635 | 			&logflags); | 
 | 3636 | 		break; | 
 | 3637 | 	case XFS_DINODE_FMT_EXTENTS: | 
 | 3638 | 		error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock, | 
 | 3639 | 			&flist, &logflags); | 
 | 3640 | 		break; | 
 | 3641 | 	case XFS_DINODE_FMT_BTREE: | 
 | 3642 | 		error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist, | 
 | 3643 | 			&logflags); | 
 | 3644 | 		break; | 
 | 3645 | 	default: | 
 | 3646 | 		error = 0; | 
 | 3647 | 		break; | 
 | 3648 | 	} | 
 | 3649 | 	if (logflags) | 
 | 3650 | 		xfs_trans_log_inode(tp, ip, logflags); | 
 | 3651 | 	if (error) | 
 | 3652 | 		goto error2; | 
| Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 3653 | 	if (!xfs_sb_version_hasattr(&mp->m_sb) || | 
 | 3654 | 	   (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { | 
| Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3655 | 		__int64_t sbfields = 0; | 
 | 3656 |  | 
| Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 3657 | 		spin_lock(&mp->m_sb_lock); | 
| Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 3658 | 		if (!xfs_sb_version_hasattr(&mp->m_sb)) { | 
 | 3659 | 			xfs_sb_version_addattr(&mp->m_sb); | 
| Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3660 | 			sbfields |= XFS_SB_VERSIONNUM; | 
| Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3661 | 		} | 
| Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 3662 | 		if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { | 
 | 3663 | 			xfs_sb_version_addattr2(&mp->m_sb); | 
| Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3664 | 			sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2); | 
| Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3665 | 		} | 
| Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3666 | 		if (sbfields) { | 
| Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 3667 | 			spin_unlock(&mp->m_sb_lock); | 
| Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3668 | 			xfs_mod_sb(tp, sbfields); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3669 | 		} else | 
| Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 3670 | 			spin_unlock(&mp->m_sb_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3671 | 	} | 
| Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 3672 | 	if ((error = xfs_bmap_finish(&tp, &flist, &committed))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3673 | 		goto error2; | 
| Dave Chinner | 713bf88 | 2010-03-08 11:26:23 +1100 | [diff] [blame] | 3674 | 	error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3675 | 	ASSERT(ip->i_df.if_ext_max == | 
 | 3676 | 	       XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t)); | 
 | 3677 | 	return error; | 
 | 3678 | error2: | 
 | 3679 | 	xfs_bmap_cancel(&flist); | 
 | 3680 | error1: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3681 | 	xfs_iunlock(ip, XFS_ILOCK_EXCL); | 
 | 3682 | error0: | 
 | 3683 | 	xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT); | 
 | 3684 | 	ASSERT(ip->i_df.if_ext_max == | 
 | 3685 | 	       XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t)); | 
 | 3686 | 	return error; | 
 | 3687 | } | 
 | 3688 |  | 
 | 3689 | /* | 
 | 3690 |  * Add the extent to the list of extents to be free at transaction end. | 
 | 3691 |  * The list is maintained sorted (by block number). | 
 | 3692 |  */ | 
 | 3693 | /* ARGSUSED */ | 
 | 3694 | void | 
 | 3695 | xfs_bmap_add_free( | 
 | 3696 | 	xfs_fsblock_t		bno,		/* fs block number of extent */ | 
 | 3697 | 	xfs_filblks_t		len,		/* length of extent */ | 
 | 3698 | 	xfs_bmap_free_t		*flist,		/* list of extents */ | 
 | 3699 | 	xfs_mount_t		*mp)		/* mount point structure */ | 
 | 3700 | { | 
 | 3701 | 	xfs_bmap_free_item_t	*cur;		/* current (next) element */ | 
 | 3702 | 	xfs_bmap_free_item_t	*new;		/* new element */ | 
 | 3703 | 	xfs_bmap_free_item_t	*prev;		/* previous element */ | 
 | 3704 | #ifdef DEBUG | 
 | 3705 | 	xfs_agnumber_t		agno; | 
 | 3706 | 	xfs_agblock_t		agbno; | 
 | 3707 |  | 
 | 3708 | 	ASSERT(bno != NULLFSBLOCK); | 
 | 3709 | 	ASSERT(len > 0); | 
 | 3710 | 	ASSERT(len <= MAXEXTLEN); | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3711 | 	ASSERT(!isnullstartblock(bno)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3712 | 	agno = XFS_FSB_TO_AGNO(mp, bno); | 
 | 3713 | 	agbno = XFS_FSB_TO_AGBNO(mp, bno); | 
 | 3714 | 	ASSERT(agno < mp->m_sb.sb_agcount); | 
 | 3715 | 	ASSERT(agbno < mp->m_sb.sb_agblocks); | 
 | 3716 | 	ASSERT(len < mp->m_sb.sb_agblocks); | 
 | 3717 | 	ASSERT(agbno + len <= mp->m_sb.sb_agblocks); | 
 | 3718 | #endif | 
 | 3719 | 	ASSERT(xfs_bmap_free_item_zone != NULL); | 
 | 3720 | 	new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP); | 
 | 3721 | 	new->xbfi_startblock = bno; | 
 | 3722 | 	new->xbfi_blockcount = (xfs_extlen_t)len; | 
 | 3723 | 	for (prev = NULL, cur = flist->xbf_first; | 
 | 3724 | 	     cur != NULL; | 
 | 3725 | 	     prev = cur, cur = cur->xbfi_next) { | 
 | 3726 | 		if (cur->xbfi_startblock >= bno) | 
 | 3727 | 			break; | 
 | 3728 | 	} | 
 | 3729 | 	if (prev) | 
 | 3730 | 		prev->xbfi_next = new; | 
 | 3731 | 	else | 
 | 3732 | 		flist->xbf_first = new; | 
 | 3733 | 	new->xbfi_next = cur; | 
 | 3734 | 	flist->xbf_count++; | 
 | 3735 | } | 
 | 3736 |  | 
 | 3737 | /* | 
 | 3738 |  * Compute and fill in the value of the maximum depth of a bmap btree | 
 | 3739 |  * in this filesystem.  Done once, during mount. | 
 | 3740 |  */ | 
 | 3741 | void | 
 | 3742 | xfs_bmap_compute_maxlevels( | 
 | 3743 | 	xfs_mount_t	*mp,		/* file system mount structure */ | 
 | 3744 | 	int		whichfork)	/* data or attr fork */ | 
 | 3745 | { | 
 | 3746 | 	int		level;		/* btree level */ | 
 | 3747 | 	uint		maxblocks;	/* max blocks at this level */ | 
 | 3748 | 	uint		maxleafents;	/* max leaf entries possible */ | 
 | 3749 | 	int		maxrootrecs;	/* max records in root block */ | 
 | 3750 | 	int		minleafrecs;	/* min records in leaf block */ | 
 | 3751 | 	int		minnoderecs;	/* min records in node block */ | 
 | 3752 | 	int		sz;		/* root block size */ | 
 | 3753 |  | 
 | 3754 | 	/* | 
 | 3755 | 	 * The maximum number of extents in a file, hence the maximum | 
 | 3756 | 	 * number of leaf entries, is controlled by the type of di_nextents | 
 | 3757 | 	 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents | 
 | 3758 | 	 * (a signed 16-bit number, xfs_aextnum_t). | 
| Tim Shimmin | 6d1337b | 2008-04-17 16:50:16 +1000 | [diff] [blame] | 3759 | 	 * | 
 | 3760 | 	 * Note that we can no longer assume that if we are in ATTR1 that | 
| Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame] | 3761 | 	 * the fork offset of all the inodes will be | 
 | 3762 | 	 * (xfs_default_attroffset(ip) >> 3) because we could have mounted | 
 | 3763 | 	 * with ATTR2 and then mounted back with ATTR1, keeping the | 
 | 3764 | 	 * di_forkoff's fixed but probably at various positions. Therefore, | 
 | 3765 | 	 * for both ATTR1 and ATTR2 we have to assume the worst case scenario | 
 | 3766 | 	 * of a minimum size available. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3767 | 	 */ | 
| Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3768 | 	if (whichfork == XFS_DATA_FORK) { | 
 | 3769 | 		maxleafents = MAXEXTNUM; | 
| Tim Shimmin | 6d1337b | 2008-04-17 16:50:16 +1000 | [diff] [blame] | 3770 | 		sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS); | 
| Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3771 | 	} else { | 
 | 3772 | 		maxleafents = MAXAEXTNUM; | 
| Tim Shimmin | 6d1337b | 2008-04-17 16:50:16 +1000 | [diff] [blame] | 3773 | 		sz = XFS_BMDR_SPACE_CALC(MINABTPTRS); | 
| Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3774 | 	} | 
| Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 3775 | 	maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3776 | 	minleafrecs = mp->m_bmap_dmnr[0]; | 
 | 3777 | 	minnoderecs = mp->m_bmap_dmnr[1]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3778 | 	maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; | 
 | 3779 | 	for (level = 1; maxblocks > 1; level++) { | 
 | 3780 | 		if (maxblocks <= maxrootrecs) | 
 | 3781 | 			maxblocks = 1; | 
 | 3782 | 		else | 
 | 3783 | 			maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; | 
 | 3784 | 	} | 
 | 3785 | 	mp->m_bm_maxlevels[whichfork] = level; | 
 | 3786 | } | 
 | 3787 |  | 
 | 3788 | /* | 
 | 3789 |  * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi | 
 | 3790 |  * caller.  Frees all the extents that need freeing, which must be done | 
 | 3791 |  * last due to locking considerations.  We never free any extents in | 
 | 3792 |  * the first transaction.  This is to allow the caller to make the first | 
 | 3793 |  * transaction a synchronous one so that the pointers to the data being | 
 | 3794 |  * broken in this transaction will be permanent before the data is actually | 
 | 3795 |  * freed.  This is necessary to prevent blocks from being reallocated | 
 | 3796 |  * and written to before the free and reallocation are actually permanent. | 
 | 3797 |  * We do not just make the first transaction synchronous here, because | 
 | 3798 |  * there are more efficient ways to gain the same protection in some cases | 
 | 3799 |  * (see the file truncation code). | 
 | 3800 |  * | 
 | 3801 |  * Return 1 if the given transaction was committed and a new one | 
 | 3802 |  * started, and 0 otherwise in the committed parameter. | 
 | 3803 |  */ | 
 | 3804 | /*ARGSUSED*/ | 
 | 3805 | int						/* error */ | 
 | 3806 | xfs_bmap_finish( | 
 | 3807 | 	xfs_trans_t		**tp,		/* transaction pointer addr */ | 
 | 3808 | 	xfs_bmap_free_t		*flist,		/* i/o: list extents to free */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3809 | 	int			*committed)	/* xact committed or not */ | 
 | 3810 | { | 
 | 3811 | 	xfs_efd_log_item_t	*efd;		/* extent free data */ | 
 | 3812 | 	xfs_efi_log_item_t	*efi;		/* extent free intention */ | 
 | 3813 | 	int			error;		/* error return value */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3814 | 	xfs_bmap_free_item_t	*free;		/* free extent item */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3815 | 	unsigned int		logres;		/* new log reservation */ | 
 | 3816 | 	unsigned int		logcount;	/* new log count */ | 
 | 3817 | 	xfs_mount_t		*mp;		/* filesystem mount structure */ | 
 | 3818 | 	xfs_bmap_free_item_t	*next;		/* next item on free list */ | 
 | 3819 | 	xfs_trans_t		*ntp;		/* new transaction pointer */ | 
 | 3820 |  | 
 | 3821 | 	ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES); | 
 | 3822 | 	if (flist->xbf_count == 0) { | 
 | 3823 | 		*committed = 0; | 
 | 3824 | 		return 0; | 
 | 3825 | 	} | 
 | 3826 | 	ntp = *tp; | 
 | 3827 | 	efi = xfs_trans_get_efi(ntp, flist->xbf_count); | 
 | 3828 | 	for (free = flist->xbf_first; free; free = free->xbfi_next) | 
 | 3829 | 		xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock, | 
 | 3830 | 			free->xbfi_blockcount); | 
 | 3831 | 	logres = ntp->t_log_res; | 
 | 3832 | 	logcount = ntp->t_log_count; | 
 | 3833 | 	ntp = xfs_trans_dup(*tp); | 
| Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 3834 | 	error = xfs_trans_commit(*tp, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3835 | 	*tp = ntp; | 
 | 3836 | 	*committed = 1; | 
 | 3837 | 	/* | 
 | 3838 | 	 * We have a new transaction, so we should return committed=1, | 
 | 3839 | 	 * even though we're returning an error. | 
 | 3840 | 	 */ | 
| Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 3841 | 	if (error) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3842 | 		return error; | 
| Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 3843 |  | 
 | 3844 | 	/* | 
 | 3845 | 	 * transaction commit worked ok so we can drop the extra ticket | 
 | 3846 | 	 * reference that we gained in xfs_trans_dup() | 
 | 3847 | 	 */ | 
 | 3848 | 	xfs_log_ticket_put(ntp->t_ticket); | 
 | 3849 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3850 | 	if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES, | 
 | 3851 | 			logcount))) | 
 | 3852 | 		return error; | 
 | 3853 | 	efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count); | 
 | 3854 | 	for (free = flist->xbf_first; free != NULL; free = next) { | 
 | 3855 | 		next = free->xbfi_next; | 
 | 3856 | 		if ((error = xfs_free_extent(ntp, free->xbfi_startblock, | 
 | 3857 | 				free->xbfi_blockcount))) { | 
 | 3858 | 			/* | 
 | 3859 | 			 * The bmap free list will be cleaned up at a | 
 | 3860 | 			 * higher level.  The EFI will be canceled when | 
 | 3861 | 			 * this transaction is aborted. | 
 | 3862 | 			 * Need to force shutdown here to make sure it | 
 | 3863 | 			 * happens, since this transaction may not be | 
 | 3864 | 			 * dirty yet. | 
 | 3865 | 			 */ | 
 | 3866 | 			mp = ntp->t_mountp; | 
 | 3867 | 			if (!XFS_FORCED_SHUTDOWN(mp)) | 
 | 3868 | 				xfs_force_shutdown(mp, | 
 | 3869 | 						   (error == EFSCORRUPTED) ? | 
| Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 3870 | 						   SHUTDOWN_CORRUPT_INCORE : | 
 | 3871 | 						   SHUTDOWN_META_IO_ERROR); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3872 | 			return error; | 
 | 3873 | 		} | 
 | 3874 | 		xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock, | 
 | 3875 | 			free->xbfi_blockcount); | 
 | 3876 | 		xfs_bmap_del_free(flist, NULL, free); | 
 | 3877 | 	} | 
 | 3878 | 	return 0; | 
 | 3879 | } | 
 | 3880 |  | 
 | 3881 | /* | 
 | 3882 |  * Free up any items left in the list. | 
 | 3883 |  */ | 
 | 3884 | void | 
 | 3885 | xfs_bmap_cancel( | 
 | 3886 | 	xfs_bmap_free_t		*flist)	/* list of bmap_free_items */ | 
 | 3887 | { | 
 | 3888 | 	xfs_bmap_free_item_t	*free;	/* free list item */ | 
 | 3889 | 	xfs_bmap_free_item_t	*next; | 
 | 3890 |  | 
 | 3891 | 	if (flist->xbf_count == 0) | 
 | 3892 | 		return; | 
 | 3893 | 	ASSERT(flist->xbf_first != NULL); | 
 | 3894 | 	for (free = flist->xbf_first; free; free = next) { | 
 | 3895 | 		next = free->xbfi_next; | 
 | 3896 | 		xfs_bmap_del_free(flist, NULL, free); | 
 | 3897 | 	} | 
 | 3898 | 	ASSERT(flist->xbf_count == 0); | 
 | 3899 | } | 
 | 3900 |  | 
 | 3901 | /* | 
 | 3902 |  * Returns the file-relative block number of the first unused block(s) | 
 | 3903 |  * in the file with at least "len" logically contiguous blocks free. | 
 | 3904 |  * This is the lowest-address hole if the file has holes, else the first block | 
 | 3905 |  * past the end of file. | 
 | 3906 |  * Return 0 if the file is currently local (in-inode). | 
 | 3907 |  */ | 
 | 3908 | int						/* error */ | 
 | 3909 | xfs_bmap_first_unused( | 
 | 3910 | 	xfs_trans_t	*tp,			/* transaction pointer */ | 
 | 3911 | 	xfs_inode_t	*ip,			/* incore inode */ | 
 | 3912 | 	xfs_extlen_t	len,			/* size of hole to find */ | 
 | 3913 | 	xfs_fileoff_t	*first_unused,		/* unused block */ | 
 | 3914 | 	int		whichfork)		/* data or attr fork */ | 
 | 3915 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3916 | 	int		error;			/* error return value */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3917 | 	int		idx;			/* extent record index */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3918 | 	xfs_ifork_t	*ifp;			/* inode fork pointer */ | 
 | 3919 | 	xfs_fileoff_t	lastaddr;		/* last block number seen */ | 
 | 3920 | 	xfs_fileoff_t	lowest;			/* lowest useful block */ | 
 | 3921 | 	xfs_fileoff_t	max;			/* starting useful block */ | 
 | 3922 | 	xfs_fileoff_t	off;			/* offset for this block */ | 
 | 3923 | 	xfs_extnum_t	nextents;		/* number of extent entries */ | 
 | 3924 |  | 
 | 3925 | 	ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || | 
 | 3926 | 	       XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || | 
 | 3927 | 	       XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); | 
 | 3928 | 	if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { | 
 | 3929 | 		*first_unused = 0; | 
 | 3930 | 		return 0; | 
 | 3931 | 	} | 
 | 3932 | 	ifp = XFS_IFORK_PTR(ip, whichfork); | 
 | 3933 | 	if (!(ifp->if_flags & XFS_IFEXTENTS) && | 
 | 3934 | 	    (error = xfs_iread_extents(tp, ip, whichfork))) | 
 | 3935 | 		return error; | 
 | 3936 | 	lowest = *first_unused; | 
 | 3937 | 	nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3938 | 	for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) { | 
| Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3939 | 		xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3940 | 		off = xfs_bmbt_get_startoff(ep); | 
 | 3941 | 		/* | 
 | 3942 | 		 * See if the hole before this extent will work. | 
 | 3943 | 		 */ | 
 | 3944 | 		if (off >= lowest + len && off - max >= len) { | 
 | 3945 | 			*first_unused = max; | 
 | 3946 | 			return 0; | 
 | 3947 | 		} | 
 | 3948 | 		lastaddr = off + xfs_bmbt_get_blockcount(ep); | 
 | 3949 | 		max = XFS_FILEOFF_MAX(lastaddr, lowest); | 
 | 3950 | 	} | 
 | 3951 | 	*first_unused = max; | 
 | 3952 | 	return 0; | 
 | 3953 | } | 
 | 3954 |  | 
 | 3955 | /* | 
 | 3956 |  * Returns the file-relative block number of the last block + 1 before | 
 | 3957 |  * last_block (input value) in the file. | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3958 |  * This is not based on i_size, it is based on the extent records. | 
 | 3959 |  * Returns 0 for local files, as they do not have extent records. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3960 |  */ | 
 | 3961 | int						/* error */ | 
 | 3962 | xfs_bmap_last_before( | 
 | 3963 | 	xfs_trans_t	*tp,			/* transaction pointer */ | 
 | 3964 | 	xfs_inode_t	*ip,			/* incore inode */ | 
 | 3965 | 	xfs_fileoff_t	*last_block,		/* last block */ | 
 | 3966 | 	int		whichfork)		/* data or attr fork */ | 
 | 3967 | { | 
 | 3968 | 	xfs_fileoff_t	bno;			/* input file offset */ | 
 | 3969 | 	int		eof;			/* hit end of file */ | 
| Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3970 | 	xfs_bmbt_rec_host_t *ep;		/* pointer to last extent */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3971 | 	int		error;			/* error return value */ | 
 | 3972 | 	xfs_bmbt_irec_t	got;			/* current extent value */ | 
 | 3973 | 	xfs_ifork_t	*ifp;			/* inode fork pointer */ | 
 | 3974 | 	xfs_extnum_t	lastx;			/* last extent used */ | 
 | 3975 | 	xfs_bmbt_irec_t	prev;			/* previous extent value */ | 
 | 3976 |  | 
 | 3977 | 	if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && | 
 | 3978 | 	    XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && | 
 | 3979 | 	    XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL) | 
 | 3980 | 	       return XFS_ERROR(EIO); | 
 | 3981 | 	if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { | 
 | 3982 | 		*last_block = 0; | 
 | 3983 | 		return 0; | 
 | 3984 | 	} | 
 | 3985 | 	ifp = XFS_IFORK_PTR(ip, whichfork); | 
 | 3986 | 	if (!(ifp->if_flags & XFS_IFEXTENTS) && | 
 | 3987 | 	    (error = xfs_iread_extents(tp, ip, whichfork))) | 
 | 3988 | 		return error; | 
 | 3989 | 	bno = *last_block - 1; | 
 | 3990 | 	ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, | 
 | 3991 | 		&prev); | 
 | 3992 | 	if (eof || xfs_bmbt_get_startoff(ep) > bno) { | 
 | 3993 | 		if (prev.br_startoff == NULLFILEOFF) | 
 | 3994 | 			*last_block = 0; | 
 | 3995 | 		else | 
 | 3996 | 			*last_block = prev.br_startoff + prev.br_blockcount; | 
 | 3997 | 	} | 
 | 3998 | 	/* | 
 | 3999 | 	 * Otherwise *last_block is already the right answer. | 
 | 4000 | 	 */ | 
 | 4001 | 	return 0; | 
 | 4002 | } | 
 | 4003 |  | 
 | 4004 | /* | 
 | 4005 |  * Returns the file-relative block number of the first block past eof in | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4006 |  * the file.  This is not based on i_size, it is based on the extent records. | 
 | 4007 |  * Returns 0 for local files, as they do not have extent records. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4008 |  */ | 
 | 4009 | int						/* error */ | 
 | 4010 | xfs_bmap_last_offset( | 
 | 4011 | 	xfs_trans_t	*tp,			/* transaction pointer */ | 
 | 4012 | 	xfs_inode_t	*ip,			/* incore inode */ | 
 | 4013 | 	xfs_fileoff_t	*last_block,		/* last block */ | 
 | 4014 | 	int		whichfork)		/* data or attr fork */ | 
 | 4015 | { | 
| Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4016 | 	xfs_bmbt_rec_host_t *ep;		/* pointer to last extent */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4017 | 	int		error;			/* error return value */ | 
 | 4018 | 	xfs_ifork_t	*ifp;			/* inode fork pointer */ | 
 | 4019 | 	xfs_extnum_t	nextents;		/* number of extent entries */ | 
 | 4020 |  | 
 | 4021 | 	if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && | 
 | 4022 | 	    XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && | 
 | 4023 | 	    XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL) | 
 | 4024 | 	       return XFS_ERROR(EIO); | 
 | 4025 | 	if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { | 
 | 4026 | 		*last_block = 0; | 
 | 4027 | 		return 0; | 
 | 4028 | 	} | 
 | 4029 | 	ifp = XFS_IFORK_PTR(ip, whichfork); | 
 | 4030 | 	if (!(ifp->if_flags & XFS_IFEXTENTS) && | 
 | 4031 | 	    (error = xfs_iread_extents(tp, ip, whichfork))) | 
 | 4032 | 		return error; | 
 | 4033 | 	nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); | 
 | 4034 | 	if (!nextents) { | 
 | 4035 | 		*last_block = 0; | 
 | 4036 | 		return 0; | 
 | 4037 | 	} | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4038 | 	ep = xfs_iext_get_ext(ifp, nextents - 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4039 | 	*last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep); | 
 | 4040 | 	return 0; | 
 | 4041 | } | 
 | 4042 |  | 
 | 4043 | /* | 
 | 4044 |  * Returns whether the selected fork of the inode has exactly one | 
 | 4045 |  * block or not.  For the data fork we check this matches di_size, | 
 | 4046 |  * implying the file's range is 0..bsize-1. | 
 | 4047 |  */ | 
 | 4048 | int					/* 1=>1 block, 0=>otherwise */ | 
 | 4049 | xfs_bmap_one_block( | 
 | 4050 | 	xfs_inode_t	*ip,		/* incore inode */ | 
 | 4051 | 	int		whichfork)	/* data or attr fork */ | 
 | 4052 | { | 
| Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4053 | 	xfs_bmbt_rec_host_t *ep;	/* ptr to fork's extent */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4054 | 	xfs_ifork_t	*ifp;		/* inode fork pointer */ | 
 | 4055 | 	int		rval;		/* return value */ | 
 | 4056 | 	xfs_bmbt_irec_t	s;		/* internal version of extent */ | 
 | 4057 |  | 
 | 4058 | #ifndef DEBUG | 
| Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 4059 | 	if (whichfork == XFS_DATA_FORK) { | 
 | 4060 | 		return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ? | 
 | 4061 | 			(ip->i_size == ip->i_mount->m_sb.sb_blocksize) : | 
 | 4062 | 			(ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize); | 
 | 4063 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4064 | #endif	/* !DEBUG */ | 
 | 4065 | 	if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) | 
 | 4066 | 		return 0; | 
 | 4067 | 	if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) | 
 | 4068 | 		return 0; | 
 | 4069 | 	ifp = XFS_IFORK_PTR(ip, whichfork); | 
 | 4070 | 	ASSERT(ifp->if_flags & XFS_IFEXTENTS); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4071 | 	ep = xfs_iext_get_ext(ifp, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4072 | 	xfs_bmbt_get_all(ep, &s); | 
 | 4073 | 	rval = s.br_startoff == 0 && s.br_blockcount == 1; | 
 | 4074 | 	if (rval && whichfork == XFS_DATA_FORK) | 
| Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 4075 | 		ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4076 | 	return rval; | 
 | 4077 | } | 
 | 4078 |  | 
| Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4079 | STATIC int | 
 | 4080 | xfs_bmap_sanity_check( | 
 | 4081 | 	struct xfs_mount	*mp, | 
 | 4082 | 	struct xfs_buf		*bp, | 
 | 4083 | 	int			level) | 
 | 4084 | { | 
 | 4085 | 	struct xfs_btree_block  *block = XFS_BUF_TO_BLOCK(bp); | 
 | 4086 |  | 
 | 4087 | 	if (be32_to_cpu(block->bb_magic) != XFS_BMAP_MAGIC || | 
 | 4088 | 	    be16_to_cpu(block->bb_level) != level || | 
 | 4089 | 	    be16_to_cpu(block->bb_numrecs) == 0 || | 
 | 4090 | 	    be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0]) | 
 | 4091 | 		return 0; | 
 | 4092 | 	return 1; | 
 | 4093 | } | 
 | 4094 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4095 | /* | 
 | 4096 |  * Read in the extents to if_extents. | 
 | 4097 |  * All inode fields are set up by caller, we just traverse the btree | 
 | 4098 |  * and copy the records in. If the file system cannot contain unwritten | 
 | 4099 |  * extents, the records are checked for no "state" flags. | 
 | 4100 |  */ | 
 | 4101 | int					/* error */ | 
 | 4102 | xfs_bmap_read_extents( | 
 | 4103 | 	xfs_trans_t		*tp,	/* transaction pointer */ | 
 | 4104 | 	xfs_inode_t		*ip,	/* incore inode */ | 
 | 4105 | 	int			whichfork) /* data or attr fork */ | 
 | 4106 | { | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4107 | 	struct xfs_btree_block	*block;	/* current btree block */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4108 | 	xfs_fsblock_t		bno;	/* block # of "block" */ | 
 | 4109 | 	xfs_buf_t		*bp;	/* buffer for "block" */ | 
 | 4110 | 	int			error;	/* error return value */ | 
 | 4111 | 	xfs_exntfmt_t		exntf;	/* XFS_EXTFMT_NOSTATE, if checking */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4112 | 	xfs_extnum_t		i, j;	/* index into the extents list */ | 
 | 4113 | 	xfs_ifork_t		*ifp;	/* fork structure */ | 
 | 4114 | 	int			level;	/* btree level, for checking */ | 
 | 4115 | 	xfs_mount_t		*mp;	/* file system mount structure */ | 
| Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 4116 | 	__be64			*pp;	/* pointer to block address */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4117 | 	/* REFERENCED */ | 
 | 4118 | 	xfs_extnum_t		room;	/* number of entries there's room for */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4119 |  | 
 | 4120 | 	bno = NULLFSBLOCK; | 
 | 4121 | 	mp = ip->i_mount; | 
 | 4122 | 	ifp = XFS_IFORK_PTR(ip, whichfork); | 
 | 4123 | 	exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE : | 
 | 4124 | 					XFS_EXTFMT_INODE(ip); | 
 | 4125 | 	block = ifp->if_broot; | 
 | 4126 | 	/* | 
 | 4127 | 	 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. | 
 | 4128 | 	 */ | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 4129 | 	level = be16_to_cpu(block->bb_level); | 
 | 4130 | 	ASSERT(level > 0); | 
| Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 4131 | 	pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); | 
| Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 4132 | 	bno = be64_to_cpu(*pp); | 
 | 4133 | 	ASSERT(bno != NULLDFSBNO); | 
 | 4134 | 	ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); | 
 | 4135 | 	ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4136 | 	/* | 
 | 4137 | 	 * Go down the tree until leaf level is reached, following the first | 
 | 4138 | 	 * pointer (leftmost) at each level. | 
 | 4139 | 	 */ | 
 | 4140 | 	while (level-- > 0) { | 
 | 4141 | 		if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, | 
 | 4142 | 				XFS_BMAP_BTREE_REF))) | 
 | 4143 | 			return error; | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4144 | 		block = XFS_BUF_TO_BLOCK(bp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4145 | 		XFS_WANT_CORRUPTED_GOTO( | 
| Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4146 | 			xfs_bmap_sanity_check(mp, bp, level), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4147 | 			error0); | 
 | 4148 | 		if (level == 0) | 
 | 4149 | 			break; | 
| Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 4150 | 		pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); | 
| Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 4151 | 		bno = be64_to_cpu(*pp); | 
 | 4152 | 		XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4153 | 		xfs_trans_brelse(tp, bp); | 
 | 4154 | 	} | 
 | 4155 | 	/* | 
 | 4156 | 	 * Here with bp and block set to the leftmost leaf node in the tree. | 
 | 4157 | 	 */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4158 | 	room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4159 | 	i = 0; | 
 | 4160 | 	/* | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4161 | 	 * Loop over all leaf nodes.  Copy information to the extent records. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4162 | 	 */ | 
 | 4163 | 	for (;;) { | 
| Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4164 | 		xfs_bmbt_rec_t	*frp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4165 | 		xfs_fsblock_t	nextbno; | 
 | 4166 | 		xfs_extnum_t	num_recs; | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4167 | 		xfs_extnum_t	start; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4168 |  | 
 | 4169 |  | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4170 | 		num_recs = xfs_btree_get_numrecs(block); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4171 | 		if (unlikely(i + num_recs > room)) { | 
 | 4172 | 			ASSERT(i + num_recs <= room); | 
| Nathan Scott | 3762ec6 | 2006-01-12 10:29:53 +1100 | [diff] [blame] | 4173 | 			xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount, | 
 | 4174 | 				"corrupt dinode %Lu, (btree extents).", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4175 | 				(unsigned long long) ip->i_ino); | 
 | 4176 | 			XFS_ERROR_REPORT("xfs_bmap_read_extents(1)", | 
 | 4177 | 					 XFS_ERRLEVEL_LOW, | 
 | 4178 | 					ip->i_mount); | 
 | 4179 | 			goto error0; | 
 | 4180 | 		} | 
 | 4181 | 		XFS_WANT_CORRUPTED_GOTO( | 
| Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4182 | 			xfs_bmap_sanity_check(mp, bp, 0), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4183 | 			error0); | 
 | 4184 | 		/* | 
 | 4185 | 		 * Read-ahead the next leaf block, if any. | 
 | 4186 | 		 */ | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4187 | 		nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4188 | 		if (nextbno != NULLFSBLOCK) | 
 | 4189 | 			xfs_btree_reada_bufl(mp, nextbno, 1); | 
 | 4190 | 		/* | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4191 | 		 * Copy records into the extent records. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4192 | 		 */ | 
| Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 4193 | 		frp = XFS_BMBT_REC_ADDR(mp, block, 1); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4194 | 		start = i; | 
 | 4195 | 		for (j = 0; j < num_recs; j++, i++, frp++) { | 
| Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4196 | 			xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i); | 
| Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 4197 | 			trp->l0 = be64_to_cpu(frp->l0); | 
 | 4198 | 			trp->l1 = be64_to_cpu(frp->l1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4199 | 		} | 
 | 4200 | 		if (exntf == XFS_EXTFMT_NOSTATE) { | 
 | 4201 | 			/* | 
 | 4202 | 			 * Check all attribute bmap btree records and | 
 | 4203 | 			 * any "older" data bmap btree records for a | 
 | 4204 | 			 * set bit in the "extent flag" position. | 
 | 4205 | 			 */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4206 | 			if (unlikely(xfs_check_nostate_extents(ifp, | 
 | 4207 | 					start, num_recs))) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4208 | 				XFS_ERROR_REPORT("xfs_bmap_read_extents(2)", | 
 | 4209 | 						 XFS_ERRLEVEL_LOW, | 
 | 4210 | 						 ip->i_mount); | 
 | 4211 | 				goto error0; | 
 | 4212 | 			} | 
 | 4213 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4214 | 		xfs_trans_brelse(tp, bp); | 
 | 4215 | 		bno = nextbno; | 
 | 4216 | 		/* | 
 | 4217 | 		 * If we've reached the end, stop. | 
 | 4218 | 		 */ | 
 | 4219 | 		if (bno == NULLFSBLOCK) | 
 | 4220 | 			break; | 
 | 4221 | 		if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, | 
 | 4222 | 				XFS_BMAP_BTREE_REF))) | 
 | 4223 | 			return error; | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4224 | 		block = XFS_BUF_TO_BLOCK(bp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4225 | 	} | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4226 | 	ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4227 | 	ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork)); | 
| Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 4228 | 	XFS_BMAP_TRACE_EXLIST(ip, i, whichfork); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4229 | 	return 0; | 
 | 4230 | error0: | 
 | 4231 | 	xfs_trans_brelse(tp, bp); | 
 | 4232 | 	return XFS_ERROR(EFSCORRUPTED); | 
 | 4233 | } | 
 | 4234 |  | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4235 | #ifdef DEBUG | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4236 | /* | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4237 |  * Add bmap trace insert entries for all the contents of the extent records. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4238 |  */ | 
 | 4239 | void | 
 | 4240 | xfs_bmap_trace_exlist( | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4241 | 	xfs_inode_t	*ip,		/* incore inode pointer */ | 
 | 4242 | 	xfs_extnum_t	cnt,		/* count of entries in the list */ | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4243 | 	int		whichfork,	/* data or attr fork */ | 
 | 4244 | 	unsigned long	caller_ip) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4245 | { | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4246 | 	xfs_extnum_t	idx;		/* extent record index */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4247 | 	xfs_ifork_t	*ifp;		/* inode fork pointer */ | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4248 | 	int		state = 0; | 
 | 4249 |  | 
 | 4250 | 	if (whichfork == XFS_ATTR_FORK) | 
 | 4251 | 		state |= BMAP_ATTRFORK; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4252 |  | 
 | 4253 | 	ifp = XFS_IFORK_PTR(ip, whichfork); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4254 | 	ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4255 | 	for (idx = 0; idx < cnt; idx++) | 
 | 4256 | 		trace_xfs_extlist(ip, idx, whichfork, caller_ip); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4257 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4258 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4259 | /* | 
 | 4260 |  * Validate that the bmbt_irecs being returned from bmapi are valid | 
 | 4261 |  * given the callers original parameters.  Specifically check the | 
 | 4262 |  * ranges of the returned irecs to ensure that they only extent beyond | 
 | 4263 |  * the given parameters if the XFS_BMAPI_ENTIRE flag was set. | 
 | 4264 |  */ | 
 | 4265 | STATIC void | 
 | 4266 | xfs_bmap_validate_ret( | 
 | 4267 | 	xfs_fileoff_t		bno, | 
 | 4268 | 	xfs_filblks_t		len, | 
 | 4269 | 	int			flags, | 
 | 4270 | 	xfs_bmbt_irec_t		*mval, | 
 | 4271 | 	int			nmap, | 
 | 4272 | 	int			ret_nmap) | 
 | 4273 | { | 
 | 4274 | 	int			i;		/* index to map values */ | 
 | 4275 |  | 
 | 4276 | 	ASSERT(ret_nmap <= nmap); | 
 | 4277 |  | 
 | 4278 | 	for (i = 0; i < ret_nmap; i++) { | 
 | 4279 | 		ASSERT(mval[i].br_blockcount > 0); | 
 | 4280 | 		if (!(flags & XFS_BMAPI_ENTIRE)) { | 
 | 4281 | 			ASSERT(mval[i].br_startoff >= bno); | 
 | 4282 | 			ASSERT(mval[i].br_blockcount <= len); | 
 | 4283 | 			ASSERT(mval[i].br_startoff + mval[i].br_blockcount <= | 
 | 4284 | 			       bno + len); | 
 | 4285 | 		} else { | 
 | 4286 | 			ASSERT(mval[i].br_startoff < bno + len); | 
 | 4287 | 			ASSERT(mval[i].br_startoff + mval[i].br_blockcount > | 
 | 4288 | 			       bno); | 
 | 4289 | 		} | 
 | 4290 | 		ASSERT(i == 0 || | 
 | 4291 | 		       mval[i - 1].br_startoff + mval[i - 1].br_blockcount == | 
 | 4292 | 		       mval[i].br_startoff); | 
 | 4293 | 		if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY)) | 
 | 4294 | 			ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK && | 
 | 4295 | 			       mval[i].br_startblock != HOLESTARTBLOCK); | 
 | 4296 | 		ASSERT(mval[i].br_state == XFS_EXT_NORM || | 
 | 4297 | 		       mval[i].br_state == XFS_EXT_UNWRITTEN); | 
 | 4298 | 	} | 
 | 4299 | } | 
 | 4300 | #endif /* DEBUG */ | 
 | 4301 |  | 
 | 4302 |  | 
 | 4303 | /* | 
 | 4304 |  * Map file blocks to filesystem blocks. | 
 | 4305 |  * File range is given by the bno/len pair. | 
 | 4306 |  * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set) | 
 | 4307 |  * into a hole or past eof. | 
 | 4308 |  * Only allocates blocks from a single allocation group, | 
 | 4309 |  * to avoid locking problems. | 
 | 4310 |  * The returned value in "firstblock" from the first call in a transaction | 
 | 4311 |  * must be remembered and presented to subsequent calls in "firstblock". | 
 | 4312 |  * An upper bound for the number of blocks to be allocated is supplied to | 
 | 4313 |  * the first call in "total"; if no allocation group has that many free | 
 | 4314 |  * blocks then the call will fail (return NULLFSBLOCK in "firstblock"). | 
 | 4315 |  */ | 
 | 4316 | int					/* error */ | 
 | 4317 | xfs_bmapi( | 
 | 4318 | 	xfs_trans_t	*tp,		/* transaction pointer */ | 
 | 4319 | 	xfs_inode_t	*ip,		/* incore inode */ | 
 | 4320 | 	xfs_fileoff_t	bno,		/* starting file offs. mapped */ | 
 | 4321 | 	xfs_filblks_t	len,		/* length to map in file */ | 
 | 4322 | 	int		flags,		/* XFS_BMAPI_... */ | 
 | 4323 | 	xfs_fsblock_t	*firstblock,	/* first allocated block | 
 | 4324 | 					   controls a.g. for allocs */ | 
 | 4325 | 	xfs_extlen_t	total,		/* total blocks needed */ | 
 | 4326 | 	xfs_bmbt_irec_t	*mval,		/* output: map values */ | 
 | 4327 | 	int		*nmap,		/* i/o: mval size/count */ | 
| Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4328 | 	xfs_bmap_free_t	*flist)		/* i/o: list extents to free */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4329 | { | 
 | 4330 | 	xfs_fsblock_t	abno;		/* allocated block number */ | 
 | 4331 | 	xfs_extlen_t	alen;		/* allocated extent length */ | 
 | 4332 | 	xfs_fileoff_t	aoff;		/* allocated file offset */ | 
| Dave Chinner | f0a0eaa | 2010-01-20 10:50:06 +1100 | [diff] [blame] | 4333 | 	xfs_bmalloca_t	bma = { 0 };	/* args for xfs_bmap_alloc */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4334 | 	xfs_btree_cur_t	*cur;		/* bmap btree cursor */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4335 | 	xfs_fileoff_t	end;		/* end of mapped file region */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4336 | 	int		eof;		/* we've hit the end of extents */ | 
| Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4337 | 	xfs_bmbt_rec_host_t *ep;	/* extent record pointer */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4338 | 	int		error;		/* error return */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4339 | 	xfs_bmbt_irec_t	got;		/* current file extent record */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4340 | 	xfs_ifork_t	*ifp;		/* inode fork pointer */ | 
 | 4341 | 	xfs_extlen_t	indlen;		/* indirect blocks length */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4342 | 	xfs_extnum_t	lastx;		/* last useful extent number */ | 
 | 4343 | 	int		logflags;	/* flags for transaction logging */ | 
 | 4344 | 	xfs_extlen_t	minleft;	/* min blocks left after allocation */ | 
 | 4345 | 	xfs_extlen_t	minlen;		/* min allocation size */ | 
 | 4346 | 	xfs_mount_t	*mp;		/* xfs mount structure */ | 
 | 4347 | 	int		n;		/* current extent index */ | 
| Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 4348 | 	int		nallocs;	/* number of extents alloc'd */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4349 | 	xfs_extnum_t	nextents;	/* number of extents in file */ | 
 | 4350 | 	xfs_fileoff_t	obno;		/* old block number (offset) */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4351 | 	xfs_bmbt_irec_t	prev;		/* previous file extent record */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4352 | 	int		tmp_logflags;	/* temp flags holder */ | 
| Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4353 | 	int		whichfork;	/* data or attr fork */ | 
 | 4354 | 	char		inhole;		/* current location is hole in file */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4355 | 	char		wasdelay;	/* old extent was delayed */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4356 | 	char		wr;		/* this is a write request */ | 
| Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4357 | 	char		rt;		/* this is a realtime file */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4358 | #ifdef DEBUG | 
 | 4359 | 	xfs_fileoff_t	orig_bno;	/* original block number value */ | 
 | 4360 | 	int		orig_flags;	/* original flags arg value */ | 
 | 4361 | 	xfs_filblks_t	orig_len;	/* original value of len arg */ | 
 | 4362 | 	xfs_bmbt_irec_t	*orig_mval;	/* original value of mval */ | 
 | 4363 | 	int		orig_nmap;	/* original value of *nmap */ | 
 | 4364 |  | 
 | 4365 | 	orig_bno = bno; | 
 | 4366 | 	orig_len = len; | 
 | 4367 | 	orig_flags = flags; | 
 | 4368 | 	orig_mval = mval; | 
 | 4369 | 	orig_nmap = *nmap; | 
 | 4370 | #endif | 
 | 4371 | 	ASSERT(*nmap >= 1); | 
 | 4372 | 	ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE)); | 
 | 4373 | 	whichfork = (flags & XFS_BMAPI_ATTRFORK) ? | 
 | 4374 | 		XFS_ATTR_FORK : XFS_DATA_FORK; | 
 | 4375 | 	mp = ip->i_mount; | 
 | 4376 | 	if (unlikely(XFS_TEST_ERROR( | 
 | 4377 | 	    (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && | 
 | 4378 | 	     XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && | 
 | 4379 | 	     XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL), | 
 | 4380 | 	     mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { | 
 | 4381 | 		XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp); | 
 | 4382 | 		return XFS_ERROR(EFSCORRUPTED); | 
 | 4383 | 	} | 
 | 4384 | 	if (XFS_FORCED_SHUTDOWN(mp)) | 
 | 4385 | 		return XFS_ERROR(EIO); | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4386 | 	rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4387 | 	ifp = XFS_IFORK_PTR(ip, whichfork); | 
 | 4388 | 	ASSERT(ifp->if_ext_max == | 
 | 4389 | 	       XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); | 
 | 4390 | 	if ((wr = (flags & XFS_BMAPI_WRITE)) != 0) | 
 | 4391 | 		XFS_STATS_INC(xs_blk_mapw); | 
 | 4392 | 	else | 
 | 4393 | 		XFS_STATS_INC(xs_blk_mapr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4394 | 	/* | 
| Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4395 | 	 * IGSTATE flag is used to combine extents which | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4396 | 	 * differ only due to the state of the extents. | 
 | 4397 | 	 * This technique is used from xfs_getbmap() | 
 | 4398 | 	 * when the caller does not wish to see the | 
 | 4399 | 	 * separation (which is the default). | 
 | 4400 | 	 * | 
 | 4401 | 	 * This technique is also used when writing a | 
 | 4402 | 	 * buffer which has been partially written, | 
 | 4403 | 	 * (usually by being flushed during a chunkread), | 
 | 4404 | 	 * to ensure one write takes place. This also | 
 | 4405 | 	 * prevents a change in the xfs inode extents at | 
 | 4406 | 	 * this time, intentionally. This change occurs | 
 | 4407 | 	 * on completion of the write operation, in | 
 | 4408 | 	 * xfs_strat_comp(), where the xfs_bmapi() call | 
 | 4409 | 	 * is transactioned, and the extents combined. | 
 | 4410 | 	 */ | 
| Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4411 | 	if ((flags & XFS_BMAPI_IGSTATE) && wr)	/* if writing unwritten space */ | 
 | 4412 | 		wr = 0;				/* no allocations are allowed */ | 
 | 4413 | 	ASSERT(wr || !(flags & XFS_BMAPI_DELAY)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4414 | 	logflags = 0; | 
 | 4415 | 	nallocs = 0; | 
 | 4416 | 	cur = NULL; | 
 | 4417 | 	if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { | 
 | 4418 | 		ASSERT(wr && tp); | 
 | 4419 | 		if ((error = xfs_bmap_local_to_extents(tp, ip, | 
 | 4420 | 				firstblock, total, &logflags, whichfork))) | 
 | 4421 | 			goto error0; | 
 | 4422 | 	} | 
 | 4423 | 	if (wr && *firstblock == NULLFSBLOCK) { | 
 | 4424 | 		if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE) | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 4425 | 			minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4426 | 		else | 
 | 4427 | 			minleft = 1; | 
 | 4428 | 	} else | 
 | 4429 | 		minleft = 0; | 
 | 4430 | 	if (!(ifp->if_flags & XFS_IFEXTENTS) && | 
 | 4431 | 	    (error = xfs_iread_extents(tp, ip, whichfork))) | 
 | 4432 | 		goto error0; | 
 | 4433 | 	ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, | 
 | 4434 | 		&prev); | 
 | 4435 | 	nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); | 
 | 4436 | 	n = 0; | 
 | 4437 | 	end = bno + len; | 
 | 4438 | 	obno = bno; | 
 | 4439 | 	bma.ip = NULL; | 
| Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4440 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4441 | 	while (bno < end && n < *nmap) { | 
 | 4442 | 		/* | 
 | 4443 | 		 * Reading past eof, act as though there's a hole | 
 | 4444 | 		 * up to end. | 
 | 4445 | 		 */ | 
 | 4446 | 		if (eof && !wr) | 
 | 4447 | 			got.br_startoff = end; | 
 | 4448 | 		inhole = eof || got.br_startoff > bno; | 
| Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4449 | 		wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) && | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4450 | 			isnullstartblock(got.br_startblock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4451 | 		/* | 
 | 4452 | 		 * First, deal with the hole before the allocated space | 
 | 4453 | 		 * that we found, if any. | 
 | 4454 | 		 */ | 
 | 4455 | 		if (wr && (inhole || wasdelay)) { | 
 | 4456 | 			/* | 
 | 4457 | 			 * For the wasdelay case, we could also just | 
 | 4458 | 			 * allocate the stuff asked for in this bmap call | 
 | 4459 | 			 * but that wouldn't be as good. | 
 | 4460 | 			 */ | 
| Christoph Hellwig | cd8b0bb | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4461 | 			if (wasdelay) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4462 | 				alen = (xfs_extlen_t)got.br_blockcount; | 
 | 4463 | 				aoff = got.br_startoff; | 
 | 4464 | 				if (lastx != NULLEXTNUM && lastx) { | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4465 | 					ep = xfs_iext_get_ext(ifp, lastx - 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4466 | 					xfs_bmbt_get_all(ep, &prev); | 
 | 4467 | 				} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4468 | 			} else { | 
 | 4469 | 				alen = (xfs_extlen_t) | 
 | 4470 | 					XFS_FILBLKS_MIN(len, MAXEXTLEN); | 
 | 4471 | 				if (!eof) | 
 | 4472 | 					alen = (xfs_extlen_t) | 
 | 4473 | 						XFS_FILBLKS_MIN(alen, | 
 | 4474 | 							got.br_startoff - bno); | 
 | 4475 | 				aoff = bno; | 
 | 4476 | 			} | 
| Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4477 | 			minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1; | 
 | 4478 | 			if (flags & XFS_BMAPI_DELAY) { | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4479 | 				xfs_extlen_t	extsz; | 
| Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4480 |  | 
 | 4481 | 				/* Figure out the extent size, adjust alen */ | 
| David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 4482 | 				extsz = xfs_get_extsz_hint(ip); | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4483 | 				if (extsz) { | 
 | 4484 | 					error = xfs_bmap_extsize_align(mp, | 
 | 4485 | 							&got, &prev, extsz, | 
| Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4486 | 							rt, eof, | 
 | 4487 | 							flags&XFS_BMAPI_DELAY, | 
 | 4488 | 							flags&XFS_BMAPI_CONVERT, | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4489 | 							&aoff, &alen); | 
 | 4490 | 					ASSERT(!error); | 
 | 4491 | 				} | 
 | 4492 |  | 
 | 4493 | 				if (rt) | 
 | 4494 | 					extsz = alen / mp->m_sb.sb_rextsize; | 
| Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4495 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4496 | 				/* | 
 | 4497 | 				 * Make a transaction-less quota reservation for | 
 | 4498 | 				 * delayed allocation blocks. This number gets | 
| Nathan Scott | 9a2a7de | 2006-03-31 13:04:49 +1000 | [diff] [blame] | 4499 | 				 * adjusted later.  We return if we haven't | 
 | 4500 | 				 * allocated blocks already inside this loop. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4501 | 				 */ | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 4502 | 				error = xfs_trans_reserve_quota_nblks( | 
 | 4503 | 						NULL, ip, (long)alen, 0, | 
| Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4504 | 						rt ? XFS_QMOPT_RES_RTBLKS : | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 4505 | 						     XFS_QMOPT_RES_REGBLKS); | 
 | 4506 | 				if (error) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4507 | 					if (n == 0) { | 
 | 4508 | 						*nmap = 0; | 
 | 4509 | 						ASSERT(cur == NULL); | 
| Nathan Scott | 9a2a7de | 2006-03-31 13:04:49 +1000 | [diff] [blame] | 4510 | 						return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4511 | 					} | 
 | 4512 | 					break; | 
 | 4513 | 				} | 
 | 4514 |  | 
 | 4515 | 				/* | 
 | 4516 | 				 * Split changing sb for alen and indlen since | 
 | 4517 | 				 * they could be coming from different places. | 
 | 4518 | 				 */ | 
| Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4519 | 				indlen = (xfs_extlen_t) | 
 | 4520 | 					xfs_bmap_worst_indlen(ip, alen); | 
 | 4521 | 				ASSERT(indlen > 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4522 |  | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4523 | 				if (rt) { | 
| Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4524 | 					error = xfs_mod_incore_sb(mp, | 
 | 4525 | 							XFS_SBS_FREXTENTS, | 
| David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 4526 | 							-((int64_t)extsz), (flags & | 
| Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4527 | 							XFS_BMAPI_RSVBLOCKS)); | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4528 | 				} else { | 
| Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4529 | 					error = xfs_mod_incore_sb(mp, | 
 | 4530 | 							XFS_SBS_FDBLOCKS, | 
| David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 4531 | 							-((int64_t)alen), (flags & | 
| Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4532 | 							XFS_BMAPI_RSVBLOCKS)); | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4533 | 				} | 
| David Chinner | 3bdbfb1 | 2005-09-02 16:40:47 +1000 | [diff] [blame] | 4534 | 				if (!error) { | 
| Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4535 | 					error = xfs_mod_incore_sb(mp, | 
 | 4536 | 							XFS_SBS_FDBLOCKS, | 
| David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 4537 | 							-((int64_t)indlen), (flags & | 
| Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4538 | 							XFS_BMAPI_RSVBLOCKS)); | 
| Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 4539 | 					if (error && rt) | 
 | 4540 | 						xfs_mod_incore_sb(mp, | 
| David Chinner | 3bdbfb1 | 2005-09-02 16:40:47 +1000 | [diff] [blame] | 4541 | 							XFS_SBS_FREXTENTS, | 
| David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 4542 | 							(int64_t)extsz, (flags & | 
| Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4543 | 							XFS_BMAPI_RSVBLOCKS)); | 
| Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 4544 | 					else if (error) | 
 | 4545 | 						xfs_mod_incore_sb(mp, | 
| David Chinner | 3bdbfb1 | 2005-09-02 16:40:47 +1000 | [diff] [blame] | 4546 | 							XFS_SBS_FDBLOCKS, | 
| David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 4547 | 							(int64_t)alen, (flags & | 
| Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4548 | 							XFS_BMAPI_RSVBLOCKS)); | 
| David Chinner | 3bdbfb1 | 2005-09-02 16:40:47 +1000 | [diff] [blame] | 4549 | 				} | 
| Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4550 |  | 
 | 4551 | 				if (error) { | 
| Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 4552 | 					if (XFS_IS_QUOTA_ON(mp)) | 
| Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4553 | 						/* unreserve the blocks now */ | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4554 | 						(void) | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 4555 | 						xfs_trans_unreserve_quota_nblks( | 
 | 4556 | 							NULL, ip, | 
| Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4557 | 							(long)alen, 0, rt ? | 
 | 4558 | 							XFS_QMOPT_RES_RTBLKS : | 
 | 4559 | 							XFS_QMOPT_RES_REGBLKS); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4560 | 					break; | 
 | 4561 | 				} | 
| Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4562 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4563 | 				ip->i_delayed_blks += alen; | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4564 | 				abno = nullstartblock(indlen); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4565 | 			} else { | 
 | 4566 | 				/* | 
 | 4567 | 				 * If first time, allocate and fill in | 
 | 4568 | 				 * once-only bma fields. | 
 | 4569 | 				 */ | 
 | 4570 | 				if (bma.ip == NULL) { | 
 | 4571 | 					bma.tp = tp; | 
 | 4572 | 					bma.ip = ip; | 
 | 4573 | 					bma.prevp = &prev; | 
 | 4574 | 					bma.gotp = &got; | 
 | 4575 | 					bma.total = total; | 
 | 4576 | 					bma.userdata = 0; | 
 | 4577 | 				} | 
 | 4578 | 				/* Indicate if this is the first user data | 
 | 4579 | 				 * in the file, or just any user data. | 
 | 4580 | 				 */ | 
| Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4581 | 				if (!(flags & XFS_BMAPI_METADATA)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4582 | 					bma.userdata = (aoff == 0) ? | 
 | 4583 | 						XFS_ALLOC_INITIAL_USER_DATA : | 
 | 4584 | 						XFS_ALLOC_USERDATA; | 
 | 4585 | 				} | 
 | 4586 | 				/* | 
 | 4587 | 				 * Fill in changeable bma fields. | 
 | 4588 | 				 */ | 
 | 4589 | 				bma.eof = eof; | 
 | 4590 | 				bma.firstblock = *firstblock; | 
 | 4591 | 				bma.alen = alen; | 
 | 4592 | 				bma.off = aoff; | 
| Adrian Bunk | 7288026 | 2006-08-30 13:41:58 +1000 | [diff] [blame] | 4593 | 				bma.conv = !!(flags & XFS_BMAPI_CONVERT); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4594 | 				bma.wasdel = wasdelay; | 
 | 4595 | 				bma.minlen = minlen; | 
 | 4596 | 				bma.low = flist->xbf_low; | 
 | 4597 | 				bma.minleft = minleft; | 
 | 4598 | 				/* | 
 | 4599 | 				 * Only want to do the alignment at the | 
 | 4600 | 				 * eof if it is userdata and allocation length | 
 | 4601 | 				 * is larger than a stripe unit. | 
 | 4602 | 				 */ | 
 | 4603 | 				if (mp->m_dalign && alen >= mp->m_dalign && | 
| Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4604 | 				    (!(flags & XFS_BMAPI_METADATA)) && | 
 | 4605 | 				    (whichfork == XFS_DATA_FORK)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4606 | 					if ((error = xfs_bmap_isaeof(ip, aoff, | 
 | 4607 | 							whichfork, &bma.aeof))) | 
 | 4608 | 						goto error0; | 
 | 4609 | 				} else | 
 | 4610 | 					bma.aeof = 0; | 
 | 4611 | 				/* | 
 | 4612 | 				 * Call allocator. | 
 | 4613 | 				 */ | 
 | 4614 | 				if ((error = xfs_bmap_alloc(&bma))) | 
 | 4615 | 					goto error0; | 
 | 4616 | 				/* | 
 | 4617 | 				 * Copy out result fields. | 
 | 4618 | 				 */ | 
 | 4619 | 				abno = bma.rval; | 
 | 4620 | 				if ((flist->xbf_low = bma.low)) | 
 | 4621 | 					minleft = 0; | 
 | 4622 | 				alen = bma.alen; | 
 | 4623 | 				aoff = bma.off; | 
 | 4624 | 				ASSERT(*firstblock == NULLFSBLOCK || | 
 | 4625 | 				       XFS_FSB_TO_AGNO(mp, *firstblock) == | 
 | 4626 | 				       XFS_FSB_TO_AGNO(mp, bma.firstblock) || | 
 | 4627 | 				       (flist->xbf_low && | 
 | 4628 | 					XFS_FSB_TO_AGNO(mp, *firstblock) < | 
 | 4629 | 					XFS_FSB_TO_AGNO(mp, bma.firstblock))); | 
 | 4630 | 				*firstblock = bma.firstblock; | 
 | 4631 | 				if (cur) | 
 | 4632 | 					cur->bc_private.b.firstblock = | 
 | 4633 | 						*firstblock; | 
 | 4634 | 				if (abno == NULLFSBLOCK) | 
 | 4635 | 					break; | 
 | 4636 | 				if ((ifp->if_flags & XFS_IFBROOT) && !cur) { | 
| Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 4637 | 					cur = xfs_bmbt_init_cursor(mp, tp, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4638 | 						ip, whichfork); | 
 | 4639 | 					cur->bc_private.b.firstblock = | 
 | 4640 | 						*firstblock; | 
 | 4641 | 					cur->bc_private.b.flist = flist; | 
 | 4642 | 				} | 
 | 4643 | 				/* | 
 | 4644 | 				 * Bump the number of extents we've allocated | 
 | 4645 | 				 * in this call. | 
 | 4646 | 				 */ | 
 | 4647 | 				nallocs++; | 
 | 4648 | 			} | 
 | 4649 | 			if (cur) | 
 | 4650 | 				cur->bc_private.b.flags = | 
 | 4651 | 					wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0; | 
 | 4652 | 			got.br_startoff = aoff; | 
 | 4653 | 			got.br_startblock = abno; | 
 | 4654 | 			got.br_blockcount = alen; | 
 | 4655 | 			got.br_state = XFS_EXT_NORM;	/* assume normal */ | 
 | 4656 | 			/* | 
 | 4657 | 			 * Determine state of extent, and the filesystem. | 
 | 4658 | 			 * A wasdelay extent has been initialized, so | 
 | 4659 | 			 * shouldn't be flagged as unwritten. | 
 | 4660 | 			 */ | 
| Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 4661 | 			if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4662 | 				if (!wasdelay && (flags & XFS_BMAPI_PREALLOC)) | 
 | 4663 | 					got.br_state = XFS_EXT_UNWRITTEN; | 
 | 4664 | 			} | 
 | 4665 | 			error = xfs_bmap_add_extent(ip, lastx, &cur, &got, | 
| Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4666 | 				firstblock, flist, &tmp_logflags, | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 4667 | 				whichfork, (flags & XFS_BMAPI_RSVBLOCKS)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4668 | 			logflags |= tmp_logflags; | 
 | 4669 | 			if (error) | 
 | 4670 | 				goto error0; | 
 | 4671 | 			lastx = ifp->if_lastex; | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4672 | 			ep = xfs_iext_get_ext(ifp, lastx); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4673 | 			nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); | 
 | 4674 | 			xfs_bmbt_get_all(ep, &got); | 
 | 4675 | 			ASSERT(got.br_startoff <= aoff); | 
 | 4676 | 			ASSERT(got.br_startoff + got.br_blockcount >= | 
 | 4677 | 				aoff + alen); | 
 | 4678 | #ifdef DEBUG | 
| Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4679 | 			if (flags & XFS_BMAPI_DELAY) { | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4680 | 				ASSERT(isnullstartblock(got.br_startblock)); | 
 | 4681 | 				ASSERT(startblockval(got.br_startblock) > 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4682 | 			} | 
 | 4683 | 			ASSERT(got.br_state == XFS_EXT_NORM || | 
 | 4684 | 			       got.br_state == XFS_EXT_UNWRITTEN); | 
 | 4685 | #endif | 
 | 4686 | 			/* | 
 | 4687 | 			 * Fall down into the found allocated space case. | 
 | 4688 | 			 */ | 
 | 4689 | 		} else if (inhole) { | 
 | 4690 | 			/* | 
 | 4691 | 			 * Reading in a hole. | 
 | 4692 | 			 */ | 
 | 4693 | 			mval->br_startoff = bno; | 
 | 4694 | 			mval->br_startblock = HOLESTARTBLOCK; | 
 | 4695 | 			mval->br_blockcount = | 
 | 4696 | 				XFS_FILBLKS_MIN(len, got.br_startoff - bno); | 
 | 4697 | 			mval->br_state = XFS_EXT_NORM; | 
 | 4698 | 			bno += mval->br_blockcount; | 
 | 4699 | 			len -= mval->br_blockcount; | 
 | 4700 | 			mval++; | 
 | 4701 | 			n++; | 
 | 4702 | 			continue; | 
 | 4703 | 		} | 
 | 4704 | 		/* | 
 | 4705 | 		 * Then deal with the allocated space we found. | 
 | 4706 | 		 */ | 
 | 4707 | 		ASSERT(ep != NULL); | 
| Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4708 | 		if (!(flags & XFS_BMAPI_ENTIRE) && | 
 | 4709 | 		    (got.br_startoff + got.br_blockcount > obno)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4710 | 			if (obno > bno) | 
 | 4711 | 				bno = obno; | 
 | 4712 | 			ASSERT((bno >= obno) || (n == 0)); | 
 | 4713 | 			ASSERT(bno < end); | 
 | 4714 | 			mval->br_startoff = bno; | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4715 | 			if (isnullstartblock(got.br_startblock)) { | 
| Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4716 | 				ASSERT(!wr || (flags & XFS_BMAPI_DELAY)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4717 | 				mval->br_startblock = DELAYSTARTBLOCK; | 
 | 4718 | 			} else | 
 | 4719 | 				mval->br_startblock = | 
 | 4720 | 					got.br_startblock + | 
 | 4721 | 					(bno - got.br_startoff); | 
 | 4722 | 			/* | 
 | 4723 | 			 * Return the minimum of what we got and what we | 
 | 4724 | 			 * asked for for the length.  We can use the len | 
 | 4725 | 			 * variable here because it is modified below | 
 | 4726 | 			 * and we could have been there before coming | 
 | 4727 | 			 * here if the first part of the allocation | 
 | 4728 | 			 * didn't overlap what was asked for. | 
 | 4729 | 			 */ | 
 | 4730 | 			mval->br_blockcount = | 
 | 4731 | 				XFS_FILBLKS_MIN(end - bno, got.br_blockcount - | 
 | 4732 | 					(bno - got.br_startoff)); | 
 | 4733 | 			mval->br_state = got.br_state; | 
 | 4734 | 			ASSERT(mval->br_blockcount <= len); | 
 | 4735 | 		} else { | 
 | 4736 | 			*mval = got; | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4737 | 			if (isnullstartblock(mval->br_startblock)) { | 
| Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4738 | 				ASSERT(!wr || (flags & XFS_BMAPI_DELAY)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4739 | 				mval->br_startblock = DELAYSTARTBLOCK; | 
 | 4740 | 			} | 
 | 4741 | 		} | 
 | 4742 |  | 
 | 4743 | 		/* | 
 | 4744 | 		 * Check if writing previously allocated but | 
 | 4745 | 		 * unwritten extents. | 
 | 4746 | 		 */ | 
 | 4747 | 		if (wr && mval->br_state == XFS_EXT_UNWRITTEN && | 
 | 4748 | 		    ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) { | 
 | 4749 | 			/* | 
 | 4750 | 			 * Modify (by adding) the state flag, if writing. | 
 | 4751 | 			 */ | 
 | 4752 | 			ASSERT(mval->br_blockcount <= len); | 
 | 4753 | 			if ((ifp->if_flags & XFS_IFBROOT) && !cur) { | 
| Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 4754 | 				cur = xfs_bmbt_init_cursor(mp, | 
 | 4755 | 					tp, ip, whichfork); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4756 | 				cur->bc_private.b.firstblock = | 
 | 4757 | 					*firstblock; | 
 | 4758 | 				cur->bc_private.b.flist = flist; | 
 | 4759 | 			} | 
 | 4760 | 			mval->br_state = XFS_EXT_NORM; | 
 | 4761 | 			error = xfs_bmap_add_extent(ip, lastx, &cur, mval, | 
| Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4762 | 				firstblock, flist, &tmp_logflags, | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 4763 | 				whichfork, (flags & XFS_BMAPI_RSVBLOCKS)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4764 | 			logflags |= tmp_logflags; | 
 | 4765 | 			if (error) | 
 | 4766 | 				goto error0; | 
 | 4767 | 			lastx = ifp->if_lastex; | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4768 | 			ep = xfs_iext_get_ext(ifp, lastx); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4769 | 			nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); | 
 | 4770 | 			xfs_bmbt_get_all(ep, &got); | 
 | 4771 | 			/* | 
 | 4772 | 			 * We may have combined previously unwritten | 
 | 4773 | 			 * space with written space, so generate | 
 | 4774 | 			 * another request. | 
 | 4775 | 			 */ | 
 | 4776 | 			if (mval->br_blockcount < len) | 
 | 4777 | 				continue; | 
 | 4778 | 		} | 
 | 4779 |  | 
| Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4780 | 		ASSERT((flags & XFS_BMAPI_ENTIRE) || | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4781 | 		       ((mval->br_startoff + mval->br_blockcount) <= end)); | 
| Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4782 | 		ASSERT((flags & XFS_BMAPI_ENTIRE) || | 
 | 4783 | 		       (mval->br_blockcount <= len) || | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4784 | 		       (mval->br_startoff < obno)); | 
 | 4785 | 		bno = mval->br_startoff + mval->br_blockcount; | 
 | 4786 | 		len = end - bno; | 
 | 4787 | 		if (n > 0 && mval->br_startoff == mval[-1].br_startoff) { | 
 | 4788 | 			ASSERT(mval->br_startblock == mval[-1].br_startblock); | 
 | 4789 | 			ASSERT(mval->br_blockcount > mval[-1].br_blockcount); | 
 | 4790 | 			ASSERT(mval->br_state == mval[-1].br_state); | 
 | 4791 | 			mval[-1].br_blockcount = mval->br_blockcount; | 
 | 4792 | 			mval[-1].br_state = mval->br_state; | 
 | 4793 | 		} else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK && | 
 | 4794 | 			   mval[-1].br_startblock != DELAYSTARTBLOCK && | 
 | 4795 | 			   mval[-1].br_startblock != HOLESTARTBLOCK && | 
 | 4796 | 			   mval->br_startblock == | 
 | 4797 | 			   mval[-1].br_startblock + mval[-1].br_blockcount && | 
| Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4798 | 			   ((flags & XFS_BMAPI_IGSTATE) || | 
 | 4799 | 				mval[-1].br_state == mval->br_state)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4800 | 			ASSERT(mval->br_startoff == | 
 | 4801 | 			       mval[-1].br_startoff + mval[-1].br_blockcount); | 
 | 4802 | 			mval[-1].br_blockcount += mval->br_blockcount; | 
 | 4803 | 		} else if (n > 0 && | 
 | 4804 | 			   mval->br_startblock == DELAYSTARTBLOCK && | 
 | 4805 | 			   mval[-1].br_startblock == DELAYSTARTBLOCK && | 
 | 4806 | 			   mval->br_startoff == | 
 | 4807 | 			   mval[-1].br_startoff + mval[-1].br_blockcount) { | 
 | 4808 | 			mval[-1].br_blockcount += mval->br_blockcount; | 
 | 4809 | 			mval[-1].br_state = mval->br_state; | 
 | 4810 | 		} else if (!((n == 0) && | 
 | 4811 | 			     ((mval->br_startoff + mval->br_blockcount) <= | 
 | 4812 | 			      obno))) { | 
 | 4813 | 			mval++; | 
 | 4814 | 			n++; | 
 | 4815 | 		} | 
 | 4816 | 		/* | 
 | 4817 | 		 * If we're done, stop now.  Stop when we've allocated | 
 | 4818 | 		 * XFS_BMAP_MAX_NMAP extents no matter what.  Otherwise | 
 | 4819 | 		 * the transaction may get too big. | 
 | 4820 | 		 */ | 
 | 4821 | 		if (bno >= end || n >= *nmap || nallocs >= *nmap) | 
 | 4822 | 			break; | 
 | 4823 | 		/* | 
 | 4824 | 		 * Else go on to the next record. | 
 | 4825 | 		 */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4826 | 		ep = xfs_iext_get_ext(ifp, ++lastx); | 
| David Chinner | 4e5ae83 | 2007-06-05 16:24:15 +1000 | [diff] [blame] | 4827 | 		prev = got; | 
 | 4828 | 		if (lastx >= nextents) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4829 | 			eof = 1; | 
| David Chinner | 4e5ae83 | 2007-06-05 16:24:15 +1000 | [diff] [blame] | 4830 | 		else | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4831 | 			xfs_bmbt_get_all(ep, &got); | 
 | 4832 | 	} | 
 | 4833 | 	ifp->if_lastex = lastx; | 
 | 4834 | 	*nmap = n; | 
 | 4835 | 	/* | 
 | 4836 | 	 * Transform from btree to extents, give it cur. | 
 | 4837 | 	 */ | 
 | 4838 | 	if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && | 
 | 4839 | 	    XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) { | 
 | 4840 | 		ASSERT(wr && cur); | 
 | 4841 | 		error = xfs_bmap_btree_to_extents(tp, ip, cur, | 
 | 4842 | 			&tmp_logflags, whichfork); | 
 | 4843 | 		logflags |= tmp_logflags; | 
 | 4844 | 		if (error) | 
 | 4845 | 			goto error0; | 
 | 4846 | 	} | 
 | 4847 | 	ASSERT(ifp->if_ext_max == | 
 | 4848 | 	       XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); | 
 | 4849 | 	ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || | 
 | 4850 | 	       XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max); | 
 | 4851 | 	error = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4852 | error0: | 
 | 4853 | 	/* | 
 | 4854 | 	 * Log everything.  Do this after conversion, there's no point in | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4855 | 	 * logging the extent records if we've converted to btree format. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4856 | 	 */ | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4857 | 	if ((logflags & xfs_ilog_fext(whichfork)) && | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4858 | 	    XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4859 | 		logflags &= ~xfs_ilog_fext(whichfork); | 
 | 4860 | 	else if ((logflags & xfs_ilog_fbroot(whichfork)) && | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4861 | 		 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4862 | 		logflags &= ~xfs_ilog_fbroot(whichfork); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4863 | 	/* | 
 | 4864 | 	 * Log whatever the flags say, even if error.  Otherwise we might miss | 
 | 4865 | 	 * detecting a case where the data is changed, there's an error, | 
 | 4866 | 	 * and it's not logged so we don't shutdown when we should. | 
 | 4867 | 	 */ | 
 | 4868 | 	if (logflags) { | 
 | 4869 | 		ASSERT(tp && wr); | 
 | 4870 | 		xfs_trans_log_inode(tp, ip, logflags); | 
 | 4871 | 	} | 
 | 4872 | 	if (cur) { | 
 | 4873 | 		if (!error) { | 
 | 4874 | 			ASSERT(*firstblock == NULLFSBLOCK || | 
 | 4875 | 			       XFS_FSB_TO_AGNO(mp, *firstblock) == | 
 | 4876 | 			       XFS_FSB_TO_AGNO(mp, | 
 | 4877 | 				       cur->bc_private.b.firstblock) || | 
 | 4878 | 			       (flist->xbf_low && | 
 | 4879 | 				XFS_FSB_TO_AGNO(mp, *firstblock) < | 
 | 4880 | 				XFS_FSB_TO_AGNO(mp, | 
 | 4881 | 					cur->bc_private.b.firstblock))); | 
 | 4882 | 			*firstblock = cur->bc_private.b.firstblock; | 
 | 4883 | 		} | 
 | 4884 | 		xfs_btree_del_cursor(cur, | 
 | 4885 | 			error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); | 
 | 4886 | 	} | 
 | 4887 | 	if (!error) | 
 | 4888 | 		xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, | 
 | 4889 | 			orig_nmap, *nmap); | 
 | 4890 | 	return error; | 
 | 4891 | } | 
 | 4892 |  | 
 | 4893 | /* | 
 | 4894 |  * Map file blocks to filesystem blocks, simple version. | 
 | 4895 |  * One block (extent) only, read-only. | 
 | 4896 |  * For flags, only the XFS_BMAPI_ATTRFORK flag is examined. | 
 | 4897 |  * For the other flag values, the effect is as if XFS_BMAPI_METADATA | 
 | 4898 |  * was set and all the others were clear. | 
 | 4899 |  */ | 
 | 4900 | int						/* error */ | 
 | 4901 | xfs_bmapi_single( | 
 | 4902 | 	xfs_trans_t	*tp,		/* transaction pointer */ | 
 | 4903 | 	xfs_inode_t	*ip,		/* incore inode */ | 
 | 4904 | 	int		whichfork,	/* data or attr fork */ | 
 | 4905 | 	xfs_fsblock_t	*fsb,		/* output: mapped block */ | 
 | 4906 | 	xfs_fileoff_t	bno)		/* starting file offs. mapped */ | 
 | 4907 | { | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4908 | 	int		eof;		/* we've hit the end of extents */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4909 | 	int		error;		/* error return */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4910 | 	xfs_bmbt_irec_t	got;		/* current file extent record */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4911 | 	xfs_ifork_t	*ifp;		/* inode fork pointer */ | 
 | 4912 | 	xfs_extnum_t	lastx;		/* last useful extent number */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4913 | 	xfs_bmbt_irec_t	prev;		/* previous file extent record */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4914 |  | 
 | 4915 | 	ifp = XFS_IFORK_PTR(ip, whichfork); | 
 | 4916 | 	if (unlikely( | 
 | 4917 | 	    XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && | 
 | 4918 | 	    XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) { | 
 | 4919 | 	       XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW, | 
 | 4920 | 				ip->i_mount); | 
 | 4921 | 	       return XFS_ERROR(EFSCORRUPTED); | 
 | 4922 | 	} | 
 | 4923 | 	if (XFS_FORCED_SHUTDOWN(ip->i_mount)) | 
 | 4924 | 		return XFS_ERROR(EIO); | 
 | 4925 | 	XFS_STATS_INC(xs_blk_mapr); | 
 | 4926 | 	if (!(ifp->if_flags & XFS_IFEXTENTS) && | 
 | 4927 | 	    (error = xfs_iread_extents(tp, ip, whichfork))) | 
 | 4928 | 		return error; | 
 | 4929 | 	(void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, | 
 | 4930 | 		&prev); | 
 | 4931 | 	/* | 
 | 4932 | 	 * Reading past eof, act as though there's a hole | 
 | 4933 | 	 * up to end. | 
 | 4934 | 	 */ | 
 | 4935 | 	if (eof || got.br_startoff > bno) { | 
 | 4936 | 		*fsb = NULLFSBLOCK; | 
 | 4937 | 		return 0; | 
 | 4938 | 	} | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4939 | 	ASSERT(!isnullstartblock(got.br_startblock)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4940 | 	ASSERT(bno < got.br_startoff + got.br_blockcount); | 
 | 4941 | 	*fsb = got.br_startblock + (bno - got.br_startoff); | 
 | 4942 | 	ifp->if_lastex = lastx; | 
 | 4943 | 	return 0; | 
 | 4944 | } | 
 | 4945 |  | 
 | 4946 | /* | 
 | 4947 |  * Unmap (remove) blocks from a file. | 
 | 4948 |  * If nexts is nonzero then the number of extents to remove is limited to | 
 | 4949 |  * that value.  If not all extents in the block range can be removed then | 
 | 4950 |  * *done is set. | 
 | 4951 |  */ | 
 | 4952 | int						/* error */ | 
 | 4953 | xfs_bunmapi( | 
 | 4954 | 	xfs_trans_t		*tp,		/* transaction pointer */ | 
 | 4955 | 	struct xfs_inode	*ip,		/* incore inode */ | 
 | 4956 | 	xfs_fileoff_t		bno,		/* starting offset to unmap */ | 
 | 4957 | 	xfs_filblks_t		len,		/* length to unmap in file */ | 
 | 4958 | 	int			flags,		/* misc flags */ | 
 | 4959 | 	xfs_extnum_t		nexts,		/* number of extents max */ | 
 | 4960 | 	xfs_fsblock_t		*firstblock,	/* first allocated block | 
 | 4961 | 						   controls a.g. for allocs */ | 
 | 4962 | 	xfs_bmap_free_t		*flist,		/* i/o: list extents to free */ | 
 | 4963 | 	int			*done)		/* set if not done yet */ | 
 | 4964 | { | 
 | 4965 | 	xfs_btree_cur_t		*cur;		/* bmap btree cursor */ | 
 | 4966 | 	xfs_bmbt_irec_t		del;		/* extent being deleted */ | 
 | 4967 | 	int			eof;		/* is deleting at eof */ | 
| Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4968 | 	xfs_bmbt_rec_host_t	*ep;		/* extent record pointer */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4969 | 	int			error;		/* error return value */ | 
 | 4970 | 	xfs_extnum_t		extno;		/* extent number in list */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4971 | 	xfs_bmbt_irec_t		got;		/* current extent record */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4972 | 	xfs_ifork_t		*ifp;		/* inode fork pointer */ | 
 | 4973 | 	int			isrt;		/* freeing in rt area */ | 
 | 4974 | 	xfs_extnum_t		lastx;		/* last extent index used */ | 
 | 4975 | 	int			logflags;	/* transaction logging flags */ | 
 | 4976 | 	xfs_extlen_t		mod;		/* rt extent offset */ | 
 | 4977 | 	xfs_mount_t		*mp;		/* mount structure */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4978 | 	xfs_extnum_t		nextents;	/* number of file extents */ | 
 | 4979 | 	xfs_bmbt_irec_t		prev;		/* previous extent record */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4980 | 	xfs_fileoff_t		start;		/* first file offset deleted */ | 
 | 4981 | 	int			tmp_logflags;	/* partial logging flags */ | 
 | 4982 | 	int			wasdel;		/* was a delayed alloc extent */ | 
 | 4983 | 	int			whichfork;	/* data or attribute fork */ | 
 | 4984 | 	int			rsvd;		/* OK to allocate reserved blocks */ | 
 | 4985 | 	xfs_fsblock_t		sum; | 
 | 4986 |  | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4987 | 	trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_); | 
 | 4988 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4989 | 	whichfork = (flags & XFS_BMAPI_ATTRFORK) ? | 
 | 4990 | 		XFS_ATTR_FORK : XFS_DATA_FORK; | 
 | 4991 | 	ifp = XFS_IFORK_PTR(ip, whichfork); | 
 | 4992 | 	if (unlikely( | 
 | 4993 | 	    XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && | 
 | 4994 | 	    XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { | 
 | 4995 | 		XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, | 
 | 4996 | 				 ip->i_mount); | 
 | 4997 | 		return XFS_ERROR(EFSCORRUPTED); | 
 | 4998 | 	} | 
 | 4999 | 	mp = ip->i_mount; | 
 | 5000 | 	if (XFS_FORCED_SHUTDOWN(mp)) | 
 | 5001 | 		return XFS_ERROR(EIO); | 
 | 5002 | 	rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0; | 
 | 5003 | 	ASSERT(len > 0); | 
 | 5004 | 	ASSERT(nexts >= 0); | 
 | 5005 | 	ASSERT(ifp->if_ext_max == | 
 | 5006 | 	       XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); | 
 | 5007 | 	if (!(ifp->if_flags & XFS_IFEXTENTS) && | 
 | 5008 | 	    (error = xfs_iread_extents(tp, ip, whichfork))) | 
 | 5009 | 		return error; | 
 | 5010 | 	nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); | 
 | 5011 | 	if (nextents == 0) { | 
 | 5012 | 		*done = 1; | 
 | 5013 | 		return 0; | 
 | 5014 | 	} | 
 | 5015 | 	XFS_STATS_INC(xs_blk_unmap); | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5016 | 	isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5017 | 	start = bno; | 
 | 5018 | 	bno = start + len - 1; | 
 | 5019 | 	ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, | 
 | 5020 | 		&prev); | 
| Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5021 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5022 | 	/* | 
 | 5023 | 	 * Check to see if the given block number is past the end of the | 
 | 5024 | 	 * file, back up to the last block if so... | 
 | 5025 | 	 */ | 
 | 5026 | 	if (eof) { | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5027 | 		ep = xfs_iext_get_ext(ifp, --lastx); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5028 | 		xfs_bmbt_get_all(ep, &got); | 
 | 5029 | 		bno = got.br_startoff + got.br_blockcount - 1; | 
 | 5030 | 	} | 
 | 5031 | 	logflags = 0; | 
 | 5032 | 	if (ifp->if_flags & XFS_IFBROOT) { | 
 | 5033 | 		ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); | 
| Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5034 | 		cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5035 | 		cur->bc_private.b.firstblock = *firstblock; | 
 | 5036 | 		cur->bc_private.b.flist = flist; | 
 | 5037 | 		cur->bc_private.b.flags = 0; | 
 | 5038 | 	} else | 
 | 5039 | 		cur = NULL; | 
 | 5040 | 	extno = 0; | 
 | 5041 | 	while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 && | 
 | 5042 | 	       (nexts == 0 || extno < nexts)) { | 
 | 5043 | 		/* | 
 | 5044 | 		 * Is the found extent after a hole in which bno lives? | 
 | 5045 | 		 * Just back up to the previous extent, if so. | 
 | 5046 | 		 */ | 
 | 5047 | 		if (got.br_startoff > bno) { | 
 | 5048 | 			if (--lastx < 0) | 
 | 5049 | 				break; | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5050 | 			ep = xfs_iext_get_ext(ifp, lastx); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5051 | 			xfs_bmbt_get_all(ep, &got); | 
 | 5052 | 		} | 
 | 5053 | 		/* | 
 | 5054 | 		 * Is the last block of this extent before the range | 
 | 5055 | 		 * we're supposed to delete?  If so, we're done. | 
 | 5056 | 		 */ | 
 | 5057 | 		bno = XFS_FILEOFF_MIN(bno, | 
 | 5058 | 			got.br_startoff + got.br_blockcount - 1); | 
 | 5059 | 		if (bno < start) | 
 | 5060 | 			break; | 
 | 5061 | 		/* | 
 | 5062 | 		 * Then deal with the (possibly delayed) allocated space | 
 | 5063 | 		 * we found. | 
 | 5064 | 		 */ | 
 | 5065 | 		ASSERT(ep != NULL); | 
 | 5066 | 		del = got; | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5067 | 		wasdel = isnullstartblock(del.br_startblock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5068 | 		if (got.br_startoff < start) { | 
 | 5069 | 			del.br_startoff = start; | 
 | 5070 | 			del.br_blockcount -= start - got.br_startoff; | 
 | 5071 | 			if (!wasdel) | 
 | 5072 | 				del.br_startblock += start - got.br_startoff; | 
 | 5073 | 		} | 
 | 5074 | 		if (del.br_startoff + del.br_blockcount > bno + 1) | 
 | 5075 | 			del.br_blockcount = bno + 1 - del.br_startoff; | 
 | 5076 | 		sum = del.br_startblock + del.br_blockcount; | 
 | 5077 | 		if (isrt && | 
 | 5078 | 		    (mod = do_mod(sum, mp->m_sb.sb_rextsize))) { | 
 | 5079 | 			/* | 
 | 5080 | 			 * Realtime extent not lined up at the end. | 
 | 5081 | 			 * The extent could have been split into written | 
 | 5082 | 			 * and unwritten pieces, or we could just be | 
 | 5083 | 			 * unmapping part of it.  But we can't really | 
 | 5084 | 			 * get rid of part of a realtime extent. | 
 | 5085 | 			 */ | 
 | 5086 | 			if (del.br_state == XFS_EXT_UNWRITTEN || | 
| Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5087 | 			    !xfs_sb_version_hasextflgbit(&mp->m_sb)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5088 | 				/* | 
 | 5089 | 				 * This piece is unwritten, or we're not | 
 | 5090 | 				 * using unwritten extents.  Skip over it. | 
 | 5091 | 				 */ | 
 | 5092 | 				ASSERT(bno >= mod); | 
 | 5093 | 				bno -= mod > del.br_blockcount ? | 
 | 5094 | 					del.br_blockcount : mod; | 
 | 5095 | 				if (bno < got.br_startoff) { | 
 | 5096 | 					if (--lastx >= 0) | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5097 | 						xfs_bmbt_get_all(xfs_iext_get_ext( | 
 | 5098 | 							ifp, lastx), &got); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5099 | 				} | 
 | 5100 | 				continue; | 
 | 5101 | 			} | 
 | 5102 | 			/* | 
 | 5103 | 			 * It's written, turn it unwritten. | 
 | 5104 | 			 * This is better than zeroing it. | 
 | 5105 | 			 */ | 
 | 5106 | 			ASSERT(del.br_state == XFS_EXT_NORM); | 
 | 5107 | 			ASSERT(xfs_trans_get_block_res(tp) > 0); | 
 | 5108 | 			/* | 
 | 5109 | 			 * If this spans a realtime extent boundary, | 
 | 5110 | 			 * chop it back to the start of the one we end at. | 
 | 5111 | 			 */ | 
 | 5112 | 			if (del.br_blockcount > mod) { | 
 | 5113 | 				del.br_startoff += del.br_blockcount - mod; | 
 | 5114 | 				del.br_startblock += del.br_blockcount - mod; | 
 | 5115 | 				del.br_blockcount = mod; | 
 | 5116 | 			} | 
 | 5117 | 			del.br_state = XFS_EXT_UNWRITTEN; | 
 | 5118 | 			error = xfs_bmap_add_extent(ip, lastx, &cur, &del, | 
| Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5119 | 				firstblock, flist, &logflags, | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5120 | 				XFS_DATA_FORK, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5121 | 			if (error) | 
 | 5122 | 				goto error0; | 
 | 5123 | 			goto nodelete; | 
 | 5124 | 		} | 
 | 5125 | 		if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) { | 
 | 5126 | 			/* | 
 | 5127 | 			 * Realtime extent is lined up at the end but not | 
 | 5128 | 			 * at the front.  We'll get rid of full extents if | 
 | 5129 | 			 * we can. | 
 | 5130 | 			 */ | 
 | 5131 | 			mod = mp->m_sb.sb_rextsize - mod; | 
 | 5132 | 			if (del.br_blockcount > mod) { | 
 | 5133 | 				del.br_blockcount -= mod; | 
 | 5134 | 				del.br_startoff += mod; | 
 | 5135 | 				del.br_startblock += mod; | 
 | 5136 | 			} else if ((del.br_startoff == start && | 
 | 5137 | 				    (del.br_state == XFS_EXT_UNWRITTEN || | 
 | 5138 | 				     xfs_trans_get_block_res(tp) == 0)) || | 
| Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5139 | 				   !xfs_sb_version_hasextflgbit(&mp->m_sb)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5140 | 				/* | 
 | 5141 | 				 * Can't make it unwritten.  There isn't | 
 | 5142 | 				 * a full extent here so just skip it. | 
 | 5143 | 				 */ | 
 | 5144 | 				ASSERT(bno >= del.br_blockcount); | 
 | 5145 | 				bno -= del.br_blockcount; | 
 | 5146 | 				if (bno < got.br_startoff) { | 
 | 5147 | 					if (--lastx >= 0) | 
 | 5148 | 						xfs_bmbt_get_all(--ep, &got); | 
 | 5149 | 				} | 
 | 5150 | 				continue; | 
 | 5151 | 			} else if (del.br_state == XFS_EXT_UNWRITTEN) { | 
 | 5152 | 				/* | 
 | 5153 | 				 * This one is already unwritten. | 
 | 5154 | 				 * It must have a written left neighbor. | 
 | 5155 | 				 * Unwrite the killed part of that one and | 
 | 5156 | 				 * try again. | 
 | 5157 | 				 */ | 
 | 5158 | 				ASSERT(lastx > 0); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5159 | 				xfs_bmbt_get_all(xfs_iext_get_ext(ifp, | 
 | 5160 | 						lastx - 1), &prev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5161 | 				ASSERT(prev.br_state == XFS_EXT_NORM); | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5162 | 				ASSERT(!isnullstartblock(prev.br_startblock)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5163 | 				ASSERT(del.br_startblock == | 
 | 5164 | 				       prev.br_startblock + prev.br_blockcount); | 
 | 5165 | 				if (prev.br_startoff < start) { | 
 | 5166 | 					mod = start - prev.br_startoff; | 
 | 5167 | 					prev.br_blockcount -= mod; | 
 | 5168 | 					prev.br_startblock += mod; | 
 | 5169 | 					prev.br_startoff = start; | 
 | 5170 | 				} | 
 | 5171 | 				prev.br_state = XFS_EXT_UNWRITTEN; | 
 | 5172 | 				error = xfs_bmap_add_extent(ip, lastx - 1, &cur, | 
 | 5173 | 					&prev, firstblock, flist, &logflags, | 
| Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5174 | 					XFS_DATA_FORK, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5175 | 				if (error) | 
 | 5176 | 					goto error0; | 
 | 5177 | 				goto nodelete; | 
 | 5178 | 			} else { | 
 | 5179 | 				ASSERT(del.br_state == XFS_EXT_NORM); | 
 | 5180 | 				del.br_state = XFS_EXT_UNWRITTEN; | 
 | 5181 | 				error = xfs_bmap_add_extent(ip, lastx, &cur, | 
 | 5182 | 					&del, firstblock, flist, &logflags, | 
| Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5183 | 					XFS_DATA_FORK, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5184 | 				if (error) | 
 | 5185 | 					goto error0; | 
 | 5186 | 				goto nodelete; | 
 | 5187 | 			} | 
 | 5188 | 		} | 
 | 5189 | 		if (wasdel) { | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5190 | 			ASSERT(startblockval(del.br_startblock) > 0); | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5191 | 			/* Update realtime/data freespace, unreserve quota */ | 
| Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5192 | 			if (isrt) { | 
 | 5193 | 				xfs_filblks_t rtexts; | 
 | 5194 |  | 
 | 5195 | 				rtexts = XFS_FSB_TO_B(mp, del.br_blockcount); | 
 | 5196 | 				do_div(rtexts, mp->m_sb.sb_rextsize); | 
 | 5197 | 				xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, | 
| David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 5198 | 						(int64_t)rtexts, rsvd); | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 5199 | 				(void)xfs_trans_reserve_quota_nblks(NULL, | 
 | 5200 | 					ip, -((long)del.br_blockcount), 0, | 
| Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5201 | 					XFS_QMOPT_RES_RTBLKS); | 
 | 5202 | 			} else { | 
 | 5203 | 				xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, | 
| David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 5204 | 						(int64_t)del.br_blockcount, rsvd); | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 5205 | 				(void)xfs_trans_reserve_quota_nblks(NULL, | 
 | 5206 | 					ip, -((long)del.br_blockcount), 0, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5207 | 					XFS_QMOPT_RES_REGBLKS); | 
| Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5208 | 			} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5209 | 			ip->i_delayed_blks -= del.br_blockcount; | 
 | 5210 | 			if (cur) | 
 | 5211 | 				cur->bc_private.b.flags |= | 
 | 5212 | 					XFS_BTCUR_BPRV_WASDEL; | 
 | 5213 | 		} else if (cur) | 
 | 5214 | 			cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL; | 
 | 5215 | 		/* | 
 | 5216 | 		 * If it's the case where the directory code is running | 
 | 5217 | 		 * with no block reservation, and the deleted block is in | 
 | 5218 | 		 * the middle of its extent, and the resulting insert | 
 | 5219 | 		 * of an extent would cause transformation to btree format, | 
 | 5220 | 		 * then reject it.  The calling code will then swap | 
 | 5221 | 		 * blocks around instead. | 
 | 5222 | 		 * We have to do this now, rather than waiting for the | 
 | 5223 | 		 * conversion to btree format, since the transaction | 
 | 5224 | 		 * will be dirty. | 
 | 5225 | 		 */ | 
 | 5226 | 		if (!wasdel && xfs_trans_get_block_res(tp) == 0 && | 
 | 5227 | 		    XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && | 
 | 5228 | 		    XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max && | 
 | 5229 | 		    del.br_startoff > got.br_startoff && | 
 | 5230 | 		    del.br_startoff + del.br_blockcount < | 
 | 5231 | 		    got.br_startoff + got.br_blockcount) { | 
 | 5232 | 			error = XFS_ERROR(ENOSPC); | 
 | 5233 | 			goto error0; | 
 | 5234 | 		} | 
 | 5235 | 		error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del, | 
| Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5236 | 				&tmp_logflags, whichfork, rsvd); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5237 | 		logflags |= tmp_logflags; | 
 | 5238 | 		if (error) | 
 | 5239 | 			goto error0; | 
 | 5240 | 		bno = del.br_startoff - 1; | 
 | 5241 | nodelete: | 
 | 5242 | 		lastx = ifp->if_lastex; | 
 | 5243 | 		/* | 
 | 5244 | 		 * If not done go on to the next (previous) record. | 
 | 5245 | 		 * Reset ep in case the extents array was re-alloced. | 
 | 5246 | 		 */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5247 | 		ep = xfs_iext_get_ext(ifp, lastx); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5248 | 		if (bno != (xfs_fileoff_t)-1 && bno >= start) { | 
 | 5249 | 			if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) || | 
 | 5250 | 			    xfs_bmbt_get_startoff(ep) > bno) { | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5251 | 				if (--lastx >= 0) | 
 | 5252 | 					ep = xfs_iext_get_ext(ifp, lastx); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5253 | 			} | 
 | 5254 | 			if (lastx >= 0) | 
 | 5255 | 				xfs_bmbt_get_all(ep, &got); | 
 | 5256 | 			extno++; | 
 | 5257 | 		} | 
 | 5258 | 	} | 
 | 5259 | 	ifp->if_lastex = lastx; | 
 | 5260 | 	*done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0; | 
 | 5261 | 	ASSERT(ifp->if_ext_max == | 
 | 5262 | 	       XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); | 
 | 5263 | 	/* | 
 | 5264 | 	 * Convert to a btree if necessary. | 
 | 5265 | 	 */ | 
 | 5266 | 	if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && | 
 | 5267 | 	    XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) { | 
 | 5268 | 		ASSERT(cur == NULL); | 
 | 5269 | 		error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, | 
 | 5270 | 			&cur, 0, &tmp_logflags, whichfork); | 
 | 5271 | 		logflags |= tmp_logflags; | 
 | 5272 | 		if (error) | 
 | 5273 | 			goto error0; | 
 | 5274 | 	} | 
 | 5275 | 	/* | 
 | 5276 | 	 * transform from btree to extents, give it cur | 
 | 5277 | 	 */ | 
 | 5278 | 	else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && | 
 | 5279 | 		 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) { | 
 | 5280 | 		ASSERT(cur != NULL); | 
 | 5281 | 		error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags, | 
 | 5282 | 			whichfork); | 
 | 5283 | 		logflags |= tmp_logflags; | 
 | 5284 | 		if (error) | 
 | 5285 | 			goto error0; | 
 | 5286 | 	} | 
 | 5287 | 	/* | 
 | 5288 | 	 * transform from extents to local? | 
 | 5289 | 	 */ | 
 | 5290 | 	ASSERT(ifp->if_ext_max == | 
 | 5291 | 	       XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); | 
 | 5292 | 	error = 0; | 
 | 5293 | error0: | 
 | 5294 | 	/* | 
 | 5295 | 	 * Log everything.  Do this after conversion, there's no point in | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5296 | 	 * logging the extent records if we've converted to btree format. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5297 | 	 */ | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5298 | 	if ((logflags & xfs_ilog_fext(whichfork)) && | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5299 | 	    XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5300 | 		logflags &= ~xfs_ilog_fext(whichfork); | 
 | 5301 | 	else if ((logflags & xfs_ilog_fbroot(whichfork)) && | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5302 | 		 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5303 | 		logflags &= ~xfs_ilog_fbroot(whichfork); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5304 | 	/* | 
 | 5305 | 	 * Log inode even in the error case, if the transaction | 
 | 5306 | 	 * is dirty we'll need to shut down the filesystem. | 
 | 5307 | 	 */ | 
 | 5308 | 	if (logflags) | 
 | 5309 | 		xfs_trans_log_inode(tp, ip, logflags); | 
 | 5310 | 	if (cur) { | 
 | 5311 | 		if (!error) { | 
 | 5312 | 			*firstblock = cur->bc_private.b.firstblock; | 
 | 5313 | 			cur->bc_private.b.allocated = 0; | 
 | 5314 | 		} | 
 | 5315 | 		xfs_btree_del_cursor(cur, | 
 | 5316 | 			error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); | 
 | 5317 | 	} | 
 | 5318 | 	return error; | 
 | 5319 | } | 
 | 5320 |  | 
 | 5321 | /* | 
| Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5322 |  * returns 1 for success, 0 if we failed to map the extent. | 
 | 5323 |  */ | 
 | 5324 | STATIC int | 
 | 5325 | xfs_getbmapx_fix_eof_hole( | 
 | 5326 | 	xfs_inode_t		*ip,		/* xfs incore inode pointer */ | 
| Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5327 | 	struct getbmapx		*out,		/* output structure */ | 
| Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5328 | 	int			prealloced,	/* this is a file with | 
| Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5329 | 						 * preallocated data space */ | 
| Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5330 | 	__int64_t		end,		/* last block requested */ | 
 | 5331 | 	xfs_fsblock_t		startblock) | 
 | 5332 | { | 
 | 5333 | 	__int64_t		fixlen; | 
 | 5334 | 	xfs_mount_t		*mp;		/* file system mount point */ | 
| Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5335 | 	xfs_ifork_t		*ifp;		/* inode fork pointer */ | 
 | 5336 | 	xfs_extnum_t		lastx;		/* last extent pointer */ | 
 | 5337 | 	xfs_fileoff_t		fileblock; | 
| Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5338 |  | 
 | 5339 | 	if (startblock == HOLESTARTBLOCK) { | 
 | 5340 | 		mp = ip->i_mount; | 
 | 5341 | 		out->bmv_block = -1; | 
 | 5342 | 		fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size)); | 
 | 5343 | 		fixlen -= out->bmv_offset; | 
 | 5344 | 		if (prealloced && out->bmv_offset + out->bmv_length == end) { | 
 | 5345 | 			/* Came to hole at EOF. Trim it. */ | 
 | 5346 | 			if (fixlen <= 0) | 
 | 5347 | 				return 0; | 
 | 5348 | 			out->bmv_length = fixlen; | 
 | 5349 | 		} | 
 | 5350 | 	} else { | 
| Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5351 | 		if (startblock == DELAYSTARTBLOCK) | 
 | 5352 | 			out->bmv_block = -2; | 
 | 5353 | 		else | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5354 | 			out->bmv_block = xfs_fsb_to_db(ip, startblock); | 
| Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5355 | 		fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset); | 
 | 5356 | 		ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); | 
 | 5357 | 		if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) && | 
 | 5358 | 		   (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1)) | 
 | 5359 | 			out->bmv_oflags |= BMV_OF_LAST; | 
| Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5360 | 	} | 
 | 5361 |  | 
 | 5362 | 	return 1; | 
 | 5363 | } | 
 | 5364 |  | 
 | 5365 | /* | 
| Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5366 |  * Get inode's extents as described in bmv, and format for output. | 
 | 5367 |  * Calls formatter to fill the user's buffer until all extents | 
 | 5368 |  * are mapped, until the passed-in bmv->bmv_count slots have | 
 | 5369 |  * been filled, or until the formatter short-circuits the loop, | 
 | 5370 |  * if it is tracking filled-in extents on its own. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5371 |  */ | 
 | 5372 | int						/* error code */ | 
 | 5373 | xfs_getbmap( | 
| Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 5374 | 	xfs_inode_t		*ip, | 
| Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5375 | 	struct getbmapx		*bmv,		/* user bmap structure */ | 
 | 5376 | 	xfs_bmap_format_t	formatter,	/* format to user */ | 
 | 5377 | 	void			*arg)		/* formatter arg */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5378 | { | 
 | 5379 | 	__int64_t		bmvend;		/* last block requested */ | 
| Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5380 | 	int			error = 0;	/* return value */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5381 | 	__int64_t		fixlen;		/* length for -1 case */ | 
 | 5382 | 	int			i;		/* extent number */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5383 | 	int			lock;		/* lock state */ | 
 | 5384 | 	xfs_bmbt_irec_t		*map;		/* buffer for user's data */ | 
 | 5385 | 	xfs_mount_t		*mp;		/* file system mount point */ | 
 | 5386 | 	int			nex;		/* # of user extents can do */ | 
 | 5387 | 	int			nexleft;	/* # of user extents left */ | 
 | 5388 | 	int			subnex;		/* # of bmapi's can do */ | 
 | 5389 | 	int			nmap;		/* number of map entries */ | 
| Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5390 | 	struct getbmapx		*out;		/* output structure */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5391 | 	int			whichfork;	/* data or attr fork */ | 
 | 5392 | 	int			prealloced;	/* this is a file with | 
 | 5393 | 						 * preallocated data space */ | 
| Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5394 | 	int			iflags;		/* interface flags */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5395 | 	int			bmapi_flags;	/* flags for xfs_bmapi */ | 
| Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5396 | 	int			cur_ext = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5397 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5398 | 	mp = ip->i_mount; | 
| Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5399 | 	iflags = bmv->bmv_iflags; | 
| Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5400 | 	whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5401 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5402 | 	if (whichfork == XFS_ATTR_FORK) { | 
 | 5403 | 		if (XFS_IFORK_Q(ip)) { | 
 | 5404 | 			if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS && | 
 | 5405 | 			    ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE && | 
 | 5406 | 			    ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL) | 
 | 5407 | 				return XFS_ERROR(EINVAL); | 
 | 5408 | 		} else if (unlikely( | 
 | 5409 | 			   ip->i_d.di_aformat != 0 && | 
 | 5410 | 			   ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) { | 
 | 5411 | 			XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW, | 
 | 5412 | 					 ip->i_mount); | 
 | 5413 | 			return XFS_ERROR(EFSCORRUPTED); | 
 | 5414 | 		} | 
| Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5415 |  | 
 | 5416 | 		prealloced = 0; | 
 | 5417 | 		fixlen = 1LL << 32; | 
 | 5418 | 	} else { | 
| Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5419 | 		if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS && | 
 | 5420 | 		    ip->i_d.di_format != XFS_DINODE_FMT_BTREE && | 
 | 5421 | 		    ip->i_d.di_format != XFS_DINODE_FMT_LOCAL) | 
 | 5422 | 			return XFS_ERROR(EINVAL); | 
 | 5423 |  | 
| David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 5424 | 		if (xfs_get_extsz_hint(ip) || | 
| Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5425 | 		    ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5426 | 			prealloced = 1; | 
 | 5427 | 			fixlen = XFS_MAXIOFFSET(mp); | 
 | 5428 | 		} else { | 
 | 5429 | 			prealloced = 0; | 
| Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 5430 | 			fixlen = ip->i_size; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5431 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5432 | 	} | 
 | 5433 |  | 
 | 5434 | 	if (bmv->bmv_length == -1) { | 
 | 5435 | 		fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen)); | 
| Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5436 | 		bmv->bmv_length = | 
 | 5437 | 			max_t(__int64_t, fixlen - bmv->bmv_offset, 0); | 
 | 5438 | 	} else if (bmv->bmv_length == 0) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5439 | 		bmv->bmv_entries = 0; | 
 | 5440 | 		return 0; | 
| Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5441 | 	} else if (bmv->bmv_length < 0) { | 
 | 5442 | 		return XFS_ERROR(EINVAL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5443 | 	} | 
| Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5444 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5445 | 	nex = bmv->bmv_count - 1; | 
 | 5446 | 	if (nex <= 0) | 
 | 5447 | 		return XFS_ERROR(EINVAL); | 
 | 5448 | 	bmvend = bmv->bmv_offset + bmv->bmv_length; | 
 | 5449 |  | 
| Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5450 |  | 
 | 5451 | 	if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx)) | 
 | 5452 | 		return XFS_ERROR(ENOMEM); | 
 | 5453 | 	out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL); | 
 | 5454 | 	if (!out) | 
 | 5455 | 		return XFS_ERROR(ENOMEM); | 
 | 5456 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5457 | 	xfs_ilock(ip, XFS_IOLOCK_SHARED); | 
| Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5458 | 	if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) { | 
 | 5459 | 		if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) { | 
 | 5460 | 			error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF); | 
 | 5461 | 			if (error) | 
 | 5462 | 				goto out_unlock_iolock; | 
| Niv Sardi | e12070a | 2008-03-06 13:43:03 +1100 | [diff] [blame] | 5463 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5464 |  | 
| Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5465 | 		ASSERT(ip->i_delayed_blks == 0); | 
 | 5466 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5467 |  | 
 | 5468 | 	lock = xfs_ilock_map_shared(ip); | 
 | 5469 |  | 
 | 5470 | 	/* | 
 | 5471 | 	 * Don't let nex be bigger than the number of extents | 
 | 5472 | 	 * we can have assuming alternating holes and real extents. | 
 | 5473 | 	 */ | 
 | 5474 | 	if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1) | 
 | 5475 | 		nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1; | 
 | 5476 |  | 
| Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5477 | 	bmapi_flags = xfs_bmapi_aflag(whichfork); | 
 | 5478 | 	if (!(iflags & BMV_IF_PREALLOC)) | 
 | 5479 | 		bmapi_flags |= XFS_BMAPI_IGSTATE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5480 |  | 
 | 5481 | 	/* | 
 | 5482 | 	 * Allocate enough space to handle "subnex" maps at a time. | 
 | 5483 | 	 */ | 
| Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5484 | 	error = ENOMEM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5485 | 	subnex = 16; | 
| Christoph Hellwig | ca35dcd | 2009-07-18 18:14:54 -0400 | [diff] [blame] | 5486 | 	map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS); | 
| Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5487 | 	if (!map) | 
 | 5488 | 		goto out_unlock_ilock; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5489 |  | 
 | 5490 | 	bmv->bmv_entries = 0; | 
 | 5491 |  | 
| Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5492 | 	if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 && | 
 | 5493 | 	    (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) { | 
 | 5494 | 		error = 0; | 
 | 5495 | 		goto out_free_map; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5496 | 	} | 
 | 5497 |  | 
 | 5498 | 	nexleft = nex; | 
 | 5499 |  | 
 | 5500 | 	do { | 
 | 5501 | 		nmap = (nexleft > subnex) ? subnex : nexleft; | 
 | 5502 | 		error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset), | 
 | 5503 | 				  XFS_BB_TO_FSB(mp, bmv->bmv_length), | 
| Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5504 | 				  bmapi_flags, NULL, 0, map, &nmap, | 
| Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5505 | 				  NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5506 | 		if (error) | 
| Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5507 | 			goto out_free_map; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5508 | 		ASSERT(nmap <= subnex); | 
 | 5509 |  | 
 | 5510 | 		for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) { | 
| Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5511 | 			out[cur_ext].bmv_oflags = 0; | 
| Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5512 | 			if (map[i].br_state == XFS_EXT_UNWRITTEN) | 
| Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5513 | 				out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC; | 
| Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5514 | 			else if (map[i].br_startblock == DELAYSTARTBLOCK) | 
| Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5515 | 				out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC; | 
 | 5516 | 			out[cur_ext].bmv_offset = | 
 | 5517 | 				XFS_FSB_TO_BB(mp, map[i].br_startoff); | 
 | 5518 | 			out[cur_ext].bmv_length = | 
 | 5519 | 				XFS_FSB_TO_BB(mp, map[i].br_blockcount); | 
 | 5520 | 			out[cur_ext].bmv_unused1 = 0; | 
 | 5521 | 			out[cur_ext].bmv_unused2 = 0; | 
| Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5522 | 			ASSERT(((iflags & BMV_IF_DELALLOC) != 0) || | 
 | 5523 | 			      (map[i].br_startblock != DELAYSTARTBLOCK)); | 
| Yingping Lu | 9af0a70 | 2005-11-02 15:09:54 +1100 | [diff] [blame] | 5524 |                         if (map[i].br_startblock == HOLESTARTBLOCK && | 
| Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5525 | 			    whichfork == XFS_ATTR_FORK) { | 
 | 5526 | 				/* came to the end of attribute fork */ | 
| Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5527 | 				out[cur_ext].bmv_oflags |= BMV_OF_LAST; | 
| Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5528 | 				goto out_free_map; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5529 | 			} | 
| Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5530 |  | 
| Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5531 | 			if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext], | 
 | 5532 | 					prealloced, bmvend, | 
 | 5533 | 					map[i].br_startblock)) | 
| Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5534 | 				goto out_free_map; | 
 | 5535 |  | 
| Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5536 | 			bmv->bmv_offset = | 
| Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5537 | 				out[cur_ext].bmv_offset + | 
 | 5538 | 				out[cur_ext].bmv_length; | 
| Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5539 | 			bmv->bmv_length = | 
 | 5540 | 				max_t(__int64_t, 0, bmvend - bmv->bmv_offset); | 
| Tao Ma | 9af2546 | 2010-08-30 02:44:03 +0000 | [diff] [blame] | 5541 |  | 
 | 5542 | 			/* | 
 | 5543 | 			 * In case we don't want to return the hole, | 
 | 5544 | 			 * don't increase cur_ext so that we can reuse | 
 | 5545 | 			 * it in the next loop. | 
 | 5546 | 			 */ | 
 | 5547 | 			if ((iflags & BMV_IF_NO_HOLES) && | 
 | 5548 | 			    map[i].br_startblock == HOLESTARTBLOCK) { | 
 | 5549 | 				memset(&out[cur_ext], 0, sizeof(out[cur_ext])); | 
 | 5550 | 				continue; | 
 | 5551 | 			} | 
 | 5552 |  | 
 | 5553 | 			nexleft--; | 
| Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5554 | 			bmv->bmv_entries++; | 
| Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5555 | 			cur_ext++; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5556 | 		} | 
 | 5557 | 	} while (nmap && nexleft && bmv->bmv_length); | 
 | 5558 |  | 
| Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5559 |  out_free_map: | 
| Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 5560 | 	kmem_free(map); | 
| Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5561 |  out_unlock_ilock: | 
 | 5562 | 	xfs_iunlock_map_shared(ip, lock); | 
 | 5563 |  out_unlock_iolock: | 
 | 5564 | 	xfs_iunlock(ip, XFS_IOLOCK_SHARED); | 
| Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5565 |  | 
 | 5566 | 	for (i = 0; i < cur_ext; i++) { | 
 | 5567 | 		int full = 0;	/* user array is full */ | 
 | 5568 |  | 
 | 5569 | 		/* format results & advance arg */ | 
 | 5570 | 		error = formatter(&arg, &out[i], &full); | 
 | 5571 | 		if (error || full) | 
 | 5572 | 			break; | 
 | 5573 | 	} | 
 | 5574 |  | 
| Felix Blyakher | 7747a0b | 2009-06-11 17:07:28 -0500 | [diff] [blame] | 5575 | 	kmem_free(out); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5576 | 	return error; | 
 | 5577 | } | 
 | 5578 |  | 
 | 5579 | /* | 
 | 5580 |  * Check the last inode extent to determine whether this allocation will result | 
 | 5581 |  * in blocks being allocated at the end of the file. When we allocate new data | 
 | 5582 |  * blocks at the end of the file which do not start at the previous data block, | 
 | 5583 |  * we will try to align the new blocks at stripe unit boundaries. | 
 | 5584 |  */ | 
| Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 5585 | STATIC int				/* error */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5586 | xfs_bmap_isaeof( | 
 | 5587 | 	xfs_inode_t	*ip,		/* incore inode pointer */ | 
 | 5588 | 	xfs_fileoff_t   off,		/* file offset in fsblocks */ | 
 | 5589 | 	int             whichfork,	/* data or attribute fork */ | 
 | 5590 | 	char		*aeof)		/* return value */ | 
 | 5591 | { | 
 | 5592 | 	int		error;		/* error return value */ | 
 | 5593 | 	xfs_ifork_t	*ifp;		/* inode fork pointer */ | 
| Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 5594 | 	xfs_bmbt_rec_host_t *lastrec;	/* extent record pointer */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5595 | 	xfs_extnum_t	nextents;	/* number of file extents */ | 
 | 5596 | 	xfs_bmbt_irec_t	s;		/* expanded extent record */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5597 |  | 
 | 5598 | 	ASSERT(whichfork == XFS_DATA_FORK); | 
 | 5599 | 	ifp = XFS_IFORK_PTR(ip, whichfork); | 
 | 5600 | 	if (!(ifp->if_flags & XFS_IFEXTENTS) && | 
 | 5601 | 	    (error = xfs_iread_extents(NULL, ip, whichfork))) | 
 | 5602 | 		return error; | 
 | 5603 | 	nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); | 
 | 5604 | 	if (nextents == 0) { | 
 | 5605 | 		*aeof = 1; | 
 | 5606 | 		return 0; | 
 | 5607 | 	} | 
 | 5608 | 	/* | 
 | 5609 | 	 * Go to the last extent | 
 | 5610 | 	 */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5611 | 	lastrec = xfs_iext_get_ext(ifp, nextents - 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5612 | 	xfs_bmbt_get_all(lastrec, &s); | 
 | 5613 | 	/* | 
 | 5614 | 	 * Check we are allocating in the last extent (for delayed allocations) | 
 | 5615 | 	 * or past the last extent for non-delayed allocations. | 
 | 5616 | 	 */ | 
 | 5617 | 	*aeof = (off >= s.br_startoff && | 
 | 5618 | 		 off < s.br_startoff + s.br_blockcount && | 
| Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5619 | 		 isnullstartblock(s.br_startblock)) || | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5620 | 		off >= s.br_startoff + s.br_blockcount; | 
 | 5621 | 	return 0; | 
 | 5622 | } | 
 | 5623 |  | 
 | 5624 | /* | 
 | 5625 |  * Check if the endoff is outside the last extent. If so the caller will grow | 
 | 5626 |  * the allocation to a stripe unit boundary. | 
 | 5627 |  */ | 
 | 5628 | int					/* error */ | 
 | 5629 | xfs_bmap_eof( | 
 | 5630 | 	xfs_inode_t	*ip,		/* incore inode pointer */ | 
 | 5631 | 	xfs_fileoff_t	endoff,		/* file offset in fsblocks */ | 
 | 5632 | 	int		whichfork,	/* data or attribute fork */ | 
 | 5633 | 	int		*eof)		/* result value */ | 
 | 5634 | { | 
 | 5635 | 	xfs_fsblock_t	blockcount;	/* extent block count */ | 
 | 5636 | 	int		error;		/* error return value */ | 
 | 5637 | 	xfs_ifork_t	*ifp;		/* inode fork pointer */ | 
| Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 5638 | 	xfs_bmbt_rec_host_t *lastrec;	/* extent record pointer */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5639 | 	xfs_extnum_t	nextents;	/* number of file extents */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5640 | 	xfs_fileoff_t	startoff;	/* extent starting file offset */ | 
 | 5641 |  | 
 | 5642 | 	ASSERT(whichfork == XFS_DATA_FORK); | 
 | 5643 | 	ifp = XFS_IFORK_PTR(ip, whichfork); | 
 | 5644 | 	if (!(ifp->if_flags & XFS_IFEXTENTS) && | 
 | 5645 | 	    (error = xfs_iread_extents(NULL, ip, whichfork))) | 
 | 5646 | 		return error; | 
 | 5647 | 	nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); | 
 | 5648 | 	if (nextents == 0) { | 
 | 5649 | 		*eof = 1; | 
 | 5650 | 		return 0; | 
 | 5651 | 	} | 
 | 5652 | 	/* | 
 | 5653 | 	 * Go to the last extent | 
 | 5654 | 	 */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5655 | 	lastrec = xfs_iext_get_ext(ifp, nextents - 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5656 | 	startoff = xfs_bmbt_get_startoff(lastrec); | 
 | 5657 | 	blockcount = xfs_bmbt_get_blockcount(lastrec); | 
 | 5658 | 	*eof = endoff >= startoff + blockcount; | 
 | 5659 | 	return 0; | 
 | 5660 | } | 
 | 5661 |  | 
 | 5662 | #ifdef DEBUG | 
| Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5663 | STATIC struct xfs_buf * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5664 | xfs_bmap_get_bp( | 
| Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5665 | 	struct xfs_btree_cur	*cur, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5666 | 	xfs_fsblock_t		bno) | 
 | 5667 | { | 
| Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5668 | 	struct xfs_log_item_desc *lidp; | 
 | 5669 | 	int			i; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5670 |  | 
 | 5671 | 	if (!cur) | 
| Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5672 | 		return NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5673 |  | 
| Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5674 | 	for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) { | 
 | 5675 | 		if (!cur->bc_bufs[i]) | 
 | 5676 | 			break; | 
 | 5677 | 		if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno) | 
 | 5678 | 			return cur->bc_bufs[i]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5679 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5680 |  | 
| Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5681 | 	/* Chase down all the log items to see if the bp is there */ | 
 | 5682 | 	list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) { | 
| Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5683 | 		struct xfs_buf_log_item	*bip; | 
| Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5684 | 		bip = (struct xfs_buf_log_item *)lidp->lid_item; | 
 | 5685 | 		if (bip->bli_item.li_type == XFS_LI_BUF && | 
 | 5686 | 		    XFS_BUF_ADDR(bip->bli_buf) == bno) | 
 | 5687 | 			return bip->bli_buf; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5688 | 	} | 
| Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5689 |  | 
| Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5690 | 	return NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5691 | } | 
 | 5692 |  | 
| Hannes Eder | 3180e66 | 2009-03-04 19:34:10 +0100 | [diff] [blame] | 5693 | STATIC void | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5694 | xfs_check_block( | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5695 | 	struct xfs_btree_block	*block, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5696 | 	xfs_mount_t		*mp, | 
 | 5697 | 	int			root, | 
 | 5698 | 	short			sz) | 
 | 5699 | { | 
 | 5700 | 	int			i, j, dmxr; | 
| Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5701 | 	__be64			*pp, *thispa;	/* pointer to block address */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5702 | 	xfs_bmbt_key_t		*prevp, *keyp; | 
 | 5703 |  | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 5704 | 	ASSERT(be16_to_cpu(block->bb_level) > 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5705 |  | 
 | 5706 | 	prevp = NULL; | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5707 | 	for( i = 1; i <= xfs_btree_get_numrecs(block); i++) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5708 | 		dmxr = mp->m_bmap_dmxr[0]; | 
| Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5709 | 		keyp = XFS_BMBT_KEY_ADDR(mp, block, i); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5710 |  | 
 | 5711 | 		if (prevp) { | 
| Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 5712 | 			ASSERT(be64_to_cpu(prevp->br_startoff) < | 
 | 5713 | 			       be64_to_cpu(keyp->br_startoff)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5714 | 		} | 
 | 5715 | 		prevp = keyp; | 
 | 5716 |  | 
 | 5717 | 		/* | 
 | 5718 | 		 * Compare the block numbers to see if there are dups. | 
 | 5719 | 		 */ | 
| Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5720 | 		if (root) | 
| Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 5721 | 			pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz); | 
| Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5722 | 		else | 
 | 5723 | 			pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr); | 
 | 5724 |  | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 5725 | 		for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) { | 
| Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5726 | 			if (root) | 
| Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 5727 | 				thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz); | 
| Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5728 | 			else | 
 | 5729 | 				thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr); | 
| Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5730 | 			if (*thispa == *pp) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5731 | 				cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld", | 
| Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 5732 | 					__func__, j, i, | 
| Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5733 | 					(unsigned long long)be64_to_cpu(*thispa)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5734 | 				panic("%s: ptrs are equal in node\n", | 
| Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 5735 | 					__func__); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5736 | 			} | 
 | 5737 | 		} | 
 | 5738 | 	} | 
 | 5739 | } | 
 | 5740 |  | 
 | 5741 | /* | 
 | 5742 |  * Check that the extents for the inode ip are in the right order in all | 
 | 5743 |  * btree leaves. | 
 | 5744 |  */ | 
 | 5745 |  | 
 | 5746 | STATIC void | 
 | 5747 | xfs_bmap_check_leaf_extents( | 
 | 5748 | 	xfs_btree_cur_t		*cur,	/* btree cursor or null */ | 
 | 5749 | 	xfs_inode_t		*ip,		/* incore inode pointer */ | 
 | 5750 | 	int			whichfork)	/* data or attr fork */ | 
 | 5751 | { | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5752 | 	struct xfs_btree_block	*block;	/* current btree block */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5753 | 	xfs_fsblock_t		bno;	/* block # of "block" */ | 
 | 5754 | 	xfs_buf_t		*bp;	/* buffer for "block" */ | 
 | 5755 | 	int			error;	/* error return value */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5756 | 	xfs_extnum_t		i=0, j;	/* index into the extents list */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5757 | 	xfs_ifork_t		*ifp;	/* fork structure */ | 
 | 5758 | 	int			level;	/* btree level, for checking */ | 
 | 5759 | 	xfs_mount_t		*mp;	/* file system mount structure */ | 
| Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5760 | 	__be64			*pp;	/* pointer to block address */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5761 | 	xfs_bmbt_rec_t		*ep;	/* pointer to current extent */ | 
| Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 5762 | 	xfs_bmbt_rec_t		last = {0, 0}; /* last extent in prev block */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5763 | 	xfs_bmbt_rec_t		*nextp;	/* pointer to next extent */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5764 | 	int			bp_release = 0; | 
 | 5765 |  | 
 | 5766 | 	if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) { | 
 | 5767 | 		return; | 
 | 5768 | 	} | 
 | 5769 |  | 
 | 5770 | 	bno = NULLFSBLOCK; | 
 | 5771 | 	mp = ip->i_mount; | 
 | 5772 | 	ifp = XFS_IFORK_PTR(ip, whichfork); | 
 | 5773 | 	block = ifp->if_broot; | 
 | 5774 | 	/* | 
 | 5775 | 	 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. | 
 | 5776 | 	 */ | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 5777 | 	level = be16_to_cpu(block->bb_level); | 
 | 5778 | 	ASSERT(level > 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5779 | 	xfs_check_block(block, mp, 1, ifp->if_broot_bytes); | 
| Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 5780 | 	pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); | 
| Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5781 | 	bno = be64_to_cpu(*pp); | 
 | 5782 |  | 
 | 5783 | 	ASSERT(bno != NULLDFSBNO); | 
 | 5784 | 	ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); | 
 | 5785 | 	ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); | 
 | 5786 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5787 | 	/* | 
 | 5788 | 	 * Go down the tree until leaf level is reached, following the first | 
 | 5789 | 	 * pointer (leftmost) at each level. | 
 | 5790 | 	 */ | 
 | 5791 | 	while (level-- > 0) { | 
 | 5792 | 		/* See if buf is in cur first */ | 
 | 5793 | 		bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); | 
 | 5794 | 		if (bp) { | 
 | 5795 | 			bp_release = 0; | 
 | 5796 | 		} else { | 
 | 5797 | 			bp_release = 1; | 
 | 5798 | 		} | 
 | 5799 | 		if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, | 
 | 5800 | 				XFS_BMAP_BTREE_REF))) | 
 | 5801 | 			goto error_norelse; | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5802 | 		block = XFS_BUF_TO_BLOCK(bp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5803 | 		XFS_WANT_CORRUPTED_GOTO( | 
| Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 5804 | 			xfs_bmap_sanity_check(mp, bp, level), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5805 | 			error0); | 
 | 5806 | 		if (level == 0) | 
 | 5807 | 			break; | 
 | 5808 |  | 
 | 5809 | 		/* | 
 | 5810 | 		 * Check this block for basic sanity (increasing keys and | 
 | 5811 | 		 * no duplicate blocks). | 
 | 5812 | 		 */ | 
 | 5813 |  | 
 | 5814 | 		xfs_check_block(block, mp, 0, 0); | 
| Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5815 | 		pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); | 
| Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5816 | 		bno = be64_to_cpu(*pp); | 
 | 5817 | 		XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5818 | 		if (bp_release) { | 
 | 5819 | 			bp_release = 0; | 
 | 5820 | 			xfs_trans_brelse(NULL, bp); | 
 | 5821 | 		} | 
 | 5822 | 	} | 
 | 5823 |  | 
 | 5824 | 	/* | 
 | 5825 | 	 * Here with bp and block set to the leftmost leaf node in the tree. | 
 | 5826 | 	 */ | 
 | 5827 | 	i = 0; | 
 | 5828 |  | 
 | 5829 | 	/* | 
 | 5830 | 	 * Loop over all leaf nodes checking that all extents are in the right order. | 
 | 5831 | 	 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5832 | 	for (;;) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5833 | 		xfs_fsblock_t	nextbno; | 
 | 5834 | 		xfs_extnum_t	num_recs; | 
 | 5835 |  | 
 | 5836 |  | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5837 | 		num_recs = xfs_btree_get_numrecs(block); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5838 |  | 
 | 5839 | 		/* | 
 | 5840 | 		 * Read-ahead the next leaf block, if any. | 
 | 5841 | 		 */ | 
 | 5842 |  | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5843 | 		nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5844 |  | 
 | 5845 | 		/* | 
 | 5846 | 		 * Check all the extents to make sure they are OK. | 
 | 5847 | 		 * If we had a previous block, the last entry should | 
 | 5848 | 		 * conform with the first entry in this one. | 
 | 5849 | 		 */ | 
 | 5850 |  | 
| Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5851 | 		ep = XFS_BMBT_REC_ADDR(mp, block, 1); | 
| Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 5852 | 		if (i) { | 
| Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 5853 | 			ASSERT(xfs_bmbt_disk_get_startoff(&last) + | 
 | 5854 | 			       xfs_bmbt_disk_get_blockcount(&last) <= | 
 | 5855 | 			       xfs_bmbt_disk_get_startoff(ep)); | 
| Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 5856 | 		} | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5857 | 		for (j = 1; j < num_recs; j++) { | 
| Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5858 | 			nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1); | 
| Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 5859 | 			ASSERT(xfs_bmbt_disk_get_startoff(ep) + | 
 | 5860 | 			       xfs_bmbt_disk_get_blockcount(ep) <= | 
 | 5861 | 			       xfs_bmbt_disk_get_startoff(nextp)); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5862 | 			ep = nextp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5863 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5864 |  | 
| Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 5865 | 		last = *ep; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5866 | 		i += num_recs; | 
 | 5867 | 		if (bp_release) { | 
 | 5868 | 			bp_release = 0; | 
 | 5869 | 			xfs_trans_brelse(NULL, bp); | 
 | 5870 | 		} | 
 | 5871 | 		bno = nextbno; | 
 | 5872 | 		/* | 
 | 5873 | 		 * If we've reached the end, stop. | 
 | 5874 | 		 */ | 
 | 5875 | 		if (bno == NULLFSBLOCK) | 
 | 5876 | 			break; | 
 | 5877 |  | 
 | 5878 | 		bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); | 
 | 5879 | 		if (bp) { | 
 | 5880 | 			bp_release = 0; | 
 | 5881 | 		} else { | 
 | 5882 | 			bp_release = 1; | 
 | 5883 | 		} | 
 | 5884 | 		if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, | 
 | 5885 | 				XFS_BMAP_BTREE_REF))) | 
 | 5886 | 			goto error_norelse; | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5887 | 		block = XFS_BUF_TO_BLOCK(bp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5888 | 	} | 
 | 5889 | 	if (bp_release) { | 
 | 5890 | 		bp_release = 0; | 
 | 5891 | 		xfs_trans_brelse(NULL, bp); | 
 | 5892 | 	} | 
 | 5893 | 	return; | 
 | 5894 |  | 
 | 5895 | error0: | 
| Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 5896 | 	cmn_err(CE_WARN, "%s: at error0", __func__); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5897 | 	if (bp_release) | 
 | 5898 | 		xfs_trans_brelse(NULL, bp); | 
 | 5899 | error_norelse: | 
 | 5900 | 	cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents", | 
| Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 5901 | 		__func__, i); | 
 | 5902 | 	panic("%s: CORRUPTED BTREE OR SOMETHING", __func__); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5903 | 	return; | 
 | 5904 | } | 
 | 5905 | #endif | 
 | 5906 |  | 
 | 5907 | /* | 
 | 5908 |  * Count fsblocks of the given fork. | 
 | 5909 |  */ | 
 | 5910 | int						/* error */ | 
 | 5911 | xfs_bmap_count_blocks( | 
 | 5912 | 	xfs_trans_t		*tp,		/* transaction pointer */ | 
 | 5913 | 	xfs_inode_t		*ip,		/* incore inode */ | 
 | 5914 | 	int			whichfork,	/* data or attr fork */ | 
 | 5915 | 	int			*count)		/* out: count of blocks */ | 
 | 5916 | { | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5917 | 	struct xfs_btree_block	*block;	/* current btree block */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5918 | 	xfs_fsblock_t		bno;	/* block # of "block" */ | 
 | 5919 | 	xfs_ifork_t		*ifp;	/* fork structure */ | 
 | 5920 | 	int			level;	/* btree level, for checking */ | 
 | 5921 | 	xfs_mount_t		*mp;	/* file system mount structure */ | 
| Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5922 | 	__be64			*pp;	/* pointer to block address */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5923 |  | 
 | 5924 | 	bno = NULLFSBLOCK; | 
 | 5925 | 	mp = ip->i_mount; | 
 | 5926 | 	ifp = XFS_IFORK_PTR(ip, whichfork); | 
 | 5927 | 	if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) { | 
| Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 5928 | 		xfs_bmap_count_leaves(ifp, 0, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5929 | 			ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t), | 
| Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 5930 | 			count); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5931 | 		return 0; | 
 | 5932 | 	} | 
 | 5933 |  | 
 | 5934 | 	/* | 
 | 5935 | 	 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. | 
 | 5936 | 	 */ | 
 | 5937 | 	block = ifp->if_broot; | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 5938 | 	level = be16_to_cpu(block->bb_level); | 
 | 5939 | 	ASSERT(level > 0); | 
| Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 5940 | 	pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); | 
| Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5941 | 	bno = be64_to_cpu(*pp); | 
 | 5942 | 	ASSERT(bno != NULLDFSBNO); | 
 | 5943 | 	ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); | 
 | 5944 | 	ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5945 |  | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5946 | 	if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5947 | 		XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW, | 
 | 5948 | 				 mp); | 
 | 5949 | 		return XFS_ERROR(EFSCORRUPTED); | 
 | 5950 | 	} | 
 | 5951 |  | 
 | 5952 | 	return 0; | 
 | 5953 | } | 
 | 5954 |  | 
 | 5955 | /* | 
 | 5956 |  * Recursively walks each level of a btree | 
 | 5957 |  * to count total fsblocks is use. | 
 | 5958 |  */ | 
| David Chinner | a8272ce | 2007-11-23 16:28:09 +1100 | [diff] [blame] | 5959 | STATIC int                                     /* error */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5960 | xfs_bmap_count_tree( | 
 | 5961 | 	xfs_mount_t     *mp,            /* file system mount point */ | 
 | 5962 | 	xfs_trans_t     *tp,            /* transaction pointer */ | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5963 | 	xfs_ifork_t	*ifp,		/* inode fork pointer */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5964 | 	xfs_fsblock_t   blockno,	/* file system block number */ | 
 | 5965 | 	int             levelin,	/* level in btree */ | 
 | 5966 | 	int		*count)		/* Count of blocks */ | 
 | 5967 | { | 
 | 5968 | 	int			error; | 
 | 5969 | 	xfs_buf_t		*bp, *nbp; | 
 | 5970 | 	int			level = levelin; | 
| Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5971 | 	__be64			*pp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5972 | 	xfs_fsblock_t           bno = blockno; | 
 | 5973 | 	xfs_fsblock_t		nextbno; | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5974 | 	struct xfs_btree_block	*block, *nextblock; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5975 | 	int			numrecs; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5976 |  | 
 | 5977 | 	if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF))) | 
 | 5978 | 		return error; | 
 | 5979 | 	*count += 1; | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5980 | 	block = XFS_BUF_TO_BLOCK(bp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5981 |  | 
 | 5982 | 	if (--level) { | 
| Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 5983 | 		/* Not at node above leaves, count this level of nodes */ | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5984 | 		nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5985 | 		while (nextbno != NULLFSBLOCK) { | 
 | 5986 | 			if ((error = xfs_btree_read_bufl(mp, tp, nextbno, | 
 | 5987 | 				0, &nbp, XFS_BMAP_BTREE_REF))) | 
 | 5988 | 				return error; | 
 | 5989 | 			*count += 1; | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5990 | 			nextblock = XFS_BUF_TO_BLOCK(nbp); | 
 | 5991 | 			nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5992 | 			xfs_trans_brelse(tp, nbp); | 
 | 5993 | 		} | 
 | 5994 |  | 
 | 5995 | 		/* Dive to the next level */ | 
| Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5996 | 		pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); | 
| Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5997 | 		bno = be64_to_cpu(*pp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5998 | 		if (unlikely((error = | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5999 | 		     xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6000 | 			xfs_trans_brelse(tp, bp); | 
 | 6001 | 			XFS_ERROR_REPORT("xfs_bmap_count_tree(1)", | 
 | 6002 | 					 XFS_ERRLEVEL_LOW, mp); | 
 | 6003 | 			return XFS_ERROR(EFSCORRUPTED); | 
 | 6004 | 		} | 
 | 6005 | 		xfs_trans_brelse(tp, bp); | 
 | 6006 | 	} else { | 
 | 6007 | 		/* count all level 1 nodes and their leaves */ | 
 | 6008 | 		for (;;) { | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6009 | 			nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 6010 | 			numrecs = be16_to_cpu(block->bb_numrecs); | 
| Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6011 | 			xfs_bmap_disk_count_leaves(mp, block, numrecs, count); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6012 | 			xfs_trans_brelse(tp, bp); | 
 | 6013 | 			if (nextbno == NULLFSBLOCK) | 
 | 6014 | 				break; | 
 | 6015 | 			bno = nextbno; | 
 | 6016 | 			if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, | 
 | 6017 | 				XFS_BMAP_BTREE_REF))) | 
 | 6018 | 				return error; | 
 | 6019 | 			*count += 1; | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6020 | 			block = XFS_BUF_TO_BLOCK(bp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6021 | 		} | 
 | 6022 | 	} | 
 | 6023 | 	return 0; | 
 | 6024 | } | 
 | 6025 |  | 
 | 6026 | /* | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6027 |  * Count leaf blocks given a range of extent records. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6028 |  */ | 
| Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 6029 | STATIC void | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6030 | xfs_bmap_count_leaves( | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6031 | 	xfs_ifork_t		*ifp, | 
 | 6032 | 	xfs_extnum_t		idx, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6033 | 	int			numrecs, | 
 | 6034 | 	int			*count) | 
 | 6035 | { | 
 | 6036 | 	int		b; | 
 | 6037 |  | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6038 | 	for (b = 0; b < numrecs; b++) { | 
| Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 6039 | 		xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b); | 
| Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6040 | 		*count += xfs_bmbt_get_blockcount(frp); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6041 | 	} | 
| Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6042 | } | 
 | 6043 |  | 
 | 6044 | /* | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6045 |  * Count leaf blocks given a range of extent records originally | 
 | 6046 |  * in btree format. | 
| Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6047 |  */ | 
| Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 6048 | STATIC void | 
| Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6049 | xfs_bmap_disk_count_leaves( | 
| Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6050 | 	struct xfs_mount	*mp, | 
| Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6051 | 	struct xfs_btree_block	*block, | 
| Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6052 | 	int			numrecs, | 
 | 6053 | 	int			*count) | 
 | 6054 | { | 
 | 6055 | 	int		b; | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6056 | 	xfs_bmbt_rec_t	*frp; | 
| Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6057 |  | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6058 | 	for (b = 1; b <= numrecs; b++) { | 
| Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6059 | 		frp = XFS_BMBT_REC_ADDR(mp, block, b); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6060 | 		*count += xfs_bmbt_disk_get_blockcount(frp); | 
| Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6061 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6062 | } |