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