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