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