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