| 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 | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 42 | /* | 
 | 43 |  * The inode lookup is done in batches to keep the amount of lock traffic and | 
 | 44 |  * radix tree lookups to a minimum. The batch size is a trade off between | 
 | 45 |  * lookup reduction and stack usage. This is in the reclaim path, so we can't | 
 | 46 |  * be too greedy. | 
 | 47 |  */ | 
 | 48 | #define XFS_LOOKUP_BATCH	32 | 
 | 49 |  | 
| Dave Chinner | e13de95 | 2010-09-28 12:28:06 +1000 | [diff] [blame] | 50 | STATIC int | 
 | 51 | xfs_inode_ag_walk_grab( | 
 | 52 | 	struct xfs_inode	*ip) | 
 | 53 | { | 
 | 54 | 	struct inode		*inode = VFS_I(ip); | 
 | 55 |  | 
| Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 56 | 	ASSERT(rcu_read_lock_held()); | 
 | 57 |  | 
 | 58 | 	/* | 
 | 59 | 	 * check for stale RCU freed inode | 
 | 60 | 	 * | 
 | 61 | 	 * If the inode has been reallocated, it doesn't matter if it's not in | 
 | 62 | 	 * the AG we are walking - we are walking for writeback, so if it | 
 | 63 | 	 * passes all the "valid inode" checks and is dirty, then we'll write | 
 | 64 | 	 * it back anyway.  If it has been reallocated and still being | 
 | 65 | 	 * initialised, the XFS_INEW check below will catch it. | 
 | 66 | 	 */ | 
 | 67 | 	spin_lock(&ip->i_flags_lock); | 
 | 68 | 	if (!ip->i_ino) | 
 | 69 | 		goto out_unlock_noent; | 
 | 70 |  | 
 | 71 | 	/* avoid new or reclaimable inodes. Leave for reclaim code to flush */ | 
 | 72 | 	if (__xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM)) | 
 | 73 | 		goto out_unlock_noent; | 
 | 74 | 	spin_unlock(&ip->i_flags_lock); | 
 | 75 |  | 
| Dave Chinner | e13de95 | 2010-09-28 12:28:06 +1000 | [diff] [blame] | 76 | 	/* nothing to sync during shutdown */ | 
 | 77 | 	if (XFS_FORCED_SHUTDOWN(ip->i_mount)) | 
 | 78 | 		return EFSCORRUPTED; | 
 | 79 |  | 
| Dave Chinner | e13de95 | 2010-09-28 12:28:06 +1000 | [diff] [blame] | 80 | 	/* If we can't grab the inode, it must on it's way to reclaim. */ | 
 | 81 | 	if (!igrab(inode)) | 
 | 82 | 		return ENOENT; | 
 | 83 |  | 
 | 84 | 	if (is_bad_inode(inode)) { | 
 | 85 | 		IRELE(ip); | 
 | 86 | 		return ENOENT; | 
 | 87 | 	} | 
 | 88 |  | 
 | 89 | 	/* inode is valid */ | 
 | 90 | 	return 0; | 
| Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 91 |  | 
 | 92 | out_unlock_noent: | 
 | 93 | 	spin_unlock(&ip->i_flags_lock); | 
 | 94 | 	return ENOENT; | 
| Dave Chinner | e13de95 | 2010-09-28 12:28:06 +1000 | [diff] [blame] | 95 | } | 
 | 96 |  | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 97 | STATIC int | 
 | 98 | xfs_inode_ag_walk( | 
 | 99 | 	struct xfs_mount	*mp, | 
| Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 100 | 	struct xfs_perag	*pag, | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 101 | 	int			(*execute)(struct xfs_inode *ip, | 
 | 102 | 					   struct xfs_perag *pag, int flags), | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 103 | 	int			flags) | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 104 | { | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 105 | 	uint32_t		first_index; | 
 | 106 | 	int			last_error = 0; | 
 | 107 | 	int			skipped; | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 108 | 	int			done; | 
| Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 109 | 	int			nr_found; | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 110 |  | 
 | 111 | restart: | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 112 | 	done = 0; | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 113 | 	skipped = 0; | 
 | 114 | 	first_index = 0; | 
| Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 115 | 	nr_found = 0; | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 116 | 	do { | 
| Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 117 | 		struct xfs_inode *batch[XFS_LOOKUP_BATCH]; | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 118 | 		int		error = 0; | 
| Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 119 | 		int		i; | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 120 |  | 
| Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 121 | 		rcu_read_lock(); | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 122 | 		nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, | 
| Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 123 | 					(void **)batch, first_index, | 
 | 124 | 					XFS_LOOKUP_BATCH); | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 125 | 		if (!nr_found) { | 
| Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 126 | 			rcu_read_unlock(); | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 127 | 			break; | 
| Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 128 | 		} | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 129 |  | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 130 | 		/* | 
| Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 131 | 		 * Grab the inodes before we drop the lock. if we found | 
 | 132 | 		 * nothing, nr == 0 and the loop will be skipped. | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 133 | 		 */ | 
| Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 134 | 		for (i = 0; i < nr_found; i++) { | 
 | 135 | 			struct xfs_inode *ip = batch[i]; | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 136 |  | 
| Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 137 | 			if (done || xfs_inode_ag_walk_grab(ip)) | 
 | 138 | 				batch[i] = NULL; | 
 | 139 |  | 
 | 140 | 			/* | 
| Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 141 | 			 * Update the index for the next lookup. Catch | 
 | 142 | 			 * overflows into the next AG range which can occur if | 
 | 143 | 			 * we have inodes in the last block of the AG and we | 
 | 144 | 			 * are currently pointing to the last inode. | 
 | 145 | 			 * | 
 | 146 | 			 * Because we may see inodes that are from the wrong AG | 
 | 147 | 			 * due to RCU freeing and reallocation, only update the | 
 | 148 | 			 * index if it lies in this AG. It was a race that lead | 
 | 149 | 			 * us to see this inode, so another lookup from the | 
 | 150 | 			 * same index will not find it again. | 
| Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 151 | 			 */ | 
| Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 152 | 			if (XFS_INO_TO_AGNO(mp, ip->i_ino) != pag->pag_agno) | 
 | 153 | 				continue; | 
| Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 154 | 			first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1); | 
 | 155 | 			if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino)) | 
 | 156 | 				done = 1; | 
| Dave Chinner | e13de95 | 2010-09-28 12:28:06 +1000 | [diff] [blame] | 157 | 		} | 
| Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 158 |  | 
 | 159 | 		/* unlock now we've grabbed the inodes. */ | 
| Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 160 | 		rcu_read_unlock(); | 
| Dave Chinner | e13de95 | 2010-09-28 12:28:06 +1000 | [diff] [blame] | 161 |  | 
| Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 162 | 		for (i = 0; i < nr_found; i++) { | 
 | 163 | 			if (!batch[i]) | 
 | 164 | 				continue; | 
 | 165 | 			error = execute(batch[i], pag, flags); | 
 | 166 | 			IRELE(batch[i]); | 
 | 167 | 			if (error == EAGAIN) { | 
 | 168 | 				skipped++; | 
 | 169 | 				continue; | 
 | 170 | 			} | 
 | 171 | 			if (error && last_error != EFSCORRUPTED) | 
 | 172 | 				last_error = error; | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 173 | 		} | 
| Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 174 |  | 
 | 175 | 		/* bail out if the filesystem is corrupted.  */ | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 176 | 		if (error == EFSCORRUPTED) | 
 | 177 | 			break; | 
 | 178 |  | 
| Dave Chinner | 78ae525 | 2010-09-28 12:28:19 +1000 | [diff] [blame] | 179 | 	} while (nr_found && !done); | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 180 |  | 
 | 181 | 	if (skipped) { | 
 | 182 | 		delay(1); | 
 | 183 | 		goto restart; | 
 | 184 | 	} | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 185 | 	return last_error; | 
 | 186 | } | 
 | 187 |  | 
| Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 188 | int | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 189 | xfs_inode_ag_iterator( | 
 | 190 | 	struct xfs_mount	*mp, | 
 | 191 | 	int			(*execute)(struct xfs_inode *ip, | 
 | 192 | 					   struct xfs_perag *pag, int flags), | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 193 | 	int			flags) | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 194 | { | 
| Dave Chinner | 16fd536 | 2010-07-20 09:43:39 +1000 | [diff] [blame] | 195 | 	struct xfs_perag	*pag; | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 196 | 	int			error = 0; | 
 | 197 | 	int			last_error = 0; | 
 | 198 | 	xfs_agnumber_t		ag; | 
 | 199 |  | 
| Dave Chinner | 16fd536 | 2010-07-20 09:43:39 +1000 | [diff] [blame] | 200 | 	ag = 0; | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 201 | 	while ((pag = xfs_perag_get(mp, ag))) { | 
 | 202 | 		ag = pag->pag_agno + 1; | 
 | 203 | 		error = xfs_inode_ag_walk(mp, pag, execute, flags); | 
| Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 204 | 		xfs_perag_put(pag); | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 205 | 		if (error) { | 
 | 206 | 			last_error = error; | 
 | 207 | 			if (error == EFSCORRUPTED) | 
 | 208 | 				break; | 
 | 209 | 		} | 
 | 210 | 	} | 
 | 211 | 	return XFS_ERROR(last_error); | 
 | 212 | } | 
 | 213 |  | 
| Dave Chinner | 5a34d5c | 2009-06-08 15:35:03 +0200 | [diff] [blame] | 214 | STATIC int | 
 | 215 | xfs_sync_inode_data( | 
 | 216 | 	struct xfs_inode	*ip, | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 217 | 	struct xfs_perag	*pag, | 
| Dave Chinner | 5a34d5c | 2009-06-08 15:35:03 +0200 | [diff] [blame] | 218 | 	int			flags) | 
 | 219 | { | 
 | 220 | 	struct inode		*inode = VFS_I(ip); | 
 | 221 | 	struct address_space *mapping = inode->i_mapping; | 
 | 222 | 	int			error = 0; | 
 | 223 |  | 
 | 224 | 	if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) | 
 | 225 | 		goto out_wait; | 
 | 226 |  | 
 | 227 | 	if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED)) { | 
 | 228 | 		if (flags & SYNC_TRYLOCK) | 
 | 229 | 			goto out_wait; | 
 | 230 | 		xfs_ilock(ip, XFS_IOLOCK_SHARED); | 
 | 231 | 	} | 
 | 232 |  | 
 | 233 | 	error = xfs_flush_pages(ip, 0, -1, (flags & SYNC_WAIT) ? | 
| Christoph Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 234 | 				0 : XBF_ASYNC, FI_NONE); | 
| Dave Chinner | 5a34d5c | 2009-06-08 15:35:03 +0200 | [diff] [blame] | 235 | 	xfs_iunlock(ip, XFS_IOLOCK_SHARED); | 
 | 236 |  | 
 | 237 |  out_wait: | 
| Christoph Hellwig | b0710cc | 2009-06-08 15:37:11 +0200 | [diff] [blame] | 238 | 	if (flags & SYNC_WAIT) | 
| Dave Chinner | 5a34d5c | 2009-06-08 15:35:03 +0200 | [diff] [blame] | 239 | 		xfs_ioend_wait(ip); | 
 | 240 | 	return error; | 
 | 241 | } | 
 | 242 |  | 
| Christoph Hellwig | 845b6d0 | 2009-06-08 15:35:05 +0200 | [diff] [blame] | 243 | STATIC int | 
 | 244 | xfs_sync_inode_attr( | 
 | 245 | 	struct xfs_inode	*ip, | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 246 | 	struct xfs_perag	*pag, | 
| Christoph Hellwig | 845b6d0 | 2009-06-08 15:35:05 +0200 | [diff] [blame] | 247 | 	int			flags) | 
 | 248 | { | 
 | 249 | 	int			error = 0; | 
 | 250 |  | 
 | 251 | 	xfs_ilock(ip, XFS_ILOCK_SHARED); | 
 | 252 | 	if (xfs_inode_clean(ip)) | 
 | 253 | 		goto out_unlock; | 
 | 254 | 	if (!xfs_iflock_nowait(ip)) { | 
 | 255 | 		if (!(flags & SYNC_WAIT)) | 
 | 256 | 			goto out_unlock; | 
 | 257 | 		xfs_iflock(ip); | 
 | 258 | 	} | 
 | 259 |  | 
 | 260 | 	if (xfs_inode_clean(ip)) { | 
 | 261 | 		xfs_ifunlock(ip); | 
 | 262 | 		goto out_unlock; | 
 | 263 | 	} | 
 | 264 |  | 
| Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 265 | 	error = xfs_iflush(ip, flags); | 
| Christoph Hellwig | 845b6d0 | 2009-06-08 15:35:05 +0200 | [diff] [blame] | 266 |  | 
 | 267 |  out_unlock: | 
 | 268 | 	xfs_iunlock(ip, XFS_ILOCK_SHARED); | 
 | 269 | 	return error; | 
 | 270 | } | 
 | 271 |  | 
| Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 272 | /* | 
 | 273 |  * Write out pagecache data for the whole filesystem. | 
 | 274 |  */ | 
| Christoph Hellwig | 64c8614 | 2010-06-24 11:45:34 +1000 | [diff] [blame] | 275 | STATIC int | 
| Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 276 | xfs_sync_data( | 
 | 277 | 	struct xfs_mount	*mp, | 
 | 278 | 	int			flags) | 
| David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 279 | { | 
| Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 280 | 	int			error; | 
| David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 281 |  | 
| Christoph Hellwig | b0710cc | 2009-06-08 15:37:11 +0200 | [diff] [blame] | 282 | 	ASSERT((flags & ~(SYNC_TRYLOCK|SYNC_WAIT)) == 0); | 
| David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 283 |  | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 284 | 	error = xfs_inode_ag_iterator(mp, xfs_sync_inode_data, flags); | 
| Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 285 | 	if (error) | 
 | 286 | 		return XFS_ERROR(error); | 
| David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 287 |  | 
| Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 288 | 	xfs_log_force(mp, (flags & SYNC_WAIT) ? XFS_LOG_SYNC : 0); | 
| Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 289 | 	return 0; | 
 | 290 | } | 
| David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 291 |  | 
| Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 292 | /* | 
 | 293 |  * Write out inode metadata (attributes) for the whole filesystem. | 
 | 294 |  */ | 
| Christoph Hellwig | 64c8614 | 2010-06-24 11:45:34 +1000 | [diff] [blame] | 295 | STATIC int | 
| Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 296 | xfs_sync_attr( | 
 | 297 | 	struct xfs_mount	*mp, | 
 | 298 | 	int			flags) | 
 | 299 | { | 
 | 300 | 	ASSERT((flags & ~SYNC_WAIT) == 0); | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 301 |  | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 302 | 	return xfs_inode_ag_iterator(mp, xfs_sync_inode_attr, flags); | 
| David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 303 | } | 
 | 304 |  | 
