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