Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "xfs_types.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 21 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "xfs_log.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 23 | #include "xfs_inum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_trans.h" |
| 25 | #include "xfs_sb.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 26 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "xfs_dir2.h" |
| 28 | #include "xfs_dmapi.h" |
| 29 | #include "xfs_mount.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 30 | #include "xfs_da_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_bmap_btree.h" |
| 32 | #include "xfs_ialloc_btree.h" |
| 33 | #include "xfs_alloc_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 34 | #include "xfs_dir2_sf.h" |
| 35 | #include "xfs_attr_sf.h" |
| 36 | #include "xfs_dinode.h" |
| 37 | #include "xfs_inode.h" |
| 38 | #include "xfs_inode_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include "xfs_btree.h" |
| 40 | #include "xfs_alloc.h" |
| 41 | #include "xfs_ialloc.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 42 | #include "xfs_quota.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include "xfs_error.h" |
| 44 | #include "xfs_bmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include "xfs_rw.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include "xfs_buf_item.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 47 | #include "xfs_log_priv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include "xfs_dir2_trace.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 49 | #include "xfs_extfree_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #include "xfs_acl.h" |
| 51 | #include "xfs_attr.h" |
| 52 | #include "xfs_clnt.h" |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 53 | #include "xfs_mru_cache.h" |
| 54 | #include "xfs_filestream.h" |
Christoph Hellwig | e13a73f | 2006-01-11 15:30:08 +1100 | [diff] [blame] | 55 | #include "xfs_fsops.h" |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 56 | #include "xfs_vnodeops.h" |
Christoph Hellwig | 48c872a9f | 2007-08-30 17:20:31 +1000 | [diff] [blame] | 57 | #include "xfs_vfsops.h" |
Christoph Hellwig | 4335509 | 2008-03-27 18:01:08 +1100 | [diff] [blame] | 58 | #include "xfs_utils.h" |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Lachlan McIlroy | de2eeea | 2008-02-06 13:37:56 +1100 | [diff] [blame] | 61 | int __init |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | xfs_init(void) |
| 63 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | #ifdef XFS_DABUF_DEBUG |
Eric Sandeen | 703e1f0 | 2007-10-11 17:41:21 +1000 | [diff] [blame] | 65 | extern spinlock_t xfs_dabuf_global_lock; |
Eric Sandeen | 007c61c | 2007-10-11 17:43:56 +1000 | [diff] [blame] | 66 | spin_lock_init(&xfs_dabuf_global_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | #endif |
| 68 | |
| 69 | /* |
| 70 | * Initialize all of the zone allocators we use. |
| 71 | */ |
David Chinner | eb01c9c | 2008-04-10 12:18:46 +1000 | [diff] [blame] | 72 | xfs_log_ticket_zone = kmem_zone_init(sizeof(xlog_ticket_t), |
| 73 | "xfs_log_ticket"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | xfs_bmap_free_item_zone = kmem_zone_init(sizeof(xfs_bmap_free_item_t), |
David Chinner | eb01c9c | 2008-04-10 12:18:46 +1000 | [diff] [blame] | 75 | "xfs_bmap_free_item"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | xfs_btree_cur_zone = kmem_zone_init(sizeof(xfs_btree_cur_t), |
David Chinner | eb01c9c | 2008-04-10 12:18:46 +1000 | [diff] [blame] | 77 | "xfs_btree_cur"); |
| 78 | xfs_da_state_zone = kmem_zone_init(sizeof(xfs_da_state_t), |
| 79 | "xfs_da_state"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | xfs_dabuf_zone = kmem_zone_init(sizeof(xfs_dabuf_t), "xfs_dabuf"); |
Nathan Scott | 8758280 | 2006-03-14 13:18:19 +1100 | [diff] [blame] | 81 | xfs_ifork_zone = kmem_zone_init(sizeof(xfs_ifork_t), "xfs_ifork"); |
David Chinner | eb01c9c | 2008-04-10 12:18:46 +1000 | [diff] [blame] | 82 | xfs_trans_zone = kmem_zone_init(sizeof(xfs_trans_t), "xfs_trans"); |
Nathan Scott | 8758280 | 2006-03-14 13:18:19 +1100 | [diff] [blame] | 83 | xfs_acl_zone_init(xfs_acl_zone, "xfs_acl"); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 84 | xfs_mru_cache_init(); |
| 85 | xfs_filestream_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * The size of the zone allocated buf log item is the maximum |
| 89 | * size possible under XFS. This wastes a little bit of memory, |
| 90 | * but it is much faster. |
| 91 | */ |
| 92 | xfs_buf_item_zone = |
| 93 | kmem_zone_init((sizeof(xfs_buf_log_item_t) + |
| 94 | (((XFS_MAX_BLOCKSIZE / XFS_BLI_CHUNK) / |
| 95 | NBWORD) * sizeof(int))), |
| 96 | "xfs_buf_item"); |
Nathan Scott | 8758280 | 2006-03-14 13:18:19 +1100 | [diff] [blame] | 97 | xfs_efd_zone = |
| 98 | kmem_zone_init((sizeof(xfs_efd_log_item_t) + |
| 99 | ((XFS_EFD_MAX_FAST_EXTENTS - 1) * |
| 100 | sizeof(xfs_extent_t))), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | "xfs_efd_item"); |
Nathan Scott | 8758280 | 2006-03-14 13:18:19 +1100 | [diff] [blame] | 102 | xfs_efi_zone = |
| 103 | kmem_zone_init((sizeof(xfs_efi_log_item_t) + |
| 104 | ((XFS_EFI_MAX_FAST_EXTENTS - 1) * |
| 105 | sizeof(xfs_extent_t))), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | "xfs_efi_item"); |
Nathan Scott | 8758280 | 2006-03-14 13:18:19 +1100 | [diff] [blame] | 107 | |
| 108 | /* |
| 109 | * These zones warrant special memory allocator hints |
| 110 | */ |
| 111 | xfs_inode_zone = |
| 112 | kmem_zone_init_flags(sizeof(xfs_inode_t), "xfs_inode", |
| 113 | KM_ZONE_HWALIGN | KM_ZONE_RECLAIM | |
| 114 | KM_ZONE_SPREAD, NULL); |
| 115 | xfs_ili_zone = |
| 116 | kmem_zone_init_flags(sizeof(xfs_inode_log_item_t), "xfs_ili", |
| 117 | KM_ZONE_SPREAD, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
| 119 | /* |
| 120 | * Allocate global trace buffers. |
| 121 | */ |
| 122 | #ifdef XFS_ALLOC_TRACE |
| 123 | xfs_alloc_trace_buf = ktrace_alloc(XFS_ALLOC_TRACE_SIZE, KM_SLEEP); |
| 124 | #endif |
| 125 | #ifdef XFS_BMAP_TRACE |
| 126 | xfs_bmap_trace_buf = ktrace_alloc(XFS_BMAP_TRACE_SIZE, KM_SLEEP); |
| 127 | #endif |
| 128 | #ifdef XFS_BMBT_TRACE |
| 129 | xfs_bmbt_trace_buf = ktrace_alloc(XFS_BMBT_TRACE_SIZE, KM_SLEEP); |
| 130 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | #ifdef XFS_ATTR_TRACE |
| 132 | xfs_attr_trace_buf = ktrace_alloc(XFS_ATTR_TRACE_SIZE, KM_SLEEP); |
| 133 | #endif |
| 134 | #ifdef XFS_DIR2_TRACE |
| 135 | xfs_dir2_trace_buf = ktrace_alloc(XFS_DIR2_GTRACE_SIZE, KM_SLEEP); |
| 136 | #endif |
| 137 | |
| 138 | xfs_dir_startup(); |
| 139 | |
| 140 | #if (defined(DEBUG) || defined(INDUCE_IO_ERROR)) |
| 141 | xfs_error_test_init(); |
| 142 | #endif /* DEBUG || INDUCE_IO_ERROR */ |
| 143 | |
| 144 | xfs_init_procfs(); |
| 145 | xfs_sysctl_register(); |
| 146 | return 0; |
| 147 | } |
| 148 | |
Lachlan McIlroy | de2eeea | 2008-02-06 13:37:56 +1100 | [diff] [blame] | 149 | void __exit |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | xfs_cleanup(void) |
| 151 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | extern kmem_zone_t *xfs_inode_zone; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | extern kmem_zone_t *xfs_efd_zone; |
| 154 | extern kmem_zone_t *xfs_efi_zone; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
| 156 | xfs_cleanup_procfs(); |
| 157 | xfs_sysctl_unregister(); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 158 | xfs_filestream_uninit(); |
| 159 | xfs_mru_cache_uninit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | xfs_acl_zone_destroy(xfs_acl_zone); |
| 161 | |
| 162 | #ifdef XFS_DIR2_TRACE |
| 163 | ktrace_free(xfs_dir2_trace_buf); |
| 164 | #endif |
| 165 | #ifdef XFS_ATTR_TRACE |
| 166 | ktrace_free(xfs_attr_trace_buf); |
| 167 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | #ifdef XFS_BMBT_TRACE |
| 169 | ktrace_free(xfs_bmbt_trace_buf); |
| 170 | #endif |
| 171 | #ifdef XFS_BMAP_TRACE |
| 172 | ktrace_free(xfs_bmap_trace_buf); |
| 173 | #endif |
| 174 | #ifdef XFS_ALLOC_TRACE |
| 175 | ktrace_free(xfs_alloc_trace_buf); |
| 176 | #endif |
| 177 | |
Nathan Scott | 3758dee | 2006-03-22 12:47:28 +1100 | [diff] [blame] | 178 | kmem_zone_destroy(xfs_bmap_free_item_zone); |
| 179 | kmem_zone_destroy(xfs_btree_cur_zone); |
| 180 | kmem_zone_destroy(xfs_inode_zone); |
| 181 | kmem_zone_destroy(xfs_trans_zone); |
| 182 | kmem_zone_destroy(xfs_da_state_zone); |
| 183 | kmem_zone_destroy(xfs_dabuf_zone); |
| 184 | kmem_zone_destroy(xfs_buf_item_zone); |
| 185 | kmem_zone_destroy(xfs_efd_zone); |
| 186 | kmem_zone_destroy(xfs_efi_zone); |
| 187 | kmem_zone_destroy(xfs_ifork_zone); |
| 188 | kmem_zone_destroy(xfs_ili_zone); |
Alexey Dobriyan | fe0754f | 2008-04-29 12:53:08 +1000 | [diff] [blame] | 189 | kmem_zone_destroy(xfs_log_ticket_zone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | } |
| 191 | |
David Chinner | 3c85c36 | 2008-04-10 12:19:40 +1000 | [diff] [blame] | 192 | STATIC void |
Christoph Hellwig | f898d6c | 2005-06-21 15:40:48 +1000 | [diff] [blame] | 193 | xfs_quiesce_fs( |
| 194 | xfs_mount_t *mp) |
| 195 | { |
| 196 | int count = 0, pincount; |
Nathan Scott | 3758dee | 2006-03-22 12:47:28 +1100 | [diff] [blame] | 197 | |
Christoph Hellwig | f898d6c | 2005-06-21 15:40:48 +1000 | [diff] [blame] | 198 | xfs_flush_buftarg(mp->m_ddev_targp, 0); |
| 199 | xfs_finish_reclaim_all(mp, 0); |
| 200 | |
| 201 | /* This loop must run at least twice. |
| 202 | * The first instance of the loop will flush |
| 203 | * most meta data but that will generate more |
| 204 | * meta data (typically directory updates). |
| 205 | * Which then must be flushed and logged before |
| 206 | * we can write the unmount record. |
Nathan Scott | 3758dee | 2006-03-22 12:47:28 +1100 | [diff] [blame] | 207 | */ |
Christoph Hellwig | f898d6c | 2005-06-21 15:40:48 +1000 | [diff] [blame] | 208 | do { |
David Chinner | 516b2e7 | 2007-06-18 16:50:48 +1000 | [diff] [blame] | 209 | xfs_syncsub(mp, SYNC_INODE_QUIESCE, NULL); |
Christoph Hellwig | f898d6c | 2005-06-21 15:40:48 +1000 | [diff] [blame] | 210 | pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1); |
| 211 | if (!pincount) { |
| 212 | delay(50); |
| 213 | count++; |
| 214 | } |
| 215 | } while (count < 2); |
Christoph Hellwig | f898d6c | 2005-06-21 15:40:48 +1000 | [diff] [blame] | 216 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
David Chinner | 516b2e7 | 2007-06-18 16:50:48 +1000 | [diff] [blame] | 218 | /* |
| 219 | * Second stage of a quiesce. The data is already synced, now we have to take |
| 220 | * care of the metadata. New transactions are already blocked, so we need to |
| 221 | * wait for any remaining transactions to drain out before proceding. |
| 222 | */ |
Christoph Hellwig | 9909c4a | 2007-10-11 18:11:14 +1000 | [diff] [blame] | 223 | void |
David Chinner | 516b2e7 | 2007-06-18 16:50:48 +1000 | [diff] [blame] | 224 | xfs_attr_quiesce( |
| 225 | xfs_mount_t *mp) |
| 226 | { |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 227 | int error = 0; |
| 228 | |
David Chinner | 516b2e7 | 2007-06-18 16:50:48 +1000 | [diff] [blame] | 229 | /* wait for all modifications to complete */ |
| 230 | while (atomic_read(&mp->m_active_trans) > 0) |
| 231 | delay(100); |
| 232 | |
| 233 | /* flush inodes and push all remaining buffers out to disk */ |
| 234 | xfs_quiesce_fs(mp); |
| 235 | |
| 236 | ASSERT_ALWAYS(atomic_read(&mp->m_active_trans) == 0); |
| 237 | |
| 238 | /* Push the superblock and write an unmount record */ |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 239 | error = xfs_log_sbcount(mp, 1); |
| 240 | if (error) |
| 241 | xfs_fs_cmn_err(CE_WARN, mp, |
| 242 | "xfs_attr_quiesce: failed to log sb changes. " |
| 243 | "Frozen image may not be consistent."); |
David Chinner | 516b2e7 | 2007-06-18 16:50:48 +1000 | [diff] [blame] | 244 | xfs_log_unmount_write(mp); |
| 245 | xfs_unmountfs_writesb(mp); |
| 246 | } |
| 247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | /* |
| 249 | * xfs_unmount_flush implements a set of flush operation on special |
| 250 | * inodes, which are needed as a separate set of operations so that |
| 251 | * they can be called as part of relocation process. |
| 252 | */ |
| 253 | int |
| 254 | xfs_unmount_flush( |
| 255 | xfs_mount_t *mp, /* Mount structure we are getting |
| 256 | rid of. */ |
| 257 | int relocation) /* Called from vfs relocation. */ |
| 258 | { |
| 259 | xfs_inode_t *rip = mp->m_rootip; |
| 260 | xfs_inode_t *rbmip; |
| 261 | xfs_inode_t *rsumip = NULL; |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 262 | bhv_vnode_t *rvp = XFS_ITOV(rip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | int error; |
| 264 | |
Lachlan McIlroy | f7c66ce | 2007-05-08 13:50:19 +1000 | [diff] [blame] | 265 | xfs_ilock(rip, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | xfs_iflock(rip); |
| 267 | |
| 268 | /* |
| 269 | * Flush out the real time inodes. |
| 270 | */ |
| 271 | if ((rbmip = mp->m_rbmip) != NULL) { |
| 272 | xfs_ilock(rbmip, XFS_ILOCK_EXCL); |
| 273 | xfs_iflock(rbmip); |
| 274 | error = xfs_iflush(rbmip, XFS_IFLUSH_SYNC); |
| 275 | xfs_iunlock(rbmip, XFS_ILOCK_EXCL); |
| 276 | |
| 277 | if (error == EFSCORRUPTED) |
| 278 | goto fscorrupt_out; |
| 279 | |
| 280 | ASSERT(vn_count(XFS_ITOV(rbmip)) == 1); |
| 281 | |
| 282 | rsumip = mp->m_rsumip; |
| 283 | xfs_ilock(rsumip, XFS_ILOCK_EXCL); |
| 284 | xfs_iflock(rsumip); |
| 285 | error = xfs_iflush(rsumip, XFS_IFLUSH_SYNC); |
| 286 | xfs_iunlock(rsumip, XFS_ILOCK_EXCL); |
| 287 | |
| 288 | if (error == EFSCORRUPTED) |
| 289 | goto fscorrupt_out; |
| 290 | |
| 291 | ASSERT(vn_count(XFS_ITOV(rsumip)) == 1); |
| 292 | } |
| 293 | |
| 294 | /* |
| 295 | * Synchronously flush root inode to disk |
| 296 | */ |
| 297 | error = xfs_iflush(rip, XFS_IFLUSH_SYNC); |
| 298 | if (error == EFSCORRUPTED) |
| 299 | goto fscorrupt_out2; |
| 300 | |
| 301 | if (vn_count(rvp) != 1 && !relocation) { |
| 302 | xfs_iunlock(rip, XFS_ILOCK_EXCL); |
| 303 | return XFS_ERROR(EBUSY); |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | * Release dquot that rootinode, rbmino and rsumino might be holding, |
| 308 | * flush and purge the quota inodes. |
| 309 | */ |
| 310 | error = XFS_QM_UNMOUNT(mp); |
| 311 | if (error == EFSCORRUPTED) |
| 312 | goto fscorrupt_out2; |
| 313 | |
| 314 | if (rbmip) { |
Christoph Hellwig | 4335509 | 2008-03-27 18:01:08 +1100 | [diff] [blame] | 315 | IRELE(rbmip); |
| 316 | IRELE(rsumip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | xfs_iunlock(rip, XFS_ILOCK_EXCL); |
| 320 | return 0; |
| 321 | |
| 322 | fscorrupt_out: |
| 323 | xfs_ifunlock(rip); |
| 324 | |
| 325 | fscorrupt_out2: |
| 326 | xfs_iunlock(rip, XFS_ILOCK_EXCL); |
| 327 | |
| 328 | return XFS_ERROR(EFSCORRUPTED); |
| 329 | } |
| 330 | |
| 331 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | * xfs_sync flushes any pending I/O to file system vfsp. |
| 333 | * |
| 334 | * This routine is called by vfs_sync() to make sure that things make it |
| 335 | * out to disk eventually, on sync() system calls to flush out everything, |
| 336 | * and when the file system is unmounted. For the vfs_sync() case, all |
| 337 | * we really need to do is sync out the log to make all of our meta-data |
| 338 | * updates permanent (except for timestamps). For calls from pflushd(), |
| 339 | * dirty pages are kept moving by calling pdflush() on the inodes |
| 340 | * containing them. We also flush the inodes that we can lock without |
| 341 | * sleeping and the superblock if we can lock it without sleeping from |
| 342 | * vfs_sync() so that items at the tail of the log are always moving out. |
| 343 | * |
| 344 | * Flags: |
| 345 | * SYNC_BDFLUSH - We're being called from vfs_sync() so we don't want |
| 346 | * to sleep if we can help it. All we really need |
| 347 | * to do is ensure that the log is synced at least |
| 348 | * periodically. We also push the inodes and |
| 349 | * superblock if we can lock them without sleeping |
| 350 | * and they are not pinned. |
| 351 | * SYNC_ATTR - We need to flush the inodes. If SYNC_BDFLUSH is not |
| 352 | * set, then we really want to lock each inode and flush |
| 353 | * it. |
| 354 | * SYNC_WAIT - All the flushes that take place in this call should |
| 355 | * be synchronous. |
| 356 | * SYNC_DELWRI - This tells us to push dirty pages associated with |
| 357 | * inodes. SYNC_WAIT and SYNC_BDFLUSH are used to |
| 358 | * determine if they should be flushed sync, async, or |
| 359 | * delwri. |
| 360 | * SYNC_CLOSE - This flag is passed when the system is being |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 361 | * unmounted. We should sync and invalidate everything. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | * SYNC_FSDATA - This indicates that the caller would like to make |
| 363 | * sure the superblock is safe on disk. We can ensure |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 364 | * this by simply making sure the log gets flushed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | * if SYNC_BDFLUSH is set, and by actually writing it |
| 366 | * out otherwise. |
David Chinner | 3c0dc77 | 2007-02-10 18:37:22 +1100 | [diff] [blame] | 367 | * SYNC_IOWAIT - The caller wants us to wait for all data I/O to complete |
| 368 | * before we return (including direct I/O). Forms the drain |
| 369 | * side of the write barrier needed to safely quiesce the |
| 370 | * filesystem. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | * |
| 372 | */ |
Christoph Hellwig | 48c872a9f | 2007-08-30 17:20:31 +1000 | [diff] [blame] | 373 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | xfs_sync( |
Christoph Hellwig | 48c872a9f | 2007-08-30 17:20:31 +1000 | [diff] [blame] | 375 | xfs_mount_t *mp, |
| 376 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | { |
Christoph Hellwig | b09cc77 | 2007-08-30 17:19:57 +1000 | [diff] [blame] | 378 | int error; |
| 379 | |
| 380 | /* |
| 381 | * Get the Quota Manager to flush the dquots. |
| 382 | * |
| 383 | * If XFS quota support is not enabled or this filesystem |
| 384 | * instance does not use quotas XFS_QM_DQSYNC will always |
| 385 | * return zero. |
| 386 | */ |
| 387 | error = XFS_QM_DQSYNC(mp, flags); |
| 388 | if (error) { |
| 389 | /* |
| 390 | * If we got an IO error, we will be shutting down. |
| 391 | * So, there's nothing more for us to do here. |
| 392 | */ |
| 393 | ASSERT(error != EIO || XFS_FORCED_SHUTDOWN(mp)); |
| 394 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 395 | return XFS_ERROR(error); |
| 396 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 398 | if (flags & SYNC_IOWAIT) |
| 399 | xfs_filestream_flush(mp); |
| 400 | |
David Chinner | 2823945 | 2007-02-10 18:36:40 +1100 | [diff] [blame] | 401 | return xfs_syncsub(mp, flags, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | /* |
| 405 | * xfs sync routine for internal use |
| 406 | * |
Nathan Scott | b83bd13 | 2006-06-09 16:48:30 +1000 | [diff] [blame] | 407 | * This routine supports all of the flags defined for the generic vfs_sync |
Lachlan McIlroy | 1f9b3b6 | 2007-02-10 18:35:33 +1100 | [diff] [blame] | 408 | * interface as explained above under xfs_sync. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | * |
| 410 | */ |
Nathan Scott | ee34807 | 2005-11-02 10:32:38 +1100 | [diff] [blame] | 411 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | xfs_sync_inodes( |
| 413 | xfs_mount_t *mp, |
| 414 | int flags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | int *bypassed) |
| 416 | { |
| 417 | xfs_inode_t *ip = NULL; |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 418 | bhv_vnode_t *vp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | int error; |
| 420 | int last_error; |
| 421 | uint64_t fflag; |
| 422 | uint lock_flags; |
| 423 | uint base_lock_flags; |
| 424 | boolean_t mount_locked; |
| 425 | boolean_t vnode_refed; |
| 426 | int preempt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | xfs_iptr_t *ipointer; |
| 428 | #ifdef DEBUG |
| 429 | boolean_t ipointer_in = B_FALSE; |
| 430 | |
| 431 | #define IPOINTER_SET ipointer_in = B_TRUE |
| 432 | #define IPOINTER_CLR ipointer_in = B_FALSE |
| 433 | #else |
| 434 | #define IPOINTER_SET |
| 435 | #define IPOINTER_CLR |
| 436 | #endif |
| 437 | |
| 438 | |
| 439 | /* Insert a marker record into the inode list after inode ip. The list |
| 440 | * must be locked when this is called. After the call the list will no |
| 441 | * longer be locked. |
| 442 | */ |
| 443 | #define IPOINTER_INSERT(ip, mp) { \ |
| 444 | ASSERT(ipointer_in == B_FALSE); \ |
| 445 | ipointer->ip_mnext = ip->i_mnext; \ |
| 446 | ipointer->ip_mprev = ip; \ |
| 447 | ip->i_mnext = (xfs_inode_t *)ipointer; \ |
| 448 | ipointer->ip_mnext->i_mprev = (xfs_inode_t *)ipointer; \ |
| 449 | preempt = 0; \ |
| 450 | XFS_MOUNT_IUNLOCK(mp); \ |
| 451 | mount_locked = B_FALSE; \ |
| 452 | IPOINTER_SET; \ |
| 453 | } |
| 454 | |
| 455 | /* Remove the marker from the inode list. If the marker was the only item |
| 456 | * in the list then there are no remaining inodes and we should zero out |
| 457 | * the whole list. If we are the current head of the list then move the head |
| 458 | * past us. |
| 459 | */ |
| 460 | #define IPOINTER_REMOVE(ip, mp) { \ |
| 461 | ASSERT(ipointer_in == B_TRUE); \ |
| 462 | if (ipointer->ip_mnext != (xfs_inode_t *)ipointer) { \ |
| 463 | ip = ipointer->ip_mnext; \ |
| 464 | ip->i_mprev = ipointer->ip_mprev; \ |
| 465 | ipointer->ip_mprev->i_mnext = ip; \ |
| 466 | if (mp->m_inodes == (xfs_inode_t *)ipointer) { \ |
| 467 | mp->m_inodes = ip; \ |
| 468 | } \ |
| 469 | } else { \ |
| 470 | ASSERT(mp->m_inodes == (xfs_inode_t *)ipointer); \ |
| 471 | mp->m_inodes = NULL; \ |
| 472 | ip = NULL; \ |
| 473 | } \ |
| 474 | IPOINTER_CLR; \ |
| 475 | } |
| 476 | |
| 477 | #define XFS_PREEMPT_MASK 0x7f |
| 478 | |
Lachlan McIlroy | 44866d3 | 2007-09-14 15:21:08 +1000 | [diff] [blame] | 479 | ASSERT(!(flags & SYNC_BDFLUSH)); |
| 480 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | if (bypassed) |
| 482 | *bypassed = 0; |
Christoph Hellwig | bd186aa | 2007-08-30 17:21:12 +1000 | [diff] [blame] | 483 | if (mp->m_flags & XFS_MOUNT_RDONLY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | return 0; |
| 485 | error = 0; |
| 486 | last_error = 0; |
| 487 | preempt = 0; |
| 488 | |
| 489 | /* Allocate a reference marker */ |
| 490 | ipointer = (xfs_iptr_t *)kmem_zalloc(sizeof(xfs_iptr_t), KM_SLEEP); |
| 491 | |
| 492 | fflag = XFS_B_ASYNC; /* default is don't wait */ |
Lachlan McIlroy | 44866d3 | 2007-09-14 15:21:08 +1000 | [diff] [blame] | 493 | if (flags & SYNC_DELWRI) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | fflag = XFS_B_DELWRI; |
| 495 | if (flags & SYNC_WAIT) |
| 496 | fflag = 0; /* synchronous overrides all */ |
| 497 | |
| 498 | base_lock_flags = XFS_ILOCK_SHARED; |
| 499 | if (flags & (SYNC_DELWRI | SYNC_CLOSE)) { |
| 500 | /* |
| 501 | * We need the I/O lock if we're going to call any of |
| 502 | * the flush/inval routines. |
| 503 | */ |
| 504 | base_lock_flags |= XFS_IOLOCK_SHARED; |
| 505 | } |
| 506 | |
| 507 | XFS_MOUNT_ILOCK(mp); |
| 508 | |
| 509 | ip = mp->m_inodes; |
| 510 | |
| 511 | mount_locked = B_TRUE; |
| 512 | vnode_refed = B_FALSE; |
| 513 | |
| 514 | IPOINTER_CLR; |
| 515 | |
| 516 | do { |
| 517 | ASSERT(ipointer_in == B_FALSE); |
| 518 | ASSERT(vnode_refed == B_FALSE); |
| 519 | |
| 520 | lock_flags = base_lock_flags; |
| 521 | |
| 522 | /* |
| 523 | * There were no inodes in the list, just break out |
| 524 | * of the loop. |
| 525 | */ |
| 526 | if (ip == NULL) { |
| 527 | break; |
| 528 | } |
| 529 | |
| 530 | /* |
| 531 | * We found another sync thread marker - skip it |
| 532 | */ |
| 533 | if (ip->i_mount == NULL) { |
| 534 | ip = ip->i_mnext; |
| 535 | continue; |
| 536 | } |
| 537 | |
| 538 | vp = XFS_ITOV_NULL(ip); |
| 539 | |
| 540 | /* |
| 541 | * If the vnode is gone then this is being torn down, |
| 542 | * call reclaim if it is flushed, else let regular flush |
| 543 | * code deal with it later in the loop. |
| 544 | */ |
| 545 | |
| 546 | if (vp == NULL) { |
| 547 | /* Skip ones already in reclaim */ |
| 548 | if (ip->i_flags & XFS_IRECLAIM) { |
| 549 | ip = ip->i_mnext; |
| 550 | continue; |
| 551 | } |
| 552 | if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0) { |
| 553 | ip = ip->i_mnext; |
| 554 | } else if ((xfs_ipincount(ip) == 0) && |
| 555 | xfs_iflock_nowait(ip)) { |
| 556 | IPOINTER_INSERT(ip, mp); |
| 557 | |
| 558 | xfs_finish_reclaim(ip, 1, |
| 559 | XFS_IFLUSH_DELWRI_ELSE_ASYNC); |
| 560 | |
| 561 | XFS_MOUNT_ILOCK(mp); |
| 562 | mount_locked = B_TRUE; |
| 563 | IPOINTER_REMOVE(ip, mp); |
| 564 | } else { |
| 565 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 566 | ip = ip->i_mnext; |
| 567 | } |
| 568 | continue; |
| 569 | } |
| 570 | |
| 571 | if (VN_BAD(vp)) { |
| 572 | ip = ip->i_mnext; |
| 573 | continue; |
| 574 | } |
| 575 | |
| 576 | if (XFS_FORCED_SHUTDOWN(mp) && !(flags & SYNC_CLOSE)) { |
| 577 | XFS_MOUNT_IUNLOCK(mp); |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 578 | kmem_free(ipointer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | * Try to lock without sleeping. We're out of order with |
| 584 | * the inode list lock here, so if we fail we need to drop |
| 585 | * the mount lock and try again. If we're called from |
| 586 | * bdflush() here, then don't bother. |
| 587 | * |
| 588 | * The inode lock here actually coordinates with the |
| 589 | * almost spurious inode lock in xfs_ireclaim() to prevent |
| 590 | * the vnode we handle here without a reference from |
| 591 | * being freed while we reference it. If we lock the inode |
| 592 | * while it's on the mount list here, then the spurious inode |
| 593 | * lock in xfs_ireclaim() after the inode is pulled from |
| 594 | * the mount list will sleep until we release it here. |
| 595 | * This keeps the vnode from being freed while we reference |
Christoph Hellwig | cdb6268 | 2005-09-02 16:24:19 +1000 | [diff] [blame] | 596 | * it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | */ |
| 598 | if (xfs_ilock_nowait(ip, lock_flags) == 0) { |
Lachlan McIlroy | 44866d3 | 2007-09-14 15:21:08 +1000 | [diff] [blame] | 599 | if (vp == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | ip = ip->i_mnext; |
| 601 | continue; |
| 602 | } |
| 603 | |
Christoph Hellwig | cdb6268 | 2005-09-02 16:24:19 +1000 | [diff] [blame] | 604 | vp = vn_grab(vp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | if (vp == NULL) { |
Christoph Hellwig | cdb6268 | 2005-09-02 16:24:19 +1000 | [diff] [blame] | 606 | ip = ip->i_mnext; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | continue; |
| 608 | } |
| 609 | |
Christoph Hellwig | cdb6268 | 2005-09-02 16:24:19 +1000 | [diff] [blame] | 610 | IPOINTER_INSERT(ip, mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | xfs_ilock(ip, lock_flags); |
| 612 | |
| 613 | ASSERT(vp == XFS_ITOV(ip)); |
| 614 | ASSERT(ip->i_mount == mp); |
| 615 | |
| 616 | vnode_refed = B_TRUE; |
| 617 | } |
| 618 | |
| 619 | /* From here on in the loop we may have a marker record |
| 620 | * in the inode list. |
| 621 | */ |
| 622 | |
David Chinner | 40095b6 | 2007-05-14 18:24:09 +1000 | [diff] [blame] | 623 | /* |
| 624 | * If we have to flush data or wait for I/O completion |
| 625 | * we need to drop the ilock that we currently hold. |
| 626 | * If we need to drop the lock, insert a marker if we |
| 627 | * have not already done so. |
| 628 | */ |
| 629 | if ((flags & (SYNC_CLOSE|SYNC_IOWAIT)) || |
| 630 | ((flags & SYNC_DELWRI) && VN_DIRTY(vp))) { |
| 631 | if (mount_locked) { |
| 632 | IPOINTER_INSERT(ip, mp); |
| 633 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
| 635 | |
David Chinner | 40095b6 | 2007-05-14 18:24:09 +1000 | [diff] [blame] | 636 | if (flags & SYNC_CLOSE) { |
| 637 | /* Shutdown case. Flush and invalidate. */ |
| 638 | if (XFS_FORCED_SHUTDOWN(mp)) |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 639 | xfs_tosspages(ip, 0, -1, |
| 640 | FI_REMAPF); |
David Chinner | 40095b6 | 2007-05-14 18:24:09 +1000 | [diff] [blame] | 641 | else |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 642 | error = xfs_flushinval_pages(ip, |
| 643 | 0, -1, FI_REMAPF); |
David Chinner | 40095b6 | 2007-05-14 18:24:09 +1000 | [diff] [blame] | 644 | } else if ((flags & SYNC_DELWRI) && VN_DIRTY(vp)) { |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 645 | error = xfs_flush_pages(ip, 0, |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 646 | -1, fflag, FI_NONE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | } |
| 648 | |
David Chinner | 40095b6 | 2007-05-14 18:24:09 +1000 | [diff] [blame] | 649 | /* |
| 650 | * When freezing, we need to wait ensure all I/O (including direct |
| 651 | * I/O) is complete to ensure no further data modification can take |
| 652 | * place after this point |
| 653 | */ |
| 654 | if (flags & SYNC_IOWAIT) |
Christoph Hellwig | b677c21 | 2007-08-29 11:46:28 +1000 | [diff] [blame] | 655 | vn_iowait(ip); |
David Chinner | 40095b6 | 2007-05-14 18:24:09 +1000 | [diff] [blame] | 656 | |
| 657 | xfs_ilock(ip, XFS_ILOCK_SHARED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Lachlan McIlroy | 44866d3 | 2007-09-14 15:21:08 +1000 | [diff] [blame] | 660 | if ((flags & SYNC_ATTR) && |
| 661 | (ip->i_update_core || |
| 662 | (ip->i_itemp && ip->i_itemp->ili_format.ilf_fields))) { |
| 663 | if (mount_locked) |
| 664 | IPOINTER_INSERT(ip, mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | |
Lachlan McIlroy | 44866d3 | 2007-09-14 15:21:08 +1000 | [diff] [blame] | 666 | if (flags & SYNC_WAIT) { |
| 667 | xfs_iflock(ip); |
| 668 | error = xfs_iflush(ip, XFS_IFLUSH_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | |
Lachlan McIlroy | 44866d3 | 2007-09-14 15:21:08 +1000 | [diff] [blame] | 670 | /* |
| 671 | * If we can't acquire the flush lock, then the inode |
| 672 | * is already being flushed so don't bother waiting. |
| 673 | * |
| 674 | * If we can lock it then do a delwri flush so we can |
| 675 | * combine multiple inode flushes in each disk write. |
| 676 | */ |
| 677 | } else if (xfs_iflock_nowait(ip)) { |
| 678 | error = xfs_iflush(ip, XFS_IFLUSH_DELWRI); |
| 679 | } else if (bypassed) { |
| 680 | (*bypassed)++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | } |
| 682 | } |
| 683 | |
| 684 | if (lock_flags != 0) { |
| 685 | xfs_iunlock(ip, lock_flags); |
| 686 | } |
| 687 | |
| 688 | if (vnode_refed) { |
| 689 | /* |
| 690 | * If we had to take a reference on the vnode |
| 691 | * above, then wait until after we've unlocked |
| 692 | * the inode to release the reference. This is |
| 693 | * because we can be already holding the inode |
Christoph Hellwig | 4335509 | 2008-03-27 18:01:08 +1100 | [diff] [blame] | 694 | * lock when IRELE() calls xfs_inactive(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | * |
| 696 | * Make sure to drop the mount lock before calling |
Christoph Hellwig | 4335509 | 2008-03-27 18:01:08 +1100 | [diff] [blame] | 697 | * IRELE() so that we don't trip over ourselves if |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | * we have to go for the mount lock again in the |
| 699 | * inactive code. |
| 700 | */ |
| 701 | if (mount_locked) { |
| 702 | IPOINTER_INSERT(ip, mp); |
| 703 | } |
| 704 | |
Christoph Hellwig | 4335509 | 2008-03-27 18:01:08 +1100 | [diff] [blame] | 705 | IRELE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | |
| 707 | vnode_refed = B_FALSE; |
| 708 | } |
| 709 | |
| 710 | if (error) { |
| 711 | last_error = error; |
| 712 | } |
| 713 | |
| 714 | /* |
| 715 | * bail out if the filesystem is corrupted. |
| 716 | */ |
| 717 | if (error == EFSCORRUPTED) { |
| 718 | if (!mount_locked) { |
| 719 | XFS_MOUNT_ILOCK(mp); |
| 720 | IPOINTER_REMOVE(ip, mp); |
| 721 | } |
| 722 | XFS_MOUNT_IUNLOCK(mp); |
| 723 | ASSERT(ipointer_in == B_FALSE); |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 724 | kmem_free(ipointer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | return XFS_ERROR(error); |
| 726 | } |
| 727 | |
| 728 | /* Let other threads have a chance at the mount lock |
| 729 | * if we have looped many times without dropping the |
| 730 | * lock. |
| 731 | */ |
| 732 | if ((++preempt & XFS_PREEMPT_MASK) == 0) { |
| 733 | if (mount_locked) { |
| 734 | IPOINTER_INSERT(ip, mp); |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | if (mount_locked == B_FALSE) { |
| 739 | XFS_MOUNT_ILOCK(mp); |
| 740 | mount_locked = B_TRUE; |
| 741 | IPOINTER_REMOVE(ip, mp); |
| 742 | continue; |
| 743 | } |
| 744 | |
| 745 | ASSERT(ipointer_in == B_FALSE); |
| 746 | ip = ip->i_mnext; |
| 747 | |
| 748 | } while (ip != mp->m_inodes); |
| 749 | |
| 750 | XFS_MOUNT_IUNLOCK(mp); |
| 751 | |
| 752 | ASSERT(ipointer_in == B_FALSE); |
| 753 | |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 754 | kmem_free(ipointer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | return XFS_ERROR(last_error); |
| 756 | } |
| 757 | |
| 758 | /* |
| 759 | * xfs sync routine for internal use |
| 760 | * |
Nathan Scott | b83bd13 | 2006-06-09 16:48:30 +1000 | [diff] [blame] | 761 | * This routine supports all of the flags defined for the generic vfs_sync |
Lachlan McIlroy | 1f9b3b6 | 2007-02-10 18:35:33 +1100 | [diff] [blame] | 762 | * interface as explained above under xfs_sync. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | * |
| 764 | */ |
| 765 | int |
| 766 | xfs_syncsub( |
| 767 | xfs_mount_t *mp, |
| 768 | int flags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | int *bypassed) |
| 770 | { |
| 771 | int error = 0; |
| 772 | int last_error = 0; |
| 773 | uint log_flags = XFS_LOG_FORCE; |
| 774 | xfs_buf_t *bp; |
| 775 | xfs_buf_log_item_t *bip; |
| 776 | |
| 777 | /* |
| 778 | * Sync out the log. This ensures that the log is periodically |
| 779 | * flushed even if there is not enough activity to fill it up. |
| 780 | */ |
| 781 | if (flags & SYNC_WAIT) |
| 782 | log_flags |= XFS_LOG_SYNC; |
| 783 | |
| 784 | xfs_log_force(mp, (xfs_lsn_t)0, log_flags); |
| 785 | |
| 786 | if (flags & (SYNC_ATTR|SYNC_DELWRI)) { |
| 787 | if (flags & SYNC_BDFLUSH) |
| 788 | xfs_finish_reclaim_all(mp, 1); |
| 789 | else |
Lachlan McIlroy | 1f9b3b6 | 2007-02-10 18:35:33 +1100 | [diff] [blame] | 790 | error = xfs_sync_inodes(mp, flags, bypassed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | /* |
| 794 | * Flushing out dirty data above probably generated more |
| 795 | * log activity, so if this isn't vfs_sync() then flush |
| 796 | * the log again. |
| 797 | */ |
| 798 | if (flags & SYNC_DELWRI) { |
| 799 | xfs_log_force(mp, (xfs_lsn_t)0, log_flags); |
| 800 | } |
| 801 | |
| 802 | if (flags & SYNC_FSDATA) { |
| 803 | /* |
| 804 | * If this is vfs_sync() then only sync the superblock |
| 805 | * if we can lock it without sleeping and it is not pinned. |
| 806 | */ |
| 807 | if (flags & SYNC_BDFLUSH) { |
| 808 | bp = xfs_getsb(mp, XFS_BUF_TRYLOCK); |
| 809 | if (bp != NULL) { |
| 810 | bip = XFS_BUF_FSPRIVATE(bp,xfs_buf_log_item_t*); |
| 811 | if ((bip != NULL) && |
| 812 | xfs_buf_item_dirty(bip)) { |
| 813 | if (!(XFS_BUF_ISPINNED(bp))) { |
| 814 | XFS_BUF_ASYNC(bp); |
| 815 | error = xfs_bwrite(mp, bp); |
| 816 | } else { |
| 817 | xfs_buf_relse(bp); |
| 818 | } |
| 819 | } else { |
| 820 | xfs_buf_relse(bp); |
| 821 | } |
| 822 | } |
| 823 | } else { |
| 824 | bp = xfs_getsb(mp, 0); |
| 825 | /* |
| 826 | * If the buffer is pinned then push on the log so |
| 827 | * we won't get stuck waiting in the write for |
| 828 | * someone, maybe ourselves, to flush the log. |
| 829 | * Even though we just pushed the log above, we |
| 830 | * did not have the superblock buffer locked at |
| 831 | * that point so it can become pinned in between |
| 832 | * there and here. |
| 833 | */ |
| 834 | if (XFS_BUF_ISPINNED(bp)) |
| 835 | xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE); |
| 836 | if (flags & SYNC_WAIT) |
| 837 | XFS_BUF_UNASYNC(bp); |
| 838 | else |
| 839 | XFS_BUF_ASYNC(bp); |
| 840 | error = xfs_bwrite(mp, bp); |
| 841 | } |
| 842 | if (error) { |
| 843 | last_error = error; |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | * Now check to see if the log needs a "dummy" transaction. |
| 849 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | if (!(flags & SYNC_REMOUNT) && xfs_log_need_covered(mp)) { |
| 851 | xfs_trans_t *tp; |
| 852 | xfs_inode_t *ip; |
| 853 | |
| 854 | /* |
| 855 | * Put a dummy transaction in the log to tell |
| 856 | * recovery that all others are OK. |
| 857 | */ |
| 858 | tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1); |
| 859 | if ((error = xfs_trans_reserve(tp, 0, |
| 860 | XFS_ICHANGE_LOG_RES(mp), |
| 861 | 0, 0, 0))) { |
| 862 | xfs_trans_cancel(tp, 0); |
| 863 | return error; |
| 864 | } |
| 865 | |
| 866 | ip = mp->m_rootip; |
| 867 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 868 | |
| 869 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 870 | xfs_trans_ihold(tp, ip); |
| 871 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 872 | error = xfs_trans_commit(tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 874 | xfs_log_force(mp, (xfs_lsn_t)0, log_flags); |
| 875 | } |
| 876 | |
| 877 | /* |
| 878 | * When shutting down, we need to insure that the AIL is pushed |
| 879 | * to disk or the filesystem can appear corrupt from the PROM. |
| 880 | */ |
| 881 | if ((flags & (SYNC_CLOSE|SYNC_WAIT)) == (SYNC_CLOSE|SYNC_WAIT)) { |
| 882 | XFS_bflush(mp->m_ddev_targp); |
| 883 | if (mp->m_rtdev_targp) { |
| 884 | XFS_bflush(mp->m_rtdev_targp); |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | return XFS_ERROR(last_error); |
| 889 | } |