| Christoph Hellwig | 2af75df | 2008-10-30 17:14:53 +1100 | [diff] [blame] | 305 | STATIC int | 
| Christoph Hellwig | 2af75df | 2008-10-30 17:14:53 +1100 | [diff] [blame] | 306 | xfs_sync_fsdata( | 
| Christoph Hellwig | df308bc | 2010-03-12 10:59:16 +0000 | [diff] [blame] | 307 | 	struct xfs_mount	*mp) | 
| Christoph Hellwig | 2af75df | 2008-10-30 17:14:53 +1100 | [diff] [blame] | 308 | { | 
 | 309 | 	struct xfs_buf		*bp; | 
| Christoph Hellwig | 2af75df | 2008-10-30 17:14:53 +1100 | [diff] [blame] | 310 |  | 
 | 311 | 	/* | 
| Christoph Hellwig | df308bc | 2010-03-12 10:59:16 +0000 | [diff] [blame] | 312 | 	 * If the buffer is pinned then push on the log so we won't get stuck | 
 | 313 | 	 * waiting in the write for someone, maybe ourselves, to flush the log. | 
 | 314 | 	 * | 
 | 315 | 	 * Even though we just pushed the log above, we did not have the | 
 | 316 | 	 * superblock buffer locked at that point so it can become pinned in | 
 | 317 | 	 * between there and here. | 
| Christoph Hellwig | 2af75df | 2008-10-30 17:14:53 +1100 | [diff] [blame] | 318 | 	 */ | 
| Christoph Hellwig | df308bc | 2010-03-12 10:59:16 +0000 | [diff] [blame] | 319 | 	bp = xfs_getsb(mp, 0); | 
 | 320 | 	if (XFS_BUF_ISPINNED(bp)) | 
 | 321 | 		xfs_log_force(mp, 0); | 
| Christoph Hellwig | 2af75df | 2008-10-30 17:14:53 +1100 | [diff] [blame] | 322 |  | 
| Christoph Hellwig | df308bc | 2010-03-12 10:59:16 +0000 | [diff] [blame] | 323 | 	return xfs_bwrite(mp, bp); | 
| Christoph Hellwig | 2af75df | 2008-10-30 17:14:53 +1100 | [diff] [blame] | 324 | } | 
 | 325 |  | 
| David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 326 | /* | 
| David Chinner | a4e4c4f | 2008-10-30 17:16:11 +1100 | [diff] [blame] | 327 |  * When remounting a filesystem read-only or freezing the filesystem, we have | 
 | 328 |  * two phases to execute. This first phase is syncing the data before we | 
 | 329 |  * quiesce the filesystem, and the second is flushing all the inodes out after | 
 | 330 |  * we've waited for all the transactions created by the first phase to | 
 | 331 |  * complete. The second phase ensures that the inodes are written to their | 
 | 332 |  * location on disk rather than just existing in transactions in the log. This | 
 | 333 |  * means after a quiesce there is no log replay required to write the inodes to | 
 | 334 |  * disk (this is the main difference between a sync and a quiesce). | 
 | 335 |  */ | 
 | 336 | /* | 
 | 337 |  * 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] | 338 |  * so we flush delwri and delalloc buffers here, then wait for all I/O to | 
 | 339 |  * complete.  Data is frozen at that point. Metadata is not frozen, | 
| David Chinner | a4e4c4f | 2008-10-30 17:16:11 +1100 | [diff] [blame] | 340 |  * transactions can still occur here so don't bother flushing the buftarg | 
 | 341 |  * because it'll just get dirty again. | 
| David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 342 |  */ | 
 | 343 | int | 
 | 344 | xfs_quiesce_data( | 
 | 345 | 	struct xfs_mount	*mp) | 
 | 346 | { | 
| Christoph Hellwig | df308bc | 2010-03-12 10:59:16 +0000 | [diff] [blame] | 347 | 	int			error, error2 = 0; | 
| David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 348 |  | 
 | 349 | 	/* push non-blocking */ | 
| Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 350 | 	xfs_sync_data(mp, 0); | 
| Christoph Hellwig | 8b5403a | 2009-06-08 15:37:16 +0200 | [diff] [blame] | 351 | 	xfs_qm_sync(mp, SYNC_TRYLOCK); | 
| David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 352 |  | 
| Dave Chinner | c90b07e | 2009-10-06 20:29:27 +0000 | [diff] [blame] | 353 | 	/* push and block till complete */ | 
| Christoph Hellwig | b0710cc | 2009-06-08 15:37:11 +0200 | [diff] [blame] | 354 | 	xfs_sync_data(mp, SYNC_WAIT); | 
| Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 355 | 	xfs_qm_sync(mp, SYNC_WAIT); | 
| David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 356 |  | 
| David Chinner | a4e4c4f | 2008-10-30 17:16:11 +1100 | [diff] [blame] | 357 | 	/* write superblock and hoover up shutdown errors */ | 
| Christoph Hellwig | df308bc | 2010-03-12 10:59:16 +0000 | [diff] [blame] | 358 | 	error = xfs_sync_fsdata(mp); | 
 | 359 |  | 
 | 360 | 	/* make sure all delwri buffers are written out */ | 
 | 361 | 	xfs_flush_buftarg(mp->m_ddev_targp, 1); | 
 | 362 |  | 
 | 363 | 	/* mark the log as covered if needed */ | 
 | 364 | 	if (xfs_log_need_covered(mp)) | 
| Dave Chinner | c58efdb | 2011-01-04 04:49:29 +0000 | [diff] [blame] | 365 | 		error2 = xfs_fs_log_dummy(mp); | 
| David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 366 |  | 
| David Chinner | a4e4c4f | 2008-10-30 17:16:11 +1100 | [diff] [blame] | 367 | 	/* flush data-only devices */ | 
| David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 368 | 	if (mp->m_rtdev_targp) | 
 | 369 | 		XFS_bflush(mp->m_rtdev_targp); | 
 | 370 |  | 
| Christoph Hellwig | df308bc | 2010-03-12 10:59:16 +0000 | [diff] [blame] | 371 | 	return error ? error : error2; | 
| David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 372 | } | 
 | 373 |  | 
