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