| David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (c) 2000-2005 Silicon Graphics, Inc. | 
 | 3 |  * All Rights Reserved. | 
 | 4 |  * | 
 | 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 | 
 | 7 |  * published by the Free Software Foundation. | 
 | 8 |  * | 
 | 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. | 
 | 13 |  * | 
 | 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 | 
 | 17 |  */ | 
 | 18 | #include "xfs.h" | 
 | 19 | #include "xfs_fs.h" | 
 | 20 | #include "xfs_types.h" | 
 | 21 | #include "xfs_bit.h" | 
 | 22 | #include "xfs_log.h" | 
 | 23 | #include "xfs_inum.h" | 
 | 24 | #include "xfs_trans.h" | 
 | 25 | #include "xfs_sb.h" | 
 | 26 | #include "xfs_ag.h" | 
| David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 27 | #include "xfs_mount.h" | 
 | 28 | #include "xfs_bmap_btree.h" | 
| David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 29 | #include "xfs_inode.h" | 
 | 30 | #include "xfs_dinode.h" | 
 | 31 | #include "xfs_error.h" | 
| David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 32 | #include "xfs_filestream.h" | 
 | 33 | #include "xfs_vnodeops.h" | 
| David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 34 | #include "xfs_inode_item.h" | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 35 | #include "xfs_quota.h" | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 36 | #include "xfs_trace.h" | 
| Dave Chinner | 1a387d3 | 2010-08-24 11:46:31 +1000 | [diff] [blame] | 37 | #include "xfs_fsops.h" | 
| David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 38 |  | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 39 | #include <linux/kthread.h> | 
 | 40 | #include <linux/freezer.h> | 
 | 41 |  | 
| Dave Chinner | 5a34d5c | 2009-06-08 15:35:03 +0200 | [diff] [blame] | 42 |  | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 43 | STATIC xfs_inode_t * | 
 | 44 | xfs_inode_ag_lookup( | 
 | 45 | 	struct xfs_mount	*mp, | 
 | 46 | 	struct xfs_perag	*pag, | 
 | 47 | 	uint32_t		*first_index, | 
 | 48 | 	int			tag) | 
 | 49 | { | 
 | 50 | 	int			nr_found; | 
 | 51 | 	struct xfs_inode	*ip; | 
 | 52 |  | 
 | 53 | 	/* | 
 | 54 | 	 * use a gang lookup to find the next inode in the tree | 
 | 55 | 	 * as the tree is sparse and a gang lookup walks to find | 
 | 56 | 	 * the number of objects requested. | 
 | 57 | 	 */ | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 58 | 	if (tag == XFS_ICI_NO_TAG) { | 
 | 59 | 		nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, | 
 | 60 | 				(void **)&ip, *first_index, 1); | 
 | 61 | 	} else { | 
 | 62 | 		nr_found = radix_tree_gang_lookup_tag(&pag->pag_ici_root, | 
 | 63 | 				(void **)&ip, *first_index, 1, tag); | 
 | 64 | 	} | 
 | 65 | 	if (!nr_found) | 
| Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 66 | 		return NULL; | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 67 |  | 
 | 68 | 	/* | 
 | 69 | 	 * Update the index for the next lookup. Catch overflows | 
 | 70 | 	 * into the next AG range which can occur if we have inodes | 
 | 71 | 	 * in the last block of the AG and we are currently | 
 | 72 | 	 * pointing to the last inode. | 
 | 73 | 	 */ | 
 | 74 | 	*first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1); | 
 | 75 | 	if (*first_index < XFS_INO_TO_AGINO(mp, ip->i_ino)) | 
| Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 76 | 		return NULL; | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 77 | 	return ip; | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 78 | } | 
 | 79 |  | 
 | 80 | STATIC int | 
 | 81 | xfs_inode_ag_walk( | 
 | 82 | 	struct xfs_mount	*mp, | 
| Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 83 | 	struct xfs_perag	*pag, | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 84 | 	int			(*execute)(struct xfs_inode *ip, | 
 | 85 | 					   struct xfs_perag *pag, int flags), | 
 | 86 | 	int			flags, | 
| Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 87 | 	int			tag, | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 88 | 	int			exclusive, | 
 | 89 | 	int			*nr_to_scan) | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 90 | { | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 91 | 	uint32_t		first_index; | 
 | 92 | 	int			last_error = 0; | 
 | 93 | 	int			skipped; | 
 | 94 |  | 
 | 95 | restart: | 
 | 96 | 	skipped = 0; | 
 | 97 | 	first_index = 0; | 
 | 98 | 	do { | 
 | 99 | 		int		error = 0; | 
 | 100 | 		xfs_inode_t	*ip; | 
 | 101 |  | 
| Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 102 | 		if (exclusive) | 
 | 103 | 			write_lock(&pag->pag_ici_lock); | 
 | 104 | 		else | 
 | 105 | 			read_lock(&pag->pag_ici_lock); | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 106 | 		ip = xfs_inode_ag_lookup(mp, pag, &first_index, tag); | 
| Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 107 | 		if (!ip) { | 
 | 108 | 			if (exclusive) | 
 | 109 | 				write_unlock(&pag->pag_ici_lock); | 
 | 110 | 			else | 
 | 111 | 				read_unlock(&pag->pag_ici_lock); | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 112 | 			break; | 
| Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 113 | 		} | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 114 |  | 
| Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 115 | 		/* execute releases pag->pag_ici_lock */ | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 116 | 		error = execute(ip, pag, flags); | 
 | 117 | 		if (error == EAGAIN) { | 
 | 118 | 			skipped++; | 
 | 119 | 			continue; | 
 | 120 | 		} | 
 | 121 | 		if (error) | 
 | 122 | 			last_error = error; | 
| Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 123 |  | 
 | 124 | 		/* bail out if the filesystem is corrupted.  */ | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 125 | 		if (error == EFSCORRUPTED) | 
 | 126 | 			break; | 
 | 127 |  | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 128 | 	} while ((*nr_to_scan)--); | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 129 |  | 
 | 130 | 	if (skipped) { | 
 | 131 | 		delay(1); | 
 | 132 | 		goto restart; | 
 | 133 | 	} | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 134 | 	return last_error; | 
 | 135 | } | 
 | 136 |  | 
| Dave Chinner | 16fd536 | 2010-07-20 09:43:39 +1000 | [diff] [blame] | 137 | /* | 
 | 138 |  * Select the next per-ag structure to iterate during the walk. The reclaim | 
 | 139 |  * walk is optimised only to walk AGs with reclaimable inodes in them. | 
 | 140 |  */ | 
 | 141 | static struct xfs_perag * | 
 | 142 | xfs_inode_ag_iter_next_pag( | 
 | 143 | 	struct xfs_mount	*mp, | 
 | 144 | 	xfs_agnumber_t		*first, | 
 | 145 | 	int			tag) | 
 | 146 | { | 
 | 147 | 	struct xfs_perag	*pag = NULL; | 
 | 148 |  | 
 | 149 | 	if (tag == XFS_ICI_RECLAIM_TAG) { | 
 | 150 | 		int found; | 
 | 151 | 		int ref; | 
 | 152 |  | 
 | 153 | 		spin_lock(&mp->m_perag_lock); | 
 | 154 | 		found = radix_tree_gang_lookup_tag(&mp->m_perag_tree, | 
 | 155 | 				(void **)&pag, *first, 1, tag); | 
 | 156 | 		if (found <= 0) { | 
 | 157 | 			spin_unlock(&mp->m_perag_lock); | 
 | 158 | 			return NULL; | 
 | 159 | 		} | 
 | 160 | 		*first = pag->pag_agno + 1; | 
 | 161 | 		/* open coded pag reference increment */ | 
 | 162 | 		ref = atomic_inc_return(&pag->pag_ref); | 
 | 163 | 		spin_unlock(&mp->m_perag_lock); | 
 | 164 | 		trace_xfs_perag_get_reclaim(mp, pag->pag_agno, ref, _RET_IP_); | 
 | 165 | 	} else { | 
 | 166 | 		pag = xfs_perag_get(mp, *first); | 
 | 167 | 		(*first)++; | 
 | 168 | 	} | 
 | 169 | 	return pag; | 
 | 170 | } | 
 | 171 |  | 
| Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 172 | int | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 173 | xfs_inode_ag_iterator( | 
 | 174 | 	struct xfs_mount	*mp, | 
 | 175 | 	int			(*execute)(struct xfs_inode *ip, | 
 | 176 | 					   struct xfs_perag *pag, int flags), | 
 | 177 | 	int			flags, | 
| Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 178 | 	int			tag, | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 179 | 	int			exclusive, | 
 | 180 | 	int			*nr_to_scan) | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 181 | { | 
| Dave Chinner | 16fd536 | 2010-07-20 09:43:39 +1000 | [diff] [blame] | 182 | 	struct xfs_perag	*pag; | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 183 | 	int			error = 0; | 
 | 184 | 	int			last_error = 0; | 
 | 185 | 	xfs_agnumber_t		ag; | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 186 | 	int			nr; | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 187 |  | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 188 | 	nr = nr_to_scan ? *nr_to_scan : INT_MAX; | 
| Dave Chinner | 16fd536 | 2010-07-20 09:43:39 +1000 | [diff] [blame] | 189 | 	ag = 0; | 
 | 190 | 	while ((pag = xfs_inode_ag_iter_next_pag(mp, &ag, tag))) { | 
| Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 191 | 		error = xfs_inode_ag_walk(mp, pag, execute, flags, tag, | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 192 | 						exclusive, &nr); | 
| Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 193 | 		xfs_perag_put(pag); | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 194 | 		if (error) { | 
 | 195 | 			last_error = error; | 
 | 196 | 			if (error == EFSCORRUPTED) | 
 | 197 | 				break; | 
 | 198 | 		} | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 199 | 		if (nr <= 0) | 
 | 200 | 			break; | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 201 | 	} | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 202 | 	if (nr_to_scan) | 
 | 203 | 		*nr_to_scan = nr; | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 204 | 	return XFS_ERROR(last_error); | 
 | 205 | } | 
 | 206 |  | 
| Dave Chinner | 1da8eec | 2009-06-08 15:35:07 +0200 | [diff] [blame] | 207 | /* must be called with pag_ici_lock held and releases it */ | 
| Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 208 | int | 
| Dave Chinner | 1da8eec | 2009-06-08 15:35:07 +0200 | [diff] [blame] | 209 | xfs_sync_inode_valid( | 
 | 210 | 	struct xfs_inode	*ip, | 
 | 211 | 	struct xfs_perag	*pag) | 
 | 212 | { | 
 | 213 | 	struct inode		*inode = VFS_I(ip); | 
| Dave Chinner | 018027b | 2010-01-10 23:51:46 +0000 | [diff] [blame] | 214 | 	int			error = EFSCORRUPTED; | 
| Dave Chinner | 1da8eec | 2009-06-08 15:35:07 +0200 | [diff] [blame] | 215 |  | 
 | 216 | 	/* nothing to sync during shutdown */ | 
| Dave Chinner | 018027b | 2010-01-10 23:51:46 +0000 | [diff] [blame] | 217 | 	if (XFS_FORCED_SHUTDOWN(ip->i_mount)) | 
 | 218 | 		goto out_unlock; | 
| Dave Chinner | 1da8eec | 2009-06-08 15:35:07 +0200 | [diff] [blame] | 219 |  | 
| Dave Chinner | 018027b | 2010-01-10 23:51:46 +0000 | [diff] [blame] | 220 | 	/* avoid new or reclaimable inodes. Leave for reclaim code to flush */ | 
 | 221 | 	error = ENOENT; | 
 | 222 | 	if (xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM)) | 
 | 223 | 		goto out_unlock; | 
| Dave Chinner | 1da8eec | 2009-06-08 15:35:07 +0200 | [diff] [blame] | 224 |  | 
| Dave Chinner | 018027b | 2010-01-10 23:51:46 +0000 | [diff] [blame] | 225 | 	/* If we can't grab the inode, it must on it's way to reclaim. */ | 
 | 226 | 	if (!igrab(inode)) | 
 | 227 | 		goto out_unlock; | 
 | 228 |  | 
 | 229 | 	if (is_bad_inode(inode)) { | 
| Dave Chinner | 1da8eec | 2009-06-08 15:35:07 +0200 | [diff] [blame] | 230 | 		IRELE(ip); | 
| Dave Chinner | 018027b | 2010-01-10 23:51:46 +0000 | [diff] [blame] | 231 | 		goto out_unlock; | 
| Dave Chinner | 1da8eec | 2009-06-08 15:35:07 +0200 | [diff] [blame] | 232 | 	} | 
 | 233 |  | 
| Dave Chinner | 018027b | 2010-01-10 23:51:46 +0000 | [diff] [blame] | 234 | 	/* inode is valid */ | 
 | 235 | 	error = 0; | 
 | 236 | out_unlock: | 
 | 237 | 	read_unlock(&pag->pag_ici_lock); | 
 | 238 | 	return error; | 
| Dave Chinner | 1da8eec | 2009-06-08 15:35:07 +0200 | [diff] [blame] | 239 | } | 
 | 240 |  | 
| Dave Chinner | 5a34d5c | 2009-06-08 15:35:03 +0200 | [diff] [blame] | 241 | STATIC int | 
 | 242 | xfs_sync_inode_data( | 
 | 243 | 	struct xfs_inode	*ip, | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 244 | 	struct xfs_perag	*pag, | 
| Dave Chinner | 5a34d5c | 2009-06-08 15:35:03 +0200 | [diff] [blame] | 245 | 	int			flags) | 
 | 246 | { | 
 | 247 | 	struct inode		*inode = VFS_I(ip); | 
 | 248 | 	struct address_space *mapping = inode->i_mapping; | 
 | 249 | 	int			error = 0; | 
 | 250 |  | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 251 | 	error = xfs_sync_inode_valid(ip, pag); | 
 | 252 | 	if (error) | 
 | 253 | 		return error; | 
 | 254 |  | 
| Dave Chinner | 5a34d5c | 2009-06-08 15:35:03 +0200 | [diff] [blame] | 255 | 	if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) | 
 | 256 | 		goto out_wait; | 
 | 257 |  | 
 | 258 | 	if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED)) { | 
 | 259 | 		if (flags & SYNC_TRYLOCK) | 
 | 260 | 			goto out_wait; | 
 | 261 | 		xfs_ilock(ip, XFS_IOLOCK_SHARED); | 
 | 262 | 	} | 
 | 263 |  | 
 | 264 | 	error = xfs_flush_pages(ip, 0, -1, (flags & SYNC_WAIT) ? | 
| Christoph Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 265 | 				0 : XBF_ASYNC, FI_NONE); | 
| Dave Chinner | 5a34d5c | 2009-06-08 15:35:03 +0200 | [diff] [blame] | 266 | 	xfs_iunlock(ip, XFS_IOLOCK_SHARED); | 
 | 267 |  | 
 | 268 |  out_wait: | 
