| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
| Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 2 |  * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. | 
 | 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" | 
 | 28 | #include "xfs_dmapi.h" | 
 | 29 | #include "xfs_mount.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include "xfs_bmap_btree.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 31 | #include "xfs_alloc_btree.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "xfs_ialloc_btree.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "xfs_dir2_sf.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 34 | #include "xfs_attr_sf.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "xfs_dinode.h" | 
 | 36 | #include "xfs_inode.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 37 | #include "xfs_btree.h" | 
 | 38 | #include "xfs_ialloc.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include "xfs_alloc.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include "xfs_rtalloc.h" | 
 | 41 | #include "xfs_error.h" | 
 | 42 | #include "xfs_bmap.h" | 
 | 43 |  | 
 | 44 | /* | 
 | 45 |  * Log specified fields for the inode given by bp and off. | 
 | 46 |  */ | 
 | 47 | STATIC void | 
 | 48 | xfs_ialloc_log_di( | 
 | 49 | 	xfs_trans_t	*tp,		/* transaction pointer */ | 
 | 50 | 	xfs_buf_t	*bp,		/* inode buffer */ | 
 | 51 | 	int		off,		/* index of inode in buffer */ | 
 | 52 | 	int		fields)		/* bitmask of fields to log */ | 
 | 53 | { | 
 | 54 | 	int			first;		/* first byte number */ | 
 | 55 | 	int			ioffset;	/* off in bytes */ | 
 | 56 | 	int			last;		/* last byte number */ | 
 | 57 | 	xfs_mount_t		*mp;		/* mount point structure */ | 
 | 58 | 	static const short	offsets[] = {	/* field offsets */ | 
 | 59 | 						/* keep in sync with bits */ | 
 | 60 | 		offsetof(xfs_dinode_core_t, di_magic), | 
 | 61 | 		offsetof(xfs_dinode_core_t, di_mode), | 
 | 62 | 		offsetof(xfs_dinode_core_t, di_version), | 
 | 63 | 		offsetof(xfs_dinode_core_t, di_format), | 
 | 64 | 		offsetof(xfs_dinode_core_t, di_onlink), | 
 | 65 | 		offsetof(xfs_dinode_core_t, di_uid), | 
 | 66 | 		offsetof(xfs_dinode_core_t, di_gid), | 
 | 67 | 		offsetof(xfs_dinode_core_t, di_nlink), | 
 | 68 | 		offsetof(xfs_dinode_core_t, di_projid), | 
 | 69 | 		offsetof(xfs_dinode_core_t, di_pad), | 
 | 70 | 		offsetof(xfs_dinode_core_t, di_atime), | 
 | 71 | 		offsetof(xfs_dinode_core_t, di_mtime), | 
 | 72 | 		offsetof(xfs_dinode_core_t, di_ctime), | 
 | 73 | 		offsetof(xfs_dinode_core_t, di_size), | 
 | 74 | 		offsetof(xfs_dinode_core_t, di_nblocks), | 
 | 75 | 		offsetof(xfs_dinode_core_t, di_extsize), | 
 | 76 | 		offsetof(xfs_dinode_core_t, di_nextents), | 
 | 77 | 		offsetof(xfs_dinode_core_t, di_anextents), | 
 | 78 | 		offsetof(xfs_dinode_core_t, di_forkoff), | 
 | 79 | 		offsetof(xfs_dinode_core_t, di_aformat), | 
 | 80 | 		offsetof(xfs_dinode_core_t, di_dmevmask), | 
 | 81 | 		offsetof(xfs_dinode_core_t, di_dmstate), | 
 | 82 | 		offsetof(xfs_dinode_core_t, di_flags), | 
 | 83 | 		offsetof(xfs_dinode_core_t, di_gen), | 
 | 84 | 		offsetof(xfs_dinode_t, di_next_unlinked), | 
 | 85 | 		offsetof(xfs_dinode_t, di_u), | 
 | 86 | 		offsetof(xfs_dinode_t, di_a), | 
 | 87 | 		sizeof(xfs_dinode_t) | 
 | 88 | 	}; | 
 | 89 |  | 
 | 90 |  | 
 | 91 | 	ASSERT(offsetof(xfs_dinode_t, di_core) == 0); | 
 | 92 | 	ASSERT((fields & (XFS_DI_U|XFS_DI_A)) == 0); | 
 | 93 | 	mp = tp->t_mountp; | 
 | 94 | 	/* | 
 | 95 | 	 * Get the inode-relative first and last bytes for these fields | 
 | 96 | 	 */ | 
 | 97 | 	xfs_btree_offsets(fields, offsets, XFS_DI_NUM_BITS, &first, &last); | 
 | 98 | 	/* | 
 | 99 | 	 * Convert to buffer offsets and log it. | 
 | 100 | 	 */ | 
 | 101 | 	ioffset = off << mp->m_sb.sb_inodelog; | 
 | 102 | 	first += ioffset; | 
 | 103 | 	last += ioffset; | 
 | 104 | 	xfs_trans_log_buf(tp, bp, first, last); | 
 | 105 | } | 
 | 106 |  | 
 | 107 | /* | 
 | 108 |  * Allocation group level functions. | 
 | 109 |  */ | 
| David Chinner | 75de2a9 | 2008-03-27 18:00:38 +1100 | [diff] [blame] | 110 | static inline int | 
 | 111 | xfs_ialloc_cluster_alignment( | 
 | 112 | 	xfs_alloc_arg_t	*args) | 
 | 113 | { | 
 | 114 | 	if (xfs_sb_version_hasalign(&args->mp->m_sb) && | 
 | 115 | 	    args->mp->m_sb.sb_inoalignmt >= | 
 | 116 | 	     XFS_B_TO_FSBT(args->mp, XFS_INODE_CLUSTER_SIZE(args->mp))) | 
 | 117 | 		return args->mp->m_sb.sb_inoalignmt; | 
 | 118 | 	return 1; | 
 | 119 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 |  | 
 | 121 | /* | 
 | 122 |  * Allocate new inodes in the allocation group specified by agbp. | 
 | 123 |  * Return 0 for success, else error code. | 
 | 124 |  */ | 
 | 125 | STATIC int				/* error code or 0 */ | 
 | 126 | xfs_ialloc_ag_alloc( | 
 | 127 | 	xfs_trans_t	*tp,		/* transaction pointer */ | 
 | 128 | 	xfs_buf_t	*agbp,		/* alloc group buffer */ | 
 | 129 | 	int		*alloc) | 
 | 130 | { | 
 | 131 | 	xfs_agi_t	*agi;		/* allocation group header */ | 
 | 132 | 	xfs_alloc_arg_t	args;		/* allocation argument structure */ | 
 | 133 | 	int		blks_per_cluster;  /* fs blocks per inode cluster */ | 
 | 134 | 	xfs_btree_cur_t	*cur;		/* inode btree cursor */ | 
 | 135 | 	xfs_daddr_t	d;		/* disk addr of buffer */ | 
| David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 136 | 	xfs_agnumber_t	agno; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | 	int		error; | 
 | 138 | 	xfs_buf_t	*fbuf;		/* new free inodes' buffer */ | 
 | 139 | 	xfs_dinode_t	*free;		/* new free inode structure */ | 
 | 140 | 	int		i;		/* inode counter */ | 
 | 141 | 	int		j;		/* block counter */ | 
 | 142 | 	int		nbufs;		/* num bufs of new inodes */ | 
 | 143 | 	xfs_agino_t	newino;		/* new first inode's number */ | 
 | 144 | 	xfs_agino_t	newlen;		/* new number of inodes */ | 
 | 145 | 	int		ninodes;	/* num inodes per buf */ | 
 | 146 | 	xfs_agino_t	thisino;	/* current inode number, for loop */ | 
 | 147 | 	int		version;	/* inode version number to use */ | 
| Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 148 | 	int		isaligned = 0;	/* inode allocation at stripe unit */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | 					/* boundary */ | 
| David Chinner | 359346a | 2008-04-29 12:53:32 +1000 | [diff] [blame] | 150 | 	unsigned int	gen; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 |  | 
 | 152 | 	args.tp = tp; | 
 | 153 | 	args.mp = tp->t_mountp; | 
 | 154 |  | 
 | 155 | 	/* | 
 | 156 | 	 * Locking will ensure that we don't have two callers in here | 
 | 157 | 	 * at one time. | 
 | 158 | 	 */ | 
 | 159 | 	newlen = XFS_IALLOC_INODES(args.mp); | 
 | 160 | 	if (args.mp->m_maxicount && | 
 | 161 | 	    args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount) | 
 | 162 | 		return XFS_ERROR(ENOSPC); | 
 | 163 | 	args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp); | 
 | 164 | 	/* | 
| Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 165 | 	 * First try to allocate inodes contiguous with the last-allocated | 
 | 166 | 	 * chunk of inodes.  If the filesystem is striped, this will fill | 
 | 167 | 	 * an entire stripe unit with inodes. | 
 | 168 |  	 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | 	agi = XFS_BUF_TO_AGI(agbp); | 
| Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 170 | 	newino = be32_to_cpu(agi->agi_newino); | 
| Nathan Scott | 019ff2d | 2006-04-11 15:45:05 +1000 | [diff] [blame] | 171 | 	args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) + | 
 | 172 | 			XFS_IALLOC_BLOCKS(args.mp); | 
 | 173 | 	if (likely(newino != NULLAGINO && | 
 | 174 | 		  (args.agbno < be32_to_cpu(agi->agi_length)))) { | 
| Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 175 | 		args.fsbno = XFS_AGB_TO_FSB(args.mp, | 
 | 176 | 				be32_to_cpu(agi->agi_seqno), args.agbno); | 
 | 177 | 		args.type = XFS_ALLOCTYPE_THIS_BNO; | 
 | 178 | 		args.mod = args.total = args.wasdel = args.isfl = | 
 | 179 | 			args.userdata = args.minalignslop = 0; | 
 | 180 | 		args.prod = 1; | 
| David Chinner | 75de2a9 | 2008-03-27 18:00:38 +1100 | [diff] [blame] | 181 |  | 
| Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 182 | 		/* | 
| David Chinner | 75de2a9 | 2008-03-27 18:00:38 +1100 | [diff] [blame] | 183 | 		 * We need to take into account alignment here to ensure that | 
 | 184 | 		 * we don't modify the free list if we fail to have an exact | 
 | 185 | 		 * block. If we don't have an exact match, and every oher | 
 | 186 | 		 * attempt allocation attempt fails, we'll end up cancelling | 
 | 187 | 		 * a dirty transaction and shutting down. | 
 | 188 | 		 * | 
 | 189 | 		 * For an exact allocation, alignment must be 1, | 
 | 190 | 		 * however we need to take cluster alignment into account when | 
 | 191 | 		 * fixing up the freelist. Use the minalignslop field to | 
 | 192 | 		 * indicate that extra blocks might be required for alignment, | 
 | 193 | 		 * but not to use them in the actual exact allocation. | 
| Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 194 | 		 */ | 
| David Chinner | 75de2a9 | 2008-03-27 18:00:38 +1100 | [diff] [blame] | 195 | 		args.alignment = 1; | 
 | 196 | 		args.minalignslop = xfs_ialloc_cluster_alignment(&args) - 1; | 
 | 197 |  | 
 | 198 | 		/* Allow space for the inode btree to split. */ | 
| Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 199 | 		args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1; | 
 | 200 | 		if ((error = xfs_alloc_vextent(&args))) | 
 | 201 | 			return error; | 
 | 202 | 	} else | 
 | 203 | 		args.fsbno = NULLFSBLOCK; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 |  | 
| Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 205 | 	if (unlikely(args.fsbno == NULLFSBLOCK)) { | 
 | 206 | 		/* | 
 | 207 | 		 * Set the alignment for the allocation. | 
 | 208 | 		 * If stripe alignment is turned on then align at stripe unit | 
 | 209 | 		 * boundary. | 
| Nathan Scott | 019ff2d | 2006-04-11 15:45:05 +1000 | [diff] [blame] | 210 | 		 * If the cluster size is smaller than a filesystem block | 
 | 211 | 		 * then we're doing I/O for inodes in filesystem block size | 
| Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 212 | 		 * pieces, so don't need alignment anyway. | 
 | 213 | 		 */ | 
 | 214 | 		isaligned = 0; | 
 | 215 | 		if (args.mp->m_sinoalign) { | 
 | 216 | 			ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN)); | 
 | 217 | 			args.alignment = args.mp->m_dalign; | 
 | 218 | 			isaligned = 1; | 
| David Chinner | 75de2a9 | 2008-03-27 18:00:38 +1100 | [diff] [blame] | 219 | 		} else | 
 | 220 | 			args.alignment = xfs_ialloc_cluster_alignment(&args); | 
| Glen Overby | 3ccb8b5 | 2006-03-29 09:52:28 +1000 | [diff] [blame] | 221 | 		/* | 
 | 222 | 		 * Need to figure out where to allocate the inode blocks. | 
 | 223 | 		 * Ideally they should be spaced out through the a.g. | 
 | 224 | 		 * For now, just allocate blocks up front. | 
 | 225 | 		 */ | 
 | 226 | 		args.agbno = be32_to_cpu(agi->agi_root); | 
 | 227 | 		args.fsbno = XFS_AGB_TO_FSB(args.mp, | 
 | 228 | 				be32_to_cpu(agi->agi_seqno), args.agbno); | 
 | 229 | 		/* | 
 | 230 | 		 * Allocate a fixed-size extent of inodes. | 
 | 231 | 		 */ | 
 | 232 | 		args.type = XFS_ALLOCTYPE_NEAR_BNO; | 
 | 233 | 		args.mod = args.total = args.wasdel = args.isfl = | 
 | 234 | 			args.userdata = args.minalignslop = 0; | 
 | 235 | 		args.prod = 1; | 
 | 236 | 		/* | 
 | 237 | 		 * Allow space for the inode btree to split. | 
 | 238 | 		 */ | 
 | 239 | 		args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1; | 
 | 240 | 		if ((error = xfs_alloc_vextent(&args))) | 
 | 241 | 			return error; | 
 | 242 | 	} | 
| Nathan Scott | 019ff2d | 2006-04-11 15:45:05 +1000 | [diff] [blame] | 243 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | 	/* | 
 | 245 | 	 * If stripe alignment is turned on, then try again with cluster | 
 | 246 | 	 * alignment. | 
 | 247 | 	 */ | 
 | 248 | 	if (isaligned && args.fsbno == NULLFSBLOCK) { | 
 | 249 | 		args.type = XFS_ALLOCTYPE_NEAR_BNO; | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 250 | 		args.agbno = be32_to_cpu(agi->agi_root); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | 		args.fsbno = XFS_AGB_TO_FSB(args.mp, | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 252 | 				be32_to_cpu(agi->agi_seqno), args.agbno); | 
| David Chinner | 75de2a9 | 2008-03-27 18:00:38 +1100 | [diff] [blame] | 253 | 		args.alignment = xfs_ialloc_cluster_alignment(&args); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | 		if ((error = xfs_alloc_vextent(&args))) | 
 | 255 | 			return error; | 
 | 256 | 	} | 
 | 257 |  | 
 | 258 | 	if (args.fsbno == NULLFSBLOCK) { | 
 | 259 | 		*alloc = 0; | 
 | 260 | 		return 0; | 
 | 261 | 	} | 
 | 262 | 	ASSERT(args.len == args.minlen); | 
 | 263 | 	/* | 
 | 264 | 	 * Convert the results. | 
 | 265 | 	 */ | 
 | 266 | 	newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0); | 
 | 267 | 	/* | 
 | 268 | 	 * Loop over the new block(s), filling in the inodes. | 
 | 269 | 	 * For small block sizes, manipulate the inodes in buffers | 
 | 270 | 	 * which are multiples of the blocks size. | 
 | 271 | 	 */ | 
 | 272 | 	if (args.mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(args.mp)) { | 
 | 273 | 		blks_per_cluster = 1; | 
 | 274 | 		nbufs = (int)args.len; | 
 | 275 | 		ninodes = args.mp->m_sb.sb_inopblock; | 
 | 276 | 	} else { | 
 | 277 | 		blks_per_cluster = XFS_INODE_CLUSTER_SIZE(args.mp) / | 
 | 278 | 				   args.mp->m_sb.sb_blocksize; | 
 | 279 | 		nbufs = (int)args.len / blks_per_cluster; | 
 | 280 | 		ninodes = blks_per_cluster * args.mp->m_sb.sb_inopblock; | 
 | 281 | 	} | 
 | 282 | 	/* | 
 | 283 | 	 * Figure out what version number to use in the inodes we create. | 
 | 284 | 	 * If the superblock version has caught up to the one that supports | 
 | 285 | 	 * the new inode format, then use the new inode version.  Otherwise | 
 | 286 | 	 * use the old version so that old kernels will continue to be | 
 | 287 | 	 * able to use the file system. | 
 | 288 | 	 */ | 
| Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 289 | 	if (xfs_sb_version_hasnlink(&args.mp->m_sb)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | 		version = XFS_DINODE_VERSION_2; | 
 | 291 | 	else | 
 | 292 | 		version = XFS_DINODE_VERSION_1; | 
 | 293 |  | 
| David Chinner | 359346a | 2008-04-29 12:53:32 +1000 | [diff] [blame] | 294 | 	/* | 
 | 295 | 	 * Seed the new inode cluster with a random generation number. This | 
 | 296 | 	 * prevents short-term reuse of generation numbers if a chunk is | 
 | 297 | 	 * freed and then immediately reallocated. We use random numbers | 
 | 298 | 	 * rather than a linear progression to prevent the next generation | 
 | 299 | 	 * number from being easily guessable. | 
 | 300 | 	 */ | 
 | 301 | 	gen = random32(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | 	for (j = 0; j < nbufs; j++) { | 
 | 303 | 		/* | 
 | 304 | 		 * Get the block. | 
 | 305 | 		 */ | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 306 | 		d = XFS_AGB_TO_DADDR(args.mp, be32_to_cpu(agi->agi_seqno), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | 				     args.agbno + (j * blks_per_cluster)); | 
 | 308 | 		fbuf = xfs_trans_get_buf(tp, args.mp->m_ddev_targp, d, | 
 | 309 | 					 args.mp->m_bsize * blks_per_cluster, | 
 | 310 | 					 XFS_BUF_LOCK); | 
 | 311 | 		ASSERT(fbuf); | 
 | 312 | 		ASSERT(!XFS_BUF_GETERROR(fbuf)); | 
 | 313 | 		/* | 
| Nathan Scott | f30a121 | 2006-03-14 14:07:36 +1100 | [diff] [blame] | 314 | 		 * Set initial values for the inodes in this buffer. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | 		 */ | 
| Nathan Scott | f30a121 | 2006-03-14 14:07:36 +1100 | [diff] [blame] | 316 | 		xfs_biozero(fbuf, 0, ninodes << args.mp->m_sb.sb_inodelog); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | 		for (i = 0; i < ninodes; i++) { | 
 | 318 | 			free = XFS_MAKE_IPTR(args.mp, fbuf, i); | 
| Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 319 | 			free->di_core.di_magic = cpu_to_be16(XFS_DINODE_MAGIC); | 
 | 320 | 			free->di_core.di_version = version; | 
| David Chinner | 359346a | 2008-04-29 12:53:32 +1000 | [diff] [blame] | 321 | 			free->di_core.di_gen = cpu_to_be32(gen); | 
| Christoph Hellwig | 347d1c0 | 2007-08-28 13:57:51 +1000 | [diff] [blame] | 322 | 			free->di_next_unlinked = cpu_to_be32(NULLAGINO); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | 			xfs_ialloc_log_di(tp, fbuf, i, | 
 | 324 | 				XFS_DI_CORE_BITS | XFS_DI_NEXT_UNLINKED); | 
 | 325 | 		} | 
 | 326 | 		xfs_trans_inode_alloc_buf(tp, fbuf); | 
 | 327 | 	} | 
| Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 328 | 	be32_add_cpu(&agi->agi_count, newlen); | 
 | 329 | 	be32_add_cpu(&agi->agi_freecount, newlen); | 
| David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 330 | 	agno = be32_to_cpu(agi->agi_seqno); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | 	down_read(&args.mp->m_peraglock); | 
| David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 332 | 	args.mp->m_perag[agno].pagi_freecount += newlen; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | 	up_read(&args.mp->m_peraglock); | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 334 | 	agi->agi_newino = cpu_to_be32(newino); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | 	/* | 
 | 336 | 	 * Insert records describing the new inode chunk into the btree. | 
 | 337 | 	 */ | 
| David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 338 | 	cur = xfs_btree_init_cursor(args.mp, tp, agbp, agno, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | 			XFS_BTNUM_INO, (xfs_inode_t *)0, 0); | 
 | 340 | 	for (thisino = newino; | 
 | 341 | 	     thisino < newino + newlen; | 
 | 342 | 	     thisino += XFS_INODES_PER_CHUNK) { | 
 | 343 | 		if ((error = xfs_inobt_lookup_eq(cur, thisino, | 
 | 344 | 				XFS_INODES_PER_CHUNK, XFS_INOBT_ALL_FREE, &i))) { | 
 | 345 | 			xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | 
 | 346 | 			return error; | 
 | 347 | 		} | 
 | 348 | 		ASSERT(i == 0); | 
 | 349 | 		if ((error = xfs_inobt_insert(cur, &i))) { | 
 | 350 | 			xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | 
 | 351 | 			return error; | 
 | 352 | 		} | 
 | 353 | 		ASSERT(i == 1); | 
 | 354 | 	} | 
 | 355 | 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | 
 | 356 | 	/* | 
 | 357 | 	 * Log allocation group header fields | 
 | 358 | 	 */ | 
 | 359 | 	xfs_ialloc_log_agi(tp, agbp, | 
 | 360 | 		XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO); | 
 | 361 | 	/* | 
 | 362 | 	 * Modify/log superblock values for inode count and inode free count. | 
 | 363 | 	 */ | 
 | 364 | 	xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen); | 
 | 365 | 	xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen); | 
 | 366 | 	*alloc = 1; | 
 | 367 | 	return 0; | 
 | 368 | } | 
 | 369 |  | 