| David Chinner | 76bf105 | 2008-10-30 17:16:21 +1100 | [diff] [blame] | 374 | STATIC void | 
 | 375 | xfs_quiesce_fs( | 
 | 376 | 	struct xfs_mount	*mp) | 
 | 377 | { | 
 | 378 | 	int	count = 0, pincount; | 
 | 379 |  | 
| Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 380 | 	xfs_reclaim_inodes(mp, 0); | 
| David Chinner | 76bf105 | 2008-10-30 17:16:21 +1100 | [diff] [blame] | 381 | 	xfs_flush_buftarg(mp->m_ddev_targp, 0); | 
| David Chinner | 76bf105 | 2008-10-30 17:16:21 +1100 | [diff] [blame] | 382 |  | 
 | 383 | 	/* | 
 | 384 | 	 * This loop must run at least twice.  The first instance of the loop | 
 | 385 | 	 * will flush most meta data but that will generate more meta data | 
 | 386 | 	 * (typically directory updates).  Which then must be flushed and | 
| Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 387 | 	 * logged before we can write the unmount record. We also so sync | 
 | 388 | 	 * reclaim of inodes to catch any that the above delwri flush skipped. | 
| David Chinner | 76bf105 | 2008-10-30 17:16:21 +1100 | [diff] [blame] | 389 | 	 */ | 
 | 390 | 	do { | 
| Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 391 | 		xfs_reclaim_inodes(mp, SYNC_WAIT); | 
| Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 392 | 		xfs_sync_attr(mp, SYNC_WAIT); | 
| David Chinner | 76bf105 | 2008-10-30 17:16:21 +1100 | [diff] [blame] | 393 | 		pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1); | 
 | 394 | 		if (!pincount) { | 
 | 395 | 			delay(50); | 
 | 396 | 			count++; | 
 | 397 | 		} | 
 | 398 | 	} while (count < 2); | 
 | 399 | } | 
 | 400 |  | 
 | 401 | /* | 
 | 402 |  * Second stage of a quiesce. The data is already synced, now we have to take | 
 | 403 |  * care of the metadata. New transactions are already blocked, so we need to | 
 | 404 |  * wait for any remaining transactions to drain out before proceding. | 
 | 405 |  */ | 
 | 406 | void | 
 | 407 | xfs_quiesce_attr( | 
 | 408 | 	struct xfs_mount	*mp) | 
 | 409 | { | 
 | 410 | 	int	error = 0; | 
 | 411 |  | 
 | 412 | 	/* wait for all modifications to complete */ | 
 | 413 | 	while (atomic_read(&mp->m_active_trans) > 0) | 
 | 414 | 		delay(100); | 
 | 415 |  | 
 | 416 | 	/* flush inodes and push all remaining buffers out to disk */ | 
 | 417 | 	xfs_quiesce_fs(mp); | 
 | 418 |  | 
| Felix Blyakher | 5e10657 | 2009-01-22 21:34:05 -0600 | [diff] [blame] | 419 | 	/* | 
 | 420 | 	 * Just warn here till VFS can correctly support | 
 | 421 | 	 * read-only remount without racing. | 
 | 422 | 	 */ | 
 | 423 | 	WARN_ON(atomic_read(&mp->m_active_trans) != 0); | 
| David Chinner | 76bf105 | 2008-10-30 17:16:21 +1100 | [diff] [blame] | 424 |  | 
 | 425 | 	/* Push the superblock and write an unmount record */ | 
 | 426 | 	error = xfs_log_sbcount(mp, 1); | 
 | 427 | 	if (error) | 
 | 428 | 		xfs_fs_cmn_err(CE_WARN, mp, | 
 | 429 | 				"xfs_attr_quiesce: failed to log sb changes. " | 
 | 430 | 				"Frozen image may not be consistent."); | 
 | 431 | 	xfs_log_unmount_write(mp); | 
 | 432 | 	xfs_unmountfs_writesb(mp); | 
 | 433 | } | 
 | 434 |  | 
| David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 435 | /* | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 436 |  * Enqueue a work item to be picked up by the vfs xfssyncd thread. | 
 | 437 |  * Doing this has two advantages: | 
 | 438 |  * - It saves on stack space, which is tight in certain situations | 
 | 439 |  * - It can be used (with care) as a mechanism to avoid deadlocks. | 
 | 440 |  * Flushing while allocating in a full filesystem requires both. | 
 | 441 |  */ | 
 | 442 | STATIC void | 
 | 443 | xfs_syncd_queue_work( | 
 | 444 | 	struct xfs_mount *mp, | 
 | 445 | 	void		*data, | 
| Dave Chinner | e43afd7 | 2009-04-06 18:47:27 +0200 | [diff] [blame] | 446 | 	void		(*syncer)(struct xfs_mount *, void *), | 
 | 447 | 	struct completion *completion) | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 448 | { | 
| Dave Chinner | a8d770d | 2009-04-06 18:44:54 +0200 | [diff] [blame] | 449 | 	struct xfs_sync_work *work; | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 450 |  | 
| Dave Chinner | a8d770d | 2009-04-06 18:44:54 +0200 | [diff] [blame] | 451 | 	work = kmem_alloc(sizeof(struct xfs_sync_work), KM_SLEEP); | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 452 | 	INIT_LIST_HEAD(&work->w_list); | 
 | 453 | 	work->w_syncer = syncer; | 
 | 454 | 	work->w_data = data; | 
 | 455 | 	work->w_mount = mp; | 
| Dave Chinner | e43afd7 | 2009-04-06 18:47:27 +0200 | [diff] [blame] | 456 | 	work->w_completion = completion; | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 457 | 	spin_lock(&mp->m_sync_lock); | 
 | 458 | 	list_add_tail(&work->w_list, &mp->m_sync_list); | 
 | 459 | 	spin_unlock(&mp->m_sync_lock); | 
 | 460 | 	wake_up_process(mp->m_sync_task); | 
 | 461 | } | 
 | 462 |  | 
 | 463 | /* | 
 | 464 |  * Flush delayed allocate data, attempting to free up reserved space | 
 | 465 |  * from existing allocations.  At this point a new allocation attempt | 
 | 466 |  * has failed with ENOSPC and we are in the process of scratching our | 
 | 467 |  * heads, looking about for more room... | 
 | 468 |  */ | 
 | 469 | STATIC void | 
| Dave Chinner | a8d770d | 2009-04-06 18:44:54 +0200 | [diff] [blame] | 470 | xfs_flush_inodes_work( | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 471 | 	struct xfs_mount *mp, | 
 | 472 | 	void		*arg) | 
 | 473 | { | 
 | 474 | 	struct inode	*inode = arg; | 
| Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 475 | 	xfs_sync_data(mp, SYNC_TRYLOCK); | 
| Christoph Hellwig | b0710cc | 2009-06-08 15:37:11 +0200 | [diff] [blame] | 476 | 	xfs_sync_data(mp, SYNC_TRYLOCK | SYNC_WAIT); | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 477 | 	iput(inode); | 
 | 478 | } | 
 | 479 |  | 
 | 480 | void | 
| Dave Chinner | a8d770d | 2009-04-06 18:44:54 +0200 | [diff] [blame] | 481 | xfs_flush_inodes( | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 482 | 	xfs_inode_t	*ip) | 
 | 483 | { | 
 | 484 | 	struct inode	*inode = VFS_I(ip); | 
| Dave Chinner | e43afd7 | 2009-04-06 18:47:27 +0200 | [diff] [blame] | 485 | 	DECLARE_COMPLETION_ONSTACK(completion); | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 486 |  | 
 | 487 | 	igrab(inode); | 
| Dave Chinner | e43afd7 | 2009-04-06 18:47:27 +0200 | [diff] [blame] | 488 | 	xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inodes_work, &completion); | 
 | 489 | 	wait_for_completion(&completion); | 
| Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 490 | 	xfs_log_force(ip->i_mount, XFS_LOG_SYNC); | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 491 | } | 
 | 492 |  | 
