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, |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 98 | int exclusive, |
| 99 | int *nr_to_scan) |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 100 | { |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 101 | uint32_t first_index; |
| 102 | int last_error = 0; |
| 103 | int skipped; |
| 104 | |
| 105 | restart: |
| 106 | skipped = 0; |
| 107 | first_index = 0; |
| 108 | do { |
| 109 | int error = 0; |
| 110 | xfs_inode_t *ip; |
| 111 | |
Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 112 | if (exclusive) |
| 113 | write_lock(&pag->pag_ici_lock); |
| 114 | else |
| 115 | read_lock(&pag->pag_ici_lock); |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 116 | ip = xfs_inode_ag_lookup(mp, pag, &first_index, tag); |
Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 117 | if (!ip) { |
| 118 | if (exclusive) |
| 119 | write_unlock(&pag->pag_ici_lock); |
| 120 | else |
| 121 | read_unlock(&pag->pag_ici_lock); |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 122 | break; |
Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 123 | } |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 124 | |
Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 125 | /* execute releases pag->pag_ici_lock */ |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 126 | error = execute(ip, pag, flags); |
| 127 | if (error == EAGAIN) { |
| 128 | skipped++; |
| 129 | continue; |
| 130 | } |
| 131 | if (error) |
| 132 | last_error = error; |
Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 133 | |
| 134 | /* bail out if the filesystem is corrupted. */ |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 135 | if (error == EFSCORRUPTED) |
| 136 | break; |
| 137 | |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 138 | } while ((*nr_to_scan)--); |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 139 | |
| 140 | if (skipped) { |
| 141 | delay(1); |
| 142 | goto restart; |
| 143 | } |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 144 | return last_error; |
| 145 | } |
| 146 | |
Dave Chinner | 16fd536 | 2010-07-20 09:43:39 +1000 | [diff] [blame] | 147 | /* |
| 148 | * Select the next per-ag structure to iterate during the walk. The reclaim |
| 149 | * walk is optimised only to walk AGs with reclaimable inodes in them. |
| 150 | */ |
| 151 | static struct xfs_perag * |
| 152 | xfs_inode_ag_iter_next_pag( |
| 153 | struct xfs_mount *mp, |
| 154 | xfs_agnumber_t *first, |
| 155 | int tag) |
| 156 | { |
| 157 | struct xfs_perag *pag = NULL; |
| 158 | |
| 159 | if (tag == XFS_ICI_RECLAIM_TAG) { |
| 160 | int found; |
| 161 | int ref; |
| 162 | |
| 163 | spin_lock(&mp->m_perag_lock); |
| 164 | found = radix_tree_gang_lookup_tag(&mp->m_perag_tree, |
| 165 | (void **)&pag, *first, 1, tag); |
| 166 | if (found <= 0) { |
| 167 | spin_unlock(&mp->m_perag_lock); |
| 168 | return NULL; |
| 169 | } |
| 170 | *first = pag->pag_agno + 1; |
| 171 | /* open coded pag reference increment */ |
| 172 | ref = atomic_inc_return(&pag->pag_ref); |
| 173 | spin_unlock(&mp->m_perag_lock); |
| 174 | trace_xfs_perag_get_reclaim(mp, pag->pag_agno, ref, _RET_IP_); |
| 175 | } else { |
| 176 | pag = xfs_perag_get(mp, *first); |
| 177 | (*first)++; |
| 178 | } |
| 179 | return pag; |
| 180 | } |
| 181 | |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 182 | int |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 183 | xfs_inode_ag_iterator( |
| 184 | struct xfs_mount *mp, |
| 185 | int (*execute)(struct xfs_inode *ip, |
| 186 | struct xfs_perag *pag, int flags), |
| 187 | int flags, |
Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 188 | int tag, |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 189 | int exclusive, |
| 190 | int *nr_to_scan) |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 191 | { |
Dave Chinner | 16fd536 | 2010-07-20 09:43:39 +1000 | [diff] [blame] | 192 | struct xfs_perag *pag; |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 193 | int error = 0; |
| 194 | int last_error = 0; |
| 195 | xfs_agnumber_t ag; |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 196 | int nr; |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 197 | |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 198 | nr = nr_to_scan ? *nr_to_scan : INT_MAX; |
Dave Chinner | 16fd536 | 2010-07-20 09:43:39 +1000 | [diff] [blame] | 199 | ag = 0; |
| 200 | while ((pag = xfs_inode_ag_iter_next_pag(mp, &ag, tag))) { |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 201 | error = xfs_inode_ag_walk(mp, pag, execute, flags, tag, |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 202 | exclusive, &nr); |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 203 | xfs_perag_put(pag); |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 204 | if (error) { |
| 205 | last_error = error; |
| 206 | if (error == EFSCORRUPTED) |
| 207 | break; |
| 208 | } |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 209 | if (nr <= 0) |
| 210 | break; |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 211 | } |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 212 | if (nr_to_scan) |
| 213 | *nr_to_scan = nr; |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 214 | return XFS_ERROR(last_error); |
| 215 | } |
| 216 | |
Dave Chinner | 1da8eec | 2009-06-08 15:35:07 +0200 | [diff] [blame] | 217 | /* must be called with pag_ici_lock held and releases it */ |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame] | 218 | int |
Dave Chinner | 1da8eec | 2009-06-08 15:35:07 +0200 | [diff] [blame] | 219 | xfs_sync_inode_valid( |
| 220 | struct xfs_inode *ip, |
| 221 | struct xfs_perag *pag) |
| 222 | { |
| 223 | struct inode *inode = VFS_I(ip); |
Dave Chinner | 018027b | 2010-01-10 23:51:46 +0000 | [diff] [blame] | 224 | int error = EFSCORRUPTED; |
Dave Chinner | 1da8eec | 2009-06-08 15:35:07 +0200 | [diff] [blame] | 225 | |
| 226 | /* nothing to sync during shutdown */ |
Dave Chinner | 018027b | 2010-01-10 23:51:46 +0000 | [diff] [blame] | 227 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
| 228 | goto out_unlock; |
Dave Chinner | 1da8eec | 2009-06-08 15:35:07 +0200 | [diff] [blame] | 229 | |
Dave Chinner | 018027b | 2010-01-10 23:51:46 +0000 | [diff] [blame] | 230 | /* avoid new or reclaimable inodes. Leave for reclaim code to flush */ |
| 231 | error = ENOENT; |
| 232 | if (xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM)) |
| 233 | goto out_unlock; |
Dave Chinner | 1da8eec | 2009-06-08 15:35:07 +0200 | [diff] [blame] | 234 | |
Dave Chinner | 018027b | 2010-01-10 23:51:46 +0000 | [diff] [blame] | 235 | /* If we can't grab the inode, it must on it's way to reclaim. */ |
| 236 | if (!igrab(inode)) |
| 237 | goto out_unlock; |
| 238 | |
| 239 | if (is_bad_inode(inode)) { |
Dave Chinner | 1da8eec | 2009-06-08 15:35:07 +0200 | [diff] [blame] | 240 | IRELE(ip); |
Dave Chinner | 018027b | 2010-01-10 23:51:46 +0000 | [diff] [blame] | 241 | goto out_unlock; |
Dave Chinner | 1da8eec | 2009-06-08 15:35:07 +0200 | [diff] [blame] | 242 | } |
| 243 | |
Dave Chinner | 018027b | 2010-01-10 23:51:46 +0000 | [diff] [blame] | 244 | /* inode is valid */ |
| 245 | error = 0; |
| 246 | out_unlock: |
| 247 | read_unlock(&pag->pag_ici_lock); |
| 248 | return error; |
Dave Chinner | 1da8eec | 2009-06-08 15:35:07 +0200 | [diff] [blame] | 249 | } |
| 250 | |
Dave Chinner | 5a34d5c | 2009-06-08 15:35:03 +0200 | [diff] [blame] | 251 | STATIC int |
| 252 | xfs_sync_inode_data( |
| 253 | struct xfs_inode *ip, |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 254 | struct xfs_perag *pag, |
Dave Chinner | 5a34d5c | 2009-06-08 15:35:03 +0200 | [diff] [blame] | 255 | int flags) |
| 256 | { |
| 257 | struct inode *inode = VFS_I(ip); |
| 258 | struct address_space *mapping = inode->i_mapping; |
| 259 | int error = 0; |
| 260 | |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 261 | error = xfs_sync_inode_valid(ip, pag); |
| 262 | if (error) |
| 263 | return error; |
| 264 | |
Dave Chinner | 5a34d5c | 2009-06-08 15:35:03 +0200 | [diff] [blame] | 265 | if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) |
| 266 | goto out_wait; |
| 267 | |
| 268 | if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED)) { |
| 269 | if (flags & SYNC_TRYLOCK) |
| 270 | goto out_wait; |
| 271 | xfs_ilock(ip, XFS_IOLOCK_SHARED); |
| 272 | } |
| 273 | |
| 274 | error = xfs_flush_pages(ip, 0, -1, (flags & SYNC_WAIT) ? |
Christoph Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 275 | 0 : XBF_ASYNC, FI_NONE); |
Dave Chinner | 5a34d5c | 2009-06-08 15:35:03 +0200 | [diff] [blame] | 276 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
| 277 | |
| 278 | out_wait: |
Christoph Hellwig | b0710cc | 2009-06-08 15:37:11 +0200 | [diff] [blame] | 279 | if (flags & SYNC_WAIT) |
Dave Chinner | 5a34d5c | 2009-06-08 15:35:03 +0200 | [diff] [blame] | 280 | xfs_ioend_wait(ip); |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 281 | IRELE(ip); |
Dave Chinner | 5a34d5c | 2009-06-08 15:35:03 +0200 | [diff] [blame] | 282 | return error; |
| 283 | } |
| 284 | |
Christoph Hellwig | 845b6d0 | 2009-06-08 15:35:05 +0200 | [diff] [blame] | 285 | STATIC int |
| 286 | xfs_sync_inode_attr( |
| 287 | struct xfs_inode *ip, |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 288 | struct xfs_perag *pag, |
Christoph Hellwig | 845b6d0 | 2009-06-08 15:35:05 +0200 | [diff] [blame] | 289 | int flags) |
| 290 | { |
| 291 | int error = 0; |
| 292 | |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 293 | error = xfs_sync_inode_valid(ip, pag); |
| 294 | if (error) |
| 295 | return error; |
| 296 | |
Christoph Hellwig | 845b6d0 | 2009-06-08 15:35:05 +0200 | [diff] [blame] | 297 | xfs_ilock(ip, XFS_ILOCK_SHARED); |
| 298 | if (xfs_inode_clean(ip)) |
| 299 | goto out_unlock; |
| 300 | if (!xfs_iflock_nowait(ip)) { |
| 301 | if (!(flags & SYNC_WAIT)) |
| 302 | goto out_unlock; |
| 303 | xfs_iflock(ip); |
| 304 | } |
| 305 | |
| 306 | if (xfs_inode_clean(ip)) { |
| 307 | xfs_ifunlock(ip); |
| 308 | goto out_unlock; |
| 309 | } |
| 310 | |
Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 311 | error = xfs_iflush(ip, flags); |
Christoph Hellwig | 845b6d0 | 2009-06-08 15:35:05 +0200 | [diff] [blame] | 312 | |
| 313 | out_unlock: |
| 314 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 315 | IRELE(ip); |
Christoph Hellwig | 845b6d0 | 2009-06-08 15:35:05 +0200 | [diff] [blame] | 316 | return error; |
| 317 | } |
| 318 | |
Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 319 | /* |
| 320 | * Write out pagecache data for the whole filesystem. |
| 321 | */ |
David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 322 | int |
Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 323 | xfs_sync_data( |
| 324 | struct xfs_mount *mp, |
| 325 | int flags) |
David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 326 | { |
Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 327 | int error; |
David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 328 | |
Christoph Hellwig | b0710cc | 2009-06-08 15:37:11 +0200 | [diff] [blame] | 329 | ASSERT((flags & ~(SYNC_TRYLOCK|SYNC_WAIT)) == 0); |
David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 330 | |
Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 331 | error = xfs_inode_ag_iterator(mp, xfs_sync_inode_data, flags, |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 332 | XFS_ICI_NO_TAG, 0, NULL); |
Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 333 | if (error) |
| 334 | return XFS_ERROR(error); |
David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 335 | |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 336 | xfs_log_force(mp, (flags & SYNC_WAIT) ? XFS_LOG_SYNC : 0); |
Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 337 | return 0; |
| 338 | } |
David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 339 | |
Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 340 | /* |
| 341 | * Write out inode metadata (attributes) for the whole filesystem. |
| 342 | */ |
| 343 | int |
| 344 | xfs_sync_attr( |
| 345 | struct xfs_mount *mp, |
| 346 | int flags) |
| 347 | { |
| 348 | ASSERT((flags & ~SYNC_WAIT) == 0); |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 349 | |
Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 350 | return xfs_inode_ag_iterator(mp, xfs_sync_inode_attr, flags, |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 351 | XFS_ICI_NO_TAG, 0, NULL); |
David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 352 | } |
| 353 | |
Christoph Hellwig | 2af75df | 2008-10-30 17:14:53 +1100 | [diff] [blame] | 354 | STATIC int |
| 355 | xfs_commit_dummy_trans( |
| 356 | struct xfs_mount *mp, |
Dave Chinner | dce5065 | 2009-10-06 20:29:30 +0000 | [diff] [blame] | 357 | uint flags) |
Christoph Hellwig | 2af75df | 2008-10-30 17:14:53 +1100 | [diff] [blame] | 358 | { |
| 359 | struct xfs_inode *ip = mp->m_rootip; |
| 360 | struct xfs_trans *tp; |
| 361 | int error; |
| 362 | |
| 363 | /* |
| 364 | * Put a dummy transaction in the log to tell recovery |
| 365 | * that all others are OK. |
| 366 | */ |
| 367 | tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1); |
| 368 | error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0); |
| 369 | if (error) { |
| 370 | xfs_trans_cancel(tp, 0); |
| 371 | return error; |
| 372 | } |
| 373 | |
| 374 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 375 | |
| 376 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 377 | xfs_trans_ihold(tp, ip); |
| 378 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Christoph Hellwig | 2af75df | 2008-10-30 17:14:53 +1100 | [diff] [blame] | 379 | error = xfs_trans_commit(tp, 0); |
Christoph Hellwig | 2af75df | 2008-10-30 17:14:53 +1100 | [diff] [blame] | 380 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 381 | |
Dave Chinner | dce5065 | 2009-10-06 20:29:30 +0000 | [diff] [blame] | 382 | /* the log force ensures this transaction is pushed to disk */ |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 383 | xfs_log_force(mp, (flags & SYNC_WAIT) ? XFS_LOG_SYNC : 0); |
Dave Chinner | dce5065 | 2009-10-06 20:29:30 +0000 | [diff] [blame] | 384 | return error; |
Christoph Hellwig | 2af75df | 2008-10-30 17:14:53 +1100 | [diff] [blame] | 385 | } |
| 386 | |
Eric Sandeen | 5d77c0d | 2009-11-19 15:52:00 +0000 | [diff] [blame] | 387 | STATIC int |
Christoph Hellwig | 2af75df | 2008-10-30 17:14:53 +1100 | [diff] [blame] | 388 | xfs_sync_fsdata( |
Christoph Hellwig | df308bc | 2010-03-12 10:59:16 +0000 | [diff] [blame] | 389 | struct xfs_mount *mp) |
Christoph Hellwig | 2af75df | 2008-10-30 17:14:53 +1100 | [diff] [blame] | 390 | { |
| 391 | struct xfs_buf *bp; |
Christoph Hellwig | 2af75df | 2008-10-30 17:14:53 +1100 | [diff] [blame] | 392 | |
| 393 | /* |
Christoph Hellwig | df308bc | 2010-03-12 10:59:16 +0000 | [diff] [blame] | 394 | * If the buffer is pinned then push on the log so we won't get stuck |
| 395 | * waiting in the write for someone, maybe ourselves, to flush the log. |
| 396 | * |
| 397 | * Even though we just pushed the log above, we did not have the |
| 398 | * superblock buffer locked at that point so it can become pinned in |
| 399 | * between there and here. |
Christoph Hellwig | 2af75df | 2008-10-30 17:14:53 +1100 | [diff] [blame] | 400 | */ |
Christoph Hellwig | df308bc | 2010-03-12 10:59:16 +0000 | [diff] [blame] | 401 | bp = xfs_getsb(mp, 0); |
| 402 | if (XFS_BUF_ISPINNED(bp)) |
| 403 | xfs_log_force(mp, 0); |
Christoph Hellwig | 2af75df | 2008-10-30 17:14:53 +1100 | [diff] [blame] | 404 | |
Christoph Hellwig | df308bc | 2010-03-12 10:59:16 +0000 | [diff] [blame] | 405 | return xfs_bwrite(mp, bp); |
Christoph Hellwig | 2af75df | 2008-10-30 17:14:53 +1100 | [diff] [blame] | 406 | } |
| 407 | |
David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 408 | /* |
David Chinner | a4e4c4f | 2008-10-30 17:16:11 +1100 | [diff] [blame] | 409 | * When remounting a filesystem read-only or freezing the filesystem, we have |
| 410 | * two phases to execute. This first phase is syncing the data before we |
| 411 | * quiesce the filesystem, and the second is flushing all the inodes out after |
| 412 | * we've waited for all the transactions created by the first phase to |
| 413 | * complete. The second phase ensures that the inodes are written to their |
| 414 | * location on disk rather than just existing in transactions in the log. This |
| 415 | * means after a quiesce there is no log replay required to write the inodes to |
| 416 | * disk (this is the main difference between a sync and a quiesce). |
| 417 | */ |
| 418 | /* |
| 419 | * 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] | 420 | * so we flush delwri and delalloc buffers here, then wait for all I/O to |
| 421 | * complete. Data is frozen at that point. Metadata is not frozen, |
David Chinner | a4e4c4f | 2008-10-30 17:16:11 +1100 | [diff] [blame] | 422 | * transactions can still occur here so don't bother flushing the buftarg |
| 423 | * because it'll just get dirty again. |
David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 424 | */ |
| 425 | int |
| 426 | xfs_quiesce_data( |
| 427 | struct xfs_mount *mp) |
| 428 | { |
Christoph Hellwig | df308bc | 2010-03-12 10:59:16 +0000 | [diff] [blame] | 429 | int error, error2 = 0; |
David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 430 | |
| 431 | /* push non-blocking */ |
Christoph Hellwig | 075fe10 | 2009-06-08 15:35:48 +0200 | [diff] [blame] | 432 | xfs_sync_data(mp, 0); |
Christoph Hellwig | 8b5403a | 2009-06-08 15:37:16 +0200 | [diff] [blame] | 433 | xfs_qm_sync(mp, SYNC_TRYLOCK); |
David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 434 | |
Dave Chinner | c90b07e | 2009-10-06 20:29:27 +0000 | [diff] [blame] | 435 | /* push and block till complete */ |
Christoph Hellwig | b0710cc | 2009-06-08 15:37:11 +0200 | [diff] [blame] | 436 | xfs_sync_data(mp, SYNC_WAIT); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 437 | xfs_qm_sync(mp, SYNC_WAIT); |
David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 438 | |
David Chinner | a4e4c4f | 2008-10-30 17:16:11 +1100 | [diff] [blame] | 439 | /* write superblock and hoover up shutdown errors */ |
Christoph Hellwig | df308bc | 2010-03-12 10:59:16 +0000 | [diff] [blame] | 440 | error = xfs_sync_fsdata(mp); |
| 441 | |
| 442 | /* make sure all delwri buffers are written out */ |
| 443 | xfs_flush_buftarg(mp->m_ddev_targp, 1); |
| 444 | |
| 445 | /* mark the log as covered if needed */ |
| 446 | if (xfs_log_need_covered(mp)) |
| 447 | error2 = xfs_commit_dummy_trans(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 | |
Christoph Hellwig | df308bc | 2010-03-12 10:59:16 +0000 | [diff] [blame] | 453 | return error ? error : error2; |
David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 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 | /* |
Christoph Hellwig | df308bc | 2010-03-12 10:59:16 +0000 | [diff] [blame] | 576 | * Every sync period we need to unpin all items, reclaim inodes and sync |
| 577 | * disk quotas. We might need to cover the log to indicate that the |
| 578 | * filesystem is idle. |
David Chinner | aacaa88 | 2008-10-30 17:15:29 +1100 | [diff] [blame] | 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); |
Christoph Hellwig | df308bc | 2010-03-12 10:59:16 +0000 | [diff] [blame] | 592 | if (xfs_log_need_covered(mp)) |
| 593 | error = xfs_commit_dummy_trans(mp, 0); |
David Chinner | aacaa88 | 2008-10-30 17:15:29 +1100 | [diff] [blame] | 594 | } |
David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 595 | mp->m_sync_seq++; |
| 596 | wake_up(&mp->m_wait_single_sync_task); |
| 597 | } |
| 598 | |
| 599 | STATIC int |
| 600 | xfssyncd( |
| 601 | void *arg) |
| 602 | { |
| 603 | struct xfs_mount *mp = arg; |
| 604 | long timeleft; |
Dave Chinner | a8d770d | 2009-04-06 18:44:54 +0200 | [diff] [blame] | 605 | xfs_sync_work_t *work, *n; |
David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 606 | LIST_HEAD (tmp); |
| 607 | |
| 608 | set_freezable(); |
| 609 | timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10); |
| 610 | for (;;) { |
Dave Chinner | 20f6b2c | 2010-03-04 01:46:23 +0000 | [diff] [blame] | 611 | if (list_empty(&mp->m_sync_list)) |
| 612 | timeleft = schedule_timeout_interruptible(timeleft); |
David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 613 | /* swsusp */ |
| 614 | try_to_freeze(); |
| 615 | if (kthread_should_stop() && list_empty(&mp->m_sync_list)) |
| 616 | break; |
| 617 | |
| 618 | spin_lock(&mp->m_sync_lock); |
| 619 | /* |
| 620 | * We can get woken by laptop mode, to do a sync - |
| 621 | * that's the (only!) case where the list would be |
| 622 | * empty with time remaining. |
| 623 | */ |
| 624 | if (!timeleft || list_empty(&mp->m_sync_list)) { |
| 625 | if (!timeleft) |
| 626 | timeleft = xfs_syncd_centisecs * |
| 627 | msecs_to_jiffies(10); |
| 628 | INIT_LIST_HEAD(&mp->m_sync_work.w_list); |
| 629 | list_add_tail(&mp->m_sync_work.w_list, |
| 630 | &mp->m_sync_list); |
| 631 | } |
Dave Chinner | 20f6b2c | 2010-03-04 01:46:23 +0000 | [diff] [blame] | 632 | list_splice_init(&mp->m_sync_list, &tmp); |
David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 633 | spin_unlock(&mp->m_sync_lock); |
| 634 | |
| 635 | list_for_each_entry_safe(work, n, &tmp, w_list) { |
| 636 | (*work->w_syncer)(mp, work->w_data); |
| 637 | list_del(&work->w_list); |
| 638 | if (work == &mp->m_sync_work) |
| 639 | continue; |
Dave Chinner | e43afd7 | 2009-04-06 18:47:27 +0200 | [diff] [blame] | 640 | if (work->w_completion) |
| 641 | complete(work->w_completion); |
David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 642 | kmem_free(work); |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | return 0; |
| 647 | } |
| 648 | |
| 649 | int |
| 650 | xfs_syncd_init( |
| 651 | struct xfs_mount *mp) |
| 652 | { |
| 653 | mp->m_sync_work.w_syncer = xfs_sync_worker; |
| 654 | mp->m_sync_work.w_mount = mp; |
Dave Chinner | e43afd7 | 2009-04-06 18:47:27 +0200 | [diff] [blame] | 655 | mp->m_sync_work.w_completion = NULL; |
Jan Engelhardt | e2a0781 | 2010-03-23 09:52:55 +1100 | [diff] [blame] | 656 | 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] | 657 | if (IS_ERR(mp->m_sync_task)) |
| 658 | return -PTR_ERR(mp->m_sync_task); |
| 659 | return 0; |
| 660 | } |
| 661 | |
| 662 | void |
| 663 | xfs_syncd_stop( |
| 664 | struct xfs_mount *mp) |
| 665 | { |
| 666 | kthread_stop(mp->m_sync_task); |
| 667 | } |
| 668 | |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 669 | void |
| 670 | __xfs_inode_set_reclaim_tag( |
| 671 | struct xfs_perag *pag, |
| 672 | struct xfs_inode *ip) |
| 673 | { |
| 674 | radix_tree_tag_set(&pag->pag_ici_root, |
| 675 | XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino), |
| 676 | XFS_ICI_RECLAIM_TAG); |
Dave Chinner | 16fd536 | 2010-07-20 09:43:39 +1000 | [diff] [blame] | 677 | |
| 678 | if (!pag->pag_ici_reclaimable) { |
| 679 | /* propagate the reclaim tag up into the perag radix tree */ |
| 680 | spin_lock(&ip->i_mount->m_perag_lock); |
| 681 | radix_tree_tag_set(&ip->i_mount->m_perag_tree, |
| 682 | XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino), |
| 683 | XFS_ICI_RECLAIM_TAG); |
| 684 | spin_unlock(&ip->i_mount->m_perag_lock); |
| 685 | trace_xfs_perag_set_reclaim(ip->i_mount, pag->pag_agno, |
| 686 | -1, _RET_IP_); |
| 687 | } |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 688 | pag->pag_ici_reclaimable++; |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 689 | } |
| 690 | |
David Chinner | 1165451 | 2008-10-30 17:37:49 +1100 | [diff] [blame] | 691 | /* |
| 692 | * We set the inode flag atomically with the radix tree tag. |
| 693 | * Once we get tag lookups on the radix tree, this inode flag |
| 694 | * can go away. |
| 695 | */ |
David Chinner | 396beb8 | 2008-10-30 17:37:26 +1100 | [diff] [blame] | 696 | void |
| 697 | xfs_inode_set_reclaim_tag( |
| 698 | xfs_inode_t *ip) |
| 699 | { |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 700 | struct xfs_mount *mp = ip->i_mount; |
| 701 | struct xfs_perag *pag; |
David Chinner | 396beb8 | 2008-10-30 17:37:26 +1100 | [diff] [blame] | 702 | |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 703 | 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] | 704 | write_lock(&pag->pag_ici_lock); |
David Chinner | 396beb8 | 2008-10-30 17:37:26 +1100 | [diff] [blame] | 705 | spin_lock(&ip->i_flags_lock); |
Christoph Hellwig | bc990f5 | 2009-08-16 20:36:34 -0400 | [diff] [blame] | 706 | __xfs_inode_set_reclaim_tag(pag, ip); |
David Chinner | 1165451 | 2008-10-30 17:37:49 +1100 | [diff] [blame] | 707 | __xfs_iflags_set(ip, XFS_IRECLAIMABLE); |
David Chinner | 396beb8 | 2008-10-30 17:37:26 +1100 | [diff] [blame] | 708 | spin_unlock(&ip->i_flags_lock); |
Christoph Hellwig | f1f724e | 2010-03-01 11:30:31 +0000 | [diff] [blame] | 709 | write_unlock(&pag->pag_ici_lock); |
Dave Chinner | 5017e97 | 2010-01-11 11:47:40 +0000 | [diff] [blame] | 710 | xfs_perag_put(pag); |
David Chinner | 396beb8 | 2008-10-30 17:37:26 +1100 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | void |
| 714 | __xfs_inode_clear_reclaim_tag( |
| 715 | xfs_mount_t *mp, |
| 716 | xfs_perag_t *pag, |
| 717 | xfs_inode_t *ip) |
| 718 | { |
| 719 | radix_tree_tag_clear(&pag->pag_ici_root, |
| 720 | XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG); |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 721 | pag->pag_ici_reclaimable--; |
Dave Chinner | 16fd536 | 2010-07-20 09:43:39 +1000 | [diff] [blame] | 722 | if (!pag->pag_ici_reclaimable) { |
| 723 | /* clear the reclaim tag from the perag radix tree */ |
| 724 | spin_lock(&ip->i_mount->m_perag_lock); |
| 725 | radix_tree_tag_clear(&ip->i_mount->m_perag_tree, |
| 726 | XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino), |
| 727 | XFS_ICI_RECLAIM_TAG); |
| 728 | spin_unlock(&ip->i_mount->m_perag_lock); |
| 729 | trace_xfs_perag_clear_reclaim(ip->i_mount, pag->pag_agno, |
| 730 | -1, _RET_IP_); |
| 731 | } |
David Chinner | 396beb8 | 2008-10-30 17:37:26 +1100 | [diff] [blame] | 732 | } |
| 733 | |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 734 | /* |
| 735 | * Inodes in different states need to be treated differently, and the return |
| 736 | * value of xfs_iflush is not sufficient to get this right. The following table |
| 737 | * lists the inode states and the reclaim actions necessary for non-blocking |
| 738 | * reclaim: |
| 739 | * |
| 740 | * |
| 741 | * inode state iflush ret required action |
| 742 | * --------------- ---------- --------------- |
| 743 | * bad - reclaim |
| 744 | * shutdown EIO unpin and reclaim |
| 745 | * clean, unpinned 0 reclaim |
| 746 | * stale, unpinned 0 reclaim |
Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 747 | * clean, pinned(*) 0 requeue |
| 748 | * stale, pinned EAGAIN requeue |
| 749 | * dirty, delwri ok 0 requeue |
| 750 | * dirty, delwri blocked EAGAIN requeue |
| 751 | * dirty, sync flush 0 reclaim |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 752 | * |
| 753 | * (*) dgc: I don't think the clean, pinned state is possible but it gets |
| 754 | * handled anyway given the order of checks implemented. |
| 755 | * |
Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 756 | * As can be seen from the table, the return value of xfs_iflush() is not |
| 757 | * sufficient to correctly decide the reclaim action here. The checks in |
| 758 | * xfs_iflush() might look like duplicates, but they are not. |
| 759 | * |
| 760 | * Also, because we get the flush lock first, we know that any inode that has |
| 761 | * been flushed delwri has had the flush completed by the time we check that |
| 762 | * the inode is clean. The clean inode check needs to be done before flushing |
| 763 | * the inode delwri otherwise we would loop forever requeuing clean inodes as |
| 764 | * we cannot tell apart a successful delwri flush and a clean inode from the |
| 765 | * return value of xfs_iflush(). |
| 766 | * |
| 767 | * Note that because the inode is flushed delayed write by background |
| 768 | * writeback, the flush lock may already be held here and waiting on it can |
| 769 | * result in very long latencies. Hence for sync reclaims, where we wait on the |
| 770 | * flush lock, the caller should push out delayed write inodes first before |
| 771 | * trying to reclaim them to minimise the amount of time spent waiting. For |
| 772 | * background relaim, we just requeue the inode for the next pass. |
| 773 | * |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 774 | * Hence the order of actions after gaining the locks should be: |
| 775 | * bad => reclaim |
| 776 | * shutdown => unpin and reclaim |
Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 777 | * pinned, delwri => requeue |
| 778 | * pinned, sync => unpin |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 779 | * stale => reclaim |
| 780 | * clean => reclaim |
Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 781 | * dirty, delwri => flush and requeue |
| 782 | * dirty, sync => flush, wait and reclaim |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 783 | */ |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 784 | STATIC int |
Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 785 | xfs_reclaim_inode( |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 786 | struct xfs_inode *ip, |
| 787 | struct xfs_perag *pag, |
Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 788 | int sync_mode) |
David Chinner | 7a3be02 | 2008-10-30 17:37:37 +1100 | [diff] [blame] | 789 | { |
Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 790 | int error = 0; |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 791 | |
Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 792 | /* |
| 793 | * The radix tree lock here protects a thread in xfs_iget from racing |
| 794 | * with us starting reclaim on the inode. Once we have the |
| 795 | * XFS_IRECLAIM flag set it will not touch us. |
| 796 | */ |
| 797 | spin_lock(&ip->i_flags_lock); |
| 798 | ASSERT_ALWAYS(__xfs_iflags_test(ip, XFS_IRECLAIMABLE)); |
| 799 | if (__xfs_iflags_test(ip, XFS_IRECLAIM)) { |
| 800 | /* ignore as it is already under reclaim */ |
| 801 | spin_unlock(&ip->i_flags_lock); |
| 802 | write_unlock(&pag->pag_ici_lock); |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 803 | return 0; |
David Chinner | 7a3be02 | 2008-10-30 17:37:37 +1100 | [diff] [blame] | 804 | } |
Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 805 | __xfs_iflags_set(ip, XFS_IRECLAIM); |
| 806 | spin_unlock(&ip->i_flags_lock); |
| 807 | write_unlock(&pag->pag_ici_lock); |
David Chinner | 7a3be02 | 2008-10-30 17:37:37 +1100 | [diff] [blame] | 808 | |
Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 809 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 810 | if (!xfs_iflock_nowait(ip)) { |
| 811 | if (!(sync_mode & SYNC_WAIT)) |
| 812 | goto out; |
| 813 | xfs_iflock(ip); |
| 814 | } |
Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 815 | |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 816 | if (is_bad_inode(VFS_I(ip))) |
| 817 | goto reclaim; |
| 818 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
| 819 | xfs_iunpin_wait(ip); |
| 820 | goto reclaim; |
| 821 | } |
Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 822 | if (xfs_ipincount(ip)) { |
| 823 | if (!(sync_mode & SYNC_WAIT)) { |
| 824 | xfs_ifunlock(ip); |
| 825 | goto out; |
| 826 | } |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 827 | xfs_iunpin_wait(ip); |
Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 828 | } |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 829 | if (xfs_iflags_test(ip, XFS_ISTALE)) |
| 830 | goto reclaim; |
| 831 | if (xfs_inode_clean(ip)) |
| 832 | goto reclaim; |
| 833 | |
| 834 | /* Now we have an inode that needs flushing */ |
| 835 | error = xfs_iflush(ip, sync_mode); |
Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 836 | if (sync_mode & SYNC_WAIT) { |
| 837 | xfs_iflock(ip); |
| 838 | goto reclaim; |
Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 841 | /* |
| 842 | * When we have to flush an inode but don't have SYNC_WAIT set, we |
| 843 | * flush the inode out using a delwri buffer and wait for the next |
| 844 | * call into reclaim to find it in a clean state instead of waiting for |
| 845 | * it now. We also don't return errors here - if the error is transient |
| 846 | * 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] | 847 | * is permanent then the next sync reclaim will reclaim the inode and |
Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 848 | * pass on the error. |
| 849 | */ |
Dave Chinner | f1d486a | 2010-04-13 15:06:45 +1000 | [diff] [blame] | 850 | if (error && error != EAGAIN && !XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 851 | xfs_fs_cmn_err(CE_WARN, ip->i_mount, |
| 852 | "inode 0x%llx background reclaim flush failed with %d", |
| 853 | (long long)ip->i_ino, error); |
| 854 | } |
| 855 | out: |
| 856 | xfs_iflags_clear(ip, XFS_IRECLAIM); |
| 857 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 858 | /* |
| 859 | * We could return EAGAIN here to make reclaim rescan the inode tree in |
| 860 | * a short while. However, this just burns CPU time scanning the tree |
| 861 | * waiting for IO to complete and xfssyncd never goes back to the idle |
| 862 | * state. Instead, return 0 to let the next scheduled background reclaim |
| 863 | * attempt to reclaim the inode again. |
| 864 | */ |
| 865 | return 0; |
| 866 | |
Dave Chinner | 777df5a | 2010-02-06 12:37:26 +1100 | [diff] [blame] | 867 | reclaim: |
| 868 | xfs_ifunlock(ip); |
Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 869 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 870 | xfs_ireclaim(ip); |
Dave Chinner | c854363 | 2010-02-06 12:39:36 +1100 | [diff] [blame] | 871 | return error; |
| 872 | |
David Chinner | 7a3be02 | 2008-10-30 17:37:37 +1100 | [diff] [blame] | 873 | } |
| 874 | |
David Chinner | fce08f2 | 2008-10-30 17:37:03 +1100 | [diff] [blame] | 875 | int |
David Chinner | 1dc3318 | 2008-10-30 17:37:15 +1100 | [diff] [blame] | 876 | xfs_reclaim_inodes( |
David Chinner | fce08f2 | 2008-10-30 17:37:03 +1100 | [diff] [blame] | 877 | xfs_mount_t *mp, |
David Chinner | fce08f2 | 2008-10-30 17:37:03 +1100 | [diff] [blame] | 878 | int mode) |
| 879 | { |
Dave Chinner | c8e20be | 2010-01-10 23:51:45 +0000 | [diff] [blame] | 880 | return xfs_inode_ag_iterator(mp, xfs_reclaim_inode, mode, |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 881 | XFS_ICI_RECLAIM_TAG, 1, NULL); |
| 882 | } |
| 883 | |
| 884 | /* |
| 885 | * Shrinker infrastructure. |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 886 | */ |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 887 | static int |
| 888 | xfs_reclaim_inode_shrink( |
Dave Chinner | 7f8275d | 2010-07-19 14:56:17 +1000 | [diff] [blame] | 889 | struct shrinker *shrink, |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 890 | int nr_to_scan, |
| 891 | gfp_t gfp_mask) |
| 892 | { |
| 893 | struct xfs_mount *mp; |
| 894 | struct xfs_perag *pag; |
| 895 | xfs_agnumber_t ag; |
Dave Chinner | 16fd536 | 2010-07-20 09:43:39 +1000 | [diff] [blame] | 896 | int reclaimable; |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 897 | |
Dave Chinner | 70e60ce | 2010-07-20 08:07:02 +1000 | [diff] [blame] | 898 | mp = container_of(shrink, struct xfs_mount, m_inode_shrink); |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 899 | if (nr_to_scan) { |
| 900 | if (!(gfp_mask & __GFP_FS)) |
| 901 | return -1; |
| 902 | |
Dave Chinner | 70e60ce | 2010-07-20 08:07:02 +1000 | [diff] [blame] | 903 | xfs_inode_ag_iterator(mp, xfs_reclaim_inode, 0, |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 904 | XFS_ICI_RECLAIM_TAG, 1, &nr_to_scan); |
Dave Chinner | 70e60ce | 2010-07-20 08:07:02 +1000 | [diff] [blame] | 905 | /* if we don't exhaust the scan, don't bother coming back */ |
| 906 | if (nr_to_scan > 0) |
| 907 | return -1; |
| 908 | } |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 909 | |
Dave Chinner | 16fd536 | 2010-07-20 09:43:39 +1000 | [diff] [blame] | 910 | reclaimable = 0; |
| 911 | ag = 0; |
| 912 | while ((pag = xfs_inode_ag_iter_next_pag(mp, &ag, |
| 913 | XFS_ICI_RECLAIM_TAG))) { |
Dave Chinner | 70e60ce | 2010-07-20 08:07:02 +1000 | [diff] [blame] | 914 | reclaimable += pag->pag_ici_reclaimable; |
| 915 | xfs_perag_put(pag); |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 916 | } |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 917 | return reclaimable; |
| 918 | } |
| 919 | |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 920 | void |
| 921 | xfs_inode_shrinker_register( |
| 922 | struct xfs_mount *mp) |
| 923 | { |
Dave Chinner | 70e60ce | 2010-07-20 08:07:02 +1000 | [diff] [blame] | 924 | mp->m_inode_shrink.shrink = xfs_reclaim_inode_shrink; |
| 925 | mp->m_inode_shrink.seeks = DEFAULT_SEEKS; |
| 926 | register_shrinker(&mp->m_inode_shrink); |
Dave Chinner | 9bf729c | 2010-04-29 09:55:50 +1000 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | void |
| 930 | xfs_inode_shrinker_unregister( |
| 931 | struct xfs_mount *mp) |
| 932 | { |
Dave Chinner | 70e60ce | 2010-07-20 08:07:02 +1000 | [diff] [blame] | 933 | unregister_shrinker(&mp->m_inode_shrink); |
David Chinner | fce08f2 | 2008-10-30 17:37:03 +1100 | [diff] [blame] | 934 | } |