| Christoph Hellwig | b0710cc | 2009-06-08 15:37:11 +0200 | [diff] [blame] | 269 | 	if (flags & SYNC_WAIT) | 
| Dave Chinner | 5a34d5c | 2009-06-08 15:35:03 +0200 | [diff] [blame] | 270 | 		xfs_ioend_wait(ip); | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 271 | 	IRELE(ip); | 
| Dave Chinner | 5a34d5c | 2009-06-08 15:35:03 +0200 | [diff] [blame] | 272 | 	return error; | 
 | 273 | } | 
 | 274 |  | 
| Christoph Hellwig | 845b6d0 | 2009-06-08 15:35:05 +0200 | [diff] [blame] | 275 | STATIC int | 
 | 276 | xfs_sync_inode_attr( | 
 | 277 | 	struct xfs_inode	*ip, | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 278 | 	struct xfs_perag	*pag, | 
| Christoph Hellwig | 845b6d0 | 2009-06-08 15:35:05 +0200 | [diff] [blame] | 279 | 	int			flags) | 
 | 280 | { | 
 | 281 | 	int			error = 0; | 
 | 282 |  | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 283 | 	error = xfs_sync_inode_valid(ip, pag); | 
 | 284 | 	if (error) | 
 | 285 | 		return error; | 
 | 286 |  | 
| Christoph Hellwig | 845b6d0 | 2009-06-08 15:35:05 +0200 | [diff] [blame] | 287 | 	xfs_ilock(ip, XFS_ILOCK_SHARED); | 
 | 288 | 	if (xfs_inode_clean(ip)) | 
 | 289 | 		goto out_unlock; | 
 | 290 | 	if (!xfs_iflock_nowait(ip)) { | 
 | 291 | 		if (!(flags & SYNC_WAIT)) | 
 | 292 | 			goto out_unlock; | 
 | 293 | 		xfs_iflock(ip); | 
 | 294 | 	} | 
 | 295 |  | 
 | 296 | 	if (xfs_inode_clean(ip)) { | 
 | 297 | 		xfs_ifunlock(ip); | 
 | 298 | 		goto out_unlock; | 
 | 299 | 	} | 
 | 300 |  | 
| Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 301 | 	error = xfs_iflush(ip, flags); | 
| Christoph Hellwig | 845b6d0 | 2009-06-08 15:35:05 +0200 | [diff] [blame] | 302 |  | 
 | 303 |  out_unlock: | 
 | 304 | 	xfs_iunlock(ip, XFS_ILOCK_SHARED); | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 305 | 	IRELE(ip); | 
| Christoph Hellwig | 845b6d0 | 2009-06-08 15:35:05 +0200 | [diff] [blame] | 306 | 	return error; | 
 | 307 | } | 
 | 308 |  | 
| Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 309 | /* | 
 | 310 |  * Write out pagecache data for the whole filesystem. | 
 | 311 |  */ | 
| Christoph Hellwig | 64c8614 | 2010-06-24 11:45:34 +1000 | [diff] [blame] | 312 | STATIC int | 
| Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 313 | xfs_sync_data( | 
 | 314 | 	struct xfs_mount	*mp, | 
 | 315 | 	int			flags) | 
| David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 316 | { | 
| Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 317 | 	int			error; | 
| David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 318 |  | 
| Christoph Hellwig | b0710cc | 2009-06-08 15:37:11 +0200 | [diff] [blame] | 319 | 	ASSERT((flags & ~(SYNC_TRYLOCK|SYNC_WAIT)) == 0); | 
| David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 320 |  | 
| Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 321 | 	error = xfs_inode_ag_iterator(mp, xfs_sync_inode_data, flags, | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 322 | 				      XFS_ICI_NO_TAG, 0, NULL); | 
| Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 323 | 	if (error) | 
 | 324 | 		return XFS_ERROR(error); | 
| David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 325 |  | 
| Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 326 | 	xfs_log_force(mp, (flags & SYNC_WAIT) ? XFS_LOG_SYNC : 0); | 
| Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 327 | 	return 0; | 
 | 328 | } | 
| David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 329 |  | 
| Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 330 | /* | 
 | 331 |  * Write out inode metadata (attributes) for the whole filesystem. | 
 | 332 |  */ | 
| Christoph Hellwig | 64c8614 | 2010-06-24 11:45:34 +1000 | [diff] [blame] | 333 | STATIC int | 
| Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 334 | xfs_sync_attr( | 
 | 335 | 	struct xfs_mount	*mp, | 
 | 336 | 	int			flags) | 
 | 337 | { | 
 | 338 | 	ASSERT((flags & ~SYNC_WAIT) == 0); | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 339 |  | 
| Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 340 | 	return xfs_inode_ag_iterator(mp, xfs_sync_inode_attr, flags, | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 341 | 				     XFS_ICI_NO_TAG, 0, NULL); | 
| David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 342 | } | 
 | 343 |  | 
| Christoph Hellwig | 2af75df | 2008-10-30 17:14:53 +1100 | [diff] [blame] | 344 | STATIC int | 
| Christoph Hellwig | 2af75df | 2008-10-30 17:14:53 +1100 | [diff] [blame] | 345 | xfs_sync_fsdata( | 
| Christoph Hellwig | df308bc | 2010-03-12 10:59:16 +0000 | [diff] [blame] | 346 | 	struct xfs_mount	*mp) | 
| Christoph Hellwig | 2af75df | 2008-10-30 17:14:53 +1100 | [diff] [blame] | 347 | { | 
 | 348 | 	struct xfs_buf		*bp; | 
| Christoph Hellwig | 2af75df | 2008-10-30 17:14:53 +1100 | [diff] [blame] | 349 |  | 
 | 350 | 	/* | 
| Christoph Hellwig | df308bc | 2010-03-12 10:59:16 +0000 | [diff] [blame] | 351 | 	 * If the buffer is pinned then push on the log so we won't get stuck | 
 | 352 | 	 * waiting in the write for someone, maybe ourselves, to flush the log. | 
 | 353 | 	 * | 
 | 354 | 	 * Even though we just pushed the log above, we did not have the | 
 | 355 | 	 * superblock buffer locked at that point so it can become pinned in | 
 | 356 | 	 * between there and here. | 
| Christoph Hellwig | 2af75df | 2008-10-30 17:14:53 +1100 | [diff] [blame] | 357 | 	 */ | 
| Christoph Hellwig | df308bc | 2010-03-12 10:59:16 +0000 | [diff] [blame] | 358 | 	bp = xfs_getsb(mp, 0); | 
 | 359 | 	if (XFS_BUF_ISPINNED(bp)) | 
 | 360 | 		xfs_log_force(mp, 0); | 
| Christoph Hellwig | 2af75df | 2008-10-30 17:14:53 +1100 | [diff] [blame] | 361 |  | 
| Christoph Hellwig | df308bc | 2010-03-12 10:59:16 +0000 | [diff] [blame] | 362 | 	return xfs_bwrite(mp, bp); | 
| Christoph Hellwig | 2af75df | 2008-10-30 17:14:53 +1100 | [diff] [blame] | 363 | } | 
 | 364 |  | 