| David Chinner | aacaa88 | 2008-10-30 17:15:29 +1100 | [diff] [blame] | 493 | /* | 
| Christoph Hellwig | df308bc | 2010-03-12 10:59:16 +0000 | [diff] [blame] | 494 |  * Every sync period we need to unpin all items, reclaim inodes and sync | 
 | 495 |  * 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] | 496 |  * filesystem is idle and not frozen. | 
| David Chinner | aacaa88 | 2008-10-30 17:15:29 +1100 | [diff] [blame] | 497 |  */ | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 498 | STATIC void | 
 | 499 | xfs_sync_worker( | 
 | 500 | 	struct xfs_mount *mp, | 
 | 501 | 	void		*unused) | 
 | 502 | { | 
 | 503 | 	int		error; | 
 | 504 |  | 
| David Chinner | aacaa88 | 2008-10-30 17:15:29 +1100 | [diff] [blame] | 505 | 	if (!(mp->m_flags & XFS_MOUNT_RDONLY)) { | 
| David Chinner | aacaa88 | 2008-10-30 17:15:29 +1100 | [diff] [blame] | 506 | 		/* dgc: errors ignored here */ | 
| Dave Chinner | 1a387d3 | 2010-08-24 11:46:31 +1000 | [diff] [blame] | 507 | 		if (mp->m_super->s_frozen == SB_UNFROZEN && | 
 | 508 | 		    xfs_log_need_covered(mp)) | 
| Dave Chinner | c58efdb | 2011-01-04 04:49:29 +0000 | [diff] [blame] | 509 | 			error = xfs_fs_log_dummy(mp); | 
 | 510 | 		else | 
 | 511 | 			xfs_log_force(mp, 0); | 
 | 512 | 		xfs_reclaim_inodes(mp, 0); | 
 | 513 | 		error = xfs_qm_sync(mp, SYNC_TRYLOCK); | 
| David Chinner | aacaa88 | 2008-10-30 17:15:29 +1100 | [diff] [blame] | 514 | 	} | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 515 | 	mp->m_sync_seq++; | 
 | 516 | 	wake_up(&mp->m_wait_single_sync_task); | 
 | 517 | } | 
 | 518 |  | 
 | 519 | STATIC int | 
 | 520 | xfssyncd( | 
 | 521 | 	void			*arg) | 
 | 522 | { | 
 | 523 | 	struct xfs_mount	*mp = arg; | 
 | 524 | 	long			timeleft; | 
| Dave Chinner | a8d770d | 2009-04-06 18:44:54 +0200 | [diff] [blame] | 525 | 	xfs_sync_work_t		*work, *n; | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 526 | 	LIST_HEAD		(tmp); | 
 | 527 |  | 
 | 528 | 	set_freezable(); | 
 | 529 | 	timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10); | 
 | 530 | 	for (;;) { | 
| Dave Chinner | 20f6b2c | 2010-03-04 01:46:23 +0000 | [diff] [blame] | 531 | 		if (list_empty(&mp->m_sync_list)) | 
 | 532 | 			timeleft = schedule_timeout_interruptible(timeleft); | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 533 | 		/* swsusp */ | 
 | 534 | 		try_to_freeze(); | 
 | 535 | 		if (kthread_should_stop() && list_empty(&mp->m_sync_list)) | 
 | 536 | 			break; | 
 | 537 |  | 
 | 538 | 		spin_lock(&mp->m_sync_lock); | 
 | 539 | 		/* | 
 | 540 | 		 * We can get woken by laptop mode, to do a sync - | 
 | 541 | 		 * that's the (only!) case where the list would be | 
 | 542 | 		 * empty with time remaining. | 
 | 543 | 		 */ | 
 | 544 | 		if (!timeleft || list_empty(&mp->m_sync_list)) { | 
 | 545 | 			if (!timeleft) | 
 | 546 | 				timeleft = xfs_syncd_centisecs * | 
 | 547 | 							msecs_to_jiffies(10); | 
 | 548 | 			INIT_LIST_HEAD(&mp->m_sync_work.w_list); | 
 | 549 | 			list_add_tail(&mp->m_sync_work.w_list, | 
 | 550 | 					&mp->m_sync_list); | 
 | 551 | 		} | 
| Dave Chinner | 20f6b2c | 2010-03-04 01:46:23 +0000 | [diff] [blame] | 552 | 		list_splice_init(&mp->m_sync_list, &tmp); | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 553 | 		spin_unlock(&mp->m_sync_lock); | 
 | 554 |  | 
 | 555 | 		list_for_each_entry_safe(work, n, &tmp, w_list) { | 
 | 556 | 			(*work->w_syncer)(mp, work->w_data); | 
 | 557 | 			list_del(&work->w_list); | 
 | 558 | 			if (work == &mp->m_sync_work) | 
 | 559 | 				continue; | 
| Dave Chinner | e43afd7 | 2009-04-06 18:47:27 +0200 | [diff] [blame] | 560 | 			if (work->w_completion) | 
 | 561 | 				complete(work->w_completion); | 
| David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 562 | 			kmem_free(work); | 
 | 563 | 		} | 
 | 564 | 	} | 
 | 565 |  | 
 | 566 | 	return 0; | 
 | 567 | } | 
 | 568 |  | 
 | 569 | int | 
 | 570 | xfs_syncd_init( | 
 | 571 | 	struct xfs_mount	*mp) | 
 | 572 | { | 
 | 573 | 	mp->m_sync_work.w_syncer = xfs_sync_worker; | 
 | 574 | 	mp->m_sync_work.w_mount = mp; | 
| Dave Chinner | e43afd7 | 2009-04-06 18:47:27 +0200 | [diff] [blame] | 575 | 	mp->m_sync_work.w_completion = NULL; | 
| Jan Engelhardt | e2a0781 | 2010-03-23 09:52:55 +1100 | [diff] [blame] | 576 | 	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] | 577 | 	if (IS_ERR(mp->m_sync_task)) | 
 | 578 | 		return -PTR_ERR(mp->m_sync_task); | 
 | 579 | 	return 0; | 
 | 580 | } | 
 | 581 |  | 
 | 582 | void | 
 | 583 | xfs_syncd_stop( | 
 | 584 | 	struct xfs_mount	*mp) | 
 | 585 | { | 
 | 586 | 	kthread_stop(mp->m_sync_task); | 
 | 587 | } | 
 | 588 |  | 
| Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 589 | void | 
 | 590 | __xfs_inode_set_reclaim_tag( | 
 | 591 | 	struct xfs_perag	*pag, | 
 | 592 | 	struct xfs_inode	*ip) | 
 | 593 | { | 
 | 594 | 	radix_tree_tag_set(&pag->pag_ici_root, | 
 | 595 | 			   XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino), | 
 | 596 | 			   XFS_ICI_RECLAIM_TAG); | 
| Dave Chinner | 16fd536 | 2010-07-20 09:43:39 +1000 | [diff] [blame] | 597 |  | 
 | 598 | 	if (!pag->pag_ici_reclaimable) { | 
 | 599 | 		/* propagate the reclaim tag up into the perag radix tree */ | 
 | 600 | 		spin_lock(&ip->i_mount->m_perag_lock); | 
 | 601 | 		radix_tree_tag_set(&ip->i_mount->m_perag_tree, | 
 | 602 | 				XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino), | 
 | 603 | 				XFS_ICI_RECLAIM_TAG); | 
 | 604 | 		spin_unlock(&ip->i_mount->m_perag_lock); | 
 | 605 | 		trace_xfs_perag_set_reclaim(ip->i_mount, pag->pag_agno, | 
 | 606 | 							-1, _RET_IP_); | 
 | 607 | 	} | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 608 | 	pag->pag_ici_reclaimable++; | 
| Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 609 | } | 
 | 610 |  | 
| David Chinner | 1165451 | 2008-10-30 17:37:49 +1100 | [diff] [blame] | 611 | /* | 
 | 612 |  * We set the inode flag atomically with the radix tree tag. | 
 | 613 |  * Once we get tag lookups on the radix tree, this inode flag | 
 | 614 |  * can go away. | 
 | 615 |  */ | 
| David Chinner | 396beb8 | 2008-10-30 17:37:26 +1100 | [diff] [blame] | 616 | void | 
 | 617 | xfs_inode_set_reclaim_tag( | 
 | 618 | 	xfs_inode_t	*ip) | 
 | 619 | { | 
| Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 620 | 	struct xfs_mount *mp = ip->i_mount; | 
 | 621 | 	struct xfs_perag *pag; | 
| David Chinner | 396beb8 | 2008-10-30 17:37:26 +1100 | [diff] [blame] | 622 |  | 
| Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 623 | 	pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino)); | 
| Dave Chinner | 1a427ab | 2010-12-16 17:08:41 +1100 | [diff] [blame] | 624 | 	spin_lock(&pag->pag_ici_lock); | 
| David Chinner | 396beb8 | 2008-10-30 17:37:26 +1100 | [diff] [blame] | 625 | 	spin_lock(&ip->i_flags_lock); | 
| Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 626 | 	__xfs_inode_set_reclaim_tag(pag, ip); | 
| David Chinner | 1165451 | 2008-10-30 17:37:49 +1100 | [diff] [blame] | 627 | 	__xfs_iflags_set(ip, XFS_IRECLAIMABLE); | 
| David Chinner | 396beb8 | 2008-10-30 17:37:26 +1100 | [diff] [blame] | 628 | 	spin_unlock(&ip->i_flags_lock); | 
| Dave Chinner | 1a427ab | 2010-12-16 17:08:41 +1100 | [diff] [blame] | 629 | 	spin_unlock(&pag->pag_ici_lock); | 
| Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 630 | 	xfs_perag_put(pag); | 
| David Chinner | 396beb8 | 2008-10-30 17:37:26 +1100 | [diff] [blame] | 631 | } | 
 | 632 |  | 
