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