| David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 365 | /* | 
| David Chinner | a4e4c4f | 2008-10-30 17:16:11 +1100 | [diff] [blame] | 366 |  * When remounting a filesystem read-only or freezing the filesystem, we have | 
 | 367 |  * two phases to execute. This first phase is syncing the data before we | 
 | 368 |  * quiesce the filesystem, and the second is flushing all the inodes out after | 
 | 369 |  * we've waited for all the transactions created by the first phase to | 
 | 370 |  * complete. The second phase ensures that the inodes are written to their | 
 | 371 |  * location on disk rather than just existing in transactions in the log. This | 
 | 372 |  * means after a quiesce there is no log replay required to write the inodes to | 
 | 373 |  * disk (this is the main difference between a sync and a quiesce). | 
 | 374 |  */ | 
 | 375 | /* | 
 | 376 |  * First stage of freeze - no writers will make progress now we are here, | 
| David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 377 |  * so we flush delwri and delalloc buffers here, then wait for all I/O to | 
 | 378 |  * complete.  Data is frozen at that point. Metadata is not frozen, | 
| David Chinner | a4e4c4f | 2008-10-30 17:16:11 +1100 | [diff] [blame] | 379 |  * transactions can still occur here so don't bother flushing the buftarg | 
 | 380 |  * because it'll just get dirty again. | 
| David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 381 |  */ | 
 | 382 | int | 
 | 383 | xfs_quiesce_data( | 
 | 384 | 	struct xfs_mount	*mp) | 
 | 385 | { | 
| Christoph Hellwig | df308bc | 2010-03-12 10:59:16 +0000 | [diff] [blame] | 386 | 	int			error, error2 = 0; | 
| David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 387 |  | 
 | 388 | 	/* push non-blocking */ | 
| Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 389 | 	xfs_sync_data(mp, 0); | 
| Christoph Hellwig | 8b5403a | 2009-06-08 15:37:16 +0200 | [diff] [blame] | 390 | 	xfs_qm_sync(mp, SYNC_TRYLOCK); | 
| David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 391 |  | 
| Dave Chinner | c90b07e | 2009-10-06 20:29:27 +0000 | [diff] [blame] | 392 | 	/* push and block till complete */ | 
| Christoph Hellwig | b0710cc | 2009-06-08 15:37:11 +0200 | [diff] [blame] | 393 | 	xfs_sync_data(mp, SYNC_WAIT); | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 394 | 	xfs_qm_sync(mp, SYNC_WAIT); | 
| David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 395 |  | 
| David Chinner | a4e4c4f | 2008-10-30 17:16:11 +1100 | [diff] [blame] | 396 | 	/* write superblock and hoover up shutdown errors */ | 
| Christoph Hellwig | df308bc | 2010-03-12 10:59:16 +0000 | [diff] [blame] | 397 | 	error = xfs_sync_fsdata(mp); | 
 | 398 |  | 
 | 399 | 	/* make sure all delwri buffers are written out */ | 
 | 400 | 	xfs_flush_buftarg(mp->m_ddev_targp, 1); | 
 | 401 |  | 
 | 402 | 	/* mark the log as covered if needed */ | 
 | 403 | 	if (xfs_log_need_covered(mp)) | 
| Dave Chinner | 1a387d3 | 2010-08-24 11:46:31 +1000 | [diff] [blame] | 404 | 		error2 = xfs_fs_log_dummy(mp, SYNC_WAIT); | 
| David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 405 |  | 
| David Chinner | a4e4c4f | 2008-10-30 17:16:11 +1100 | [diff] [blame] | 406 | 	/* flush data-only devices */ | 
| David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 407 | 	if (mp->m_rtdev_targp) | 
 | 408 | 		XFS_bflush(mp->m_rtdev_targp); | 
 | 409 |  | 
| Christoph Hellwig | df308bc | 2010-03-12 10:59:16 +0000 | [diff] [blame] | 410 | 	return error ? error : error2; | 
| David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 411 | } | 
 | 412 |  | 
| David Chinner | 76bf105 | 2008-10-30 17:16:21 +1100 | [diff] [blame] | 413 | STATIC void | 
 | 414 | xfs_quiesce_fs( | 
 | 415 | 	struct xfs_mount	*mp) | 
 | 416 | { | 
 | 417 | 	int	count = 0, pincount; | 
 | 418 |  | 
| Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 419 | 	xfs_reclaim_inodes(mp, 0); | 
| David Chinner | 76bf105 | 2008-10-30 17:16:21 +1100 | [diff] [blame] | 420 | 	xfs_flush_buftarg(mp->m_ddev_targp, 0); | 
| David Chinner | 76bf105 | 2008-10-30 17:16:21 +1100 | [diff] [blame] | 421 |  | 
 | 422 | 	/* | 
 | 423 | 	 * This loop must run at least twice.  The first instance of the loop | 
 | 424 | 	 * will flush most meta data but that will generate more meta data | 
 | 425 | 	 * (typically directory updates).  Which then must be flushed and | 
| Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 426 | 	 * logged before we can write the unmount record. We also so sync | 
 | 427 | 	 * reclaim of inodes to catch any that the above delwri flush skipped. | 
| David Chinner | 76bf105 | 2008-10-30 17:16:21 +1100 | [diff] [blame] | 428 | 	 */ | 
 | 429 | 	do { | 
| Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 430 | 		xfs_reclaim_inodes(mp, SYNC_WAIT); | 
| Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 431 | 		xfs_sync_attr(mp, SYNC_WAIT); | 
| David Chinner | 76bf105 | 2008-10-30 17:16:21 +1100 | [diff] [blame] | 432 | 		pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1); | 
 | 433 | 		if (!pincount) { | 
 | 434 | 			delay(50); | 
 | 435 | 			count++; | 
 | 436 | 		} | 
 | 437 | 	} while (count < 2); | 
 | 438 | } | 
 | 439 |  | 
 | 440 | /* | 
 | 441 |  * Second stage of a quiesce. The data is already synced, now we have to take | 
 | 442 |  * care of the metadata. New transactions are already blocked, so we need to | 
 | 443 |  * wait for any remaining transactions to drain out before proceding. | 
 | 444 |  */ | 
 | 445 | void | 
 | 446 | xfs_quiesce_attr( | 
 | 447 | 	struct xfs_mount	*mp) | 
 | 448 | { | 
 | 449 | 	int	error = 0; | 
 | 450 |  | 
 | 451 | 	/* wait for all modifications to complete */ | 
 | 452 | 	while (atomic_read(&mp->m_active_trans) > 0) | 
 | 453 | 		delay(100); | 
 | 454 |  | 
 | 455 | 	/* flush inodes and push all remaining buffers out to disk */ | 
 | 456 | 	xfs_quiesce_fs(mp); | 
 | 457 |  | 
| Felix Blyakher | 5e10657 | 2009-01-22 21:34:05 -0600 | [diff] [blame] | 458 | 	/* | 
 | 459 | 	 * Just warn here till VFS can correctly support | 
 | 460 | 	 * read-only remount without racing. | 
 | 461 | 	 */ | 
 | 462 | 	WARN_ON(atomic_read(&mp->m_active_trans) != 0); | 
| David Chinner | 76bf105 | 2008-10-30 17:16:21 +1100 | [diff] [blame] | 463 |  | 
 | 464 | 	/* Push the superblock and write an unmount record */ | 
 | 465 | 	error = xfs_log_sbcount(mp, 1); | 
 | 466 | 	if (error) | 
 | 467 | 		xfs_fs_cmn_err(CE_WARN, mp, | 
 | 468 | 				"xfs_attr_quiesce: failed to log sb changes. " | 
 | 469 | 				"Frozen image may not be consistent."); | 
 | 470 | 	xfs_log_unmount_write(mp); | 
 | 471 | 	xfs_unmountfs_writesb(mp); | 
 | 472 | } | 
 | 473 |  | 