| Johannes Weiner | 081003f | 2010-10-01 07:43:54 +0000 | [diff] [blame] | 633 | STATIC void | 
 | 634 | __xfs_inode_clear_reclaim( | 
| David Chinner | 396beb8 | 2008-10-30 17:37:26 +1100 | [diff] [blame] | 635 | 	xfs_perag_t	*pag, | 
 | 636 | 	xfs_inode_t	*ip) | 
 | 637 | { | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 638 | 	pag->pag_ici_reclaimable--; | 
| Dave Chinner | 16fd536 | 2010-07-20 09:43:39 +1000 | [diff] [blame] | 639 | 	if (!pag->pag_ici_reclaimable) { | 
 | 640 | 		/* clear the reclaim tag from the perag radix tree */ | 
 | 641 | 		spin_lock(&ip->i_mount->m_perag_lock); | 
 | 642 | 		radix_tree_tag_clear(&ip->i_mount->m_perag_tree, | 
 | 643 | 				XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino), | 
 | 644 | 				XFS_ICI_RECLAIM_TAG); | 
 | 645 | 		spin_unlock(&ip->i_mount->m_perag_lock); | 
 | 646 | 		trace_xfs_perag_clear_reclaim(ip->i_mount, pag->pag_agno, | 
 | 647 | 							-1, _RET_IP_); | 
 | 648 | 	} | 
| David Chinner | 396beb8 | 2008-10-30 17:37:26 +1100 | [diff] [blame] | 649 | } | 
 | 650 |  | 
| Johannes Weiner | 081003f | 2010-10-01 07:43:54 +0000 | [diff] [blame] | 651 | void | 
 | 652 | __xfs_inode_clear_reclaim_tag( | 
 | 653 | 	xfs_mount_t	*mp, | 
 | 654 | 	xfs_perag_t	*pag, | 
 | 655 | 	xfs_inode_t	*ip) | 
 | 656 | { | 
 | 657 | 	radix_tree_tag_clear(&pag->pag_ici_root, | 
 | 658 | 			XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG); | 
 | 659 | 	__xfs_inode_clear_reclaim(pag, ip); | 
 | 660 | } | 
 | 661 |  | 
| Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 662 | /* | 
| Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 663 |  * Grab the inode for reclaim exclusively. | 
 | 664 |  * Return 0 if we grabbed it, non-zero otherwise. | 
 | 665 |  */ | 
 | 666 | STATIC int | 
 | 667 | xfs_reclaim_inode_grab( | 
 | 668 | 	struct xfs_inode	*ip, | 
 | 669 | 	int			flags) | 
 | 670 | { | 
| Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 671 | 	ASSERT(rcu_read_lock_held()); | 
 | 672 |  | 
 | 673 | 	/* quick check for stale RCU freed inode */ | 
 | 674 | 	if (!ip->i_ino) | 
 | 675 | 		return 1; | 
| Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 676 |  | 
 | 677 | 	/* | 
| Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 678 | 	 * do some unlocked checks first to avoid unnecessary lock traffic. | 
| Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 679 | 	 * The first is a flush lock check, the second is a already in reclaim | 
 | 680 | 	 * check. Only do these checks if we are not going to block on locks. | 
 | 681 | 	 */ | 
 | 682 | 	if ((flags & SYNC_TRYLOCK) && | 
 | 683 | 	    (!ip->i_flush.done || __xfs_iflags_test(ip, XFS_IRECLAIM))) { | 
 | 684 | 		return 1; | 
 | 685 | 	} | 
 | 686 |  | 
 | 687 | 	/* | 
 | 688 | 	 * The radix tree lock here protects a thread in xfs_iget from racing | 
 | 689 | 	 * with us starting reclaim on the inode.  Once we have the | 
 | 690 | 	 * XFS_IRECLAIM flag set it will not touch us. | 
| Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 691 | 	 * | 
 | 692 | 	 * Due to RCU lookup, we may find inodes that have been freed and only | 
 | 693 | 	 * have XFS_IRECLAIM set.  Indeed, we may see reallocated inodes that | 
 | 694 | 	 * aren't candidates for reclaim at all, so we must check the | 
 | 695 | 	 * XFS_IRECLAIMABLE is set first before proceeding to reclaim. | 
| Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 696 | 	 */ | 
 | 697 | 	spin_lock(&ip->i_flags_lock); | 
| Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 698 | 	if (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) || | 
 | 699 | 	    __xfs_iflags_test(ip, XFS_IRECLAIM)) { | 
 | 700 | 		/* not a reclaim candidate. */ | 
| Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 701 | 		spin_unlock(&ip->i_flags_lock); | 
 | 702 | 		return 1; | 
 | 703 | 	} | 
 | 704 | 	__xfs_iflags_set(ip, XFS_IRECLAIM); | 
 | 705 | 	spin_unlock(&ip->i_flags_lock); | 
 | 706 | 	return 0; | 
 | 707 | } | 
 | 708 |  | 
 | 709 | /* | 
| Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 710 |  * Inodes in different states need to be treated differently, and the return | 
 | 711 |  * value of xfs_iflush is not sufficient to get this right. The following table | 
 | 712 |  * lists the inode states and the reclaim actions necessary for non-blocking | 
 | 713 |  * reclaim: | 
 | 714 |  * | 
 | 715 |  * | 
 | 716 |  *	inode state	     iflush ret		required action | 
 | 717 |  *      ---------------      ----------         --------------- | 
 | 718 |  *	bad			-		reclaim | 
 | 719 |  *	shutdown		EIO		unpin and reclaim | 
 | 720 |  *	clean, unpinned		0		reclaim | 
 | 721 |  *	stale, unpinned		0		reclaim | 
| Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 722 |  *	clean, pinned(*)	0		requeue | 
 | 723 |  *	stale, pinned		EAGAIN		requeue | 
 | 724 |  *	dirty, delwri ok	0		requeue | 
 | 725 |  *	dirty, delwri blocked	EAGAIN		requeue | 
 | 726 |  *	dirty, sync flush	0		reclaim | 
| Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 727 |  * | 
 | 728 |  * (*) dgc: I don't think the clean, pinned state is possible but it gets | 
 | 729 |  * handled anyway given the order of checks implemented. | 
 | 730 |  * | 
| Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 731 |  * As can be seen from the table, the return value of xfs_iflush() is not | 
 | 732 |  * sufficient to correctly decide the reclaim action here. The checks in | 
 | 733 |  * xfs_iflush() might look like duplicates, but they are not. | 
 | 734 |  * | 
 | 735 |  * Also, because we get the flush lock first, we know that any inode that has | 
 | 736 |  * been flushed delwri has had the flush completed by the time we check that | 
 | 737 |  * the inode is clean. The clean inode check needs to be done before flushing | 
 | 738 |  * the inode delwri otherwise we would loop forever requeuing clean inodes as | 
 | 739 |  * we cannot tell apart a successful delwri flush and a clean inode from the | 
 | 740 |  * return value of xfs_iflush(). | 
 | 741 |  * | 
 | 742 |  * Note that because the inode is flushed delayed write by background | 
 | 743 |  * writeback, the flush lock may already be held here and waiting on it can | 
 | 744 |  * result in very long latencies. Hence for sync reclaims, where we wait on the | 
 | 745 |  * flush lock, the caller should push out delayed write inodes first before | 
 | 746 |  * trying to reclaim them to minimise the amount of time spent waiting. For | 
 | 747 |  * background relaim, we just requeue the inode for the next pass. | 
 | 748 |  * | 
| Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 749 |  * Hence the order of actions after gaining the locks should be: | 
 | 750 |  *	bad		=> reclaim | 
 | 751 |  *	shutdown	=> unpin and reclaim | 
| Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 752 |  *	pinned, delwri	=> requeue | 
 | 753 |  *	pinned, sync	=> unpin | 
| Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 754 |  *	stale		=> reclaim | 
 | 755 |  *	clean		=> reclaim | 
| Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 756 |  *	dirty, delwri	=> flush and requeue | 
 | 757 |  *	dirty, sync	=> flush, wait and reclaim | 
| Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 758 |  */ | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 759 | STATIC int | 
| Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 760 | xfs_reclaim_inode( | 
| Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 761 | 	struct xfs_inode	*ip, | 
 | 762 | 	struct xfs_perag	*pag, | 
| Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 763 | 	int			sync_mode) | 
| David Chinner | 7a3be02 | 2008-10-30 17:37:37 +1100 | [diff] [blame] | 764 | { | 
| Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 765 | 	int	error = 0; | 
| Dave Chinner | 777df5a | 2010-02-06 12:37:26 +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 | 	 */ | 
| Dave Chinner | 1a427ab | 2010-12-16 17:08:41 +1100 | [diff] [blame] | 837 | 	spin_lock(&pag->pag_ici_lock); | 
| Dave Chinner | 2f11fea | 2010-07-20 17:53:25 +1000 | [diff] [blame] | 838 | 	if (!radix_tree_delete(&pag->pag_ici_root, | 
 | 839 | 				XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino))) | 
 | 840 | 		ASSERT(0); | 
