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" |
David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 47 | |
David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 48 | #include <linux/kthread.h> |
| 49 | #include <linux/freezer.h> |
| 50 | |
Dave Chinner | 5a34d5c | 2009-06-08 15:35:03 +0200 | [diff] [blame] | 51 | |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 52 | STATIC xfs_inode_t * |
| 53 | xfs_inode_ag_lookup( |
| 54 | struct xfs_mount *mp, |
| 55 | struct xfs_perag *pag, |
| 56 | uint32_t *first_index, |
| 57 | int tag) |
| 58 | { |
| 59 | int nr_found; |
| 60 | struct xfs_inode *ip; |
| 61 | |
| 62 | /* |
| 63 | * use a gang lookup to find the next inode in the tree |
| 64 | * as the tree is sparse and a gang lookup walks to find |
| 65 | * the number of objects requested. |
| 66 | */ |
| 67 | read_lock(&pag->pag_ici_lock); |
| 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) |
| 76 | goto unlock; |
| 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)) |
| 86 | goto unlock; |
| 87 | |
| 88 | return ip; |
| 89 | |
| 90 | unlock: |
| 91 | read_unlock(&pag->pag_ici_lock); |
| 92 | return NULL; |
| 93 | } |
| 94 | |
| 95 | STATIC int |
| 96 | xfs_inode_ag_walk( |
| 97 | struct xfs_mount *mp, |
| 98 | xfs_agnumber_t ag, |
| 99 | int (*execute)(struct xfs_inode *ip, |
| 100 | struct xfs_perag *pag, int flags), |
| 101 | int flags, |
| 102 | int tag) |
| 103 | { |
| 104 | struct xfs_perag *pag = &mp->m_perag[ag]; |
| 105 | uint32_t first_index; |
| 106 | int last_error = 0; |
| 107 | int skipped; |
| 108 | |
| 109 | restart: |
| 110 | skipped = 0; |
| 111 | first_index = 0; |
| 112 | do { |
| 113 | int error = 0; |
| 114 | xfs_inode_t *ip; |
| 115 | |
| 116 | ip = xfs_inode_ag_lookup(mp, pag, &first_index, tag); |
| 117 | if (!ip) |
| 118 | break; |
| 119 | |
| 120 | error = execute(ip, pag, flags); |
| 121 | if (error == EAGAIN) { |
| 122 | skipped++; |
| 123 | continue; |
| 124 | } |
| 125 | if (error) |
| 126 | last_error = error; |
| 127 | /* |
| 128 | * bail out if the filesystem is corrupted. |
| 129 | */ |
| 130 | if (error == EFSCORRUPTED) |
| 131 | break; |
| 132 | |
| 133 | } while (1); |
| 134 | |
| 135 | if (skipped) { |
| 136 | delay(1); |
| 137 | goto restart; |
| 138 | } |
| 139 | |
| 140 | xfs_put_perag(mp, pag); |
| 141 | return last_error; |
| 142 | } |
| 143 | |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame^] | 144 | int |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 145 | xfs_inode_ag_iterator( |
| 146 | struct xfs_mount *mp, |
| 147 | int (*execute)(struct xfs_inode *ip, |
| 148 | struct xfs_perag *pag, int flags), |
| 149 | int flags, |
| 150 | int tag) |
| 151 | { |
| 152 | int error = 0; |
| 153 | int last_error = 0; |
| 154 | xfs_agnumber_t ag; |
| 155 | |
| 156 | for (ag = 0; ag < mp->m_sb.sb_agcount; ag++) { |
| 157 | if (!mp->m_perag[ag].pag_ici_init) |
| 158 | continue; |
| 159 | error = xfs_inode_ag_walk(mp, ag, execute, flags, tag); |
| 160 | if (error) { |
| 161 | last_error = error; |
| 162 | if (error == EFSCORRUPTED) |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | return XFS_ERROR(last_error); |
| 167 | } |
| 168 | |
Dave Chinner | 1da8eec | 2009-06-08 15:35:07 +0200 | [diff] [blame] | 169 | /* must be called with pag_ici_lock held and releases it */ |
Christoph Hellwig | fe588ed | 2009-06-08 15:35:27 +0200 | [diff] [blame^] | 170 | int |
Dave Chinner | 1da8eec | 2009-06-08 15:35:07 +0200 | [diff] [blame] | 171 | xfs_sync_inode_valid( |
| 172 | struct xfs_inode *ip, |
| 173 | struct xfs_perag *pag) |
| 174 | { |
| 175 | struct inode *inode = VFS_I(ip); |
| 176 | |
| 177 | /* nothing to sync during shutdown */ |
| 178 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
| 179 | read_unlock(&pag->pag_ici_lock); |
| 180 | return EFSCORRUPTED; |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * If we can't get a reference on the inode, it must be in reclaim. |
| 185 | * Leave it for the reclaim code to flush. Also avoid inodes that |
| 186 | * haven't been fully initialised. |
| 187 | */ |
| 188 | if (!igrab(inode)) { |
| 189 | read_unlock(&pag->pag_ici_lock); |
| 190 | return ENOENT; |
| 191 | } |
| 192 | read_unlock(&pag->pag_ici_lock); |
| 193 | |
| 194 | if (is_bad_inode(inode) || xfs_iflags_test(ip, XFS_INEW)) { |
| 195 | IRELE(ip); |
| 196 | return ENOENT; |
| 197 | } |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
Dave Chinner | 5a34d5c | 2009-06-08 15:35:03 +0200 | [diff] [blame] | 202 | STATIC int |
| 203 | xfs_sync_inode_data( |
| 204 | struct xfs_inode *ip, |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 205 | struct xfs_perag *pag, |
Dave Chinner | 5a34d5c | 2009-06-08 15:35:03 +0200 | [diff] [blame] | 206 | int flags) |
| 207 | { |
| 208 | struct inode *inode = VFS_I(ip); |
| 209 | struct address_space *mapping = inode->i_mapping; |
| 210 | int error = 0; |
| 211 | |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 212 | error = xfs_sync_inode_valid(ip, pag); |
| 213 | if (error) |
| 214 | return error; |
| 215 | |
Dave Chinner | 5a34d5c | 2009-06-08 15:35:03 +0200 | [diff] [blame] | 216 | if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) |
| 217 | goto out_wait; |
| 218 | |
| 219 | if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED)) { |
| 220 | if (flags & SYNC_TRYLOCK) |
| 221 | goto out_wait; |
| 222 | xfs_ilock(ip, XFS_IOLOCK_SHARED); |
| 223 | } |
| 224 | |
| 225 | error = xfs_flush_pages(ip, 0, -1, (flags & SYNC_WAIT) ? |
| 226 | 0 : XFS_B_ASYNC, FI_NONE); |
| 227 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
| 228 | |
| 229 | out_wait: |
| 230 | if (flags & SYNC_IOWAIT) |
| 231 | xfs_ioend_wait(ip); |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 232 | IRELE(ip); |
Dave Chinner | 5a34d5c | 2009-06-08 15:35:03 +0200 | [diff] [blame] | 233 | return error; |
| 234 | } |
| 235 | |
Christoph Hellwig | 845b6d0 | 2009-06-08 15:35:05 +0200 | [diff] [blame] | 236 | STATIC int |
| 237 | xfs_sync_inode_attr( |
| 238 | struct xfs_inode *ip, |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 239 | struct xfs_perag *pag, |
Christoph Hellwig | 845b6d0 | 2009-06-08 15:35:05 +0200 | [diff] [blame] | 240 | int flags) |
| 241 | { |
| 242 | int error = 0; |
| 243 | |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 244 | error = xfs_sync_inode_valid(ip, pag); |
| 245 | if (error) |
| 246 | return error; |
| 247 | |
Christoph Hellwig | 845b6d0 | 2009-06-08 15:35:05 +0200 | [diff] [blame] | 248 | xfs_ilock(ip, XFS_ILOCK_SHARED); |
| 249 | if (xfs_inode_clean(ip)) |
| 250 | goto out_unlock; |
| 251 | if (!xfs_iflock_nowait(ip)) { |
| 252 | if (!(flags & SYNC_WAIT)) |
| 253 | goto out_unlock; |
| 254 | xfs_iflock(ip); |
| 255 | } |
| 256 | |
| 257 | if (xfs_inode_clean(ip)) { |
| 258 | xfs_ifunlock(ip); |
| 259 | goto out_unlock; |
| 260 | } |
| 261 | |
| 262 | error = xfs_iflush(ip, (flags & SYNC_WAIT) ? |
| 263 | XFS_IFLUSH_SYNC : XFS_IFLUSH_DELWRI); |
| 264 | |
| 265 | out_unlock: |
| 266 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 267 | IRELE(ip); |
Christoph Hellwig | 845b6d0 | 2009-06-08 15:35:05 +0200 | [diff] [blame] | 268 | return error; |
| 269 | } |
| 270 | |
David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 271 | int |
| 272 | xfs_sync_inodes( |
| 273 | xfs_mount_t *mp, |
David Chinner | 2030b5a | 2008-10-30 17:15:12 +1100 | [diff] [blame] | 274 | int flags) |
David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 275 | { |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 276 | int error = 0; |
David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 277 | int lflags = XFS_LOG_FORCE; |
David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 278 | |
David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 279 | if (mp->m_flags & XFS_MOUNT_RDONLY) |
| 280 | return 0; |
David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 281 | |
David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 282 | if (flags & SYNC_WAIT) |
| 283 | lflags |= XFS_LOG_SYNC; |
| 284 | |
David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 285 | if (flags & SYNC_DELWRI) |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 286 | error = xfs_inode_ag_iterator(mp, xfs_sync_inode_data, flags, XFS_ICI_NO_TAG); |
David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 287 | |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 288 | if (flags & SYNC_ATTR) |
| 289 | error = xfs_inode_ag_iterator(mp, xfs_sync_inode_attr, flags, XFS_ICI_NO_TAG); |
| 290 | |
| 291 | if (!error && (flags & SYNC_DELWRI)) |
| 292 | xfs_log_force(mp, 0, lflags); |
| 293 | return XFS_ERROR(error); |
David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 294 | } |
| 295 | |
Christoph Hellwig | 2af75df | 2008-10-30 17:14:53 +1100 | [diff] [blame] | 296 | STATIC int |
| 297 | xfs_commit_dummy_trans( |
| 298 | struct xfs_mount *mp, |
| 299 | uint log_flags) |
| 300 | { |
| 301 | struct xfs_inode *ip = mp->m_rootip; |
| 302 | struct xfs_trans *tp; |
| 303 | int error; |
| 304 | |
| 305 | /* |
| 306 | * Put a dummy transaction in the log to tell recovery |
| 307 | * that all others are OK. |
| 308 | */ |
| 309 | tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1); |
| 310 | error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0); |
| 311 | if (error) { |
| 312 | xfs_trans_cancel(tp, 0); |
| 313 | return error; |
| 314 | } |
| 315 | |
| 316 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 317 | |
| 318 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 319 | xfs_trans_ihold(tp, ip); |
| 320 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 321 | /* XXX(hch): ignoring the error here.. */ |
| 322 | error = xfs_trans_commit(tp, 0); |
| 323 | |
| 324 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 325 | |
| 326 | xfs_log_force(mp, 0, log_flags); |
| 327 | return 0; |
| 328 | } |
| 329 | |
David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 330 | int |
Christoph Hellwig | 2af75df | 2008-10-30 17:14:53 +1100 | [diff] [blame] | 331 | xfs_sync_fsdata( |
| 332 | struct xfs_mount *mp, |
| 333 | int flags) |
| 334 | { |
| 335 | struct xfs_buf *bp; |
| 336 | struct xfs_buf_log_item *bip; |
| 337 | int error = 0; |
| 338 | |
| 339 | /* |
| 340 | * If this is xfssyncd() then only sync the superblock if we can |
| 341 | * lock it without sleeping and it is not pinned. |
| 342 | */ |
| 343 | if (flags & SYNC_BDFLUSH) { |
| 344 | ASSERT(!(flags & SYNC_WAIT)); |
| 345 | |
| 346 | bp = xfs_getsb(mp, XFS_BUF_TRYLOCK); |
| 347 | if (!bp) |
| 348 | goto out; |
| 349 | |
| 350 | bip = XFS_BUF_FSPRIVATE(bp, struct xfs_buf_log_item *); |
| 351 | if (!bip || !xfs_buf_item_dirty(bip) || XFS_BUF_ISPINNED(bp)) |
| 352 | goto out_brelse; |
| 353 | } else { |
| 354 | bp = xfs_getsb(mp, 0); |
| 355 | |
| 356 | /* |
| 357 | * If the buffer is pinned then push on the log so we won't |
| 358 | * get stuck waiting in the write for someone, maybe |
| 359 | * ourselves, to flush the log. |
| 360 | * |
| 361 | * Even though we just pushed the log above, we did not have |
| 362 | * the superblock buffer locked at that point so it can |
| 363 | * become pinned in between there and here. |
| 364 | */ |
| 365 | if (XFS_BUF_ISPINNED(bp)) |
| 366 | xfs_log_force(mp, 0, XFS_LOG_FORCE); |
| 367 | } |
| 368 | |
| 369 | |
| 370 | if (flags & SYNC_WAIT) |
| 371 | XFS_BUF_UNASYNC(bp); |
| 372 | else |
| 373 | XFS_BUF_ASYNC(bp); |
| 374 | |
| 375 | return xfs_bwrite(mp, bp); |
| 376 | |
| 377 | out_brelse: |
| 378 | xfs_buf_relse(bp); |
| 379 | out: |
| 380 | return error; |
| 381 | } |
| 382 | |
David Chinner | fe4fa4b | 2008-10-30 17:06:08 +1100 | [diff] [blame] | 383 | /* |
David Chinner | a4e4c4f | 2008-10-30 17:16:11 +1100 | [diff] [blame] | 384 | * When remounting a filesystem read-only or freezing the filesystem, we have |
| 385 | * two phases to execute. This first phase is syncing the data before we |
| 386 | * quiesce the filesystem, and the second is flushing all the inodes out after |
| 387 | * we've waited for all the transactions created by the first phase to |
| 388 | * complete. The second phase ensures that the inodes are written to their |
| 389 | * location on disk rather than just existing in transactions in the log. This |
| 390 | * means after a quiesce there is no log replay required to write the inodes to |
| 391 | * disk (this is the main difference between a sync and a quiesce). |
| 392 | */ |
| 393 | /* |
| 394 | * 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] | 395 | * so we flush delwri and delalloc buffers here, then wait for all I/O to |
| 396 | * complete. Data is frozen at that point. Metadata is not frozen, |
David Chinner | a4e4c4f | 2008-10-30 17:16:11 +1100 | [diff] [blame] | 397 | * transactions can still occur here so don't bother flushing the buftarg |
| 398 | * because it'll just get dirty again. |
David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 399 | */ |
| 400 | int |
| 401 | xfs_quiesce_data( |
| 402 | struct xfs_mount *mp) |
| 403 | { |
| 404 | int error; |
| 405 | |
| 406 | /* push non-blocking */ |
| 407 | xfs_sync_inodes(mp, SYNC_DELWRI|SYNC_BDFLUSH); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 408 | xfs_qm_sync(mp, SYNC_BDFLUSH); |
David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 409 | xfs_filestream_flush(mp); |
| 410 | |
| 411 | /* push and block */ |
| 412 | xfs_sync_inodes(mp, SYNC_DELWRI|SYNC_WAIT|SYNC_IOWAIT); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 413 | xfs_qm_sync(mp, SYNC_WAIT); |
David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 414 | |
David Chinner | a4e4c4f | 2008-10-30 17:16:11 +1100 | [diff] [blame] | 415 | /* write superblock and hoover up shutdown errors */ |
David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 416 | error = xfs_sync_fsdata(mp, 0); |
| 417 | |
David Chinner | a4e4c4f | 2008-10-30 17:16:11 +1100 | [diff] [blame] | 418 | /* flush data-only devices */ |
David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 419 | if (mp->m_rtdev_targp) |
| 420 | XFS_bflush(mp->m_rtdev_targp); |
| 421 | |
| 422 | return error; |
| 423 | } |
| 424 | |
David Chinner | 76bf105 | 2008-10-30 17:16:21 +1100 | [diff] [blame] | 425 | STATIC void |
| 426 | xfs_quiesce_fs( |
| 427 | struct xfs_mount *mp) |
| 428 | { |
| 429 | int count = 0, pincount; |
| 430 | |
| 431 | xfs_flush_buftarg(mp->m_ddev_targp, 0); |
Dave Chinner | abc1064 | 2009-06-08 15:35:12 +0200 | [diff] [blame] | 432 | xfs_reclaim_inodes(mp, XFS_IFLUSH_DELWRI_ELSE_ASYNC); |
David Chinner | 76bf105 | 2008-10-30 17:16:21 +1100 | [diff] [blame] | 433 | |
| 434 | /* |
| 435 | * This loop must run at least twice. The first instance of the loop |
| 436 | * will flush most meta data but that will generate more meta data |
| 437 | * (typically directory updates). Which then must be flushed and |
| 438 | * logged before we can write the unmount record. |
| 439 | */ |
| 440 | do { |
| 441 | xfs_sync_inodes(mp, SYNC_ATTR|SYNC_WAIT); |
| 442 | pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1); |
| 443 | if (!pincount) { |
| 444 | delay(50); |
| 445 | count++; |
| 446 | } |
| 447 | } while (count < 2); |
| 448 | } |
| 449 | |
| 450 | /* |
| 451 | * Second stage of a quiesce. The data is already synced, now we have to take |
| 452 | * care of the metadata. New transactions are already blocked, so we need to |
| 453 | * wait for any remaining transactions to drain out before proceding. |
| 454 | */ |
| 455 | void |
| 456 | xfs_quiesce_attr( |
| 457 | struct xfs_mount *mp) |
| 458 | { |
| 459 | int error = 0; |
| 460 | |
| 461 | /* wait for all modifications to complete */ |
| 462 | while (atomic_read(&mp->m_active_trans) > 0) |
| 463 | delay(100); |
| 464 | |
| 465 | /* flush inodes and push all remaining buffers out to disk */ |
| 466 | xfs_quiesce_fs(mp); |
| 467 | |
Felix Blyakher | 5e10657 | 2009-01-22 21:34:05 -0600 | [diff] [blame] | 468 | /* |
| 469 | * Just warn here till VFS can correctly support |
| 470 | * read-only remount without racing. |
| 471 | */ |
| 472 | WARN_ON(atomic_read(&mp->m_active_trans) != 0); |
David Chinner | 76bf105 | 2008-10-30 17:16:21 +1100 | [diff] [blame] | 473 | |
| 474 | /* Push the superblock and write an unmount record */ |
| 475 | error = xfs_log_sbcount(mp, 1); |
| 476 | if (error) |
| 477 | xfs_fs_cmn_err(CE_WARN, mp, |
| 478 | "xfs_attr_quiesce: failed to log sb changes. " |
| 479 | "Frozen image may not be consistent."); |
| 480 | xfs_log_unmount_write(mp); |
| 481 | xfs_unmountfs_writesb(mp); |
| 482 | } |
| 483 | |
David Chinner | e9f1c6e | 2008-10-30 17:15:50 +1100 | [diff] [blame] | 484 | /* |
David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 485 | * Enqueue a work item to be picked up by the vfs xfssyncd thread. |
| 486 | * Doing this has two advantages: |
| 487 | * - It saves on stack space, which is tight in certain situations |
| 488 | * - It can be used (with care) as a mechanism to avoid deadlocks. |
| 489 | * Flushing while allocating in a full filesystem requires both. |
| 490 | */ |
| 491 | STATIC void |
| 492 | xfs_syncd_queue_work( |
| 493 | struct xfs_mount *mp, |
| 494 | void *data, |
Dave Chinner | e43afd7 | 2009-04-06 18:47:27 +0200 | [diff] [blame] | 495 | void (*syncer)(struct xfs_mount *, void *), |
| 496 | struct completion *completion) |
David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 497 | { |
Dave Chinner | a8d770d | 2009-04-06 18:44:54 +0200 | [diff] [blame] | 498 | struct xfs_sync_work *work; |
David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 499 | |
Dave Chinner | a8d770d | 2009-04-06 18:44:54 +0200 | [diff] [blame] | 500 | work = kmem_alloc(sizeof(struct xfs_sync_work), KM_SLEEP); |
David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 501 | INIT_LIST_HEAD(&work->w_list); |
| 502 | work->w_syncer = syncer; |
| 503 | work->w_data = data; |
| 504 | work->w_mount = mp; |
Dave Chinner | e43afd7 | 2009-04-06 18:47:27 +0200 | [diff] [blame] | 505 | work->w_completion = completion; |
David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 506 | spin_lock(&mp->m_sync_lock); |
| 507 | list_add_tail(&work->w_list, &mp->m_sync_list); |
| 508 | spin_unlock(&mp->m_sync_lock); |
| 509 | wake_up_process(mp->m_sync_task); |
| 510 | } |
| 511 | |
| 512 | /* |
| 513 | * Flush delayed allocate data, attempting to free up reserved space |
| 514 | * from existing allocations. At this point a new allocation attempt |
| 515 | * has failed with ENOSPC and we are in the process of scratching our |
| 516 | * heads, looking about for more room... |
| 517 | */ |
| 518 | STATIC void |
Dave Chinner | a8d770d | 2009-04-06 18:44:54 +0200 | [diff] [blame] | 519 | xfs_flush_inodes_work( |
David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 520 | struct xfs_mount *mp, |
| 521 | void *arg) |
| 522 | { |
| 523 | struct inode *inode = arg; |
Dave Chinner | a8d770d | 2009-04-06 18:44:54 +0200 | [diff] [blame] | 524 | xfs_sync_inodes(mp, SYNC_DELWRI | SYNC_TRYLOCK); |
| 525 | xfs_sync_inodes(mp, SYNC_DELWRI | SYNC_TRYLOCK | SYNC_IOWAIT); |
David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 526 | iput(inode); |
| 527 | } |
| 528 | |
| 529 | void |
Dave Chinner | a8d770d | 2009-04-06 18:44:54 +0200 | [diff] [blame] | 530 | xfs_flush_inodes( |
David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 531 | xfs_inode_t *ip) |
| 532 | { |
| 533 | struct inode *inode = VFS_I(ip); |
Dave Chinner | e43afd7 | 2009-04-06 18:47:27 +0200 | [diff] [blame] | 534 | DECLARE_COMPLETION_ONSTACK(completion); |
David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 535 | |
| 536 | igrab(inode); |
Dave Chinner | e43afd7 | 2009-04-06 18:47:27 +0200 | [diff] [blame] | 537 | xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inodes_work, &completion); |
| 538 | wait_for_completion(&completion); |
David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 539 | xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC); |
| 540 | } |
| 541 | |
David Chinner | aacaa88 | 2008-10-30 17:15:29 +1100 | [diff] [blame] | 542 | /* |
| 543 | * Every sync period we need to unpin all items, reclaim inodes, sync |
| 544 | * quota and write out the superblock. We might need to cover the log |
| 545 | * to indicate it is idle. |
| 546 | */ |
David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 547 | STATIC void |
| 548 | xfs_sync_worker( |
| 549 | struct xfs_mount *mp, |
| 550 | void *unused) |
| 551 | { |
| 552 | int error; |
| 553 | |
David Chinner | aacaa88 | 2008-10-30 17:15:29 +1100 | [diff] [blame] | 554 | if (!(mp->m_flags & XFS_MOUNT_RDONLY)) { |
| 555 | xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE); |
Dave Chinner | abc1064 | 2009-06-08 15:35:12 +0200 | [diff] [blame] | 556 | xfs_reclaim_inodes(mp, XFS_IFLUSH_DELWRI_ELSE_ASYNC); |
David Chinner | aacaa88 | 2008-10-30 17:15:29 +1100 | [diff] [blame] | 557 | /* dgc: errors ignored here */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 558 | error = xfs_qm_sync(mp, SYNC_BDFLUSH); |
David Chinner | aacaa88 | 2008-10-30 17:15:29 +1100 | [diff] [blame] | 559 | error = xfs_sync_fsdata(mp, SYNC_BDFLUSH); |
| 560 | if (xfs_log_need_covered(mp)) |
| 561 | error = xfs_commit_dummy_trans(mp, XFS_LOG_FORCE); |
| 562 | } |
David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 563 | mp->m_sync_seq++; |
| 564 | wake_up(&mp->m_wait_single_sync_task); |
| 565 | } |
| 566 | |
| 567 | STATIC int |
| 568 | xfssyncd( |
| 569 | void *arg) |
| 570 | { |
| 571 | struct xfs_mount *mp = arg; |
| 572 | long timeleft; |
Dave Chinner | a8d770d | 2009-04-06 18:44:54 +0200 | [diff] [blame] | 573 | xfs_sync_work_t *work, *n; |
David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 574 | LIST_HEAD (tmp); |
| 575 | |
| 576 | set_freezable(); |
| 577 | timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10); |
| 578 | for (;;) { |
| 579 | timeleft = schedule_timeout_interruptible(timeleft); |
| 580 | /* swsusp */ |
| 581 | try_to_freeze(); |
| 582 | if (kthread_should_stop() && list_empty(&mp->m_sync_list)) |
| 583 | break; |
| 584 | |
| 585 | spin_lock(&mp->m_sync_lock); |
| 586 | /* |
| 587 | * We can get woken by laptop mode, to do a sync - |
| 588 | * that's the (only!) case where the list would be |
| 589 | * empty with time remaining. |
| 590 | */ |
| 591 | if (!timeleft || list_empty(&mp->m_sync_list)) { |
| 592 | if (!timeleft) |
| 593 | timeleft = xfs_syncd_centisecs * |
| 594 | msecs_to_jiffies(10); |
| 595 | INIT_LIST_HEAD(&mp->m_sync_work.w_list); |
| 596 | list_add_tail(&mp->m_sync_work.w_list, |
| 597 | &mp->m_sync_list); |
| 598 | } |
| 599 | list_for_each_entry_safe(work, n, &mp->m_sync_list, w_list) |
| 600 | list_move(&work->w_list, &tmp); |
| 601 | spin_unlock(&mp->m_sync_lock); |
| 602 | |
| 603 | list_for_each_entry_safe(work, n, &tmp, w_list) { |
| 604 | (*work->w_syncer)(mp, work->w_data); |
| 605 | list_del(&work->w_list); |
| 606 | if (work == &mp->m_sync_work) |
| 607 | continue; |
Dave Chinner | e43afd7 | 2009-04-06 18:47:27 +0200 | [diff] [blame] | 608 | if (work->w_completion) |
| 609 | complete(work->w_completion); |
David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 610 | kmem_free(work); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | int |
| 618 | xfs_syncd_init( |
| 619 | struct xfs_mount *mp) |
| 620 | { |
| 621 | mp->m_sync_work.w_syncer = xfs_sync_worker; |
| 622 | mp->m_sync_work.w_mount = mp; |
Dave Chinner | e43afd7 | 2009-04-06 18:47:27 +0200 | [diff] [blame] | 623 | mp->m_sync_work.w_completion = NULL; |
David Chinner | a167b17 | 2008-10-30 17:06:18 +1100 | [diff] [blame] | 624 | mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd"); |
| 625 | if (IS_ERR(mp->m_sync_task)) |
| 626 | return -PTR_ERR(mp->m_sync_task); |
| 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | void |
| 631 | xfs_syncd_stop( |
| 632 | struct xfs_mount *mp) |
| 633 | { |
| 634 | kthread_stop(mp->m_sync_task); |
| 635 | } |
| 636 | |
David Chinner | fce08f2 | 2008-10-30 17:37:03 +1100 | [diff] [blame] | 637 | int |
David Chinner | 1dc3318 | 2008-10-30 17:37:15 +1100 | [diff] [blame] | 638 | xfs_reclaim_inode( |
David Chinner | fce08f2 | 2008-10-30 17:37:03 +1100 | [diff] [blame] | 639 | xfs_inode_t *ip, |
| 640 | int locked, |
| 641 | int sync_mode) |
| 642 | { |
| 643 | xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino); |
| 644 | |
| 645 | /* The hash lock here protects a thread in xfs_iget_core from |
| 646 | * racing with us on linking the inode back with a vnode. |
| 647 | * Once we have the XFS_IRECLAIM flag set it will not touch |
| 648 | * us. |
| 649 | */ |
| 650 | write_lock(&pag->pag_ici_lock); |
| 651 | spin_lock(&ip->i_flags_lock); |
| 652 | if (__xfs_iflags_test(ip, XFS_IRECLAIM) || |
| 653 | !__xfs_iflags_test(ip, XFS_IRECLAIMABLE)) { |
| 654 | spin_unlock(&ip->i_flags_lock); |
| 655 | write_unlock(&pag->pag_ici_lock); |
| 656 | if (locked) { |
| 657 | xfs_ifunlock(ip); |
| 658 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 659 | } |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 660 | return -EAGAIN; |
David Chinner | fce08f2 | 2008-10-30 17:37:03 +1100 | [diff] [blame] | 661 | } |
| 662 | __xfs_iflags_set(ip, XFS_IRECLAIM); |
| 663 | spin_unlock(&ip->i_flags_lock); |
| 664 | write_unlock(&pag->pag_ici_lock); |
| 665 | xfs_put_perag(ip->i_mount, pag); |
| 666 | |
| 667 | /* |
| 668 | * If the inode is still dirty, then flush it out. If the inode |
| 669 | * is not in the AIL, then it will be OK to flush it delwri as |
| 670 | * long as xfs_iflush() does not keep any references to the inode. |
| 671 | * We leave that decision up to xfs_iflush() since it has the |
| 672 | * knowledge of whether it's OK to simply do a delwri flush of |
| 673 | * the inode or whether we need to wait until the inode is |
| 674 | * pulled from the AIL. |
| 675 | * We get the flush lock regardless, though, just to make sure |
| 676 | * we don't free it while it is being flushed. |
| 677 | */ |
| 678 | if (!locked) { |
| 679 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 680 | xfs_iflock(ip); |
| 681 | } |
| 682 | |
| 683 | /* |
| 684 | * In the case of a forced shutdown we rely on xfs_iflush() to |
| 685 | * wait for the inode to be unpinned before returning an error. |
| 686 | */ |
| 687 | if (!is_bad_inode(VFS_I(ip)) && xfs_iflush(ip, sync_mode) == 0) { |
| 688 | /* synchronize with xfs_iflush_done */ |
| 689 | xfs_iflock(ip); |
| 690 | xfs_ifunlock(ip); |
| 691 | } |
| 692 | |
| 693 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 694 | xfs_ireclaim(ip); |
| 695 | return 0; |
| 696 | } |
| 697 | |
David Chinner | 1165451 | 2008-10-30 17:37:49 +1100 | [diff] [blame] | 698 | /* |
| 699 | * We set the inode flag atomically with the radix tree tag. |
| 700 | * Once we get tag lookups on the radix tree, this inode flag |
| 701 | * can go away. |
| 702 | */ |
David Chinner | 396beb8 | 2008-10-30 17:37:26 +1100 | [diff] [blame] | 703 | void |
| 704 | xfs_inode_set_reclaim_tag( |
| 705 | xfs_inode_t *ip) |
| 706 | { |
| 707 | xfs_mount_t *mp = ip->i_mount; |
| 708 | xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino); |
| 709 | |
| 710 | read_lock(&pag->pag_ici_lock); |
| 711 | spin_lock(&ip->i_flags_lock); |
| 712 | radix_tree_tag_set(&pag->pag_ici_root, |
| 713 | XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG); |
David Chinner | 1165451 | 2008-10-30 17:37:49 +1100 | [diff] [blame] | 714 | __xfs_iflags_set(ip, XFS_IRECLAIMABLE); |
David Chinner | 396beb8 | 2008-10-30 17:37:26 +1100 | [diff] [blame] | 715 | spin_unlock(&ip->i_flags_lock); |
| 716 | read_unlock(&pag->pag_ici_lock); |
| 717 | xfs_put_perag(mp, pag); |
| 718 | } |
| 719 | |
| 720 | void |
| 721 | __xfs_inode_clear_reclaim_tag( |
| 722 | xfs_mount_t *mp, |
| 723 | xfs_perag_t *pag, |
| 724 | xfs_inode_t *ip) |
| 725 | { |
| 726 | radix_tree_tag_clear(&pag->pag_ici_root, |
| 727 | XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG); |
| 728 | } |
| 729 | |
| 730 | void |
| 731 | xfs_inode_clear_reclaim_tag( |
| 732 | xfs_inode_t *ip) |
| 733 | { |
| 734 | xfs_mount_t *mp = ip->i_mount; |
| 735 | xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino); |
| 736 | |
| 737 | read_lock(&pag->pag_ici_lock); |
| 738 | spin_lock(&ip->i_flags_lock); |
| 739 | __xfs_inode_clear_reclaim_tag(mp, pag, ip); |
| 740 | spin_unlock(&ip->i_flags_lock); |
| 741 | read_unlock(&pag->pag_ici_lock); |
| 742 | xfs_put_perag(mp, pag); |
| 743 | } |
| 744 | |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 745 | STATIC int |
| 746 | xfs_reclaim_inode_now( |
| 747 | struct xfs_inode *ip, |
| 748 | struct xfs_perag *pag, |
| 749 | int flags) |
David Chinner | 7a3be02 | 2008-10-30 17:37:37 +1100 | [diff] [blame] | 750 | { |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 751 | /* ignore if already under reclaim */ |
| 752 | if (xfs_iflags_test(ip, XFS_IRECLAIM)) { |
David Chinner | 7a3be02 | 2008-10-30 17:37:37 +1100 | [diff] [blame] | 753 | read_unlock(&pag->pag_ici_lock); |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 754 | return 0; |
David Chinner | 7a3be02 | 2008-10-30 17:37:37 +1100 | [diff] [blame] | 755 | } |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 756 | read_unlock(&pag->pag_ici_lock); |
David Chinner | 7a3be02 | 2008-10-30 17:37:37 +1100 | [diff] [blame] | 757 | |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 758 | return xfs_reclaim_inode(ip, 0, flags); |
David Chinner | 7a3be02 | 2008-10-30 17:37:37 +1100 | [diff] [blame] | 759 | } |
| 760 | |
David Chinner | fce08f2 | 2008-10-30 17:37:03 +1100 | [diff] [blame] | 761 | int |
David Chinner | 1dc3318 | 2008-10-30 17:37:15 +1100 | [diff] [blame] | 762 | xfs_reclaim_inodes( |
David Chinner | fce08f2 | 2008-10-30 17:37:03 +1100 | [diff] [blame] | 763 | xfs_mount_t *mp, |
David Chinner | fce08f2 | 2008-10-30 17:37:03 +1100 | [diff] [blame] | 764 | int mode) |
| 765 | { |
Dave Chinner | 75f3cb1 | 2009-06-08 15:35:14 +0200 | [diff] [blame] | 766 | return xfs_inode_ag_iterator(mp, xfs_reclaim_inode_now, mode, |
| 767 | XFS_ICI_RECLAIM_TAG); |
David Chinner | fce08f2 | 2008-10-30 17:37:03 +1100 | [diff] [blame] | 768 | } |