| David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 474 | /* | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 475 |  * Enqueue a work item to be picked up by the vfs xfssyncd thread. | 
 | 476 |  * Doing this has two advantages: | 
 | 477 |  * - It saves on stack space, which is tight in certain situations | 
 | 478 |  * - It can be used (with care) as a mechanism to avoid deadlocks. | 
 | 479 |  * Flushing while allocating in a full filesystem requires both. | 
 | 480 |  */ | 
 | 481 | STATIC void | 
 | 482 | xfs_syncd_queue_work( | 
 | 483 | 	struct xfs_mount *mp, | 
 | 484 | 	void		*data, | 
| Dave Chinner | e43afd7 | 2009-04-06 18:47:27 +0200 | [diff] [blame] | 485 | 	void		(*syncer)(struct xfs_mount *, void *), | 
 | 486 | 	struct completion *completion) | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 487 | { | 
| Dave Chinner | a8d770d | 2009-04-06 18:44:54 +0200 | [diff] [blame] | 488 | 	struct xfs_sync_work *work; | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 489 |  | 
| Dave Chinner | a8d770d | 2009-04-06 18:44:54 +0200 | [diff] [blame] | 490 | 	work = kmem_alloc(sizeof(struct xfs_sync_work), KM_SLEEP); | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 491 | 	INIT_LIST_HEAD(&work->w_list); | 
 | 492 | 	work->w_syncer = syncer; | 
 | 493 | 	work->w_data = data; | 
 | 494 | 	work->w_mount = mp; | 
| Dave Chinner | e43afd7 | 2009-04-06 18:47:27 +0200 | [diff] [blame] | 495 | 	work->w_completion = completion; | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 496 | 	spin_lock(&mp->m_sync_lock); | 
 | 497 | 	list_add_tail(&work->w_list, &mp->m_sync_list); | 
 | 498 | 	spin_unlock(&mp->m_sync_lock); | 
 | 499 | 	wake_up_process(mp->m_sync_task); | 
 | 500 | } | 
 | 501 |  | 
 | 502 | /* | 
 | 503 |  * Flush delayed allocate data, attempting to free up reserved space | 
 | 504 |  * from existing allocations.  At this point a new allocation attempt | 
 | 505 |  * has failed with ENOSPC and we are in the process of scratching our | 
 | 506 |  * heads, looking about for more room... | 
 | 507 |  */ | 
 | 508 | STATIC void | 
| Dave Chinner | a8d770d | 2009-04-06 18:44:54 +0200 | [diff] [blame] | 509 | xfs_flush_inodes_work( | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 510 | 	struct xfs_mount *mp, | 
 | 511 | 	void		*arg) | 
 | 512 | { | 
 | 513 | 	struct inode	*inode = arg; | 
| Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 514 | 	xfs_sync_data(mp, SYNC_TRYLOCK); | 
| Christoph Hellwig | b0710cc | 2009-06-08 15:37:11 +0200 | [diff] [blame] | 515 | 	xfs_sync_data(mp, SYNC_TRYLOCK | SYNC_WAIT); | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 516 | 	iput(inode); | 
 | 517 | } | 
 | 518 |  | 
 | 519 | void | 
| Dave Chinner | a8d770d | 2009-04-06 18:44:54 +0200 | [diff] [blame] | 520 | xfs_flush_inodes( | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 521 | 	xfs_inode_t	*ip) | 
 | 522 | { | 
 | 523 | 	struct inode	*inode = VFS_I(ip); | 
| Dave Chinner | e43afd7 | 2009-04-06 18:47:27 +0200 | [diff] [blame] | 524 | 	DECLARE_COMPLETION_ONSTACK(completion); | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 525 |  | 
 | 526 | 	igrab(inode); | 
| Dave Chinner | e43afd7 | 2009-04-06 18:47:27 +0200 | [diff] [blame] | 527 | 	xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inodes_work, &completion); | 
 | 528 | 	wait_for_completion(&completion); | 
| Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 529 | 	xfs_log_force(ip->i_mount, XFS_LOG_SYNC); | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 530 | } | 
 | 531 |  | 
| David Chinner | aacaa88 | 2008-10-30 17:15:29 +1100 | [diff] [blame] | 532 | /* | 
| Christoph Hellwig | df308bc | 2010-03-12 10:59:16 +0000 | [diff] [blame] | 533 |  * Every sync period we need to unpin all items, reclaim inodes and sync | 
 | 534 |  * disk quotas.  We might need to cover the log to indicate that the | 
| Dave Chinner | 1a387d3 | 2010-08-24 11:46:31 +1000 | [diff] [blame] | 535 |  * filesystem is idle and not frozen. | 
| David Chinner | aacaa88 | 2008-10-30 17:15:29 +1100 | [diff] [blame] | 536 |  */ | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 537 | STATIC void | 
 | 538 | xfs_sync_worker( | 
 | 539 | 	struct xfs_mount *mp, | 
 | 540 | 	void		*unused) | 
 | 541 | { | 
 | 542 | 	int		error; | 
 | 543 |  | 
| David Chinner | aacaa88 | 2008-10-30 17:15:29 +1100 | [diff] [blame] | 544 | 	if (!(mp->m_flags & XFS_MOUNT_RDONLY)) { | 
| Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 545 | 		xfs_log_force(mp, 0); | 
| Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 546 | 		xfs_reclaim_inodes(mp, 0); | 
| David Chinner | aacaa88 | 2008-10-30 17:15:29 +1100 | [diff] [blame] | 547 | 		/* dgc: errors ignored here */ | 
| Christoph Hellwig | 8b5403a | 2009-06-08 15:37:16 +0200 | [diff] [blame] | 548 | 		error = xfs_qm_sync(mp, SYNC_TRYLOCK); | 
| Dave Chinner | 1a387d3 | 2010-08-24 11:46:31 +1000 | [diff] [blame] | 549 | 		if (mp->m_super->s_frozen == SB_UNFROZEN && | 
 | 550 | 		    xfs_log_need_covered(mp)) | 
 | 551 | 			error = xfs_fs_log_dummy(mp, 0); | 
| David Chinner | aacaa88 | 2008-10-30 17:15:29 +1100 | [diff] [blame] | 552 | 	} | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 553 | 	mp->m_sync_seq++; | 
 | 554 | 	wake_up(&mp->m_wait_single_sync_task); | 
 | 555 | } | 
 | 556 |  | 
 | 557 | STATIC int | 
 | 558 | xfssyncd( | 
 | 559 | 	void			*arg) | 
 | 560 | { | 
 | 561 | 	struct xfs_mount	*mp = arg; | 
 | 562 | 	long			timeleft; | 
| Dave Chinner | a8d770d | 2009-04-06 18:44:54 +0200 | [diff] [blame] | 563 | 	xfs_sync_work_t		*work, *n; | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 564 | 	LIST_HEAD		(tmp); | 
 | 565 |  | 
 | 566 | 	set_freezable(); | 
 | 567 | 	timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10); | 
 | 568 | 	for (;;) { | 
| Dave Chinner | 20f6b2c | 2010-03-04 01:46:23 +0000 | [diff] [blame] | 569 | 		if (list_empty(&mp->m_sync_list)) | 
 | 570 | 			timeleft = schedule_timeout_interruptible(timeleft); | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 571 | 		/* swsusp */ | 
 | 572 | 		try_to_freeze(); | 
 | 573 | 		if (kthread_should_stop() && list_empty(&mp->m_sync_list)) | 
 | 574 | 			break; | 
 | 575 |  | 
 | 576 | 		spin_lock(&mp->m_sync_lock); | 
 | 577 | 		/* | 
 | 578 | 		 * We can get woken by laptop mode, to do a sync - | 
 | 579 | 		 * that's the (only!) case where the list would be | 
 | 580 | 		 * empty with time remaining. | 
 | 581 | 		 */ | 
 | 582 | 		if (!timeleft || list_empty(&mp->m_sync_list)) { | 
 | 583 | 			if (!timeleft) | 
 | 584 | 				timeleft = xfs_syncd_centisecs * | 
 | 585 | 							msecs_to_jiffies(10); | 
 | 586 | 			INIT_LIST_HEAD(&mp->m_sync_work.w_list); | 
 | 587 | 			list_add_tail(&mp->m_sync_work.w_list, | 
 | 588 | 					&mp->m_sync_list); | 
 | 589 | 		} | 
| Dave Chinner | 20f6b2c | 2010-03-04 01:46:23 +0000 | [diff] [blame] | 590 | 		list_splice_init(&mp->m_sync_list, &tmp); | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 591 | 		spin_unlock(&mp->m_sync_lock); | 
 | 592 |  | 
 | 593 | 		list_for_each_entry_safe(work, n, &tmp, w_list) { | 
 | 594 | 			(*work->w_syncer)(mp, work->w_data); | 
 | 595 | 			list_del(&work->w_list); | 
 | 596 | 			if (work == &mp->m_sync_work) | 
 | 597 | 				continue; | 
| Dave Chinner | e43afd7 | 2009-04-06 18:47:27 +0200 | [diff] [blame] | 598 | 			if (work->w_completion) | 
 | 599 | 				complete(work->w_completion); | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 600 | 			kmem_free(work); | 
 | 601 | 		} | 
 | 602 | 	} | 
 | 603 |  | 
 | 604 | 	return 0; | 
 | 605 | } | 
 | 606 |  | 
 | 607 | int | 
 | 608 | xfs_syncd_init( | 
 | 609 | 	struct xfs_mount	*mp) | 
 | 610 | { | 
 | 611 | 	mp->m_sync_work.w_syncer = xfs_sync_worker; | 
 | 612 | 	mp->m_sync_work.w_mount = mp; | 
| Dave Chinner | e43afd7 | 2009-04-06 18:47:27 +0200 | [diff] [blame] | 613 | 	mp->m_sync_work.w_completion = NULL; | 
| Jan Engelhardt | e2a0781 | 2010-03-23 09:52:55 +1100 | [diff] [blame] | 614 | 	mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd/%s", mp->m_fsname); | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 615 | 	if (IS_ERR(mp->m_sync_task)) | 
 | 616 | 		return -PTR_ERR(mp->m_sync_task); | 
 | 617 | 	return 0; | 
 | 618 | } | 
 | 619 |  | 
 | 620 | void | 
 | 621 | xfs_syncd_stop( | 
 | 622 | 	struct xfs_mount	*mp) | 
 | 623 | { | 
 | 624 | 	kthread_stop(mp->m_sync_task); | 
 | 625 | } | 
 | 626 |  | 
| Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 627 | void | 
 | 628 | __xfs_inode_set_reclaim_tag( | 
 | 629 | 	struct xfs_perag	*pag, | 
 | 630 | 	struct xfs_inode	*ip) | 
 | 631 | { | 
 | 632 | 	radix_tree_tag_set(&pag->pag_ici_root, | 
 | 633 | 			   XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino), | 
 | 634 | 			   XFS_ICI_RECLAIM_TAG); | 
| Dave Chinner | 16fd536 | 2010-07-20 09:43:39 +1000 | [diff] [blame] | 635 |  | 
 | 636 | 	if (!pag->pag_ici_reclaimable) { | 
 | 637 | 		/* propagate the reclaim tag up into the perag radix tree */ | 
 | 638 | 		spin_lock(&ip->i_mount->m_perag_lock); | 
 | 639 | 		radix_tree_tag_set(&ip->i_mount->m_perag_tree, | 
 | 640 | 				XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino), | 
 | 641 | 				XFS_ICI_RECLAIM_TAG); | 
 | 642 | 		spin_unlock(&ip->i_mount->m_perag_lock); | 
 | 643 | 		trace_xfs_perag_set_reclaim(ip->i_mount, pag->pag_agno, | 
 | 644 | 							-1, _RET_IP_); | 
 | 645 | 	} | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 646 | 	pag->pag_ici_reclaimable++; | 
| Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 647 | } | 
 | 648 |  | 
| David Chinner | 1165451 | 2008-10-30 17:37:49 +1100 | [diff] [blame] | 649 | /* | 
 | 650 |  * We set the inode flag atomically with the radix tree tag. | 
 | 651 |  * Once we get tag lookups on the radix tree, this inode flag | 
 | 652 |  * can go away. | 
 | 653 |  */ | 
| David Chinner | 396beb8 | 2008-10-30 17:37:26 +1100 | [diff] [blame] | 654 | void | 
 | 655 | xfs_inode_set_reclaim_tag( | 
 | 656 | 	xfs_inode_t	*ip) | 
 | 657 | { | 
| Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 658 | 	struct xfs_mount *mp = ip->i_mount; | 
 | 659 | 	struct xfs_perag *pag; | 
| David Chinner | 396beb8 | 2008-10-30 17:37:26 +1100 | [diff] [blame] | 660 |  | 
| Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 661 | 	pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino)); | 
| Christoph Hellwig | f1f724e | 2010-03-01 11:30:31 +0000 | [diff] [blame] | 662 | 	write_lock(&pag->pag_ici_lock); | 
| David Chinner | 396beb8 | 2008-10-30 17:37:26 +1100 | [diff] [blame] | 663 | 	spin_lock(&ip->i_flags_lock); | 
| Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 664 | 	__xfs_inode_set_reclaim_tag(pag, ip); | 
| David Chinner | 1165451 | 2008-10-30 17:37:49 +1100 | [diff] [blame] | 665 | 	__xfs_iflags_set(ip, XFS_IRECLAIMABLE); | 
| David Chinner | 396beb8 | 2008-10-30 17:37:26 +1100 | [diff] [blame] | 666 | 	spin_unlock(&ip->i_flags_lock); | 
| Christoph Hellwig | f1f724e | 2010-03-01 11:30:31 +0000 | [diff] [blame] | 667 | 	write_unlock(&pag->pag_ici_lock); | 
| Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 668 | 	xfs_perag_put(pag); | 
| David Chinner | 396beb8 | 2008-10-30 17:37:26 +1100 | [diff] [blame] | 669 | } | 
 | 670 |  | 
 | 671 | void | 
 | 672 | __xfs_inode_clear_reclaim_tag( | 
 | 673 | 	xfs_mount_t	*mp, | 
 | 674 | 	xfs_perag_t	*pag, | 
 | 675 | 	xfs_inode_t	*ip) | 
 | 676 | { | 
 | 677 | 	radix_tree_tag_clear(&pag->pag_ici_root, | 
 | 678 | 			XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG); | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 679 | 	pag->pag_ici_reclaimable--; | 
| Dave Chinner | 16fd536 | 2010-07-20 09:43:39 +1000 | [diff] [blame] | 680 | 	if (!pag->pag_ici_reclaimable) { | 
 | 681 | 		/* clear the reclaim tag from the perag radix tree */ | 
 | 682 | 		spin_lock(&ip->i_mount->m_perag_lock); | 
 | 683 | 		radix_tree_tag_clear(&ip->i_mount->m_perag_tree, | 
 | 684 | 				XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino), | 
 | 685 | 				XFS_ICI_RECLAIM_TAG); | 
 | 686 | 		spin_unlock(&ip->i_mount->m_perag_lock); | 
 | 687 | 		trace_xfs_perag_clear_reclaim(ip->i_mount, pag->pag_agno, | 
 | 688 | 							-1, _RET_IP_); | 
 | 689 | 	} | 
| David Chinner | 396beb8 | 2008-10-30 17:37:26 +1100 | [diff] [blame] | 690 | } | 
 | 691 |  | 
| Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 692 | /* | 
 | 693 |  * Inodes in different states need to be treated differently, and the return | 
 | 694 |  * value of xfs_iflush is not sufficient to get this right. The following table | 
 | 695 |  * lists the inode states and the reclaim actions necessary for non-blocking | 
 | 696 |  * reclaim: | 
 | 697 |  * | 
 | 698 |  * | 
 | 699 |  *	inode state	     iflush ret		required action | 
 | 700 |  *      ---------------      ----------         --------------- | 
 | 701 |  *	bad			-		reclaim | 
 | 702 |  *	shutdown		EIO		unpin and reclaim | 
 | 703 |  *	clean, unpinned		0		reclaim | 
 | 704 |  *	stale, unpinned		0		reclaim | 
| Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 705 |  *	clean, pinned(*)	0		requeue | 
 | 706 |  *	stale, pinned		EAGAIN		requeue | 
 | 707 |  *	dirty, delwri ok	0		requeue | 
 | 708 |  *	dirty, delwri blocked	EAGAIN		requeue | 
 | 709 |  *	dirty, sync flush	0		reclaim | 
| Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 710 |  * | 
 | 711 |  * (*) dgc: I don't think the clean, pinned state is possible but it gets | 
 | 712 |  * handled anyway given the order of checks implemented. | 
 | 713 |  * | 
| Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 714 |  * As can be seen from the table, the return value of xfs_iflush() is not | 
 | 715 |  * sufficient to correctly decide the reclaim action here. The checks in | 
 | 716 |  * xfs_iflush() might look like duplicates, but they are not. | 
 | 717 |  * | 
 | 718 |  * Also, because we get the flush lock first, we know that any inode that has | 
 | 719 |  * been flushed delwri has had the flush completed by the time we check that | 
 | 720 |  * the inode is clean. The clean inode check needs to be done before flushing | 
 | 721 |  * the inode delwri otherwise we would loop forever requeuing clean inodes as | 
 | 722 |  * we cannot tell apart a successful delwri flush and a clean inode from the | 
 | 723 |  * return value of xfs_iflush(). | 
 | 724 |  * | 
 | 725 |  * Note that because the inode is flushed delayed write by background | 
 | 726 |  * writeback, the flush lock may already be held here and waiting on it can | 
 | 727 |  * result in very long latencies. Hence for sync reclaims, where we wait on the | 
 | 728 |  * flush lock, the caller should push out delayed write inodes first before | 
 | 729 |  * trying to reclaim them to minimise the amount of time spent waiting. For | 
 | 730 |  * background relaim, we just requeue the inode for the next pass. | 
 | 731 |  * | 
| Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 732 |  * Hence the order of actions after gaining the locks should be: | 
 | 733 |  *	bad		=> reclaim | 
 | 734 |  *	shutdown	=> unpin and reclaim | 
| Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 735 |  *	pinned, delwri	=> requeue | 
 | 736 |  *	pinned, sync	=> unpin | 
| Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 737 |  *	stale		=> reclaim | 
 | 738 |  *	clean		=> reclaim | 
| Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 739 |  *	dirty, delwri	=> flush and requeue | 
 | 740 |  *	dirty, sync	=> flush, wait and reclaim | 
| Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 741 |  */ | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 742 | STATIC int | 
| Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 743 | xfs_reclaim_inode( | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 744 | 	struct xfs_inode	*ip, | 
 | 745 | 	struct xfs_perag	*pag, | 
| Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 746 | 	int			sync_mode) | 
| David Chinner | 7a3be02 | 2008-10-30 17:37:37 +1100 | [diff] [blame] | 747 | { | 
| Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 748 | 	int	error = 0; | 
| Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 749 |  | 
| Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 750 | 	/* | 
 | 751 | 	 * The radix tree lock here protects a thread in xfs_iget from racing | 
 | 752 | 	 * with us starting reclaim on the inode.  Once we have the | 
 | 753 | 	 * XFS_IRECLAIM flag set it will not touch us. | 
 | 754 | 	 */ | 
 | 755 | 	spin_lock(&ip->i_flags_lock); | 
 | 756 | 	ASSERT_ALWAYS(__xfs_iflags_test(ip, XFS_IRECLAIMABLE)); | 
 | 757 | 	if (__xfs_iflags_test(ip, XFS_IRECLAIM)) { | 
 | 758 | 		/* ignore as it is already under reclaim */ | 
 | 759 | 		spin_unlock(&ip->i_flags_lock); | 
 | 760 | 		write_unlock(&pag->pag_ici_lock); | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 761 | 		return 0; | 
| David Chinner | 7a3be02 | 2008-10-30 17:37:37 +1100 | [diff] [blame] | 762 | 	} | 
| Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 763 | 	__xfs_iflags_set(ip, XFS_IRECLAIM); | 
 | 764 | 	spin_unlock(&ip->i_flags_lock); | 
 | 765 | 	write_unlock(&pag->pag_ici_lock); | 
| David Chinner | 7a3be02 | 2008-10-30 17:37:37 +1100 | [diff] [blame] | 766 |  | 
| Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 767 | 	xfs_ilock(ip, XFS_ILOCK_EXCL); | 
| Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 768 | 	if (!xfs_iflock_nowait(ip)) { | 
 | 769 | 		if (!(sync_mode & SYNC_WAIT)) | 
 | 770 | 			goto out; | 
 | 771 | 		xfs_iflock(ip); | 
 | 772 | 	} | 
| Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 773 |  | 
| Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 774 | 	if (is_bad_inode(VFS_I(ip))) | 
 | 775 | 		goto reclaim; | 
 | 776 | 	if (XFS_FORCED_SHUTDOWN(ip->i_mount)) { | 
 | 777 | 		xfs_iunpin_wait(ip); | 
 | 778 | 		goto reclaim; | 
 | 779 | 	} | 
| Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 780 | 	if (xfs_ipincount(ip)) { | 
 | 781 | 		if (!(sync_mode & SYNC_WAIT)) { | 
 | 782 | 			xfs_ifunlock(ip); | 
 | 783 | 			goto out; | 
 | 784 | 		} | 
| Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 785 | 		xfs_iunpin_wait(ip); | 
| Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 786 | 	} | 
| Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 787 | 	if (xfs_iflags_test(ip, XFS_ISTALE)) | 
 | 788 | 		goto reclaim; | 
 | 789 | 	if (xfs_inode_clean(ip)) | 
 | 790 | 		goto reclaim; | 
 | 791 |  | 
 | 792 | 	/* Now we have an inode that needs flushing */ | 
 | 793 | 	error = xfs_iflush(ip, sync_mode); | 
| Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 794 | 	if (sync_mode & SYNC_WAIT) { | 
 | 795 | 		xfs_iflock(ip); | 
 | 796 | 		goto reclaim; | 
| Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 797 | 	} | 
 | 798 |  | 
| Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 799 | 	/* | 
 | 800 | 	 * When we have to flush an inode but don't have SYNC_WAIT set, we | 
 | 801 | 	 * flush the inode out using a delwri buffer and wait for the next | 
 | 802 | 	 * call into reclaim to find it in a clean state instead of waiting for | 
 | 803 | 	 * it now. We also don't return errors here - if the error is transient | 
 | 804 | 	 * then the next reclaim pass will flush the inode, and if the error | 
| Dave Chinner | f1d486a | 2010-04-13 15:06:45 +1000 | [diff] [blame] | 805 | 	 * is permanent then the next sync reclaim will reclaim the inode and | 
| Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 806 | 	 * pass on the error. | 
 | 807 | 	 */ | 
| Dave Chinner | f1d486a | 2010-04-13 15:06:45 +1000 | [diff] [blame] | 808 | 	if (error && error != EAGAIN && !XFS_FORCED_SHUTDOWN(ip->i_mount)) { | 
| Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 809 | 		xfs_fs_cmn_err(CE_WARN, ip->i_mount, | 
 | 810 | 			"inode 0x%llx background reclaim flush failed with %d", | 
 | 811 | 			(long long)ip->i_ino, error); | 
 | 812 | 	} | 
 | 813 | out: | 
 | 814 | 	xfs_iflags_clear(ip, XFS_IRECLAIM); | 
 | 815 | 	xfs_iunlock(ip, XFS_ILOCK_EXCL); | 
 | 816 | 	/* | 
 | 817 | 	 * We could return EAGAIN here to make reclaim rescan the inode tree in | 
 | 818 | 	 * a short while. However, this just burns CPU time scanning the tree | 
 | 819 | 	 * waiting for IO to complete and xfssyncd never goes back to the idle | 
 | 820 | 	 * state. Instead, return 0 to let the next scheduled background reclaim | 
 | 821 | 	 * attempt to reclaim the inode again. | 
 | 822 | 	 */ | 
 | 823 | 	return 0; | 
 | 824 |  | 
| Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 825 | reclaim: | 
 | 826 | 	xfs_ifunlock(ip); | 
| Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 827 | 	xfs_iunlock(ip, XFS_ILOCK_EXCL); | 
| Dave Chinner | 2f11fea | 2010-07-20 17:53:25 +1000 | [diff] [blame] | 828 |  | 
 | 829 | 	XFS_STATS_INC(xs_ig_reclaims); | 
 | 830 | 	/* | 
 | 831 | 	 * Remove the inode from the per-AG radix tree. | 
 | 832 | 	 * | 
 | 833 | 	 * Because radix_tree_delete won't complain even if the item was never | 
 | 834 | 	 * added to the tree assert that it's been there before to catch | 
 | 835 | 	 * problems with the inode life time early on. | 
 | 836 | 	 */ | 
 | 837 | 	write_lock(&pag->pag_ici_lock); | 
 | 838 | 	if (!radix_tree_delete(&pag->pag_ici_root, | 
 | 839 | 				XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino))) | 
 | 840 | 		ASSERT(0); | 
 | 841 | 	write_unlock(&pag->pag_ici_lock); | 
 | 842 |  | 
 | 843 | 	/* | 
 | 844 | 	 * Here we do an (almost) spurious inode lock in order to coordinate | 
 | 845 | 	 * with inode cache radix tree lookups.  This is because the lookup | 
 | 846 | 	 * can reference the inodes in the cache without taking references. | 
 | 847 | 	 * | 
 | 848 | 	 * We make that OK here by ensuring that we wait until the inode is | 
 | 849 | 	 * unlocked after the lookup before we go ahead and free it.  We get | 
 | 850 | 	 * both the ilock and the iolock because the code may need to drop the | 
 | 851 | 	 * ilock one but will still hold the iolock. | 
 | 852 | 	 */ | 
 | 853 | 	xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); | 
 | 854 | 	xfs_qm_dqdetach(ip); | 
 | 855 | 	xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); | 
 | 856 |  | 
 | 857 | 	xfs_inode_free(ip); | 
| Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 858 | 	return error; | 
 | 859 |  | 
| David Chinner | 7a3be02 | 2008-10-30 17:37:37 +1100 | [diff] [blame] | 860 | } | 
 | 861 |  | 
| David Chinner | fce08f2 | 2008-10-30 17:37:03 +1100 | [diff] [blame] | 862 | int | 
| David Chinner | 1dc3318 | 2008-10-30 17:37:15 +1100 | [diff] [blame] | 863 | xfs_reclaim_inodes( | 
| David Chinner | fce08f2 | 2008-10-30 17:37:03 +1100 | [diff] [blame] | 864 | 	xfs_mount_t	*mp, | 
| David Chinner | fce08f2 | 2008-10-30 17:37:03 +1100 | [diff] [blame] | 865 | 	int		mode) | 
 | 866 | { | 
| Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 867 | 	return xfs_inode_ag_iterator(mp, xfs_reclaim_inode, mode, | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 868 | 					XFS_ICI_RECLAIM_TAG, 1, NULL); | 
 | 869 | } | 
 | 870 |  | 
 | 871 | /* | 
 | 872 |  * Shrinker infrastructure. | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 873 |  */ | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 874 | static int | 
 | 875 | xfs_reclaim_inode_shrink( | 
| Dave Chinner | 7f8275d | 2010-07-19 14:56:17 +1000 | [diff] [blame] | 876 | 	struct shrinker	*shrink, | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 877 | 	int		nr_to_scan, | 
 | 878 | 	gfp_t		gfp_mask) | 
 | 879 | { | 
 | 880 | 	struct xfs_mount *mp; | 
 | 881 | 	struct xfs_perag *pag; | 
 | 882 | 	xfs_agnumber_t	ag; | 
| Dave Chinner | 16fd536 | 2010-07-20 09:43:39 +1000 | [diff] [blame] | 883 | 	int		reclaimable; | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 884 |  | 
| Dave Chinner | 70e60ce | 2010-07-20 08:07:02 +1000 | [diff] [blame] | 885 | 	mp = container_of(shrink, struct xfs_mount, m_inode_shrink); | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 886 | 	if (nr_to_scan) { | 
 | 887 | 		if (!(gfp_mask & __GFP_FS)) | 
 | 888 | 			return -1; | 
 | 889 |  | 
| Dave Chinner | 70e60ce | 2010-07-20 08:07:02 +1000 | [diff] [blame] | 890 | 		xfs_inode_ag_iterator(mp, xfs_reclaim_inode, 0, | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 891 | 					XFS_ICI_RECLAIM_TAG, 1, &nr_to_scan); | 
| Dave Chinner | 70e60ce | 2010-07-20 08:07:02 +1000 | [diff] [blame] | 892 | 		/* if we don't exhaust the scan, don't bother coming back */ | 
 | 893 | 		if (nr_to_scan > 0) | 
 | 894 | 			return -1; | 
 | 895 |        } | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 896 |  | 
| Dave Chinner | 16fd536 | 2010-07-20 09:43:39 +1000 | [diff] [blame] | 897 | 	reclaimable = 0; | 
 | 898 | 	ag = 0; | 
 | 899 | 	while ((pag = xfs_inode_ag_iter_next_pag(mp, &ag, | 
 | 900 | 					XFS_ICI_RECLAIM_TAG))) { | 
| Dave Chinner | 70e60ce | 2010-07-20 08:07:02 +1000 | [diff] [blame] | 901 | 		reclaimable += pag->pag_ici_reclaimable; | 
 | 902 | 		xfs_perag_put(pag); | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 903 | 	} | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 904 | 	return reclaimable; | 
 | 905 | } | 
 | 906 |  | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 907 | void | 
 | 908 | xfs_inode_shrinker_register( | 
 | 909 | 	struct xfs_mount	*mp) | 
 | 910 | { | 
| Dave Chinner | 70e60ce | 2010-07-20 08:07:02 +1000 | [diff] [blame] | 911 | 	mp->m_inode_shrink.shrink = xfs_reclaim_inode_shrink; | 
 | 912 | 	mp->m_inode_shrink.seeks = DEFAULT_SEEKS; | 
 | 913 | 	register_shrinker(&mp->m_inode_shrink); | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 914 | } | 
 | 915 |  | 
 | 916 | void | 
 | 917 | xfs_inode_shrinker_unregister( | 
 | 918 | 	struct xfs_mount	*mp) | 
 | 919 | { | 
| Dave Chinner | 70e60ce | 2010-07-20 08:07:02 +1000 | [diff] [blame] | 920 | 	unregister_shrinker(&mp->m_inode_shrink); | 
| David Chinner | fce08f2 | 2008-10-30 17:37:03 +1100 | [diff] [blame] | 921 | } |