| David Chinner | 7989cb8 | 2007-02-10 18:34:56 +1100 | [diff] [blame] | 370 | STATIC_INLINE xfs_agnumber_t | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | xfs_ialloc_next_ag( | 
 | 372 | 	xfs_mount_t	*mp) | 
 | 373 | { | 
 | 374 | 	xfs_agnumber_t	agno; | 
 | 375 |  | 
 | 376 | 	spin_lock(&mp->m_agirotor_lock); | 
 | 377 | 	agno = mp->m_agirotor; | 
 | 378 | 	if (++mp->m_agirotor == mp->m_maxagi) | 
 | 379 | 		mp->m_agirotor = 0; | 
 | 380 | 	spin_unlock(&mp->m_agirotor_lock); | 
 | 381 |  | 
 | 382 | 	return agno; | 
 | 383 | } | 
 | 384 |  | 
 | 385 | /* | 
 | 386 |  * Select an allocation group to look for a free inode in, based on the parent | 
 | 387 |  * inode and then mode.  Return the allocation group buffer. | 
 | 388 |  */ | 
 | 389 | STATIC xfs_buf_t *			/* allocation group buffer */ | 
 | 390 | xfs_ialloc_ag_select( | 
 | 391 | 	xfs_trans_t	*tp,		/* transaction pointer */ | 
 | 392 | 	xfs_ino_t	parent,		/* parent directory inode number */ | 
 | 393 | 	mode_t		mode,		/* bits set to indicate file type */ | 
 | 394 | 	int		okalloc)	/* ok to allocate more space */ | 
 | 395 | { | 
 | 396 | 	xfs_buf_t	*agbp;		/* allocation group header buffer */ | 
 | 397 | 	xfs_agnumber_t	agcount;	/* number of ag's in the filesystem */ | 
 | 398 | 	xfs_agnumber_t	agno;		/* current ag number */ | 
 | 399 | 	int		flags;		/* alloc buffer locking flags */ | 
 | 400 | 	xfs_extlen_t	ineed;		/* blocks needed for inode allocation */ | 
 | 401 | 	xfs_extlen_t	longest = 0;	/* longest extent available */ | 
 | 402 | 	xfs_mount_t	*mp;		/* mount point structure */ | 
 | 403 | 	int		needspace;	/* file mode implies space allocated */ | 
 | 404 | 	xfs_perag_t	*pag;		/* per allocation group data */ | 
 | 405 | 	xfs_agnumber_t	pagno;		/* parent (starting) ag number */ | 
 | 406 |  | 
 | 407 | 	/* | 
 | 408 | 	 * Files of these types need at least one block if length > 0 | 
 | 409 | 	 * (and they won't fit in the inode, but that's hard to figure out). | 
 | 410 | 	 */ | 
 | 411 | 	needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode); | 
 | 412 | 	mp = tp->t_mountp; | 
 | 413 | 	agcount = mp->m_maxagi; | 
 | 414 | 	if (S_ISDIR(mode)) | 
 | 415 | 		pagno = xfs_ialloc_next_ag(mp); | 
 | 416 | 	else { | 
 | 417 | 		pagno = XFS_INO_TO_AGNO(mp, parent); | 
 | 418 | 		if (pagno >= agcount) | 
 | 419 | 			pagno = 0; | 
 | 420 | 	} | 
 | 421 | 	ASSERT(pagno < agcount); | 
 | 422 | 	/* | 
 | 423 | 	 * Loop through allocation groups, looking for one with a little | 
 | 424 | 	 * free space in it.  Note we don't look for free inodes, exactly. | 
 | 425 | 	 * Instead, we include whether there is a need to allocate inodes | 
 | 426 | 	 * to mean that blocks must be allocated for them, | 
 | 427 | 	 * if none are currently free. | 
 | 428 | 	 */ | 
 | 429 | 	agno = pagno; | 
 | 430 | 	flags = XFS_ALLOC_FLAG_TRYLOCK; | 
 | 431 | 	down_read(&mp->m_peraglock); | 
 | 432 | 	for (;;) { | 
 | 433 | 		pag = &mp->m_perag[agno]; | 
 | 434 | 		if (!pag->pagi_init) { | 
 | 435 | 			if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) { | 
 | 436 | 				agbp = NULL; | 
 | 437 | 				goto nextag; | 
 | 438 | 			} | 
 | 439 | 		} else | 
 | 440 | 			agbp = NULL; | 
 | 441 |  | 
 | 442 | 		if (!pag->pagi_inodeok) { | 
 | 443 | 			xfs_ialloc_next_ag(mp); | 
 | 444 | 			goto unlock_nextag; | 
 | 445 | 		} | 
 | 446 |  | 
 | 447 | 		/* | 
 | 448 | 		 * Is there enough free space for the file plus a block | 
 | 449 | 		 * of inodes (if we need to allocate some)? | 
 | 450 | 		 */ | 
 | 451 | 		ineed = pag->pagi_freecount ? 0 : XFS_IALLOC_BLOCKS(mp); | 
 | 452 | 		if (ineed && !pag->pagf_init) { | 
 | 453 | 			if (agbp == NULL && | 
 | 454 | 			    xfs_ialloc_read_agi(mp, tp, agno, &agbp)) { | 
 | 455 | 				agbp = NULL; | 
 | 456 | 				goto nextag; | 
 | 457 | 			} | 
 | 458 | 			(void)xfs_alloc_pagf_init(mp, tp, agno, flags); | 
 | 459 | 		} | 
 | 460 | 		if (!ineed || pag->pagf_init) { | 
 | 461 | 			if (ineed && !(longest = pag->pagf_longest)) | 
 | 462 | 				longest = pag->pagf_flcount > 0; | 
 | 463 | 			if (!ineed || | 
 | 464 | 			    (pag->pagf_freeblks >= needspace + ineed && | 
 | 465 | 			     longest >= ineed && | 
 | 466 | 			     okalloc)) { | 
 | 467 | 				if (agbp == NULL && | 
 | 468 | 				    xfs_ialloc_read_agi(mp, tp, agno, &agbp)) { | 
 | 469 | 					agbp = NULL; | 
 | 470 | 					goto nextag; | 
 | 471 | 				} | 
 | 472 | 				up_read(&mp->m_peraglock); | 
 | 473 | 				return agbp; | 
 | 474 | 			} | 
 | 475 | 		} | 
 | 476 | unlock_nextag: | 
 | 477 | 		if (agbp) | 
 | 478 | 			xfs_trans_brelse(tp, agbp); | 
 | 479 | nextag: | 
 | 480 | 		/* | 
 | 481 | 		 * No point in iterating over the rest, if we're shutting | 
 | 482 | 		 * down. | 
 | 483 | 		 */ | 
 | 484 | 		if (XFS_FORCED_SHUTDOWN(mp)) { | 
 | 485 | 			up_read(&mp->m_peraglock); | 
| Nathan Scott | 1121b21 | 2006-09-28 10:58:40 +1000 | [diff] [blame] | 486 | 			return NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | 		} | 
 | 488 | 		agno++; | 
 | 489 | 		if (agno >= agcount) | 
 | 490 | 			agno = 0; | 
 | 491 | 		if (agno == pagno) { | 
 | 492 | 			if (flags == 0) { | 
 | 493 | 				up_read(&mp->m_peraglock); | 
| Nathan Scott | 1121b21 | 2006-09-28 10:58:40 +1000 | [diff] [blame] | 494 | 				return NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | 			} | 
 | 496 | 			flags = 0; | 
 | 497 | 		} | 
 | 498 | 	} | 
 | 499 | } | 
 | 500 |  | 
 | 501 | /* | 
 | 502 |  * Visible inode allocation functions. | 
 | 503 |  */ | 
 | 504 |  | 
 | 505 | /* | 
 | 506 |  * Allocate an inode on disk. | 
 | 507 |  * Mode is used to tell whether the new inode will need space, and whether | 
 | 508 |  * it is a directory. | 
 | 509 |  * | 
 | 510 |  * The arguments IO_agbp and alloc_done are defined to work within | 
 | 511 |  * the constraint of one allocation per transaction. | 
 | 512 |  * xfs_dialloc() is designed to be called twice if it has to do an | 
 | 513 |  * allocation to make more free inodes.  On the first call, | 
 | 514 |  * IO_agbp should be set to NULL. If an inode is available, | 
 | 515 |  * i.e., xfs_dialloc() did not need to do an allocation, an inode | 
 | 516 |  * number is returned.  In this case, IO_agbp would be set to the | 
 | 517 |  * current ag_buf and alloc_done set to false. | 
 | 518 |  * If an allocation needed to be done, xfs_dialloc would return | 
 | 519 |  * the current ag_buf in IO_agbp and set alloc_done to true. | 
 | 520 |  * The caller should then commit the current transaction, allocate a new | 
 | 521 |  * transaction, and call xfs_dialloc() again, passing in the previous | 
 | 522 |  * value of IO_agbp.  IO_agbp should be held across the transactions. | 
 | 523 |  * Since the agbp is locked across the two calls, the second call is | 
 | 524 |  * guaranteed to have a free inode available. | 
 | 525 |  * | 
 | 526 |  * Once we successfully pick an inode its number is returned and the | 
 | 527 |  * on-disk data structures are updated.  The inode itself is not read | 
 | 528 |  * in, since doing so would break ordering constraints with xfs_reclaim. | 
 | 529 |  */ | 
 | 530 | int | 
 | 531 | xfs_dialloc( | 
 | 532 | 	xfs_trans_t	*tp,		/* transaction pointer */ | 
 | 533 | 	xfs_ino_t	parent,		/* parent inode (directory) */ | 
 | 534 | 	mode_t		mode,		/* mode bits for new inode */ | 
 | 535 | 	int		okalloc,	/* ok to allocate more space */ | 
 | 536 | 	xfs_buf_t	**IO_agbp,	/* in/out ag header's buffer */ | 
 | 537 | 	boolean_t	*alloc_done,	/* true if we needed to replenish | 
 | 538 | 					   inode freelist */ | 
 | 539 | 	xfs_ino_t	*inop)		/* inode number allocated */ | 
 | 540 | { | 
 | 541 | 	xfs_agnumber_t	agcount;	/* number of allocation groups */ | 
 | 542 | 	xfs_buf_t	*agbp;		/* allocation group header's buffer */ | 
 | 543 | 	xfs_agnumber_t	agno;		/* allocation group number */ | 
 | 544 | 	xfs_agi_t	*agi;		/* allocation group header structure */ | 
 | 545 | 	xfs_btree_cur_t	*cur;		/* inode allocation btree cursor */ | 
 | 546 | 	int		error;		/* error return value */ | 
 | 547 | 	int		i;		/* result code */ | 
 | 548 | 	int		ialloced;	/* inode allocation status */ | 
 | 549 | 	int		noroom = 0;	/* no space for inode blk allocation */ | 
 | 550 | 	xfs_ino_t	ino;		/* fs-relative inode to be returned */ | 
 | 551 | 	/* REFERENCED */ | 
 | 552 | 	int		j;		/* result code */ | 
 | 553 | 	xfs_mount_t	*mp;		/* file system mount structure */ | 
 | 554 | 	int		offset;		/* index of inode in chunk */ | 
 | 555 | 	xfs_agino_t	pagino;		/* parent's a.g. relative inode # */ | 
 | 556 | 	xfs_agnumber_t	pagno;		/* parent's allocation group number */ | 
| Christoph Hellwig | 61a2584 | 2006-09-28 10:57:04 +1000 | [diff] [blame] | 557 | 	xfs_inobt_rec_incore_t rec;	/* inode allocation record */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | 	xfs_agnumber_t	tagno;		/* testing allocation group number */ | 
 | 559 | 	xfs_btree_cur_t	*tcur;		/* temp cursor */ | 
| Christoph Hellwig | 61a2584 | 2006-09-28 10:57:04 +1000 | [diff] [blame] | 560 | 	xfs_inobt_rec_incore_t trec;	/* temp inode allocation record */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 |  | 
 | 562 |  | 
 | 563 | 	if (*IO_agbp == NULL) { | 
 | 564 | 		/* | 
 | 565 | 		 * We do not have an agbp, so select an initial allocation | 
 | 566 | 		 * group for inode allocation. | 
 | 567 | 		 */ | 
 | 568 | 		agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc); | 
 | 569 | 		/* | 
 | 570 | 		 * Couldn't find an allocation group satisfying the | 
 | 571 | 		 * criteria, give up. | 
 | 572 | 		 */ | 
 | 573 | 		if (!agbp) { | 
 | 574 | 			*inop = NULLFSINO; | 
 | 575 | 			return 0; | 
 | 576 | 		} | 
 | 577 | 		agi = XFS_BUF_TO_AGI(agbp); | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 578 | 		ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | 	} else { | 
 | 580 | 		/* | 
 | 581 | 		 * Continue where we left off before.  In this case, we | 
 | 582 | 		 * know that the allocation group has free inodes. | 
 | 583 | 		 */ | 
 | 584 | 		agbp = *IO_agbp; | 
 | 585 | 		agi = XFS_BUF_TO_AGI(agbp); | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 586 | 		ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC); | 
 | 587 | 		ASSERT(be32_to_cpu(agi->agi_freecount) > 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | 	} | 
 | 589 | 	mp = tp->t_mountp; | 
 | 590 | 	agcount = mp->m_sb.sb_agcount; | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 591 | 	agno = be32_to_cpu(agi->agi_seqno); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | 	tagno = agno; | 
 | 593 | 	pagno = XFS_INO_TO_AGNO(mp, parent); | 
 | 594 | 	pagino = XFS_INO_TO_AGINO(mp, parent); | 
 | 595 |  | 
 | 596 | 	/* | 
 | 597 | 	 * If we have already hit the ceiling of inode blocks then clear | 
 | 598 | 	 * okalloc so we scan all available agi structures for a free | 
 | 599 | 	 * inode. | 
 | 600 | 	 */ | 
 | 601 |  | 
 | 602 | 	if (mp->m_maxicount && | 
 | 603 | 	    mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) { | 
 | 604 | 		noroom = 1; | 
 | 605 | 		okalloc = 0; | 
 | 606 | 	} | 
 | 607 |  | 
 | 608 | 	/* | 
 | 609 | 	 * Loop until we find an allocation group that either has free inodes | 
 | 610 | 	 * or in which we can allocate some inodes.  Iterate through the | 
 | 611 | 	 * allocation groups upward, wrapping at the end. | 
 | 612 | 	 */ | 
 | 613 | 	*alloc_done = B_FALSE; | 
 | 614 | 	while (!agi->agi_freecount) { | 
 | 615 | 		/* | 
 | 616 | 		 * Don't do anything if we're not supposed to allocate | 
 | 617 | 		 * any blocks, just go on to the next ag. | 
 | 618 | 		 */ | 
 | 619 | 		if (okalloc) { | 
 | 620 | 			/* | 
 | 621 | 			 * Try to allocate some new inodes in the allocation | 
 | 622 | 			 * group. | 
 | 623 | 			 */ | 
 | 624 | 			if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) { | 
 | 625 | 				xfs_trans_brelse(tp, agbp); | 
 | 626 | 				if (error == ENOSPC) { | 
 | 627 | 					*inop = NULLFSINO; | 
 | 628 | 					return 0; | 
 | 629 | 				} else | 
 | 630 | 					return error; | 
 | 631 | 			} | 
 | 632 | 			if (ialloced) { | 
 | 633 | 				/* | 
 | 634 | 				 * We successfully allocated some inodes, return | 
 | 635 | 				 * the current context to the caller so that it | 
 | 636 | 				 * can commit the current transaction and call | 
 | 637 | 				 * us again where we left off. | 
 | 638 | 				 */ | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 639 | 				ASSERT(be32_to_cpu(agi->agi_freecount) > 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | 				*alloc_done = B_TRUE; | 
 | 641 | 				*IO_agbp = agbp; | 
 | 642 | 				*inop = NULLFSINO; | 
 | 643 | 				return 0; | 
 | 644 | 			} | 
 | 645 | 		} | 
 | 646 | 		/* | 
 | 647 | 		 * If it failed, give up on this ag. | 
 | 648 | 		 */ | 
 | 649 | 		xfs_trans_brelse(tp, agbp); | 
 | 650 | 		/* | 
 | 651 | 		 * Go on to the next ag: get its ag header. | 
 | 652 | 		 */ | 
 | 653 | nextag: | 
 | 654 | 		if (++tagno == agcount) | 
 | 655 | 			tagno = 0; | 
 | 656 | 		if (tagno == agno) { | 
 | 657 | 			*inop = NULLFSINO; | 
 | 658 | 			return noroom ? ENOSPC : 0; | 
 | 659 | 		} | 
 | 660 | 		down_read(&mp->m_peraglock); | 
 | 661 | 		if (mp->m_perag[tagno].pagi_inodeok == 0) { | 
 | 662 | 			up_read(&mp->m_peraglock); | 
 | 663 | 			goto nextag; | 
 | 664 | 		} | 
 | 665 | 		error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp); | 
 | 666 | 		up_read(&mp->m_peraglock); | 
 | 667 | 		if (error) | 
 | 668 | 			goto nextag; | 
 | 669 | 		agi = XFS_BUF_TO_AGI(agbp); | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 670 | 		ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | 	} | 
 | 672 | 	/* | 
 | 673 | 	 * Here with an allocation group that has a free inode. | 
 | 674 | 	 * Reset agno since we may have chosen a new ag in the | 
 | 675 | 	 * loop above. | 
 | 676 | 	 */ | 
 | 677 | 	agno = tagno; | 
 | 678 | 	*IO_agbp = NULL; | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 679 | 	cur = xfs_btree_init_cursor(mp, tp, agbp, be32_to_cpu(agi->agi_seqno), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | 				    XFS_BTNUM_INO, (xfs_inode_t *)0, 0); | 
 | 681 | 	/* | 
 | 682 | 	 * If pagino is 0 (this is the root inode allocation) use newino. | 
 | 683 | 	 * This must work because we've just allocated some. | 
 | 684 | 	 */ | 
 | 685 | 	if (!pagino) | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 686 | 		pagino = be32_to_cpu(agi->agi_newino); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | #ifdef DEBUG | 
 | 688 | 	if (cur->bc_nlevels == 1) { | 
 | 689 | 		int	freecount = 0; | 
 | 690 |  | 
 | 691 | 		if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i))) | 
 | 692 | 			goto error0; | 
 | 693 | 		XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | 
 | 694 | 		do { | 
 | 695 | 			if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, | 
 | 696 | 					&rec.ir_freecount, &rec.ir_free, &i))) | 
 | 697 | 				goto error0; | 
 | 698 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | 
 | 699 | 			freecount += rec.ir_freecount; | 
 | 700 | 			if ((error = xfs_inobt_increment(cur, 0, &i))) | 
 | 701 | 				goto error0; | 
 | 702 | 		} while (i == 1); | 
 | 703 |  | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 704 | 		ASSERT(freecount == be32_to_cpu(agi->agi_freecount) || | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | 		       XFS_FORCED_SHUTDOWN(mp)); | 
 | 706 | 	} | 
 | 707 | #endif | 
 | 708 | 	/* | 
 | 709 | 	 * If in the same a.g. as the parent, try to get near the parent. | 
 | 710 | 	 */ | 
 | 711 | 	if (pagno == agno) { | 
 | 712 | 		if ((error = xfs_inobt_lookup_le(cur, pagino, 0, 0, &i))) | 
 | 713 | 			goto error0; | 
 | 714 | 		if (i != 0 && | 
 | 715 | 		    (error = xfs_inobt_get_rec(cur, &rec.ir_startino, | 
 | 716 | 			    &rec.ir_freecount, &rec.ir_free, &j)) == 0 && | 
 | 717 | 		    j == 1 && | 
 | 718 | 		    rec.ir_freecount > 0) { | 
 | 719 | 			/* | 
 | 720 | 			 * Found a free inode in the same chunk | 
 | 721 | 			 * as parent, done. | 
 | 722 | 			 */ | 
 | 723 | 		} | 
 | 724 | 		/* | 
 | 725 | 		 * In the same a.g. as parent, but parent's chunk is full. | 
 | 726 | 		 */ | 
 | 727 | 		else { | 
 | 728 | 			int	doneleft;	/* done, to the left */ | 
 | 729 | 			int	doneright;	/* done, to the right */ | 
 | 730 |  | 
 | 731 | 			if (error) | 
 | 732 | 				goto error0; | 
 | 733 | 			ASSERT(i == 1); | 
 | 734 | 			ASSERT(j == 1); | 
 | 735 | 			/* | 
 | 736 | 			 * Duplicate the cursor, search left & right | 
 | 737 | 			 * simultaneously. | 
 | 738 | 			 */ | 
 | 739 | 			if ((error = xfs_btree_dup_cursor(cur, &tcur))) | 
 | 740 | 				goto error0; | 
 | 741 | 			/* | 
 | 742 | 			 * Search left with tcur, back up 1 record. | 
 | 743 | 			 */ | 
 | 744 | 			if ((error = xfs_inobt_decrement(tcur, 0, &i))) | 
 | 745 | 				goto error1; | 
 | 746 | 			doneleft = !i; | 
 | 747 | 			if (!doneleft) { | 
 | 748 | 				if ((error = xfs_inobt_get_rec(tcur, | 
 | 749 | 						&trec.ir_startino, | 
 | 750 | 						&trec.ir_freecount, | 
 | 751 | 						&trec.ir_free, &i))) | 
 | 752 | 					goto error1; | 
 | 753 | 				XFS_WANT_CORRUPTED_GOTO(i == 1, error1); | 
 | 754 | 			} | 
 | 755 | 			/* | 
 | 756 | 			 * Search right with cur, go forward 1 record. | 
 | 757 | 			 */ | 
 | 758 | 			if ((error = xfs_inobt_increment(cur, 0, &i))) | 
 | 759 | 				goto error1; | 
 | 760 | 			doneright = !i; | 
 | 761 | 			if (!doneright) { | 
 | 762 | 				if ((error = xfs_inobt_get_rec(cur, | 
 | 763 | 						&rec.ir_startino, | 
 | 764 | 						&rec.ir_freecount, | 
 | 765 | 						&rec.ir_free, &i))) | 
 | 766 | 					goto error1; | 
 | 767 | 				XFS_WANT_CORRUPTED_GOTO(i == 1, error1); | 
 | 768 | 			} | 
 | 769 | 			/* | 
 | 770 | 			 * Loop until we find the closest inode chunk | 
 | 771 | 			 * with a free one. | 
 | 772 | 			 */ | 
 | 773 | 			while (!doneleft || !doneright) { | 
 | 774 | 				int	useleft;  /* using left inode | 
 | 775 | 						     chunk this time */ | 
 | 776 |  | 
 | 777 | 				/* | 
 | 778 | 				 * Figure out which block is closer, | 
 | 779 | 				 * if both are valid. | 
 | 780 | 				 */ | 
 | 781 | 				if (!doneleft && !doneright) | 
 | 782 | 					useleft = | 
 | 783 | 						pagino - | 
 | 784 | 						(trec.ir_startino + | 
 | 785 | 						 XFS_INODES_PER_CHUNK - 1) < | 
 | 786 | 						 rec.ir_startino - pagino; | 
 | 787 | 				else | 
 | 788 | 					useleft = !doneleft; | 
 | 789 | 				/* | 
 | 790 | 				 * If checking the left, does it have | 
 | 791 | 				 * free inodes? | 
 | 792 | 				 */ | 
 | 793 | 				if (useleft && trec.ir_freecount) { | 
 | 794 | 					/* | 
 | 795 | 					 * Yes, set it up as the chunk to use. | 
 | 796 | 					 */ | 
 | 797 | 					rec = trec; | 
 | 798 | 					xfs_btree_del_cursor(cur, | 
 | 799 | 						XFS_BTREE_NOERROR); | 
 | 800 | 					cur = tcur; | 
 | 801 | 					break; | 
 | 802 | 				} | 
 | 803 | 				/* | 
 | 804 | 				 * If checking the right, does it have | 
 | 805 | 				 * free inodes? | 
 | 806 | 				 */ | 
 | 807 | 				if (!useleft && rec.ir_freecount) { | 
 | 808 | 					/* | 
 | 809 | 					 * Yes, it's already set up. | 
 | 810 | 					 */ | 
 | 811 | 					xfs_btree_del_cursor(tcur, | 
 | 812 | 						XFS_BTREE_NOERROR); | 
 | 813 | 					break; | 
 | 814 | 				} | 
 | 815 | 				/* | 
 | 816 | 				 * If used the left, get another one | 
 | 817 | 				 * further left. | 
 | 818 | 				 */ | 
 | 819 | 				if (useleft) { | 
 | 820 | 					if ((error = xfs_inobt_decrement(tcur, 0, | 
 | 821 | 							&i))) | 
 | 822 | 						goto error1; | 
 | 823 | 					doneleft = !i; | 
 | 824 | 					if (!doneleft) { | 
 | 825 | 						if ((error = xfs_inobt_get_rec( | 
 | 826 | 							    tcur, | 
 | 827 | 							    &trec.ir_startino, | 
 | 828 | 							    &trec.ir_freecount, | 
 | 829 | 							    &trec.ir_free, &i))) | 
 | 830 | 							goto error1; | 
 | 831 | 						XFS_WANT_CORRUPTED_GOTO(i == 1, | 
 | 832 | 							error1); | 
 | 833 | 					} | 
 | 834 | 				} | 
 | 835 | 				/* | 
 | 836 | 				 * If used the right, get another one | 
 | 837 | 				 * further right. | 
 | 838 | 				 */ | 
 | 839 | 				else { | 
 | 840 | 					if ((error = xfs_inobt_increment(cur, 0, | 
 | 841 | 							&i))) | 
 | 842 | 						goto error1; | 
 | 843 | 					doneright = !i; | 
 | 844 | 					if (!doneright) { | 
 | 845 | 						if ((error = xfs_inobt_get_rec( | 
 | 846 | 							    cur, | 
 | 847 | 							    &rec.ir_startino, | 
 | 848 | 							    &rec.ir_freecount, | 
 | 849 | 							    &rec.ir_free, &i))) | 
 | 850 | 							goto error1; | 
 | 851 | 						XFS_WANT_CORRUPTED_GOTO(i == 1, | 
 | 852 | 							error1); | 
 | 853 | 					} | 
 | 854 | 				} | 
 | 855 | 			} | 
 | 856 | 			ASSERT(!doneleft || !doneright); | 
 | 857 | 		} | 
 | 858 | 	} | 
 | 859 | 	/* | 
 | 860 | 	 * In a different a.g. from the parent. | 
 | 861 | 	 * See if the most recently allocated block has any free. | 
 | 862 | 	 */ | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 863 | 	else if (be32_to_cpu(agi->agi_newino) != NULLAGINO) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | 		if ((error = xfs_inobt_lookup_eq(cur, | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 865 | 				be32_to_cpu(agi->agi_newino), 0, 0, &i))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | 			goto error0; | 
 | 867 | 		if (i == 1 && | 
 | 868 | 		    (error = xfs_inobt_get_rec(cur, &rec.ir_startino, | 
 | 869 | 			    &rec.ir_freecount, &rec.ir_free, &j)) == 0 && | 
 | 870 | 		    j == 1 && | 
 | 871 | 		    rec.ir_freecount > 0) { | 
 | 872 | 			/* | 
 | 873 | 			 * The last chunk allocated in the group still has | 
 | 874 | 			 * a free inode. | 
 | 875 | 			 */ | 
 | 876 | 		} | 
 | 877 | 		/* | 
 | 878 | 		 * None left in the last group, search the whole a.g. | 
 | 879 | 		 */ | 
 | 880 | 		else { | 
 | 881 | 			if (error) | 
 | 882 | 				goto error0; | 
 | 883 | 			if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i))) | 
 | 884 | 				goto error0; | 
 | 885 | 			ASSERT(i == 1); | 
 | 886 | 			for (;;) { | 
 | 887 | 				if ((error = xfs_inobt_get_rec(cur, | 
 | 888 | 						&rec.ir_startino, | 
 | 889 | 						&rec.ir_freecount, &rec.ir_free, | 
 | 890 | 						&i))) | 
 | 891 | 					goto error0; | 
 | 892 | 				XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | 
 | 893 | 				if (rec.ir_freecount > 0) | 
 | 894 | 					break; | 
 | 895 | 				if ((error = xfs_inobt_increment(cur, 0, &i))) | 
 | 896 | 					goto error0; | 
 | 897 | 				XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | 
 | 898 | 			} | 
 | 899 | 		} | 
 | 900 | 	} | 
 | 901 | 	offset = XFS_IALLOC_FIND_FREE(&rec.ir_free); | 
 | 902 | 	ASSERT(offset >= 0); | 
 | 903 | 	ASSERT(offset < XFS_INODES_PER_CHUNK); | 
 | 904 | 	ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) % | 
 | 905 | 				   XFS_INODES_PER_CHUNK) == 0); | 
 | 906 | 	ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset); | 
 | 907 | 	XFS_INOBT_CLR_FREE(&rec, offset); | 
 | 908 | 	rec.ir_freecount--; | 
 | 909 | 	if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, | 
 | 910 | 			rec.ir_free))) | 
 | 911 | 		goto error0; | 
| Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 912 | 	be32_add_cpu(&agi->agi_freecount, -1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | 	xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); | 
 | 914 | 	down_read(&mp->m_peraglock); | 
 | 915 | 	mp->m_perag[tagno].pagi_freecount--; | 
 | 916 | 	up_read(&mp->m_peraglock); | 
 | 917 | #ifdef DEBUG | 
 | 918 | 	if (cur->bc_nlevels == 1) { | 
 | 919 | 		int	freecount = 0; | 
 | 920 |  | 
 | 921 | 		if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i))) | 
 | 922 | 			goto error0; | 
 | 923 | 		do { | 
 | 924 | 			if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, | 
 | 925 | 					&rec.ir_freecount, &rec.ir_free, &i))) | 
 | 926 | 				goto error0; | 
 | 927 | 			XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | 
 | 928 | 			freecount += rec.ir_freecount; | 
 | 929 | 			if ((error = xfs_inobt_increment(cur, 0, &i))) | 
 | 930 | 				goto error0; | 
 | 931 | 		} while (i == 1); | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 932 | 		ASSERT(freecount == be32_to_cpu(agi->agi_freecount) || | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | 		       XFS_FORCED_SHUTDOWN(mp)); | 
 | 934 | 	} | 
 | 935 | #endif | 
 | 936 | 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | 
 | 937 | 	xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1); | 
 | 938 | 	*inop = ino; | 
 | 939 | 	return 0; | 
 | 940 | error1: | 
 | 941 | 	xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR); | 
 | 942 | error0: | 
 | 943 | 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | 
 | 944 | 	return error; | 
 | 945 | } | 
 | 946 |  | 
 | 947 | /* | 
 | 948 |  * Free disk inode.  Carefully avoids touching the incore inode, all | 
 | 949 |  * manipulations incore are the caller's responsibility. | 
 | 950 |  * The on-disk inode is not changed by this operation, only the | 
 | 951 |  * btree (free inode mask) is changed. | 
 | 952 |  */ | 
 | 953 | int | 
 | 954 | xfs_difree( | 
 | 955 | 	xfs_trans_t	*tp,		/* transaction pointer */ | 
 | 956 | 	xfs_ino_t	inode,		/* inode to be freed */ | 
 | 957 | 	xfs_bmap_free_t	*flist,		/* extents to free */ | 
 | 958 | 	int		*delete,	/* set if inode cluster was deleted */ | 
 | 959 | 	xfs_ino_t	*first_ino)	/* first inode in deleted cluster */ | 
 | 960 | { | 
 | 961 | 	/* REFERENCED */ | 
 | 962 | 	xfs_agblock_t	agbno;	/* block number containing inode */ | 
 | 963 | 	xfs_buf_t	*agbp;	/* buffer containing allocation group header */ | 
 | 964 | 	xfs_agino_t	agino;	/* inode number relative to allocation group */ | 
 | 965 | 	xfs_agnumber_t	agno;	/* allocation group number */ | 
 | 966 | 	xfs_agi_t	*agi;	/* allocation group header */ | 
 | 967 | 	xfs_btree_cur_t	*cur;	/* inode btree cursor */ | 
 | 968 | 	int		error;	/* error return value */ | 
 | 969 | 	int		i;	/* result code */ | 
 | 970 | 	int		ilen;	/* inodes in an inode cluster */ | 
 | 971 | 	xfs_mount_t	*mp;	/* mount structure for filesystem */ | 
 | 972 | 	int		off;	/* offset of inode in inode chunk */ | 
| Christoph Hellwig | 61a2584 | 2006-09-28 10:57:04 +1000 | [diff] [blame] | 973 | 	xfs_inobt_rec_incore_t rec;	/* btree record */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 |  | 
 | 975 | 	mp = tp->t_mountp; | 
 | 976 |  | 
 | 977 | 	/* | 
 | 978 | 	 * Break up inode number into its components. | 
 | 979 | 	 */ | 
 | 980 | 	agno = XFS_INO_TO_AGNO(mp, inode); | 
 | 981 | 	if (agno >= mp->m_sb.sb_agcount)  { | 
 | 982 | 		cmn_err(CE_WARN, | 
 | 983 | 			"xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s.  Returning EINVAL.", | 
 | 984 | 			agno, mp->m_sb.sb_agcount, mp->m_fsname); | 
 | 985 | 		ASSERT(0); | 
 | 986 | 		return XFS_ERROR(EINVAL); | 
 | 987 | 	} | 
 | 988 | 	agino = XFS_INO_TO_AGINO(mp, inode); | 
 | 989 | 	if (inode != XFS_AGINO_TO_INO(mp, agno, agino))  { | 
 | 990 | 		cmn_err(CE_WARN, | 
| Christoph Hellwig | da1650a | 2005-11-02 10:21:35 +1100 | [diff] [blame] | 991 | 			"xfs_difree: inode != XFS_AGINO_TO_INO() " | 
 | 992 | 			"(%llu != %llu) on %s.  Returning EINVAL.", | 
 | 993 | 			(unsigned long long)inode, | 
 | 994 | 			(unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino), | 
 | 995 | 			mp->m_fsname); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | 		ASSERT(0); | 
 | 997 | 		return XFS_ERROR(EINVAL); | 
 | 998 | 	} | 
 | 999 | 	agbno = XFS_AGINO_TO_AGBNO(mp, agino); | 
 | 1000 | 	if (agbno >= mp->m_sb.sb_agblocks)  { | 
 | 1001 | 		cmn_err(CE_WARN, | 
 | 1002 | 			"xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s.  Returning EINVAL.", | 
 | 1003 | 			agbno, mp->m_sb.sb_agblocks, mp->m_fsname); | 
 | 1004 | 		ASSERT(0); | 
 | 1005 | 		return XFS_ERROR(EINVAL); | 
 | 1006 | 	} | 
 | 1007 | 	/* | 
 | 1008 | 	 * Get the allocation group header. | 
 | 1009 | 	 */ | 
 | 1010 | 	down_read(&mp->m_peraglock); | 
 | 1011 | 	error = xfs_ialloc_read_agi(mp, tp, agno, &agbp); | 
 | 1012 | 	up_read(&mp->m_peraglock); | 
 | 1013 | 	if (error) { | 
 | 1014 | 		cmn_err(CE_WARN, | 
 | 1015 | 			"xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s.  Returning error.", | 
 | 1016 | 			error, mp->m_fsname); | 
 | 1017 | 		return error; | 
 | 1018 | 	} | 
 | 1019 | 	agi = XFS_BUF_TO_AGI(agbp); | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1020 | 	ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC); | 
 | 1021 | 	ASSERT(agbno < be32_to_cpu(agi->agi_length)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | 	/* | 
 | 1023 | 	 * Initialize the cursor. | 
 | 1024 | 	 */ | 
 | 1025 | 	cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO, | 
 | 1026 | 		(xfs_inode_t *)0, 0); | 
 | 1027 | #ifdef DEBUG | 
 | 1028 | 	if (cur->bc_nlevels == 1) { | 
 | 1029 | 		int freecount = 0; | 
 | 1030 |  | 
 | 1031 | 		if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i))) | 
 | 1032 | 			goto error0; | 
 | 1033 | 		do { | 
 | 1034 | 			if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, | 
 | 1035 | 					&rec.ir_freecount, &rec.ir_free, &i))) | 
 | 1036 | 				goto error0; | 
 | 1037 | 			if (i) { | 
 | 1038 | 				freecount += rec.ir_freecount; | 
 | 1039 | 				if ((error = xfs_inobt_increment(cur, 0, &i))) | 
 | 1040 | 					goto error0; | 
 | 1041 | 			} | 
 | 1042 | 		} while (i == 1); | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1043 | 		ASSERT(freecount == be32_to_cpu(agi->agi_freecount) || | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | 		       XFS_FORCED_SHUTDOWN(mp)); | 
 | 1045 | 	} | 
 | 1046 | #endif | 
 | 1047 | 	/* | 
 | 1048 | 	 * Look for the entry describing this inode. | 
 | 1049 | 	 */ | 
 | 1050 | 	if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) { | 
 | 1051 | 		cmn_err(CE_WARN, | 
 | 1052 | 			"xfs_difree: xfs_inobt_lookup_le returned()  an error %d on %s.  Returning error.", | 
 | 1053 | 			error, mp->m_fsname); | 
 | 1054 | 		goto error0; | 
 | 1055 | 	} | 
 | 1056 | 	XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | 
 | 1057 | 	if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, &rec.ir_freecount, | 
 | 1058 | 			&rec.ir_free, &i))) { | 
 | 1059 | 		cmn_err(CE_WARN, | 
 | 1060 | 			"xfs_difree: xfs_inobt_get_rec()  returned an error %d on %s.  Returning error.", | 
 | 1061 | 			error, mp->m_fsname); | 
 | 1062 | 		goto error0; | 
 | 1063 | 	} | 
 | 1064 | 	XFS_WANT_CORRUPTED_GOTO(i == 1, error0); | 
 | 1065 | 	/* | 
 | 1066 | 	 * Get the offset in the inode chunk. | 
 | 1067 | 	 */ | 
 | 1068 | 	off = agino - rec.ir_startino; | 
 | 1069 | 	ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK); | 
 | 1070 | 	ASSERT(!XFS_INOBT_IS_FREE(&rec, off)); | 
 | 1071 | 	/* | 
 | 1072 | 	 * Mark the inode free & increment the count. | 
 | 1073 | 	 */ | 
 | 1074 | 	XFS_INOBT_SET_FREE(&rec, off); | 
 | 1075 | 	rec.ir_freecount++; | 
 | 1076 |  | 
 | 1077 | 	/* | 
| Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 1078 | 	 * When an inode cluster is free, it becomes eligible for removal | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | 	 */ | 
| Josef Jeff Sipek | 1bd960e | 2008-02-29 13:58:40 +1100 | [diff] [blame] | 1080 | 	if (!(mp->m_flags & XFS_MOUNT_IKEEP) && | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | 	    (rec.ir_freecount == XFS_IALLOC_INODES(mp))) { | 
 | 1082 |  | 
 | 1083 | 		*delete = 1; | 
 | 1084 | 		*first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino); | 
 | 1085 |  | 
 | 1086 | 		/* | 
 | 1087 | 		 * Remove the inode cluster from the AGI B+Tree, adjust the | 
 | 1088 | 		 * AGI and Superblock inode counts, and mark the disk space | 
 | 1089 | 		 * to be freed when the transaction is committed. | 
 | 1090 | 		 */ | 
 | 1091 | 		ilen = XFS_IALLOC_INODES(mp); | 
| Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 1092 | 		be32_add_cpu(&agi->agi_count, -ilen); | 
 | 1093 | 		be32_add_cpu(&agi->agi_freecount, -(ilen - 1)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | 		xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT); | 
 | 1095 | 		down_read(&mp->m_peraglock); | 
 | 1096 | 		mp->m_perag[agno].pagi_freecount -= ilen - 1; | 
 | 1097 | 		up_read(&mp->m_peraglock); | 
 | 1098 | 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen); | 
 | 1099 | 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1)); | 
 | 1100 |  | 
 | 1101 | 		if ((error = xfs_inobt_delete(cur, &i))) { | 
 | 1102 | 			cmn_err(CE_WARN, "xfs_difree: xfs_inobt_delete returned an error %d on %s.\n", | 
 | 1103 | 				error, mp->m_fsname); | 
 | 1104 | 			goto error0; | 
 | 1105 | 		} | 
 | 1106 |  | 
 | 1107 | 		xfs_bmap_add_free(XFS_AGB_TO_FSB(mp, | 
 | 1108 | 				agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)), | 
 | 1109 | 				XFS_IALLOC_BLOCKS(mp), flist, mp); | 
 | 1110 | 	} else { | 
 | 1111 | 		*delete = 0; | 
 | 1112 |  | 
 | 1113 | 		if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, rec.ir_free))) { | 
 | 1114 | 			cmn_err(CE_WARN, | 
 | 1115 | 				"xfs_difree: xfs_inobt_update()  returned an error %d on %s.  Returning error.", | 
 | 1116 | 				error, mp->m_fsname); | 
 | 1117 | 			goto error0; | 
 | 1118 | 		} | 
 | 1119 | 		/*  | 
 | 1120 | 		 * Change the inode free counts and log the ag/sb changes. | 
 | 1121 | 		 */ | 
| Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 1122 | 		be32_add_cpu(&agi->agi_freecount, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | 		xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT); | 
 | 1124 | 		down_read(&mp->m_peraglock); | 
 | 1125 | 		mp->m_perag[agno].pagi_freecount++; | 
 | 1126 | 		up_read(&mp->m_peraglock); | 
 | 1127 | 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1); | 
 | 1128 | 	} | 
 | 1129 |  | 
 | 1130 | #ifdef DEBUG | 
 | 1131 | 	if (cur->bc_nlevels == 1) { | 
 | 1132 | 		int freecount = 0; | 
 | 1133 |  | 
 | 1134 | 		if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i))) | 
 | 1135 | 			goto error0; | 
 | 1136 | 		do { | 
 | 1137 | 			if ((error = xfs_inobt_get_rec(cur, | 
 | 1138 | 					&rec.ir_startino, | 
 | 1139 | 					&rec.ir_freecount, | 
 | 1140 | 					&rec.ir_free, &i))) | 
 | 1141 | 				goto error0; | 
 | 1142 | 			if (i) { | 
 | 1143 | 				freecount += rec.ir_freecount; | 
 | 1144 | 				if ((error = xfs_inobt_increment(cur, 0, &i))) | 
 | 1145 | 					goto error0; | 
 | 1146 | 			} | 
 | 1147 | 		} while (i == 1); | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1148 | 		ASSERT(freecount == be32_to_cpu(agi->agi_freecount) || | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | 		       XFS_FORCED_SHUTDOWN(mp)); | 
 | 1150 | 	} | 
 | 1151 | #endif | 
 | 1152 | 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | 
 | 1153 | 	return 0; | 
 | 1154 |  | 
 | 1155 | error0: | 
 | 1156 | 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | 
 | 1157 | 	return error; | 
 | 1158 | } | 
 | 1159 |  | 
 | 1160 | /* | 
 | 1161 |  * Return the location of the inode in bno/off, for mapping it into a buffer. | 
 | 1162 |  */ | 
 | 1163 | /*ARGSUSED*/ | 
 | 1164 | int | 
 | 1165 | xfs_dilocate( | 
 | 1166 | 	xfs_mount_t	*mp,	/* file system mount structure */ | 
 | 1167 | 	xfs_trans_t	*tp,	/* transaction pointer */ | 
 | 1168 | 	xfs_ino_t	ino,	/* inode to locate */ | 
 | 1169 | 	xfs_fsblock_t	*bno,	/* output: block containing inode */ | 
 | 1170 | 	int		*len,	/* output: num blocks in inode cluster */ | 
 | 1171 | 	int		*off,	/* output: index in block of inode */ | 
 | 1172 | 	uint		flags)	/* flags concerning inode lookup */ | 
 | 1173 | { | 
 | 1174 | 	xfs_agblock_t	agbno;	/* block number of inode in the alloc group */ | 
 | 1175 | 	xfs_buf_t	*agbp;	/* agi buffer */ | 
 | 1176 | 	xfs_agino_t	agino;	/* inode number within alloc group */ | 
 | 1177 | 	xfs_agnumber_t	agno;	/* allocation group number */ | 
 | 1178 | 	int		blks_per_cluster; /* num blocks per inode cluster */ | 
 | 1179 | 	xfs_agblock_t	chunk_agbno;	/* first block in inode chunk */ | 
 | 1180 | 	xfs_agino_t	chunk_agino;	/* first agino in inode chunk */ | 
 | 1181 | 	__int32_t	chunk_cnt;	/* count of free inodes in chunk */ | 
 | 1182 | 	xfs_inofree_t	chunk_free;	/* mask of free inodes in chunk */ | 
 | 1183 | 	xfs_agblock_t	cluster_agbno;	/* first block in inode cluster */ | 
 | 1184 | 	xfs_btree_cur_t	*cur;	/* inode btree cursor */ | 
 | 1185 | 	int		error;	/* error code */ | 
 | 1186 | 	int		i;	/* temp state */ | 
 | 1187 | 	int		offset;	/* index of inode in its buffer */ | 
 | 1188 | 	int		offset_agbno;	/* blks from chunk start to inode */ | 
 | 1189 |  | 
 | 1190 | 	ASSERT(ino != NULLFSINO); | 
 | 1191 | 	/* | 
 | 1192 | 	 * Split up the inode number into its parts. | 
 | 1193 | 	 */ | 
 | 1194 | 	agno = XFS_INO_TO_AGNO(mp, ino); | 
 | 1195 | 	agino = XFS_INO_TO_AGINO(mp, ino); | 
 | 1196 | 	agbno = XFS_AGINO_TO_AGBNO(mp, agino); | 
 | 1197 | 	if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks || | 
 | 1198 | 	    ino != XFS_AGINO_TO_INO(mp, agno, agino)) { | 
 | 1199 | #ifdef DEBUG | 
| Nathan Scott | 4d1a2ed | 2006-06-09 17:12:28 +1000 | [diff] [blame] | 1200 | 		/* no diagnostics for bulkstat, ino comes from userspace */ | 
 | 1201 | 		if (flags & XFS_IMAP_BULKSTAT) | 
 | 1202 | 			return XFS_ERROR(EINVAL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | 		if (agno >= mp->m_sb.sb_agcount) { | 
 | 1204 | 			xfs_fs_cmn_err(CE_ALERT, mp, | 
 | 1205 | 					"xfs_dilocate: agno (%d) >= " | 
 | 1206 | 					"mp->m_sb.sb_agcount (%d)", | 
 | 1207 | 					agno,  mp->m_sb.sb_agcount); | 
 | 1208 | 		} | 
 | 1209 | 		if (agbno >= mp->m_sb.sb_agblocks) { | 
 | 1210 | 			xfs_fs_cmn_err(CE_ALERT, mp, | 
 | 1211 | 					"xfs_dilocate: agbno (0x%llx) >= " | 
 | 1212 | 					"mp->m_sb.sb_agblocks (0x%lx)", | 
 | 1213 | 					(unsigned long long) agbno, | 
 | 1214 | 					(unsigned long) mp->m_sb.sb_agblocks); | 
 | 1215 | 		} | 
 | 1216 | 		if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) { | 
 | 1217 | 			xfs_fs_cmn_err(CE_ALERT, mp, | 
 | 1218 | 					"xfs_dilocate: ino (0x%llx) != " | 
 | 1219 | 					"XFS_AGINO_TO_INO(mp, agno, agino) " | 
 | 1220 | 					"(0x%llx)", | 
 | 1221 | 					ino, XFS_AGINO_TO_INO(mp, agno, agino)); | 
 | 1222 | 		} | 
| Nathan Scott | 745b1f47 | 2006-09-28 11:02:23 +1000 | [diff] [blame] | 1223 | 		xfs_stack_trace(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | #endif /* DEBUG */ | 
 | 1225 | 		return XFS_ERROR(EINVAL); | 
 | 1226 | 	} | 
 | 1227 | 	if ((mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) || | 
 | 1228 | 	    !(flags & XFS_IMAP_LOOKUP)) { | 
 | 1229 | 		offset = XFS_INO_TO_OFFSET(mp, ino); | 
 | 1230 | 		ASSERT(offset < mp->m_sb.sb_inopblock); | 
 | 1231 | 		*bno = XFS_AGB_TO_FSB(mp, agno, agbno); | 
 | 1232 | 		*off = offset; | 
 | 1233 | 		*len = 1; | 
 | 1234 | 		return 0; | 
 | 1235 | 	} | 
 | 1236 | 	blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog; | 
 | 1237 | 	if (*bno != NULLFSBLOCK) { | 
 | 1238 | 		offset = XFS_INO_TO_OFFSET(mp, ino); | 
 | 1239 | 		ASSERT(offset < mp->m_sb.sb_inopblock); | 
 | 1240 | 		cluster_agbno = XFS_FSB_TO_AGBNO(mp, *bno); | 
 | 1241 | 		*off = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) + | 
 | 1242 | 			offset; | 
 | 1243 | 		*len = blks_per_cluster; | 
 | 1244 | 		return 0; | 
 | 1245 | 	} | 
 | 1246 | 	if (mp->m_inoalign_mask) { | 
 | 1247 | 		offset_agbno = agbno & mp->m_inoalign_mask; | 
 | 1248 | 		chunk_agbno = agbno - offset_agbno; | 
 | 1249 | 	} else { | 
 | 1250 | 		down_read(&mp->m_peraglock); | 
 | 1251 | 		error = xfs_ialloc_read_agi(mp, tp, agno, &agbp); | 
 | 1252 | 		up_read(&mp->m_peraglock); | 
 | 1253 | 		if (error) { | 
 | 1254 | #ifdef DEBUG | 
 | 1255 | 			xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: " | 
 | 1256 | 					"xfs_ialloc_read_agi() returned " | 
 | 1257 | 					"error %d, agno %d", | 
 | 1258 | 					error, agno); | 
 | 1259 | #endif /* DEBUG */ | 
 | 1260 | 			return error; | 
 | 1261 | 		} | 
 | 1262 | 		cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO, | 
 | 1263 | 			(xfs_inode_t *)0, 0); | 
 | 1264 | 		if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) { | 
 | 1265 | #ifdef DEBUG | 
 | 1266 | 			xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: " | 
 | 1267 | 					"xfs_inobt_lookup_le() failed"); | 
 | 1268 | #endif /* DEBUG */ | 
 | 1269 | 			goto error0; | 
 | 1270 | 		} | 
 | 1271 | 		if ((error = xfs_inobt_get_rec(cur, &chunk_agino, &chunk_cnt, | 
 | 1272 | 				&chunk_free, &i))) { | 
 | 1273 | #ifdef DEBUG | 
 | 1274 | 			xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: " | 
 | 1275 | 					"xfs_inobt_get_rec() failed"); | 
 | 1276 | #endif /* DEBUG */ | 
 | 1277 | 			goto error0; | 
 | 1278 | 		} | 
 | 1279 | 		if (i == 0) { | 
 | 1280 | #ifdef DEBUG | 
 | 1281 | 			xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: " | 
 | 1282 | 					"xfs_inobt_get_rec() failed"); | 
 | 1283 | #endif /* DEBUG */ | 
 | 1284 | 			error = XFS_ERROR(EINVAL); | 
 | 1285 | 		} | 
 | 1286 | 		xfs_trans_brelse(tp, agbp); | 
 | 1287 | 		xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | 
 | 1288 | 		if (error) | 
 | 1289 | 			return error; | 
 | 1290 | 		chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_agino); | 
 | 1291 | 		offset_agbno = agbno - chunk_agbno; | 
 | 1292 | 	} | 
 | 1293 | 	ASSERT(agbno >= chunk_agbno); | 
 | 1294 | 	cluster_agbno = chunk_agbno + | 
 | 1295 | 		((offset_agbno / blks_per_cluster) * blks_per_cluster); | 
 | 1296 | 	offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) + | 
 | 1297 | 		XFS_INO_TO_OFFSET(mp, ino); | 
 | 1298 | 	*bno = XFS_AGB_TO_FSB(mp, agno, cluster_agbno); | 
 | 1299 | 	*off = offset; | 
 | 1300 | 	*len = blks_per_cluster; | 
 | 1301 | 	return 0; | 
 | 1302 | error0: | 
 | 1303 | 	xfs_trans_brelse(tp, agbp); | 
 | 1304 | 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | 
 | 1305 | 	return error; | 
 | 1306 | } | 
 | 1307 |  | 
 | 1308 | /* | 
 | 1309 |  * Compute and fill in value of m_in_maxlevels. | 
 | 1310 |  */ | 
 | 1311 | void | 
 | 1312 | xfs_ialloc_compute_maxlevels( | 
 | 1313 | 	xfs_mount_t	*mp)		/* file system mount structure */ | 
 | 1314 | { | 
 | 1315 | 	int		level; | 
 | 1316 | 	uint		maxblocks; | 
 | 1317 | 	uint		maxleafents; | 
 | 1318 | 	int		minleafrecs; | 
 | 1319 | 	int		minnoderecs; | 
 | 1320 |  | 
 | 1321 | 	maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >> | 
 | 1322 | 		XFS_INODES_PER_CHUNK_LOG; | 
 | 1323 | 	minleafrecs = mp->m_alloc_mnr[0]; | 
 | 1324 | 	minnoderecs = mp->m_alloc_mnr[1]; | 
 | 1325 | 	maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; | 
 | 1326 | 	for (level = 1; maxblocks > 1; level++) | 
 | 1327 | 		maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; | 
 | 1328 | 	mp->m_in_maxlevels = level; | 
 | 1329 | } | 
 | 1330 |  | 
 | 1331 | /* | 
 | 1332 |  * Log specified fields for the ag hdr (inode section) | 
 | 1333 |  */ | 
 | 1334 | void | 
 | 1335 | xfs_ialloc_log_agi( | 
 | 1336 | 	xfs_trans_t	*tp,		/* transaction pointer */ | 
 | 1337 | 	xfs_buf_t	*bp,		/* allocation group header buffer */ | 
 | 1338 | 	int		fields)		/* bitmask of fields to log */ | 
 | 1339 | { | 
 | 1340 | 	int			first;		/* first byte number */ | 
 | 1341 | 	int			last;		/* last byte number */ | 
 | 1342 | 	static const short	offsets[] = {	/* field starting offsets */ | 
 | 1343 | 					/* keep in sync with bit definitions */ | 
 | 1344 | 		offsetof(xfs_agi_t, agi_magicnum), | 
 | 1345 | 		offsetof(xfs_agi_t, agi_versionnum), | 
 | 1346 | 		offsetof(xfs_agi_t, agi_seqno), | 
 | 1347 | 		offsetof(xfs_agi_t, agi_length), | 
 | 1348 | 		offsetof(xfs_agi_t, agi_count), | 
 | 1349 | 		offsetof(xfs_agi_t, agi_root), | 
 | 1350 | 		offsetof(xfs_agi_t, agi_level), | 
 | 1351 | 		offsetof(xfs_agi_t, agi_freecount), | 
 | 1352 | 		offsetof(xfs_agi_t, agi_newino), | 
 | 1353 | 		offsetof(xfs_agi_t, agi_dirino), | 
 | 1354 | 		offsetof(xfs_agi_t, agi_unlinked), | 
 | 1355 | 		sizeof(xfs_agi_t) | 
 | 1356 | 	}; | 
 | 1357 | #ifdef DEBUG | 
 | 1358 | 	xfs_agi_t		*agi;	/* allocation group header */ | 
 | 1359 |  | 
 | 1360 | 	agi = XFS_BUF_TO_AGI(bp); | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1361 | 	ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | #endif | 
 | 1363 | 	/* | 
 | 1364 | 	 * Compute byte offsets for the first and last fields. | 
 | 1365 | 	 */ | 
 | 1366 | 	xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last); | 
 | 1367 | 	/* | 
 | 1368 | 	 * Log the allocation group inode header buffer. | 
 | 1369 | 	 */ | 
 | 1370 | 	xfs_trans_log_buf(tp, bp, first, last); | 
 | 1371 | } | 
 | 1372 |  | 
 | 1373 | /* | 
 | 1374 |  * Read in the allocation group header (inode allocation section) | 
 | 1375 |  */ | 
 | 1376 | int | 
 | 1377 | xfs_ialloc_read_agi( | 
 | 1378 | 	xfs_mount_t	*mp,		/* file system mount structure */ | 
 | 1379 | 	xfs_trans_t	*tp,		/* transaction pointer */ | 
 | 1380 | 	xfs_agnumber_t	agno,		/* allocation group number */ | 
 | 1381 | 	xfs_buf_t	**bpp)		/* allocation group hdr buf */ | 
 | 1382 | { | 
 | 1383 | 	xfs_agi_t	*agi;		/* allocation group header */ | 
 | 1384 | 	int		agi_ok;		/* agi is consistent */ | 
 | 1385 | 	xfs_buf_t	*bp;		/* allocation group hdr buf */ | 
 | 1386 | 	xfs_perag_t	*pag;		/* per allocation group data */ | 
 | 1387 | 	int		error; | 
 | 1388 |  | 
 | 1389 | 	ASSERT(agno != NULLAGNUMBER); | 
 | 1390 | 	error = xfs_trans_read_buf( | 
 | 1391 | 			mp, tp, mp->m_ddev_targp, | 
 | 1392 | 			XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)), | 
 | 1393 | 			XFS_FSS_TO_BB(mp, 1), 0, &bp); | 
 | 1394 | 	if (error) | 
 | 1395 | 		return error; | 
 | 1396 | 	ASSERT(bp && !XFS_BUF_GETERROR(bp)); | 
 | 1397 |  | 
 | 1398 | 	/* | 
 | 1399 | 	 * Validate the magic number of the agi block. | 
 | 1400 | 	 */ | 
 | 1401 | 	agi = XFS_BUF_TO_AGI(bp); | 
 | 1402 | 	agi_ok = | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1403 | 		be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC && | 
 | 1404 | 		XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | 	if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI, | 
 | 1406 | 			XFS_RANDOM_IALLOC_READ_AGI))) { | 
 | 1407 | 		XFS_CORRUPTION_ERROR("xfs_ialloc_read_agi", XFS_ERRLEVEL_LOW, | 
 | 1408 | 				     mp, agi); | 
 | 1409 | 		xfs_trans_brelse(tp, bp); | 
 | 1410 | 		return XFS_ERROR(EFSCORRUPTED); | 
 | 1411 | 	} | 
 | 1412 | 	pag = &mp->m_perag[agno]; | 
 | 1413 | 	if (!pag->pagi_init) { | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1414 | 		pag->pagi_freecount = be32_to_cpu(agi->agi_freecount); | 
| David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 1415 | 		pag->pagi_count = be32_to_cpu(agi->agi_count); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | 		pag->pagi_init = 1; | 
 | 1417 | 	} else { | 
 | 1418 | 		/* | 
 | 1419 | 		 * It's possible for these to be out of sync if | 
 | 1420 | 		 * we are in the middle of a forced shutdown. | 
 | 1421 | 		 */ | 
| Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 1422 | 		ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) || | 
 | 1423 | 			XFS_FORCED_SHUTDOWN(mp)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | 	} | 
 | 1425 |  | 
 | 1426 | #ifdef DEBUG | 
 | 1427 | 	{ | 
 | 1428 | 		int	i; | 
 | 1429 |  | 
 | 1430 | 		for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) | 
 | 1431 | 			ASSERT(agi->agi_unlinked[i]); | 
 | 1432 | 	} | 
 | 1433 | #endif | 
 | 1434 |  | 
 | 1435 | 	XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGI, XFS_AGI_REF); | 
 | 1436 | 	*bpp = bp; | 
 | 1437 | 	return 0; | 
 | 1438 | } | 
| David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 1439 |  | 
 | 1440 | /* | 
 | 1441 |  * Read in the agi to initialise the per-ag data in the mount structure | 
 | 1442 |  */ | 
 | 1443 | int | 
 | 1444 | xfs_ialloc_pagi_init( | 
 | 1445 | 	xfs_mount_t	*mp,		/* file system mount structure */ | 
 | 1446 | 	xfs_trans_t	*tp,		/* transaction pointer */ | 
 | 1447 | 	xfs_agnumber_t	agno)		/* allocation group number */ | 
 | 1448 | { | 
 | 1449 | 	xfs_buf_t	*bp = NULL; | 
 | 1450 | 	int		error; | 
 | 1451 |  | 
 | 1452 | 	error = xfs_ialloc_read_agi(mp, tp, agno, &bp); | 
 | 1453 | 	if (error) | 
 | 1454 | 		return error; | 
 | 1455 | 	if (bp) | 
 | 1456 | 		xfs_trans_brelse(tp, bp); | 
 | 1457 | 	return 0; | 
 | 1458 | } |