| Johannes Weiner | 081003f | 2010-10-01 07:43:54 +0000 | [diff] [blame] | 841 | 	__xfs_inode_clear_reclaim(pag, ip); | 
| Dave Chinner | 1a427ab | 2010-12-16 17:08:41 +1100 | [diff] [blame] | 842 | 	spin_unlock(&pag->pag_ici_lock); | 
| Dave Chinner | 2f11fea | 2010-07-20 17:53:25 +1000 | [diff] [blame] | 843 |  | 
 | 844 | 	/* | 
 | 845 | 	 * Here we do an (almost) spurious inode lock in order to coordinate | 
 | 846 | 	 * with inode cache radix tree lookups.  This is because the lookup | 
 | 847 | 	 * can reference the inodes in the cache without taking references. | 
 | 848 | 	 * | 
 | 849 | 	 * We make that OK here by ensuring that we wait until the inode is | 
 | 850 | 	 * unlocked after the lookup before we go ahead and free it.  We get | 
 | 851 | 	 * both the ilock and the iolock because the code may need to drop the | 
 | 852 | 	 * ilock one but will still hold the iolock. | 
 | 853 | 	 */ | 
 | 854 | 	xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); | 
 | 855 | 	xfs_qm_dqdetach(ip); | 
 | 856 | 	xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); | 
 | 857 |  | 
 | 858 | 	xfs_inode_free(ip); | 
| Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 859 | 	return error; | 
 | 860 |  | 
| David Chinner | 7a3be02 | 2008-10-30 17:37:37 +1100 | [diff] [blame] | 861 | } | 
 | 862 |  | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 863 | /* | 
 | 864 |  * Walk the AGs and reclaim the inodes in them. Even if the filesystem is | 
 | 865 |  * corrupted, we still want to try to reclaim all the inodes. If we don't, | 
 | 866 |  * then a shut down during filesystem unmount reclaim walk leak all the | 
 | 867 |  * unreclaimed inodes. | 
 | 868 |  */ | 
 | 869 | int | 
 | 870 | xfs_reclaim_inodes_ag( | 
 | 871 | 	struct xfs_mount	*mp, | 
 | 872 | 	int			flags, | 
 | 873 | 	int			*nr_to_scan) | 
 | 874 | { | 
 | 875 | 	struct xfs_perag	*pag; | 
 | 876 | 	int			error = 0; | 
 | 877 | 	int			last_error = 0; | 
 | 878 | 	xfs_agnumber_t		ag; | 
| Dave Chinner | 69b491c | 2010-09-27 11:09:51 +1000 | [diff] [blame] | 879 | 	int			trylock = flags & SYNC_TRYLOCK; | 
 | 880 | 	int			skipped; | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 881 |  | 
| Dave Chinner | 69b491c | 2010-09-27 11:09:51 +1000 | [diff] [blame] | 882 | restart: | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 883 | 	ag = 0; | 
| Dave Chinner | 69b491c | 2010-09-27 11:09:51 +1000 | [diff] [blame] | 884 | 	skipped = 0; | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 885 | 	while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) { | 
 | 886 | 		unsigned long	first_index = 0; | 
 | 887 | 		int		done = 0; | 
| Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 888 | 		int		nr_found = 0; | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 889 |  | 
 | 890 | 		ag = pag->pag_agno + 1; | 
 | 891 |  | 
| Dave Chinner | 69b491c | 2010-09-27 11:09:51 +1000 | [diff] [blame] | 892 | 		if (trylock) { | 
 | 893 | 			if (!mutex_trylock(&pag->pag_ici_reclaim_lock)) { | 
 | 894 | 				skipped++; | 
| Dave Chinner | f83282a | 2010-11-08 08:55:04 +0000 | [diff] [blame] | 895 | 				xfs_perag_put(pag); | 
| Dave Chinner | 69b491c | 2010-09-27 11:09:51 +1000 | [diff] [blame] | 896 | 				continue; | 
 | 897 | 			} | 
 | 898 | 			first_index = pag->pag_ici_reclaim_cursor; | 
 | 899 | 		} else | 
 | 900 | 			mutex_lock(&pag->pag_ici_reclaim_lock); | 
 | 901 |  | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 902 | 		do { | 
| Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 903 | 			struct xfs_inode *batch[XFS_LOOKUP_BATCH]; | 
 | 904 | 			int	i; | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 905 |  | 
| Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 906 | 			rcu_read_lock(); | 
| Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 907 | 			nr_found = radix_tree_gang_lookup_tag( | 
 | 908 | 					&pag->pag_ici_root, | 
 | 909 | 					(void **)batch, first_index, | 
 | 910 | 					XFS_LOOKUP_BATCH, | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 911 | 					XFS_ICI_RECLAIM_TAG); | 
 | 912 | 			if (!nr_found) { | 
| Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 913 | 				rcu_read_unlock(); | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 914 | 				break; | 
 | 915 | 			} | 
 | 916 |  | 
 | 917 | 			/* | 
| Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 918 | 			 * Grab the inodes before we drop the lock. if we found | 
 | 919 | 			 * nothing, nr == 0 and the loop will be skipped. | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 920 | 			 */ | 
| Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 921 | 			for (i = 0; i < nr_found; i++) { | 
 | 922 | 				struct xfs_inode *ip = batch[i]; | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 923 |  | 
| Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 924 | 				if (done || xfs_reclaim_inode_grab(ip, flags)) | 
 | 925 | 					batch[i] = NULL; | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 926 |  | 
| Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 927 | 				/* | 
 | 928 | 				 * Update the index for the next lookup. Catch | 
 | 929 | 				 * overflows into the next AG range which can | 
 | 930 | 				 * occur if we have inodes in the last block of | 
 | 931 | 				 * the AG and we are currently pointing to the | 
 | 932 | 				 * last inode. | 
| Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 933 | 				 * | 
 | 934 | 				 * Because we may see inodes that are from the | 
 | 935 | 				 * wrong AG due to RCU freeing and | 
 | 936 | 				 * reallocation, only update the index if it | 
 | 937 | 				 * lies in this AG. It was a race that lead us | 
 | 938 | 				 * to see this inode, so another lookup from | 
 | 939 | 				 * the same index will not find it again. | 
| Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 940 | 				 */ | 
| Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 941 | 				if (XFS_INO_TO_AGNO(mp, ip->i_ino) != | 
 | 942 | 								pag->pag_agno) | 
 | 943 | 					continue; | 
| Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 944 | 				first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1); | 
 | 945 | 				if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino)) | 
 | 946 | 					done = 1; | 
 | 947 | 			} | 
 | 948 |  | 
 | 949 | 			/* unlock now we've grabbed the inodes. */ | 
