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