| Dave Chinner | 1a3e8f3 | 2010-12-17 17:29:43 +1100 | [diff] [blame] | 950 | 			rcu_read_unlock(); | 
| Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 951 |  | 
 | 952 | 			for (i = 0; i < nr_found; i++) { | 
 | 953 | 				if (!batch[i]) | 
 | 954 | 					continue; | 
 | 955 | 				error = xfs_reclaim_inode(batch[i], pag, flags); | 
 | 956 | 				if (error && last_error != EFSCORRUPTED) | 
 | 957 | 					last_error = error; | 
 | 958 | 			} | 
 | 959 |  | 
 | 960 | 			*nr_to_scan -= XFS_LOOKUP_BATCH; | 
 | 961 |  | 
 | 962 | 		} while (nr_found && !done && *nr_to_scan > 0); | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 963 |  | 
| Dave Chinner | 69b491c | 2010-09-27 11:09:51 +1000 | [diff] [blame] | 964 | 		if (trylock && !done) | 
 | 965 | 			pag->pag_ici_reclaim_cursor = first_index; | 
 | 966 | 		else | 
 | 967 | 			pag->pag_ici_reclaim_cursor = 0; | 
 | 968 | 		mutex_unlock(&pag->pag_ici_reclaim_lock); | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 969 | 		xfs_perag_put(pag); | 
 | 970 | 	} | 
| Dave Chinner | 69b491c | 2010-09-27 11:09:51 +1000 | [diff] [blame] | 971 |  | 
 | 972 | 	/* | 
 | 973 | 	 * if we skipped any AG, and we still have scan count remaining, do | 
 | 974 | 	 * another pass this time using blocking reclaim semantics (i.e | 
 | 975 | 	 * waiting on the reclaim locks and ignoring the reclaim cursors). This | 
 | 976 | 	 * ensure that when we get more reclaimers than AGs we block rather | 
 | 977 | 	 * than spin trying to execute reclaim. | 
 | 978 | 	 */ | 
 | 979 | 	if (trylock && skipped && *nr_to_scan > 0) { | 
 | 980 | 		trylock = 0; | 
 | 981 | 		goto restart; | 
 | 982 | 	} | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 983 | 	return XFS_ERROR(last_error); | 
 | 984 | } | 
 | 985 |  | 
| David Chinner | fce08f2 | 2008-10-30 17:37:03 +1100 | [diff] [blame] | 986 | int | 
| David Chinner | 1dc3318 | 2008-10-30 17:37:15 +1100 | [diff] [blame] | 987 | xfs_reclaim_inodes( | 
| David Chinner | fce08f2 | 2008-10-30 17:37:03 +1100 | [diff] [blame] | 988 | 	xfs_mount_t	*mp, | 
| David Chinner | fce08f2 | 2008-10-30 17:37:03 +1100 | [diff] [blame] | 989 | 	int		mode) | 
 | 990 | { | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 991 | 	int		nr_to_scan = INT_MAX; | 
 | 992 |  | 
 | 993 | 	return xfs_reclaim_inodes_ag(mp, mode, &nr_to_scan); | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 994 | } | 
 | 995 |  | 
 | 996 | /* | 
 | 997 |  * Shrinker infrastructure. | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 998 |  */ | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 999 | static int | 
 | 1000 | xfs_reclaim_inode_shrink( | 
| Dave Chinner | 7f8275d | 2010-07-19 14:56:17 +1000 | [diff] [blame] | 1001 | 	struct shrinker	*shrink, | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 1002 | 	int		nr_to_scan, | 
 | 1003 | 	gfp_t		gfp_mask) | 
 | 1004 | { | 
 | 1005 | 	struct xfs_mount *mp; | 
 | 1006 | 	struct xfs_perag *pag; | 
 | 1007 | 	xfs_agnumber_t	ag; | 
| Dave Chinner | 16fd536 | 2010-07-20 09:43:39 +1000 | [diff] [blame] | 1008 | 	int		reclaimable; | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 1009 |  | 
| Dave Chinner | 70e60ce | 2010-07-20 08:07:02 +1000 | [diff] [blame] | 1010 | 	mp = container_of(shrink, struct xfs_mount, m_inode_shrink); | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 1011 | 	if (nr_to_scan) { | 
 | 1012 | 		if (!(gfp_mask & __GFP_FS)) | 
 | 1013 | 			return -1; | 
 | 1014 |  | 
| Dave Chinner | e3a20c0 | 2010-09-24 19:51:50 +1000 | [diff] [blame] | 1015 | 		xfs_reclaim_inodes_ag(mp, SYNC_TRYLOCK, &nr_to_scan); | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1016 | 		/* terminate if we don't exhaust the scan */ | 
| Dave Chinner | 70e60ce | 2010-07-20 08:07:02 +1000 | [diff] [blame] | 1017 | 		if (nr_to_scan > 0) | 
 | 1018 | 			return -1; | 
 | 1019 |        } | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 1020 |  | 
| Dave Chinner | 16fd536 | 2010-07-20 09:43:39 +1000 | [diff] [blame] | 1021 | 	reclaimable = 0; | 
 | 1022 | 	ag = 0; | 
| Dave Chinner | 65d0f20 | 2010-09-24 18:40:15 +1000 | [diff] [blame] | 1023 | 	while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) { | 
 | 1024 | 		ag = pag->pag_agno + 1; | 
| Dave Chinner | 70e60ce | 2010-07-20 08:07:02 +1000 | [diff] [blame] | 1025 | 		reclaimable += pag->pag_ici_reclaimable; | 
 | 1026 | 		xfs_perag_put(pag); | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 1027 | 	} | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 1028 | 	return reclaimable; | 
 | 1029 | } | 
 | 1030 |  | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 1031 | void | 
 | 1032 | xfs_inode_shrinker_register( | 
 | 1033 | 	struct xfs_mount	*mp) | 
 | 1034 | { | 
| Dave Chinner | 70e60ce | 2010-07-20 08:07:02 +1000 | [diff] [blame] | 1035 | 	mp->m_inode_shrink.shrink = xfs_reclaim_inode_shrink; | 
 | 1036 | 	mp->m_inode_shrink.seeks = DEFAULT_SEEKS; | 
 | 1037 | 	register_shrinker(&mp->m_inode_shrink); | 
| Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 1038 | } | 
 | 1039 |  | 
 | 1040 | void | 
 | 1041 | xfs_inode_shrinker_unregister( | 
 | 1042 | 	struct xfs_mount	*mp) | 
 | 1043 | { | 
| Dave Chinner | 70e60ce | 2010-07-20 08:07:02 +1000 | [diff] [blame] | 1044 | 	unregister_shrinker(&mp->m_inode_shrink); | 
| David Chinner | fce08f2 | 2008-10-30 17:37:03 +1100 | [diff] [blame] | 1045 | } |