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" |
| 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" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include "xfs_bmap_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 31 | #include "xfs_alloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "xfs_ialloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "xfs_dir2_sf.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 34 | #include "xfs_attr_sf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "xfs_dinode.h" |
| 36 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 37 | #include "xfs_btree.h" |
| 38 | #include "xfs_ialloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include "xfs_alloc.h" |
| 40 | #include "xfs_rtalloc.h" |
| 41 | #include "xfs_bmap.h" |
| 42 | #include "xfs_error.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include "xfs_rw.h" |
| 44 | #include "xfs_quota.h" |
| 45 | #include "xfs_fsops.h" |
Christoph Hellwig | 4335509 | 2008-03-27 18:01:08 +1100 | [diff] [blame] | 46 | #include "xfs_utils.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | STATIC int xfs_uuid_mount(xfs_mount_t *); |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 49 | STATIC void xfs_unmountfs_wait(xfs_mount_t *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 51 | |
| 52 | #ifdef HAVE_PERCPU_SB |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 53 | STATIC void xfs_icsb_balance_counter(xfs_mount_t *, xfs_sb_field_t, |
Christoph Hellwig | 45af6c6 | 2008-04-22 17:34:44 +1000 | [diff] [blame] | 54 | int); |
| 55 | STATIC void xfs_icsb_balance_counter_locked(xfs_mount_t *, xfs_sb_field_t, |
| 56 | int); |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 57 | STATIC int xfs_icsb_modify_counters(xfs_mount_t *, xfs_sb_field_t, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 58 | int64_t, int); |
David Chinner | 36fbe6e | 2008-04-10 12:19:56 +1000 | [diff] [blame] | 59 | STATIC void xfs_icsb_disable_counter(xfs_mount_t *, xfs_sb_field_t); |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 60 | |
| 61 | #else |
| 62 | |
Christoph Hellwig | 45af6c6 | 2008-04-22 17:34:44 +1000 | [diff] [blame] | 63 | #define xfs_icsb_balance_counter(mp, a, b) do { } while (0) |
| 64 | #define xfs_icsb_balance_counter_locked(mp, a, b) do { } while (0) |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 65 | #define xfs_icsb_modify_counters(mp, a, b, c) do { } while (0) |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 66 | |
| 67 | #endif |
| 68 | |
Christoph Hellwig | 1df84c9 | 2006-01-11 15:29:52 +1100 | [diff] [blame] | 69 | static const struct { |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 70 | short offset; |
| 71 | short type; /* 0 = integer |
| 72 | * 1 = binary / string (no translation) |
| 73 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } xfs_sb_info[] = { |
| 75 | { offsetof(xfs_sb_t, sb_magicnum), 0 }, |
| 76 | { offsetof(xfs_sb_t, sb_blocksize), 0 }, |
| 77 | { offsetof(xfs_sb_t, sb_dblocks), 0 }, |
| 78 | { offsetof(xfs_sb_t, sb_rblocks), 0 }, |
| 79 | { offsetof(xfs_sb_t, sb_rextents), 0 }, |
| 80 | { offsetof(xfs_sb_t, sb_uuid), 1 }, |
| 81 | { offsetof(xfs_sb_t, sb_logstart), 0 }, |
| 82 | { offsetof(xfs_sb_t, sb_rootino), 0 }, |
| 83 | { offsetof(xfs_sb_t, sb_rbmino), 0 }, |
| 84 | { offsetof(xfs_sb_t, sb_rsumino), 0 }, |
| 85 | { offsetof(xfs_sb_t, sb_rextsize), 0 }, |
| 86 | { offsetof(xfs_sb_t, sb_agblocks), 0 }, |
| 87 | { offsetof(xfs_sb_t, sb_agcount), 0 }, |
| 88 | { offsetof(xfs_sb_t, sb_rbmblocks), 0 }, |
| 89 | { offsetof(xfs_sb_t, sb_logblocks), 0 }, |
| 90 | { offsetof(xfs_sb_t, sb_versionnum), 0 }, |
| 91 | { offsetof(xfs_sb_t, sb_sectsize), 0 }, |
| 92 | { offsetof(xfs_sb_t, sb_inodesize), 0 }, |
| 93 | { offsetof(xfs_sb_t, sb_inopblock), 0 }, |
| 94 | { offsetof(xfs_sb_t, sb_fname[0]), 1 }, |
| 95 | { offsetof(xfs_sb_t, sb_blocklog), 0 }, |
| 96 | { offsetof(xfs_sb_t, sb_sectlog), 0 }, |
| 97 | { offsetof(xfs_sb_t, sb_inodelog), 0 }, |
| 98 | { offsetof(xfs_sb_t, sb_inopblog), 0 }, |
| 99 | { offsetof(xfs_sb_t, sb_agblklog), 0 }, |
| 100 | { offsetof(xfs_sb_t, sb_rextslog), 0 }, |
| 101 | { offsetof(xfs_sb_t, sb_inprogress), 0 }, |
| 102 | { offsetof(xfs_sb_t, sb_imax_pct), 0 }, |
| 103 | { offsetof(xfs_sb_t, sb_icount), 0 }, |
| 104 | { offsetof(xfs_sb_t, sb_ifree), 0 }, |
| 105 | { offsetof(xfs_sb_t, sb_fdblocks), 0 }, |
| 106 | { offsetof(xfs_sb_t, sb_frextents), 0 }, |
| 107 | { offsetof(xfs_sb_t, sb_uquotino), 0 }, |
| 108 | { offsetof(xfs_sb_t, sb_gquotino), 0 }, |
| 109 | { offsetof(xfs_sb_t, sb_qflags), 0 }, |
| 110 | { offsetof(xfs_sb_t, sb_flags), 0 }, |
| 111 | { offsetof(xfs_sb_t, sb_shared_vn), 0 }, |
| 112 | { offsetof(xfs_sb_t, sb_inoalignmt), 0 }, |
| 113 | { offsetof(xfs_sb_t, sb_unit), 0 }, |
| 114 | { offsetof(xfs_sb_t, sb_width), 0 }, |
| 115 | { offsetof(xfs_sb_t, sb_dirblklog), 0 }, |
| 116 | { offsetof(xfs_sb_t, sb_logsectlog), 0 }, |
| 117 | { offsetof(xfs_sb_t, sb_logsectsize),0 }, |
| 118 | { offsetof(xfs_sb_t, sb_logsunit), 0 }, |
| 119 | { offsetof(xfs_sb_t, sb_features2), 0 }, |
David Chinner | ee1c090 | 2008-03-06 13:45:50 +1100 | [diff] [blame] | 120 | { offsetof(xfs_sb_t, sb_bad_features2), 0 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | { sizeof(xfs_sb_t), 0 } |
| 122 | }; |
| 123 | |
| 124 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | * Free up the resources associated with a mount structure. Assume that |
| 126 | * the structure was initially zeroed, so we can tell which fields got |
| 127 | * initialized. |
| 128 | */ |
Christoph Hellwig | c962fb7 | 2008-05-20 15:10:52 +1000 | [diff] [blame] | 129 | STATIC void |
Christoph Hellwig | ff4f038 | 2008-08-13 16:50:47 +1000 | [diff] [blame] | 130 | xfs_free_perag( |
Christoph Hellwig | 745f691 | 2007-08-30 17:20:39 +1000 | [diff] [blame] | 131 | xfs_mount_t *mp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | if (mp->m_perag) { |
| 134 | int agno; |
| 135 | |
| 136 | for (agno = 0; agno < mp->m_maxagi; agno++) |
| 137 | if (mp->m_perag[agno].pagb_list) |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 138 | kmem_free(mp->m_perag[agno].pagb_list); |
| 139 | kmem_free(mp->m_perag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Nathan Scott | 4cc929e | 2007-05-14 18:24:02 +1000 | [diff] [blame] | 143 | /* |
| 144 | * Check size of device based on the (data/realtime) block count. |
| 145 | * Note: this check is used by the growfs code as well as mount. |
| 146 | */ |
| 147 | int |
| 148 | xfs_sb_validate_fsb_count( |
| 149 | xfs_sb_t *sbp, |
| 150 | __uint64_t nblocks) |
| 151 | { |
| 152 | ASSERT(PAGE_SHIFT >= sbp->sb_blocklog); |
| 153 | ASSERT(sbp->sb_blocklog >= BBSHIFT); |
| 154 | |
| 155 | #if XFS_BIG_BLKNOS /* Limited by ULONG_MAX of page cache index */ |
| 156 | if (nblocks >> (PAGE_CACHE_SHIFT - sbp->sb_blocklog) > ULONG_MAX) |
| 157 | return E2BIG; |
| 158 | #else /* Limited by UINT_MAX of sectors */ |
| 159 | if (nblocks << (sbp->sb_blocklog - BBSHIFT) > UINT_MAX) |
| 160 | return E2BIG; |
| 161 | #endif |
| 162 | return 0; |
| 163 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
| 165 | /* |
| 166 | * Check the validity of the SB found. |
| 167 | */ |
| 168 | STATIC int |
| 169 | xfs_mount_validate_sb( |
| 170 | xfs_mount_t *mp, |
Nathan Scott | 764d1f8 | 2006-03-31 13:04:17 +1000 | [diff] [blame] | 171 | xfs_sb_t *sbp, |
| 172 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | { |
| 174 | /* |
| 175 | * If the log device and data device have the |
| 176 | * same device number, the log is internal. |
| 177 | * Consequently, the sb_logstart should be non-zero. If |
| 178 | * we have a zero sb_logstart in this case, we may be trying to mount |
| 179 | * a volume filesystem in a non-volume manner. |
| 180 | */ |
| 181 | if (sbp->sb_magicnum != XFS_SB_MAGIC) { |
Nathan Scott | 764d1f8 | 2006-03-31 13:04:17 +1000 | [diff] [blame] | 182 | xfs_fs_mount_cmn_err(flags, "bad magic number"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | return XFS_ERROR(EWRONGFS); |
| 184 | } |
| 185 | |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 186 | if (!xfs_sb_good_version(sbp)) { |
Nathan Scott | 764d1f8 | 2006-03-31 13:04:17 +1000 | [diff] [blame] | 187 | xfs_fs_mount_cmn_err(flags, "bad version"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | return XFS_ERROR(EWRONGFS); |
| 189 | } |
| 190 | |
| 191 | if (unlikely( |
| 192 | sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) { |
Nathan Scott | 764d1f8 | 2006-03-31 13:04:17 +1000 | [diff] [blame] | 193 | xfs_fs_mount_cmn_err(flags, |
| 194 | "filesystem is marked as having an external log; " |
| 195 | "specify logdev on the\nmount command line."); |
| 196 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | if (unlikely( |
| 200 | sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) { |
Nathan Scott | 764d1f8 | 2006-03-31 13:04:17 +1000 | [diff] [blame] | 201 | xfs_fs_mount_cmn_err(flags, |
| 202 | "filesystem is marked as having an internal log; " |
| 203 | "do not specify logdev on\nthe mount command line."); |
| 204 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /* |
| 208 | * More sanity checking. These were stolen directly from |
| 209 | * xfs_repair. |
| 210 | */ |
| 211 | if (unlikely( |
| 212 | sbp->sb_agcount <= 0 || |
| 213 | sbp->sb_sectsize < XFS_MIN_SECTORSIZE || |
| 214 | sbp->sb_sectsize > XFS_MAX_SECTORSIZE || |
| 215 | sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG || |
| 216 | sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG || |
| 217 | sbp->sb_blocksize < XFS_MIN_BLOCKSIZE || |
| 218 | sbp->sb_blocksize > XFS_MAX_BLOCKSIZE || |
| 219 | sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG || |
| 220 | sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG || |
| 221 | sbp->sb_inodesize < XFS_DINODE_MIN_SIZE || |
| 222 | sbp->sb_inodesize > XFS_DINODE_MAX_SIZE || |
Nathan Scott | 9f989c9 | 2006-03-14 13:29:32 +1100 | [diff] [blame] | 223 | sbp->sb_inodelog < XFS_DINODE_MIN_LOG || |
| 224 | sbp->sb_inodelog > XFS_DINODE_MAX_LOG || |
| 225 | (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) || |
| 227 | (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) || |
Nathan Scott | e50bd16 | 2006-04-11 15:10:45 +1000 | [diff] [blame] | 228 | (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */))) { |
Nathan Scott | 764d1f8 | 2006-03-31 13:04:17 +1000 | [diff] [blame] | 229 | xfs_fs_mount_cmn_err(flags, "SB sanity check 1 failed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | return XFS_ERROR(EFSCORRUPTED); |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * Sanity check AG count, size fields against data size field |
| 235 | */ |
| 236 | if (unlikely( |
| 237 | sbp->sb_dblocks == 0 || |
| 238 | sbp->sb_dblocks > |
| 239 | (xfs_drfsbno_t)sbp->sb_agcount * sbp->sb_agblocks || |
| 240 | sbp->sb_dblocks < (xfs_drfsbno_t)(sbp->sb_agcount - 1) * |
| 241 | sbp->sb_agblocks + XFS_MIN_AG_BLOCKS)) { |
Nathan Scott | 764d1f8 | 2006-03-31 13:04:17 +1000 | [diff] [blame] | 242 | xfs_fs_mount_cmn_err(flags, "SB sanity check 2 failed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | return XFS_ERROR(EFSCORRUPTED); |
| 244 | } |
| 245 | |
Lachlan McIlroy | 2edbddd | 2008-06-27 13:34:34 +1000 | [diff] [blame] | 246 | /* |
| 247 | * Until this is fixed only page-sized or smaller data blocks work. |
| 248 | */ |
| 249 | if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) { |
| 250 | xfs_fs_mount_cmn_err(flags, |
| 251 | "file system with blocksize %d bytes", |
| 252 | sbp->sb_blocksize); |
| 253 | xfs_fs_mount_cmn_err(flags, |
| 254 | "only pagesize (%ld) or less will currently work.", |
| 255 | PAGE_SIZE); |
| 256 | return XFS_ERROR(ENOSYS); |
| 257 | } |
| 258 | |
Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame^] | 259 | /* |
| 260 | * Currently only very few inode sizes are supported. |
| 261 | */ |
| 262 | switch (sbp->sb_inodesize) { |
| 263 | case 256: |
| 264 | case 512: |
| 265 | case 1024: |
| 266 | case 2048: |
| 267 | break; |
| 268 | default: |
| 269 | xfs_fs_mount_cmn_err(flags, |
| 270 | "inode size of %d bytes not supported", |
| 271 | sbp->sb_inodesize); |
| 272 | return XFS_ERROR(ENOSYS); |
| 273 | } |
| 274 | |
Nathan Scott | 4cc929e | 2007-05-14 18:24:02 +1000 | [diff] [blame] | 275 | if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) || |
| 276 | xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) { |
Nathan Scott | 764d1f8 | 2006-03-31 13:04:17 +1000 | [diff] [blame] | 277 | xfs_fs_mount_cmn_err(flags, |
| 278 | "file system too large to be mounted on this system."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | return XFS_ERROR(E2BIG); |
| 280 | } |
| 281 | |
| 282 | if (unlikely(sbp->sb_inprogress)) { |
Nathan Scott | 764d1f8 | 2006-03-31 13:04:17 +1000 | [diff] [blame] | 283 | xfs_fs_mount_cmn_err(flags, "file system busy"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | return XFS_ERROR(EFSCORRUPTED); |
| 285 | } |
| 286 | |
| 287 | /* |
Nathan Scott | de20614 | 2005-05-05 13:24:13 -0700 | [diff] [blame] | 288 | * Version 1 directory format has never worked on Linux. |
| 289 | */ |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 290 | if (unlikely(!xfs_sb_version_hasdirv2(sbp))) { |
Nathan Scott | 764d1f8 | 2006-03-31 13:04:17 +1000 | [diff] [blame] | 291 | xfs_fs_mount_cmn_err(flags, |
| 292 | "file system using version 1 directory format"); |
Nathan Scott | de20614 | 2005-05-05 13:24:13 -0700 | [diff] [blame] | 293 | return XFS_ERROR(ENOSYS); |
| 294 | } |
| 295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | return 0; |
| 297 | } |
| 298 | |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 299 | STATIC void |
| 300 | xfs_initialize_perag_icache( |
| 301 | xfs_perag_t *pag) |
| 302 | { |
| 303 | if (!pag->pag_ici_init) { |
| 304 | rwlock_init(&pag->pag_ici_lock); |
| 305 | INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC); |
| 306 | pag->pag_ici_init = 1; |
| 307 | } |
| 308 | } |
| 309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | xfs_agnumber_t |
Nathan Scott | c11e2c3 | 2005-11-02 15:11:45 +1100 | [diff] [blame] | 311 | xfs_initialize_perag( |
Nathan Scott | c11e2c3 | 2005-11-02 15:11:45 +1100 | [diff] [blame] | 312 | xfs_mount_t *mp, |
| 313 | xfs_agnumber_t agcount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | { |
| 315 | xfs_agnumber_t index, max_metadata; |
| 316 | xfs_perag_t *pag; |
| 317 | xfs_agino_t agino; |
| 318 | xfs_ino_t ino; |
| 319 | xfs_sb_t *sbp = &mp->m_sb; |
| 320 | xfs_ino_t max_inum = XFS_MAXINUMBER_32; |
| 321 | |
| 322 | /* Check to see if the filesystem can overflow 32 bit inodes */ |
| 323 | agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0); |
| 324 | ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino); |
| 325 | |
| 326 | /* Clear the mount flag if no inode can overflow 32 bits |
| 327 | * on this filesystem, or if specifically requested.. |
| 328 | */ |
Christoph Hellwig | bd186aa | 2007-08-30 17:21:12 +1000 | [diff] [blame] | 329 | if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) && ino > max_inum) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | mp->m_flags |= XFS_MOUNT_32BITINODES; |
| 331 | } else { |
| 332 | mp->m_flags &= ~XFS_MOUNT_32BITINODES; |
| 333 | } |
| 334 | |
| 335 | /* If we can overflow then setup the ag headers accordingly */ |
| 336 | if (mp->m_flags & XFS_MOUNT_32BITINODES) { |
| 337 | /* Calculate how much should be reserved for inodes to |
| 338 | * meet the max inode percentage. |
| 339 | */ |
| 340 | if (mp->m_maxicount) { |
| 341 | __uint64_t icount; |
| 342 | |
| 343 | icount = sbp->sb_dblocks * sbp->sb_imax_pct; |
| 344 | do_div(icount, 100); |
| 345 | icount += sbp->sb_agblocks - 1; |
Eric Sandeen | a749ee8 | 2005-11-02 15:13:42 +1100 | [diff] [blame] | 346 | do_div(icount, sbp->sb_agblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | max_metadata = icount; |
| 348 | } else { |
| 349 | max_metadata = agcount; |
| 350 | } |
| 351 | for (index = 0; index < agcount; index++) { |
| 352 | ino = XFS_AGINO_TO_INO(mp, index, agino); |
| 353 | if (ino > max_inum) { |
| 354 | index++; |
| 355 | break; |
| 356 | } |
| 357 | |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 358 | /* This ag is preferred for inodes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | pag = &mp->m_perag[index]; |
| 360 | pag->pagi_inodeok = 1; |
| 361 | if (index < max_metadata) |
| 362 | pag->pagf_metadata = 1; |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 363 | xfs_initialize_perag_icache(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | } |
| 365 | } else { |
| 366 | /* Setup default behavior for smaller filesystems */ |
| 367 | for (index = 0; index < agcount; index++) { |
| 368 | pag = &mp->m_perag[index]; |
| 369 | pag->pagi_inodeok = 1; |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 370 | xfs_initialize_perag_icache(pag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | return index; |
| 374 | } |
| 375 | |
Christoph Hellwig | 2bdf7cd | 2007-08-28 13:58:06 +1000 | [diff] [blame] | 376 | void |
| 377 | xfs_sb_from_disk( |
| 378 | xfs_sb_t *to, |
| 379 | xfs_dsb_t *from) |
| 380 | { |
| 381 | to->sb_magicnum = be32_to_cpu(from->sb_magicnum); |
| 382 | to->sb_blocksize = be32_to_cpu(from->sb_blocksize); |
| 383 | to->sb_dblocks = be64_to_cpu(from->sb_dblocks); |
| 384 | to->sb_rblocks = be64_to_cpu(from->sb_rblocks); |
| 385 | to->sb_rextents = be64_to_cpu(from->sb_rextents); |
| 386 | memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid)); |
| 387 | to->sb_logstart = be64_to_cpu(from->sb_logstart); |
| 388 | to->sb_rootino = be64_to_cpu(from->sb_rootino); |
| 389 | to->sb_rbmino = be64_to_cpu(from->sb_rbmino); |
| 390 | to->sb_rsumino = be64_to_cpu(from->sb_rsumino); |
| 391 | to->sb_rextsize = be32_to_cpu(from->sb_rextsize); |
| 392 | to->sb_agblocks = be32_to_cpu(from->sb_agblocks); |
| 393 | to->sb_agcount = be32_to_cpu(from->sb_agcount); |
| 394 | to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks); |
| 395 | to->sb_logblocks = be32_to_cpu(from->sb_logblocks); |
| 396 | to->sb_versionnum = be16_to_cpu(from->sb_versionnum); |
| 397 | to->sb_sectsize = be16_to_cpu(from->sb_sectsize); |
| 398 | to->sb_inodesize = be16_to_cpu(from->sb_inodesize); |
| 399 | to->sb_inopblock = be16_to_cpu(from->sb_inopblock); |
| 400 | memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname)); |
| 401 | to->sb_blocklog = from->sb_blocklog; |
| 402 | to->sb_sectlog = from->sb_sectlog; |
| 403 | to->sb_inodelog = from->sb_inodelog; |
| 404 | to->sb_inopblog = from->sb_inopblog; |
| 405 | to->sb_agblklog = from->sb_agblklog; |
| 406 | to->sb_rextslog = from->sb_rextslog; |
| 407 | to->sb_inprogress = from->sb_inprogress; |
| 408 | to->sb_imax_pct = from->sb_imax_pct; |
| 409 | to->sb_icount = be64_to_cpu(from->sb_icount); |
| 410 | to->sb_ifree = be64_to_cpu(from->sb_ifree); |
| 411 | to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks); |
| 412 | to->sb_frextents = be64_to_cpu(from->sb_frextents); |
| 413 | to->sb_uquotino = be64_to_cpu(from->sb_uquotino); |
| 414 | to->sb_gquotino = be64_to_cpu(from->sb_gquotino); |
| 415 | to->sb_qflags = be16_to_cpu(from->sb_qflags); |
| 416 | to->sb_flags = from->sb_flags; |
| 417 | to->sb_shared_vn = from->sb_shared_vn; |
| 418 | to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt); |
| 419 | to->sb_unit = be32_to_cpu(from->sb_unit); |
| 420 | to->sb_width = be32_to_cpu(from->sb_width); |
| 421 | to->sb_dirblklog = from->sb_dirblklog; |
| 422 | to->sb_logsectlog = from->sb_logsectlog; |
| 423 | to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize); |
| 424 | to->sb_logsunit = be32_to_cpu(from->sb_logsunit); |
| 425 | to->sb_features2 = be32_to_cpu(from->sb_features2); |
David Chinner | ee1c090 | 2008-03-06 13:45:50 +1100 | [diff] [blame] | 426 | to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2); |
Christoph Hellwig | 2bdf7cd | 2007-08-28 13:58:06 +1000 | [diff] [blame] | 427 | } |
| 428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | /* |
Christoph Hellwig | 2bdf7cd | 2007-08-28 13:58:06 +1000 | [diff] [blame] | 430 | * Copy in core superblock to ondisk one. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | * |
Christoph Hellwig | 2bdf7cd | 2007-08-28 13:58:06 +1000 | [diff] [blame] | 432 | * The fields argument is mask of superblock fields to copy. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | */ |
| 434 | void |
Christoph Hellwig | 2bdf7cd | 2007-08-28 13:58:06 +1000 | [diff] [blame] | 435 | xfs_sb_to_disk( |
| 436 | xfs_dsb_t *to, |
| 437 | xfs_sb_t *from, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | __int64_t fields) |
| 439 | { |
Christoph Hellwig | 2bdf7cd | 2007-08-28 13:58:06 +1000 | [diff] [blame] | 440 | xfs_caddr_t to_ptr = (xfs_caddr_t)to; |
| 441 | xfs_caddr_t from_ptr = (xfs_caddr_t)from; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | xfs_sb_field_t f; |
| 443 | int first; |
| 444 | int size; |
| 445 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | ASSERT(fields); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | if (!fields) |
| 448 | return; |
| 449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | while (fields) { |
| 451 | f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields); |
| 452 | first = xfs_sb_info[f].offset; |
| 453 | size = xfs_sb_info[f + 1].offset - first; |
| 454 | |
| 455 | ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1); |
| 456 | |
| 457 | if (size == 1 || xfs_sb_info[f].type == 1) { |
Christoph Hellwig | 2bdf7cd | 2007-08-28 13:58:06 +1000 | [diff] [blame] | 458 | memcpy(to_ptr + first, from_ptr + first, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | } else { |
| 460 | switch (size) { |
| 461 | case 2: |
Christoph Hellwig | 2bdf7cd | 2007-08-28 13:58:06 +1000 | [diff] [blame] | 462 | *(__be16 *)(to_ptr + first) = |
| 463 | cpu_to_be16(*(__u16 *)(from_ptr + first)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | break; |
| 465 | case 4: |
Christoph Hellwig | 2bdf7cd | 2007-08-28 13:58:06 +1000 | [diff] [blame] | 466 | *(__be32 *)(to_ptr + first) = |
| 467 | cpu_to_be32(*(__u32 *)(from_ptr + first)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | break; |
| 469 | case 8: |
Christoph Hellwig | 2bdf7cd | 2007-08-28 13:58:06 +1000 | [diff] [blame] | 470 | *(__be64 *)(to_ptr + first) = |
| 471 | cpu_to_be64(*(__u64 *)(from_ptr + first)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | break; |
| 473 | default: |
| 474 | ASSERT(0); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | fields &= ~(1LL << f); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | /* |
| 483 | * xfs_readsb |
| 484 | * |
| 485 | * Does the initial read of the superblock. |
| 486 | */ |
| 487 | int |
Nathan Scott | 764d1f8 | 2006-03-31 13:04:17 +1000 | [diff] [blame] | 488 | xfs_readsb(xfs_mount_t *mp, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | { |
| 490 | unsigned int sector_size; |
| 491 | unsigned int extra_flags; |
| 492 | xfs_buf_t *bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | int error; |
| 494 | |
| 495 | ASSERT(mp->m_sb_bp == NULL); |
| 496 | ASSERT(mp->m_ddev_targp != NULL); |
| 497 | |
| 498 | /* |
| 499 | * Allocate a (locked) buffer to hold the superblock. |
| 500 | * This will be kept around at all times to optimize |
| 501 | * access to the superblock. |
| 502 | */ |
| 503 | sector_size = xfs_getsize_buftarg(mp->m_ddev_targp); |
| 504 | extra_flags = XFS_BUF_LOCK | XFS_BUF_MANAGE | XFS_BUF_MAPPED; |
| 505 | |
| 506 | bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR, |
| 507 | BTOBB(sector_size), extra_flags); |
| 508 | if (!bp || XFS_BUF_ISERROR(bp)) { |
Nathan Scott | 764d1f8 | 2006-03-31 13:04:17 +1000 | [diff] [blame] | 509 | xfs_fs_mount_cmn_err(flags, "SB read failed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM; |
| 511 | goto fail; |
| 512 | } |
| 513 | ASSERT(XFS_BUF_ISBUSY(bp)); |
| 514 | ASSERT(XFS_BUF_VALUSEMA(bp) <= 0); |
| 515 | |
| 516 | /* |
| 517 | * Initialize the mount structure from the superblock. |
| 518 | * But first do some basic consistency checking. |
| 519 | */ |
Christoph Hellwig | 2bdf7cd | 2007-08-28 13:58:06 +1000 | [diff] [blame] | 520 | xfs_sb_from_disk(&mp->m_sb, XFS_BUF_TO_SBP(bp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | |
Nathan Scott | 764d1f8 | 2006-03-31 13:04:17 +1000 | [diff] [blame] | 522 | error = xfs_mount_validate_sb(mp, &(mp->m_sb), flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | if (error) { |
Nathan Scott | 764d1f8 | 2006-03-31 13:04:17 +1000 | [diff] [blame] | 524 | xfs_fs_mount_cmn_err(flags, "SB validate failed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | goto fail; |
| 526 | } |
| 527 | |
| 528 | /* |
| 529 | * We must be able to do sector-sized and sector-aligned IO. |
| 530 | */ |
| 531 | if (sector_size > mp->m_sb.sb_sectsize) { |
Nathan Scott | 764d1f8 | 2006-03-31 13:04:17 +1000 | [diff] [blame] | 532 | xfs_fs_mount_cmn_err(flags, |
| 533 | "device supports only %u byte sectors (not %u)", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | sector_size, mp->m_sb.sb_sectsize); |
| 535 | error = ENOSYS; |
| 536 | goto fail; |
| 537 | } |
| 538 | |
| 539 | /* |
| 540 | * If device sector size is smaller than the superblock size, |
| 541 | * re-read the superblock so the buffer is correctly sized. |
| 542 | */ |
| 543 | if (sector_size < mp->m_sb.sb_sectsize) { |
| 544 | XFS_BUF_UNMANAGE(bp); |
| 545 | xfs_buf_relse(bp); |
| 546 | sector_size = mp->m_sb.sb_sectsize; |
| 547 | bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR, |
| 548 | BTOBB(sector_size), extra_flags); |
| 549 | if (!bp || XFS_BUF_ISERROR(bp)) { |
Nathan Scott | 764d1f8 | 2006-03-31 13:04:17 +1000 | [diff] [blame] | 550 | xfs_fs_mount_cmn_err(flags, "SB re-read failed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM; |
| 552 | goto fail; |
| 553 | } |
| 554 | ASSERT(XFS_BUF_ISBUSY(bp)); |
| 555 | ASSERT(XFS_BUF_VALUSEMA(bp) <= 0); |
| 556 | } |
| 557 | |
Lachlan McIlroy | 5478eea | 2007-02-10 18:36:29 +1100 | [diff] [blame] | 558 | /* Initialize per-cpu counters */ |
| 559 | xfs_icsb_reinit_counters(mp); |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 560 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | mp->m_sb_bp = bp; |
| 562 | xfs_buf_relse(bp); |
| 563 | ASSERT(XFS_BUF_VALUSEMA(bp) > 0); |
| 564 | return 0; |
| 565 | |
| 566 | fail: |
| 567 | if (bp) { |
| 568 | XFS_BUF_UNMANAGE(bp); |
| 569 | xfs_buf_relse(bp); |
| 570 | } |
| 571 | return error; |
| 572 | } |
| 573 | |
| 574 | |
| 575 | /* |
| 576 | * xfs_mount_common |
| 577 | * |
| 578 | * Mount initialization code establishing various mount |
| 579 | * fields from the superblock associated with the given |
| 580 | * mount structure |
| 581 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 582 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | xfs_mount_common(xfs_mount_t *mp, xfs_sb_t *sbp) |
| 584 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | mp->m_agfrotor = mp->m_agirotor = 0; |
Eric Sandeen | 007c61c | 2007-10-11 17:43:56 +1000 | [diff] [blame] | 586 | spin_lock_init(&mp->m_agirotor_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | mp->m_maxagi = mp->m_sb.sb_agcount; |
| 588 | mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG; |
| 589 | mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT; |
| 590 | mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT; |
| 591 | mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1; |
| 592 | mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | mp->m_blockmask = sbp->sb_blocksize - 1; |
| 594 | mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG; |
| 595 | mp->m_blockwmask = mp->m_blockwsize - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 597 | mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1); |
| 598 | mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0); |
| 599 | mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2; |
| 600 | mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2; |
| 601 | |
| 602 | mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1); |
| 603 | mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0); |
| 604 | mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2; |
| 605 | mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2; |
| 606 | |
| 607 | mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1); |
| 608 | mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0); |
| 609 | mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2; |
| 610 | mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | |
| 612 | mp->m_bsize = XFS_FSB_TO_BB(mp, 1); |
| 613 | mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK, |
| 614 | sbp->sb_inopblock); |
| 615 | mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog; |
| 616 | } |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 617 | |
| 618 | /* |
| 619 | * xfs_initialize_perag_data |
| 620 | * |
| 621 | * Read in each per-ag structure so we can count up the number of |
| 622 | * allocated inodes, free inodes and used filesystem blocks as this |
| 623 | * information is no longer persistent in the superblock. Once we have |
| 624 | * this information, write it into the in-core superblock structure. |
| 625 | */ |
| 626 | STATIC int |
| 627 | xfs_initialize_perag_data(xfs_mount_t *mp, xfs_agnumber_t agcount) |
| 628 | { |
| 629 | xfs_agnumber_t index; |
| 630 | xfs_perag_t *pag; |
| 631 | xfs_sb_t *sbp = &mp->m_sb; |
| 632 | uint64_t ifree = 0; |
| 633 | uint64_t ialloc = 0; |
| 634 | uint64_t bfree = 0; |
| 635 | uint64_t bfreelst = 0; |
| 636 | uint64_t btree = 0; |
| 637 | int error; |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 638 | |
| 639 | for (index = 0; index < agcount; index++) { |
| 640 | /* |
| 641 | * read the agf, then the agi. This gets us |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 642 | * all the information we need and populates the |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 643 | * per-ag structures for us. |
| 644 | */ |
| 645 | error = xfs_alloc_pagf_init(mp, NULL, index, 0); |
| 646 | if (error) |
| 647 | return error; |
| 648 | |
| 649 | error = xfs_ialloc_pagi_init(mp, NULL, index); |
| 650 | if (error) |
| 651 | return error; |
| 652 | pag = &mp->m_perag[index]; |
| 653 | ifree += pag->pagi_freecount; |
| 654 | ialloc += pag->pagi_count; |
| 655 | bfree += pag->pagf_freeblks; |
| 656 | bfreelst += pag->pagf_flcount; |
| 657 | btree += pag->pagf_btreeblks; |
| 658 | } |
| 659 | /* |
| 660 | * Overwrite incore superblock counters with just-read data |
| 661 | */ |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 662 | spin_lock(&mp->m_sb_lock); |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 663 | sbp->sb_ifree = ifree; |
| 664 | sbp->sb_icount = ialloc; |
| 665 | sbp->sb_fdblocks = bfree + bfreelst + btree; |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 666 | spin_unlock(&mp->m_sb_lock); |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 667 | |
| 668 | /* Fixup the per-cpu counters as well. */ |
| 669 | xfs_icsb_reinit_counters(mp); |
| 670 | |
| 671 | return 0; |
| 672 | } |
| 673 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | /* |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 675 | * Update alignment values based on mount options and sb values |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | */ |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 677 | STATIC int |
Christoph Hellwig | 7884bc8 | 2009-01-19 02:04:07 +0100 | [diff] [blame] | 678 | xfs_update_alignment(xfs_mount_t *mp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | xfs_sb_t *sbp = &(mp->m_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | |
Christoph Hellwig | 4249023 | 2008-08-13 16:49:32 +1000 | [diff] [blame] | 682 | if (mp->m_dalign) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | /* |
| 684 | * If stripe unit and stripe width are not multiples |
| 685 | * of the fs blocksize turn off alignment. |
| 686 | */ |
| 687 | if ((BBTOB(mp->m_dalign) & mp->m_blockmask) || |
| 688 | (BBTOB(mp->m_swidth) & mp->m_blockmask)) { |
| 689 | if (mp->m_flags & XFS_MOUNT_RETERR) { |
| 690 | cmn_err(CE_WARN, |
| 691 | "XFS: alignment check 1 failed"); |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 692 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | } |
| 694 | mp->m_dalign = mp->m_swidth = 0; |
| 695 | } else { |
| 696 | /* |
| 697 | * Convert the stripe unit and width to FSBs. |
| 698 | */ |
| 699 | mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign); |
| 700 | if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) { |
| 701 | if (mp->m_flags & XFS_MOUNT_RETERR) { |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 702 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | } |
| 704 | xfs_fs_cmn_err(CE_WARN, mp, |
| 705 | "stripe alignment turned off: sunit(%d)/swidth(%d) incompatible with agsize(%d)", |
| 706 | mp->m_dalign, mp->m_swidth, |
| 707 | sbp->sb_agblocks); |
| 708 | |
| 709 | mp->m_dalign = 0; |
| 710 | mp->m_swidth = 0; |
| 711 | } else if (mp->m_dalign) { |
| 712 | mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth); |
| 713 | } else { |
| 714 | if (mp->m_flags & XFS_MOUNT_RETERR) { |
| 715 | xfs_fs_cmn_err(CE_WARN, mp, |
| 716 | "stripe alignment turned off: sunit(%d) less than bsize(%d)", |
| 717 | mp->m_dalign, |
| 718 | mp->m_blockmask +1); |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 719 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | } |
| 721 | mp->m_swidth = 0; |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | /* |
| 726 | * Update superblock with new values |
| 727 | * and log changes |
| 728 | */ |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 729 | if (xfs_sb_version_hasdalign(sbp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | if (sbp->sb_unit != mp->m_dalign) { |
| 731 | sbp->sb_unit = mp->m_dalign; |
Christoph Hellwig | 7884bc8 | 2009-01-19 02:04:07 +0100 | [diff] [blame] | 732 | mp->m_update_flags |= XFS_SB_UNIT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | } |
| 734 | if (sbp->sb_width != mp->m_swidth) { |
| 735 | sbp->sb_width = mp->m_swidth; |
Christoph Hellwig | 7884bc8 | 2009-01-19 02:04:07 +0100 | [diff] [blame] | 736 | mp->m_update_flags |= XFS_SB_WIDTH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | } |
| 738 | } |
| 739 | } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN && |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 740 | xfs_sb_version_hasdalign(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | mp->m_dalign = sbp->sb_unit; |
| 742 | mp->m_swidth = sbp->sb_width; |
| 743 | } |
| 744 | |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 745 | return 0; |
| 746 | } |
| 747 | |
| 748 | /* |
| 749 | * Set the maximum inode count for this filesystem |
| 750 | */ |
| 751 | STATIC void |
| 752 | xfs_set_maxicount(xfs_mount_t *mp) |
| 753 | { |
| 754 | xfs_sb_t *sbp = &(mp->m_sb); |
| 755 | __uint64_t icount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | |
| 757 | if (sbp->sb_imax_pct) { |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 758 | /* |
| 759 | * Make sure the maximum inode count is a multiple |
| 760 | * of the units we allocate inodes in. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | icount = sbp->sb_dblocks * sbp->sb_imax_pct; |
| 763 | do_div(icount, 100); |
| 764 | do_div(icount, mp->m_ialloc_blks); |
| 765 | mp->m_maxicount = (icount * mp->m_ialloc_blks) << |
| 766 | sbp->sb_inopblog; |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 767 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | mp->m_maxicount = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | } |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 770 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 772 | /* |
| 773 | * Set the default minimum read and write sizes unless |
| 774 | * already specified in a mount option. |
| 775 | * We use smaller I/O sizes when the file system |
| 776 | * is being used for NFS service (wsync mount option). |
| 777 | */ |
| 778 | STATIC void |
| 779 | xfs_set_rw_sizes(xfs_mount_t *mp) |
| 780 | { |
| 781 | xfs_sb_t *sbp = &(mp->m_sb); |
| 782 | int readio_log, writeio_log; |
| 783 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) { |
| 785 | if (mp->m_flags & XFS_MOUNT_WSYNC) { |
| 786 | readio_log = XFS_WSYNC_READIO_LOG; |
| 787 | writeio_log = XFS_WSYNC_WRITEIO_LOG; |
| 788 | } else { |
| 789 | readio_log = XFS_READIO_LOG_LARGE; |
| 790 | writeio_log = XFS_WRITEIO_LOG_LARGE; |
| 791 | } |
| 792 | } else { |
| 793 | readio_log = mp->m_readio_log; |
| 794 | writeio_log = mp->m_writeio_log; |
| 795 | } |
| 796 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | if (sbp->sb_blocklog > readio_log) { |
| 798 | mp->m_readio_log = sbp->sb_blocklog; |
| 799 | } else { |
| 800 | mp->m_readio_log = readio_log; |
| 801 | } |
| 802 | mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog); |
| 803 | if (sbp->sb_blocklog > writeio_log) { |
| 804 | mp->m_writeio_log = sbp->sb_blocklog; |
| 805 | } else { |
| 806 | mp->m_writeio_log = writeio_log; |
| 807 | } |
| 808 | mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog); |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 809 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 811 | /* |
| 812 | * Set whether we're using inode alignment. |
| 813 | */ |
| 814 | STATIC void |
| 815 | xfs_set_inoalignment(xfs_mount_t *mp) |
| 816 | { |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 817 | if (xfs_sb_version_hasalign(&mp->m_sb) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | mp->m_sb.sb_inoalignmt >= |
| 819 | XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size)) |
| 820 | mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1; |
| 821 | else |
| 822 | mp->m_inoalign_mask = 0; |
| 823 | /* |
| 824 | * If we are using stripe alignment, check whether |
| 825 | * the stripe unit is a multiple of the inode alignment |
| 826 | */ |
| 827 | if (mp->m_dalign && mp->m_inoalign_mask && |
| 828 | !(mp->m_dalign & mp->m_inoalign_mask)) |
| 829 | mp->m_sinoalign = mp->m_dalign; |
| 830 | else |
| 831 | mp->m_sinoalign = 0; |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | /* |
| 835 | * Check that the data (and log if separate) are an ok size. |
| 836 | */ |
| 837 | STATIC int |
Christoph Hellwig | 4249023 | 2008-08-13 16:49:32 +1000 | [diff] [blame] | 838 | xfs_check_sizes(xfs_mount_t *mp) |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 839 | { |
| 840 | xfs_buf_t *bp; |
| 841 | xfs_daddr_t d; |
| 842 | int error; |
| 843 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks); |
| 845 | if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) { |
| 846 | cmn_err(CE_WARN, "XFS: size check 1 failed"); |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 847 | return XFS_ERROR(E2BIG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | } |
| 849 | error = xfs_read_buf(mp, mp->m_ddev_targp, |
| 850 | d - XFS_FSS_TO_BB(mp, 1), |
| 851 | XFS_FSS_TO_BB(mp, 1), 0, &bp); |
| 852 | if (!error) { |
| 853 | xfs_buf_relse(bp); |
| 854 | } else { |
| 855 | cmn_err(CE_WARN, "XFS: size check 2 failed"); |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 856 | if (error == ENOSPC) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | error = XFS_ERROR(E2BIG); |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 858 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | } |
| 860 | |
Christoph Hellwig | 4249023 | 2008-08-13 16:49:32 +1000 | [diff] [blame] | 861 | if (mp->m_logdev_targp != mp->m_ddev_targp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks); |
| 863 | if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) { |
| 864 | cmn_err(CE_WARN, "XFS: size check 3 failed"); |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 865 | return XFS_ERROR(E2BIG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | } |
| 867 | error = xfs_read_buf(mp, mp->m_logdev_targp, |
| 868 | d - XFS_FSB_TO_BB(mp, 1), |
| 869 | XFS_FSB_TO_BB(mp, 1), 0, &bp); |
| 870 | if (!error) { |
| 871 | xfs_buf_relse(bp); |
| 872 | } else { |
| 873 | cmn_err(CE_WARN, "XFS: size check 3 failed"); |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 874 | if (error == ENOSPC) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | error = XFS_ERROR(E2BIG); |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 876 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | } |
| 878 | } |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 879 | return 0; |
| 880 | } |
| 881 | |
| 882 | /* |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 883 | * This function does the following on an initial mount of a file system: |
| 884 | * - reads the superblock from disk and init the mount struct |
| 885 | * - if we're a 32-bit kernel, do a size check on the superblock |
| 886 | * so we don't mount terabyte filesystems |
| 887 | * - init mount struct realtime fields |
| 888 | * - allocate inode hash table for fs |
| 889 | * - init directory manager |
| 890 | * - perform recovery and init the log manager |
| 891 | */ |
| 892 | int |
| 893 | xfs_mountfs( |
Christoph Hellwig | 4249023 | 2008-08-13 16:49:32 +1000 | [diff] [blame] | 894 | xfs_mount_t *mp) |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 895 | { |
| 896 | xfs_sb_t *sbp = &(mp->m_sb); |
| 897 | xfs_inode_t *rip; |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 898 | __uint64_t resblks; |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 899 | uint quotamount, quotaflags; |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 900 | int error = 0; |
| 901 | |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 902 | xfs_mount_common(mp, sbp); |
| 903 | |
| 904 | /* |
Eric Sandeen | e6957ea | 2008-04-10 12:19:34 +1000 | [diff] [blame] | 905 | * Check for a mismatched features2 values. Older kernels |
| 906 | * read & wrote into the wrong sb offset for sb_features2 |
| 907 | * on some platforms due to xfs_sb_t not being 64bit size aligned |
| 908 | * when sb_features2 was added, which made older superblock |
| 909 | * reading/writing routines swap it as a 64-bit value. |
David Chinner | ee1c090 | 2008-03-06 13:45:50 +1100 | [diff] [blame] | 910 | * |
Eric Sandeen | e6957ea | 2008-04-10 12:19:34 +1000 | [diff] [blame] | 911 | * For backwards compatibility, we make both slots equal. |
| 912 | * |
| 913 | * If we detect a mismatched field, we OR the set bits into the |
| 914 | * existing features2 field in case it has already been modified; we |
| 915 | * don't want to lose any features. We then update the bad location |
| 916 | * with the ORed value so that older kernels will see any features2 |
| 917 | * flags, and mark the two fields as needing updates once the |
| 918 | * transaction subsystem is online. |
David Chinner | ee1c090 | 2008-03-06 13:45:50 +1100 | [diff] [blame] | 919 | */ |
Eric Sandeen | e6957ea | 2008-04-10 12:19:34 +1000 | [diff] [blame] | 920 | if (xfs_sb_has_mismatched_features2(sbp)) { |
David Chinner | ee1c090 | 2008-03-06 13:45:50 +1100 | [diff] [blame] | 921 | cmn_err(CE_WARN, |
| 922 | "XFS: correcting sb_features alignment problem"); |
| 923 | sbp->sb_features2 |= sbp->sb_bad_features2; |
Eric Sandeen | e6957ea | 2008-04-10 12:19:34 +1000 | [diff] [blame] | 924 | sbp->sb_bad_features2 = sbp->sb_features2; |
Christoph Hellwig | 7884bc8 | 2009-01-19 02:04:07 +0100 | [diff] [blame] | 925 | mp->m_update_flags |= XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2; |
Eric Sandeen | e6957ea | 2008-04-10 12:19:34 +1000 | [diff] [blame] | 926 | |
| 927 | /* |
| 928 | * Re-check for ATTR2 in case it was found in bad_features2 |
| 929 | * slot. |
| 930 | */ |
Tim Shimmin | 7c12f29 | 2008-04-30 18:15:28 +1000 | [diff] [blame] | 931 | if (xfs_sb_version_hasattr2(&mp->m_sb) && |
| 932 | !(mp->m_flags & XFS_MOUNT_NOATTR2)) |
Eric Sandeen | e6957ea | 2008-04-10 12:19:34 +1000 | [diff] [blame] | 933 | mp->m_flags |= XFS_MOUNT_ATTR2; |
Tim Shimmin | 7c12f29 | 2008-04-30 18:15:28 +1000 | [diff] [blame] | 934 | } |
Eric Sandeen | e6957ea | 2008-04-10 12:19:34 +1000 | [diff] [blame] | 935 | |
Tim Shimmin | 7c12f29 | 2008-04-30 18:15:28 +1000 | [diff] [blame] | 936 | if (xfs_sb_version_hasattr2(&mp->m_sb) && |
| 937 | (mp->m_flags & XFS_MOUNT_NOATTR2)) { |
| 938 | xfs_sb_version_removeattr2(&mp->m_sb); |
Christoph Hellwig | 7884bc8 | 2009-01-19 02:04:07 +0100 | [diff] [blame] | 939 | mp->m_update_flags |= XFS_SB_FEATURES2; |
Tim Shimmin | 7c12f29 | 2008-04-30 18:15:28 +1000 | [diff] [blame] | 940 | |
| 941 | /* update sb_versionnum for the clearing of the morebits */ |
| 942 | if (!sbp->sb_features2) |
Christoph Hellwig | 7884bc8 | 2009-01-19 02:04:07 +0100 | [diff] [blame] | 943 | mp->m_update_flags |= XFS_SB_VERSIONNUM; |
David Chinner | ee1c090 | 2008-03-06 13:45:50 +1100 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | /* |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 947 | * Check if sb_agblocks is aligned at stripe boundary |
| 948 | * If sb_agblocks is NOT aligned turn off m_dalign since |
| 949 | * allocator alignment is within an ag, therefore ag has |
| 950 | * to be aligned at stripe boundary. |
| 951 | */ |
Christoph Hellwig | 7884bc8 | 2009-01-19 02:04:07 +0100 | [diff] [blame] | 952 | error = xfs_update_alignment(mp); |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 953 | if (error) |
Christoph Hellwig | f9057e3 | 2009-02-04 09:31:52 +0100 | [diff] [blame] | 954 | goto out; |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 955 | |
| 956 | xfs_alloc_compute_maxlevels(mp); |
| 957 | xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK); |
| 958 | xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK); |
| 959 | xfs_ialloc_compute_maxlevels(mp); |
| 960 | |
| 961 | xfs_set_maxicount(mp); |
| 962 | |
| 963 | mp->m_maxioffset = xfs_max_file_offset(sbp->sb_blocklog); |
| 964 | |
| 965 | /* |
| 966 | * XFS uses the uuid from the superblock as the unique |
| 967 | * identifier for fsid. We can not use the uuid from the volume |
| 968 | * since a single partition filesystem is identical to a single |
| 969 | * partition volume/filesystem. |
| 970 | */ |
Christoph Hellwig | f9057e3 | 2009-02-04 09:31:52 +0100 | [diff] [blame] | 971 | if (!(mp->m_flags & XFS_MOUNT_NOUUID)) { |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 972 | if (xfs_uuid_mount(mp)) { |
| 973 | error = XFS_ERROR(EINVAL); |
Christoph Hellwig | f9057e3 | 2009-02-04 09:31:52 +0100 | [diff] [blame] | 974 | goto out; |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 975 | } |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | /* |
| 979 | * Set the minimum read and write sizes |
| 980 | */ |
| 981 | xfs_set_rw_sizes(mp); |
| 982 | |
| 983 | /* |
| 984 | * Set the inode cluster size. |
| 985 | * This may still be overridden by the file system |
| 986 | * block size if it is larger than the chosen cluster size. |
| 987 | */ |
| 988 | mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE; |
| 989 | |
| 990 | /* |
| 991 | * Set inode alignment fields |
| 992 | */ |
| 993 | xfs_set_inoalignment(mp); |
| 994 | |
| 995 | /* |
| 996 | * Check that the data (and log if separate) are an ok size. |
| 997 | */ |
Christoph Hellwig | 4249023 | 2008-08-13 16:49:32 +1000 | [diff] [blame] | 998 | error = xfs_check_sizes(mp); |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 999 | if (error) |
Christoph Hellwig | f9057e3 | 2009-02-04 09:31:52 +0100 | [diff] [blame] | 1000 | goto out_remove_uuid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | |
| 1002 | /* |
| 1003 | * Initialize realtime fields in the mount structure |
| 1004 | */ |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 1005 | error = xfs_rtmount_init(mp); |
| 1006 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | cmn_err(CE_WARN, "XFS: RT mount failed"); |
Christoph Hellwig | f9057e3 | 2009-02-04 09:31:52 +0100 | [diff] [blame] | 1008 | goto out_remove_uuid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | * Copies the low order bits of the timestamp and the randomly |
| 1013 | * set "sequence" number out of a UUID. |
| 1014 | */ |
| 1015 | uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid); |
| 1016 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | mp->m_dmevmask = 0; /* not persistent; set after each mount */ |
| 1018 | |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 1019 | xfs_dir_mount(mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | |
| 1021 | /* |
| 1022 | * Initialize the attribute manager's entries. |
| 1023 | */ |
| 1024 | mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100; |
| 1025 | |
| 1026 | /* |
| 1027 | * Initialize the precomputed transaction reservations values. |
| 1028 | */ |
| 1029 | xfs_trans_init(mp); |
| 1030 | |
| 1031 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | * Allocate and initialize the per-ag data. |
| 1033 | */ |
| 1034 | init_rwsem(&mp->m_peraglock); |
Christoph Hellwig | d62c251 | 2008-08-13 16:51:29 +1000 | [diff] [blame] | 1035 | mp->m_perag = kmem_zalloc(sbp->sb_agcount * sizeof(xfs_perag_t), |
| 1036 | KM_MAYFAIL); |
| 1037 | if (!mp->m_perag) |
Christoph Hellwig | f9057e3 | 2009-02-04 09:31:52 +0100 | [diff] [blame] | 1038 | goto out_remove_uuid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | |
Christoph Hellwig | b267ce9 | 2007-08-30 17:21:30 +1000 | [diff] [blame] | 1040 | mp->m_maxagi = xfs_initialize_perag(mp, sbp->sb_agcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | |
Christoph Hellwig | f9057e3 | 2009-02-04 09:31:52 +0100 | [diff] [blame] | 1042 | if (!sbp->sb_logblocks) { |
| 1043 | cmn_err(CE_WARN, "XFS: no log defined"); |
| 1044 | XFS_ERROR_REPORT("xfs_mountfs", XFS_ERRLEVEL_LOW, mp); |
| 1045 | error = XFS_ERROR(EFSCORRUPTED); |
| 1046 | goto out_free_perag; |
| 1047 | } |
| 1048 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | /* |
| 1050 | * log's mount-time initialization. Perform 1st part recovery if needed |
| 1051 | */ |
Christoph Hellwig | f9057e3 | 2009-02-04 09:31:52 +0100 | [diff] [blame] | 1052 | error = xfs_log_mount(mp, mp->m_logdev_targp, |
| 1053 | XFS_FSB_TO_DADDR(mp, sbp->sb_logstart), |
| 1054 | XFS_FSB_TO_BB(mp, sbp->sb_logblocks)); |
| 1055 | if (error) { |
| 1056 | cmn_err(CE_WARN, "XFS: log mount failed"); |
| 1057 | goto out_free_perag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | /* |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 1061 | * Now the log is mounted, we know if it was an unclean shutdown or |
| 1062 | * not. If it was, with the first phase of recovery has completed, we |
| 1063 | * have consistent AG blocks on disk. We have not recovered EFIs yet, |
| 1064 | * but they are recovered transactionally in the second recovery phase |
| 1065 | * later. |
| 1066 | * |
| 1067 | * Hence we can safely re-initialise incore superblock counters from |
| 1068 | * the per-ag data. These may not be correct if the filesystem was not |
| 1069 | * cleanly unmounted, so we need to wait for recovery to finish before |
| 1070 | * doing this. |
| 1071 | * |
| 1072 | * If the filesystem was cleanly unmounted, then we can trust the |
| 1073 | * values in the superblock to be correct and we don't need to do |
| 1074 | * anything here. |
| 1075 | * |
| 1076 | * If we are currently making the filesystem, the initialisation will |
| 1077 | * fail as the perag data is in an undefined state. |
| 1078 | */ |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 1079 | if (xfs_sb_version_haslazysbcount(&mp->m_sb) && |
| 1080 | !XFS_LAST_UNMOUNT_WAS_CLEAN(mp) && |
| 1081 | !mp->m_sb.sb_inprogress) { |
| 1082 | error = xfs_initialize_perag_data(mp, sbp->sb_agcount); |
Christoph Hellwig | f9057e3 | 2009-02-04 09:31:52 +0100 | [diff] [blame] | 1083 | if (error) |
| 1084 | goto out_free_perag; |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 1085 | } |
Christoph Hellwig | f9057e3 | 2009-02-04 09:31:52 +0100 | [diff] [blame] | 1086 | |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 1087 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | * Get and sanity-check the root inode. |
| 1089 | * Save the pointer to it in the mount structure. |
| 1090 | */ |
| 1091 | error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip, 0); |
| 1092 | if (error) { |
| 1093 | cmn_err(CE_WARN, "XFS: failed to read root inode"); |
Christoph Hellwig | f9057e3 | 2009-02-04 09:31:52 +0100 | [diff] [blame] | 1094 | goto out_log_dealloc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | ASSERT(rip != NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | |
| 1099 | if (unlikely((rip->i_d.di_mode & S_IFMT) != S_IFDIR)) { |
| 1100 | cmn_err(CE_WARN, "XFS: corrupted root inode"); |
Nathan Scott | b657452 | 2006-06-09 15:29:40 +1000 | [diff] [blame] | 1101 | cmn_err(CE_WARN, "Device %s - root %llu is not a directory", |
| 1102 | XFS_BUFTARG_NAME(mp->m_ddev_targp), |
| 1103 | (unsigned long long)rip->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | xfs_iunlock(rip, XFS_ILOCK_EXCL); |
| 1105 | XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW, |
| 1106 | mp); |
| 1107 | error = XFS_ERROR(EFSCORRUPTED); |
Christoph Hellwig | f9057e3 | 2009-02-04 09:31:52 +0100 | [diff] [blame] | 1108 | goto out_rele_rip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | } |
| 1110 | mp->m_rootip = rip; /* save it */ |
| 1111 | |
| 1112 | xfs_iunlock(rip, XFS_ILOCK_EXCL); |
| 1113 | |
| 1114 | /* |
| 1115 | * Initialize realtime inode pointers in the mount structure |
| 1116 | */ |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 1117 | error = xfs_rtmount_inodes(mp); |
| 1118 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | /* |
| 1120 | * Free up the root inode. |
| 1121 | */ |
| 1122 | cmn_err(CE_WARN, "XFS: failed to read RT inodes"); |
Christoph Hellwig | f9057e3 | 2009-02-04 09:31:52 +0100 | [diff] [blame] | 1123 | goto out_rele_rip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | /* |
Christoph Hellwig | 7884bc8 | 2009-01-19 02:04:07 +0100 | [diff] [blame] | 1127 | * If this is a read-only mount defer the superblock updates until |
| 1128 | * the next remount into writeable mode. Otherwise we would never |
| 1129 | * perform the update e.g. for the root filesystem. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | */ |
Christoph Hellwig | 7884bc8 | 2009-01-19 02:04:07 +0100 | [diff] [blame] | 1131 | if (mp->m_update_flags && !(mp->m_flags & XFS_MOUNT_RDONLY)) { |
| 1132 | error = xfs_mount_log_sb(mp, mp->m_update_flags); |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1133 | if (error) { |
| 1134 | cmn_err(CE_WARN, "XFS: failed to write sb changes"); |
Christoph Hellwig | b93b6e4 | 2009-02-04 09:33:58 +0100 | [diff] [blame] | 1135 | goto out_rtunmount; |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1136 | } |
| 1137 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | |
| 1139 | /* |
| 1140 | * Initialise the XFS quota management subsystem for this mount |
| 1141 | */ |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 1142 | error = XFS_QM_INIT(mp, "amount, "aflags); |
| 1143 | if (error) |
Christoph Hellwig | b93b6e4 | 2009-02-04 09:33:58 +0100 | [diff] [blame] | 1144 | goto out_rtunmount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | |
| 1146 | /* |
| 1147 | * Finish recovering the file system. This part needed to be |
| 1148 | * delayed until after the root and real-time bitmap inodes |
| 1149 | * were consistently read in. |
| 1150 | */ |
Christoph Hellwig | 4249023 | 2008-08-13 16:49:32 +1000 | [diff] [blame] | 1151 | error = xfs_log_mount_finish(mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | if (error) { |
| 1153 | cmn_err(CE_WARN, "XFS: log mount finish failed"); |
Christoph Hellwig | b93b6e4 | 2009-02-04 09:33:58 +0100 | [diff] [blame] | 1154 | goto out_rtunmount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | /* |
| 1158 | * Complete the quota initialisation, post-log-replay component. |
| 1159 | */ |
Christoph Hellwig | 4249023 | 2008-08-13 16:49:32 +1000 | [diff] [blame] | 1160 | error = XFS_QM_MOUNT(mp, quotamount, quotaflags); |
Eric Sandeen | 0771fb4 | 2007-10-12 11:03:40 +1000 | [diff] [blame] | 1161 | if (error) |
Christoph Hellwig | b93b6e4 | 2009-02-04 09:33:58 +0100 | [diff] [blame] | 1162 | goto out_rtunmount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | |
David Chinner | 84e1e99 | 2007-06-18 16:50:27 +1000 | [diff] [blame] | 1164 | /* |
| 1165 | * Now we are mounted, reserve a small amount of unused space for |
| 1166 | * privileged transactions. This is needed so that transaction |
| 1167 | * space required for critical operations can dip into this pool |
| 1168 | * when at ENOSPC. This is needed for operations like create with |
| 1169 | * attr, unwritten extent conversion at ENOSPC, etc. Data allocations |
| 1170 | * are not allowed to use this reserved space. |
| 1171 | * |
| 1172 | * We default to 5% or 1024 fsbs of space reserved, whichever is smaller. |
| 1173 | * This may drive us straight to ENOSPC on mount, but that implies |
David Chinner | 714082b | 2008-04-10 12:20:03 +1000 | [diff] [blame] | 1174 | * we were already there on the last unmount. Warn if this occurs. |
David Chinner | 84e1e99 | 2007-06-18 16:50:27 +1000 | [diff] [blame] | 1175 | */ |
Christoph Hellwig | 39726be | 2007-06-18 17:57:45 +1000 | [diff] [blame] | 1176 | resblks = mp->m_sb.sb_dblocks; |
| 1177 | do_div(resblks, 20); |
| 1178 | resblks = min_t(__uint64_t, resblks, 1024); |
David Chinner | 714082b | 2008-04-10 12:20:03 +1000 | [diff] [blame] | 1179 | error = xfs_reserve_blocks(mp, &resblks, NULL); |
| 1180 | if (error) |
| 1181 | cmn_err(CE_WARN, "XFS: Unable to allocate reserve blocks. " |
| 1182 | "Continuing without a reserve pool."); |
David Chinner | 84e1e99 | 2007-06-18 16:50:27 +1000 | [diff] [blame] | 1183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | return 0; |
| 1185 | |
Christoph Hellwig | b93b6e4 | 2009-02-04 09:33:58 +0100 | [diff] [blame] | 1186 | out_rtunmount: |
| 1187 | xfs_rtunmount_inodes(mp); |
Christoph Hellwig | f9057e3 | 2009-02-04 09:31:52 +0100 | [diff] [blame] | 1188 | out_rele_rip: |
Christoph Hellwig | 4335509 | 2008-03-27 18:01:08 +1100 | [diff] [blame] | 1189 | IRELE(rip); |
Christoph Hellwig | f9057e3 | 2009-02-04 09:31:52 +0100 | [diff] [blame] | 1190 | out_log_dealloc: |
Christoph Hellwig | 21b699c | 2009-03-16 08:19:29 +0100 | [diff] [blame] | 1191 | xfs_log_unmount(mp); |
Christoph Hellwig | f9057e3 | 2009-02-04 09:31:52 +0100 | [diff] [blame] | 1192 | out_free_perag: |
Christoph Hellwig | ff4f038 | 2008-08-13 16:50:47 +1000 | [diff] [blame] | 1193 | xfs_free_perag(mp); |
Christoph Hellwig | f9057e3 | 2009-02-04 09:31:52 +0100 | [diff] [blame] | 1194 | out_remove_uuid: |
| 1195 | if (!(mp->m_flags & XFS_MOUNT_NOUUID)) |
Christoph Hellwig | fa6adbe | 2008-05-20 11:30:33 +1000 | [diff] [blame] | 1196 | uuid_table_remove(&mp->m_sb.sb_uuid); |
Christoph Hellwig | f9057e3 | 2009-02-04 09:31:52 +0100 | [diff] [blame] | 1197 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | return error; |
| 1199 | } |
| 1200 | |
| 1201 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | * This flushes out the inodes,dquots and the superblock, unmounts the |
| 1203 | * log and makes sure that incore structures are freed. |
| 1204 | */ |
Christoph Hellwig | 41b5c2e | 2008-08-13 16:49:57 +1000 | [diff] [blame] | 1205 | void |
| 1206 | xfs_unmountfs( |
| 1207 | struct xfs_mount *mp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | { |
Christoph Hellwig | 41b5c2e | 2008-08-13 16:49:57 +1000 | [diff] [blame] | 1209 | __uint64_t resblks; |
| 1210 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | |
Christoph Hellwig | f95099b | 2008-12-03 12:20:37 +0100 | [diff] [blame] | 1212 | /* |
| 1213 | * Release dquot that rootinode, rbmino and rsumino might be holding, |
| 1214 | * and release the quota inodes. |
| 1215 | */ |
| 1216 | XFS_QM_UNMOUNT(mp); |
| 1217 | |
Christoph Hellwig | b93b6e4 | 2009-02-04 09:33:58 +0100 | [diff] [blame] | 1218 | xfs_rtunmount_inodes(mp); |
Christoph Hellwig | 77508ec | 2008-08-13 16:49:04 +1000 | [diff] [blame] | 1219 | IRELE(mp->m_rootip); |
| 1220 | |
David Chinner | 641c56f | 2007-06-18 16:50:17 +1000 | [diff] [blame] | 1221 | /* |
| 1222 | * We can potentially deadlock here if we have an inode cluster |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 1223 | * that has been freed has its buffer still pinned in memory because |
David Chinner | 641c56f | 2007-06-18 16:50:17 +1000 | [diff] [blame] | 1224 | * the transaction is still sitting in a iclog. The stale inodes |
| 1225 | * on that buffer will have their flush locks held until the |
| 1226 | * transaction hits the disk and the callbacks run. the inode |
| 1227 | * flush takes the flush lock unconditionally and with nothing to |
| 1228 | * push out the iclog we will never get that unlocked. hence we |
| 1229 | * need to force the log first. |
| 1230 | */ |
| 1231 | xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC); |
David Chinner | 1dc3318 | 2008-10-30 17:37:15 +1100 | [diff] [blame] | 1232 | xfs_reclaim_inodes(mp, 0, XFS_IFLUSH_ASYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | |
Nathan Scott | ee2a4f7 | 2006-01-11 15:33:36 +1100 | [diff] [blame] | 1234 | XFS_QM_DQPURGEALL(mp, XFS_QMOPT_QUOTALL | XFS_QMOPT_UMOUNTING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | |
Lachlan McIlroy | a357a12 | 2008-10-30 16:53:25 +1100 | [diff] [blame] | 1236 | if (mp->m_quotainfo) |
| 1237 | XFS_QM_DONE(mp); |
| 1238 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | /* |
| 1240 | * Flush out the log synchronously so that we know for sure |
| 1241 | * that nothing is pinned. This is important because bflush() |
| 1242 | * will skip pinned buffers. |
| 1243 | */ |
| 1244 | xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC); |
| 1245 | |
| 1246 | xfs_binval(mp->m_ddev_targp); |
| 1247 | if (mp->m_rtdev_targp) { |
| 1248 | xfs_binval(mp->m_rtdev_targp); |
| 1249 | } |
| 1250 | |
David Chinner | 84e1e99 | 2007-06-18 16:50:27 +1000 | [diff] [blame] | 1251 | /* |
| 1252 | * Unreserve any blocks we have so that when we unmount we don't account |
| 1253 | * the reserved free space as used. This is really only necessary for |
| 1254 | * lazy superblock counting because it trusts the incore superblock |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 1255 | * counters to be absolutely correct on clean unmount. |
David Chinner | 84e1e99 | 2007-06-18 16:50:27 +1000 | [diff] [blame] | 1256 | * |
| 1257 | * We don't bother correcting this elsewhere for lazy superblock |
| 1258 | * counting because on mount of an unclean filesystem we reconstruct the |
| 1259 | * correct counter value and this is irrelevant. |
| 1260 | * |
| 1261 | * For non-lazy counter filesystems, this doesn't matter at all because |
| 1262 | * we only every apply deltas to the superblock and hence the incore |
| 1263 | * value does not matter.... |
| 1264 | */ |
| 1265 | resblks = 0; |
David Chinner | 714082b | 2008-04-10 12:20:03 +1000 | [diff] [blame] | 1266 | error = xfs_reserve_blocks(mp, &resblks, NULL); |
| 1267 | if (error) |
| 1268 | cmn_err(CE_WARN, "XFS: Unable to free reserved block pool. " |
| 1269 | "Freespace may not be correct on next mount."); |
| 1270 | |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1271 | error = xfs_log_sbcount(mp, 1); |
| 1272 | if (error) |
| 1273 | cmn_err(CE_WARN, "XFS: Unable to update superblock counters. " |
| 1274 | "Freespace may not be correct on next mount."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | xfs_unmountfs_writesb(mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | xfs_unmountfs_wait(mp); /* wait for async bufs */ |
Christoph Hellwig | 21b699c | 2009-03-16 08:19:29 +0100 | [diff] [blame] | 1277 | xfs_log_unmount_write(mp); |
| 1278 | xfs_log_unmount(mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | if ((mp->m_flags & XFS_MOUNT_NOUUID) == 0) |
Christoph Hellwig | fa6adbe | 2008-05-20 11:30:33 +1000 | [diff] [blame] | 1281 | uuid_table_remove(&mp->m_sb.sb_uuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | |
Christoph Hellwig | 1550d0b | 2008-08-13 16:17:37 +1000 | [diff] [blame] | 1283 | #if defined(DEBUG) |
Christoph Hellwig | 0ce4cfd | 2007-08-30 17:20:53 +1000 | [diff] [blame] | 1284 | xfs_errortag_clearall(mp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | #endif |
Christoph Hellwig | ff4f038 | 2008-08-13 16:50:47 +1000 | [diff] [blame] | 1286 | xfs_free_perag(mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | } |
| 1288 | |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 1289 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | xfs_unmountfs_wait(xfs_mount_t *mp) |
| 1291 | { |
| 1292 | if (mp->m_logdev_targp != mp->m_ddev_targp) |
| 1293 | xfs_wait_buftarg(mp->m_logdev_targp); |
| 1294 | if (mp->m_rtdev_targp) |
| 1295 | xfs_wait_buftarg(mp->m_rtdev_targp); |
| 1296 | xfs_wait_buftarg(mp->m_ddev_targp); |
| 1297 | } |
| 1298 | |
| 1299 | int |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 1300 | xfs_fs_writable(xfs_mount_t *mp) |
| 1301 | { |
Christoph Hellwig | b267ce9 | 2007-08-30 17:21:30 +1000 | [diff] [blame] | 1302 | return !(xfs_test_for_freeze(mp) || XFS_FORCED_SHUTDOWN(mp) || |
Christoph Hellwig | bd186aa | 2007-08-30 17:21:12 +1000 | [diff] [blame] | 1303 | (mp->m_flags & XFS_MOUNT_RDONLY)); |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | /* |
| 1307 | * xfs_log_sbcount |
| 1308 | * |
| 1309 | * Called either periodically to keep the on disk superblock values |
| 1310 | * roughly up to date or from unmount to make sure the values are |
| 1311 | * correct on a clean unmount. |
| 1312 | * |
| 1313 | * Note this code can be called during the process of freezing, so |
| 1314 | * we may need to use the transaction allocator which does not not |
| 1315 | * block when the transaction subsystem is in its frozen state. |
| 1316 | */ |
| 1317 | int |
| 1318 | xfs_log_sbcount( |
| 1319 | xfs_mount_t *mp, |
| 1320 | uint sync) |
| 1321 | { |
| 1322 | xfs_trans_t *tp; |
| 1323 | int error; |
| 1324 | |
| 1325 | if (!xfs_fs_writable(mp)) |
| 1326 | return 0; |
| 1327 | |
Christoph Hellwig | d4d90b5 | 2008-04-22 17:34:37 +1000 | [diff] [blame] | 1328 | xfs_icsb_sync_counters(mp, 0); |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 1329 | |
| 1330 | /* |
| 1331 | * we don't need to do this if we are updating the superblock |
| 1332 | * counters on every modification. |
| 1333 | */ |
| 1334 | if (!xfs_sb_version_haslazysbcount(&mp->m_sb)) |
| 1335 | return 0; |
| 1336 | |
| 1337 | tp = _xfs_trans_alloc(mp, XFS_TRANS_SB_COUNT); |
| 1338 | error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0, |
| 1339 | XFS_DEFAULT_LOG_COUNT); |
| 1340 | if (error) { |
| 1341 | xfs_trans_cancel(tp, 0); |
| 1342 | return error; |
| 1343 | } |
| 1344 | |
| 1345 | xfs_mod_sb(tp, XFS_SB_IFREE | XFS_SB_ICOUNT | XFS_SB_FDBLOCKS); |
| 1346 | if (sync) |
| 1347 | xfs_trans_set_sync(tp); |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1348 | error = xfs_trans_commit(tp, 0); |
| 1349 | return error; |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | xfs_unmountfs_writesb(xfs_mount_t *mp) |
| 1354 | { |
| 1355 | xfs_buf_t *sbp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | int error = 0; |
| 1357 | |
| 1358 | /* |
| 1359 | * skip superblock write if fs is read-only, or |
| 1360 | * if we are doing a forced umount. |
| 1361 | */ |
Christoph Hellwig | bd186aa | 2007-08-30 17:21:12 +1000 | [diff] [blame] | 1362 | if (!((mp->m_flags & XFS_MOUNT_RDONLY) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | XFS_FORCED_SHUTDOWN(mp))) { |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 1364 | |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 1365 | sbp = xfs_getsb(mp, 0); |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 1366 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | XFS_BUF_UNDONE(sbp); |
| 1368 | XFS_BUF_UNREAD(sbp); |
| 1369 | XFS_BUF_UNDELAYWRITE(sbp); |
| 1370 | XFS_BUF_WRITE(sbp); |
| 1371 | XFS_BUF_UNASYNC(sbp); |
| 1372 | ASSERT(XFS_BUF_TARGET(sbp) == mp->m_ddev_targp); |
| 1373 | xfsbdstrat(mp, sbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | error = xfs_iowait(sbp); |
| 1375 | if (error) |
| 1376 | xfs_ioerror_alert("xfs_unmountfs_writesb", |
| 1377 | mp, sbp, XFS_BUF_ADDR(sbp)); |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 1378 | xfs_buf_relse(sbp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | } |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1380 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | /* |
| 1384 | * xfs_mod_sb() can be used to copy arbitrary changes to the |
| 1385 | * in-core superblock into the superblock buffer to be logged. |
| 1386 | * It does not provide the higher level of locking that is |
| 1387 | * needed to protect the in-core superblock from concurrent |
| 1388 | * access. |
| 1389 | */ |
| 1390 | void |
| 1391 | xfs_mod_sb(xfs_trans_t *tp, __int64_t fields) |
| 1392 | { |
| 1393 | xfs_buf_t *bp; |
| 1394 | int first; |
| 1395 | int last; |
| 1396 | xfs_mount_t *mp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | xfs_sb_field_t f; |
| 1398 | |
| 1399 | ASSERT(fields); |
| 1400 | if (!fields) |
| 1401 | return; |
| 1402 | mp = tp->t_mountp; |
| 1403 | bp = xfs_trans_getsb(tp, mp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | first = sizeof(xfs_sb_t); |
| 1405 | last = 0; |
| 1406 | |
| 1407 | /* translate/copy */ |
| 1408 | |
Christoph Hellwig | 2bdf7cd | 2007-08-28 13:58:06 +1000 | [diff] [blame] | 1409 | xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb, fields); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | |
| 1411 | /* find modified range */ |
| 1412 | |
| 1413 | f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields); |
| 1414 | ASSERT((1LL << f) & XFS_SB_MOD_BITS); |
| 1415 | first = xfs_sb_info[f].offset; |
| 1416 | |
| 1417 | f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields); |
| 1418 | ASSERT((1LL << f) & XFS_SB_MOD_BITS); |
| 1419 | last = xfs_sb_info[f + 1].offset - 1; |
| 1420 | |
| 1421 | xfs_trans_log_buf(tp, bp, first, last); |
| 1422 | } |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 1423 | |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 1424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | /* |
| 1426 | * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply |
| 1427 | * a delta to a specified field in the in-core superblock. Simply |
| 1428 | * switch on the field indicated and apply the delta to that field. |
| 1429 | * Fields are not allowed to dip below zero, so if the delta would |
| 1430 | * do this do not apply it and return EINVAL. |
| 1431 | * |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 1432 | * The m_sb_lock must be held when this routine is called. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | */ |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 1434 | int |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 1435 | xfs_mod_incore_sb_unlocked( |
| 1436 | xfs_mount_t *mp, |
| 1437 | xfs_sb_field_t field, |
| 1438 | int64_t delta, |
| 1439 | int rsvd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | { |
| 1441 | int scounter; /* short counter for 32 bit fields */ |
| 1442 | long long lcounter; /* long counter for 64 bit fields */ |
| 1443 | long long res_used, rem; |
| 1444 | |
| 1445 | /* |
| 1446 | * With the in-core superblock spin lock held, switch |
| 1447 | * on the indicated field. Apply the delta to the |
| 1448 | * proper field. If the fields value would dip below |
| 1449 | * 0, then do not apply the delta and return EINVAL. |
| 1450 | */ |
| 1451 | switch (field) { |
| 1452 | case XFS_SBS_ICOUNT: |
| 1453 | lcounter = (long long)mp->m_sb.sb_icount; |
| 1454 | lcounter += delta; |
| 1455 | if (lcounter < 0) { |
| 1456 | ASSERT(0); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1457 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | } |
| 1459 | mp->m_sb.sb_icount = lcounter; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1460 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | case XFS_SBS_IFREE: |
| 1462 | lcounter = (long long)mp->m_sb.sb_ifree; |
| 1463 | lcounter += delta; |
| 1464 | if (lcounter < 0) { |
| 1465 | ASSERT(0); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1466 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | } |
| 1468 | mp->m_sb.sb_ifree = lcounter; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1469 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | case XFS_SBS_FDBLOCKS: |
David Chinner | 4be536d | 2006-09-07 14:26:50 +1000 | [diff] [blame] | 1471 | lcounter = (long long) |
| 1472 | mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | res_used = (long long)(mp->m_resblks - mp->m_resblks_avail); |
| 1474 | |
| 1475 | if (delta > 0) { /* Putting blocks back */ |
| 1476 | if (res_used > delta) { |
| 1477 | mp->m_resblks_avail += delta; |
| 1478 | } else { |
| 1479 | rem = delta - res_used; |
| 1480 | mp->m_resblks_avail = mp->m_resblks; |
| 1481 | lcounter += rem; |
| 1482 | } |
| 1483 | } else { /* Taking blocks away */ |
| 1484 | |
| 1485 | lcounter += delta; |
| 1486 | |
| 1487 | /* |
| 1488 | * If were out of blocks, use any available reserved blocks if |
| 1489 | * were allowed to. |
| 1490 | */ |
| 1491 | |
| 1492 | if (lcounter < 0) { |
| 1493 | if (rsvd) { |
| 1494 | lcounter = (long long)mp->m_resblks_avail + delta; |
| 1495 | if (lcounter < 0) { |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1496 | return XFS_ERROR(ENOSPC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | } |
| 1498 | mp->m_resblks_avail = lcounter; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1499 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | } else { /* not reserved */ |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1501 | return XFS_ERROR(ENOSPC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | } |
| 1503 | } |
| 1504 | } |
| 1505 | |
David Chinner | 4be536d | 2006-09-07 14:26:50 +1000 | [diff] [blame] | 1506 | mp->m_sb.sb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1507 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | case XFS_SBS_FREXTENTS: |
| 1509 | lcounter = (long long)mp->m_sb.sb_frextents; |
| 1510 | lcounter += delta; |
| 1511 | if (lcounter < 0) { |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1512 | return XFS_ERROR(ENOSPC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | } |
| 1514 | mp->m_sb.sb_frextents = lcounter; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1515 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | case XFS_SBS_DBLOCKS: |
| 1517 | lcounter = (long long)mp->m_sb.sb_dblocks; |
| 1518 | lcounter += delta; |
| 1519 | if (lcounter < 0) { |
| 1520 | ASSERT(0); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1521 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | } |
| 1523 | mp->m_sb.sb_dblocks = lcounter; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1524 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | case XFS_SBS_AGCOUNT: |
| 1526 | scounter = mp->m_sb.sb_agcount; |
| 1527 | scounter += delta; |
| 1528 | if (scounter < 0) { |
| 1529 | ASSERT(0); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1530 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | } |
| 1532 | mp->m_sb.sb_agcount = scounter; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1533 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | case XFS_SBS_IMAX_PCT: |
| 1535 | scounter = mp->m_sb.sb_imax_pct; |
| 1536 | scounter += delta; |
| 1537 | if (scounter < 0) { |
| 1538 | ASSERT(0); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1539 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | } |
| 1541 | mp->m_sb.sb_imax_pct = scounter; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1542 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | case XFS_SBS_REXTSIZE: |
| 1544 | scounter = mp->m_sb.sb_rextsize; |
| 1545 | scounter += delta; |
| 1546 | if (scounter < 0) { |
| 1547 | ASSERT(0); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1548 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | } |
| 1550 | mp->m_sb.sb_rextsize = scounter; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1551 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | case XFS_SBS_RBMBLOCKS: |
| 1553 | scounter = mp->m_sb.sb_rbmblocks; |
| 1554 | scounter += delta; |
| 1555 | if (scounter < 0) { |
| 1556 | ASSERT(0); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1557 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | } |
| 1559 | mp->m_sb.sb_rbmblocks = scounter; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1560 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | case XFS_SBS_RBLOCKS: |
| 1562 | lcounter = (long long)mp->m_sb.sb_rblocks; |
| 1563 | lcounter += delta; |
| 1564 | if (lcounter < 0) { |
| 1565 | ASSERT(0); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1566 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | } |
| 1568 | mp->m_sb.sb_rblocks = lcounter; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1569 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | case XFS_SBS_REXTENTS: |
| 1571 | lcounter = (long long)mp->m_sb.sb_rextents; |
| 1572 | lcounter += delta; |
| 1573 | if (lcounter < 0) { |
| 1574 | ASSERT(0); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1575 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | } |
| 1577 | mp->m_sb.sb_rextents = lcounter; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1578 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | case XFS_SBS_REXTSLOG: |
| 1580 | scounter = mp->m_sb.sb_rextslog; |
| 1581 | scounter += delta; |
| 1582 | if (scounter < 0) { |
| 1583 | ASSERT(0); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1584 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | } |
| 1586 | mp->m_sb.sb_rextslog = scounter; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1587 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | default: |
| 1589 | ASSERT(0); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1590 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | /* |
| 1595 | * xfs_mod_incore_sb() is used to change a field in the in-core |
| 1596 | * superblock structure by the specified delta. This modification |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 1597 | * is protected by the m_sb_lock. Just use the xfs_mod_incore_sb_unlocked() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | * routine to do the work. |
| 1599 | */ |
| 1600 | int |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 1601 | xfs_mod_incore_sb( |
| 1602 | xfs_mount_t *mp, |
| 1603 | xfs_sb_field_t field, |
| 1604 | int64_t delta, |
| 1605 | int rsvd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | int status; |
| 1608 | |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 1609 | /* check for per-cpu counters */ |
| 1610 | switch (field) { |
| 1611 | #ifdef HAVE_PERCPU_SB |
| 1612 | case XFS_SBS_ICOUNT: |
| 1613 | case XFS_SBS_IFREE: |
| 1614 | case XFS_SBS_FDBLOCKS: |
| 1615 | if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) { |
| 1616 | status = xfs_icsb_modify_counters(mp, field, |
| 1617 | delta, rsvd); |
| 1618 | break; |
| 1619 | } |
| 1620 | /* FALLTHROUGH */ |
| 1621 | #endif |
| 1622 | default: |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 1623 | spin_lock(&mp->m_sb_lock); |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 1624 | status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd); |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 1625 | spin_unlock(&mp->m_sb_lock); |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 1626 | break; |
| 1627 | } |
| 1628 | |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1629 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | /* |
| 1633 | * xfs_mod_incore_sb_batch() is used to change more than one field |
| 1634 | * in the in-core superblock structure at a time. This modification |
| 1635 | * is protected by a lock internal to this module. The fields and |
| 1636 | * changes to those fields are specified in the array of xfs_mod_sb |
| 1637 | * structures passed in. |
| 1638 | * |
| 1639 | * Either all of the specified deltas will be applied or none of |
| 1640 | * them will. If any modified field dips below 0, then all modifications |
| 1641 | * will be backed out and EINVAL will be returned. |
| 1642 | */ |
| 1643 | int |
| 1644 | xfs_mod_incore_sb_batch(xfs_mount_t *mp, xfs_mod_sb_t *msb, uint nmsb, int rsvd) |
| 1645 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | int status=0; |
| 1647 | xfs_mod_sb_t *msbp; |
| 1648 | |
| 1649 | /* |
| 1650 | * Loop through the array of mod structures and apply each |
| 1651 | * individually. If any fail, then back out all those |
| 1652 | * which have already been applied. Do all of this within |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 1653 | * the scope of the m_sb_lock so that all of the changes will |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | * be atomic. |
| 1655 | */ |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 1656 | spin_lock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | msbp = &msb[0]; |
| 1658 | for (msbp = &msbp[0]; msbp < (msb + nmsb); msbp++) { |
| 1659 | /* |
| 1660 | * Apply the delta at index n. If it fails, break |
| 1661 | * from the loop so we'll fall into the undo loop |
| 1662 | * below. |
| 1663 | */ |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 1664 | switch (msbp->msb_field) { |
| 1665 | #ifdef HAVE_PERCPU_SB |
| 1666 | case XFS_SBS_ICOUNT: |
| 1667 | case XFS_SBS_IFREE: |
| 1668 | case XFS_SBS_FDBLOCKS: |
| 1669 | if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) { |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 1670 | spin_unlock(&mp->m_sb_lock); |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 1671 | status = xfs_icsb_modify_counters(mp, |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 1672 | msbp->msb_field, |
| 1673 | msbp->msb_delta, rsvd); |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 1674 | spin_lock(&mp->m_sb_lock); |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 1675 | break; |
| 1676 | } |
| 1677 | /* FALLTHROUGH */ |
| 1678 | #endif |
| 1679 | default: |
| 1680 | status = xfs_mod_incore_sb_unlocked(mp, |
| 1681 | msbp->msb_field, |
| 1682 | msbp->msb_delta, rsvd); |
| 1683 | break; |
| 1684 | } |
| 1685 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | if (status != 0) { |
| 1687 | break; |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | /* |
| 1692 | * If we didn't complete the loop above, then back out |
| 1693 | * any changes made to the superblock. If you add code |
| 1694 | * between the loop above and here, make sure that you |
| 1695 | * preserve the value of status. Loop back until |
| 1696 | * we step below the beginning of the array. Make sure |
| 1697 | * we don't touch anything back there. |
| 1698 | */ |
| 1699 | if (status != 0) { |
| 1700 | msbp--; |
| 1701 | while (msbp >= msb) { |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 1702 | switch (msbp->msb_field) { |
| 1703 | #ifdef HAVE_PERCPU_SB |
| 1704 | case XFS_SBS_ICOUNT: |
| 1705 | case XFS_SBS_IFREE: |
| 1706 | case XFS_SBS_FDBLOCKS: |
| 1707 | if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) { |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 1708 | spin_unlock(&mp->m_sb_lock); |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 1709 | status = xfs_icsb_modify_counters(mp, |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 1710 | msbp->msb_field, |
| 1711 | -(msbp->msb_delta), |
| 1712 | rsvd); |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 1713 | spin_lock(&mp->m_sb_lock); |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 1714 | break; |
| 1715 | } |
| 1716 | /* FALLTHROUGH */ |
| 1717 | #endif |
| 1718 | default: |
| 1719 | status = xfs_mod_incore_sb_unlocked(mp, |
| 1720 | msbp->msb_field, |
| 1721 | -(msbp->msb_delta), |
| 1722 | rsvd); |
| 1723 | break; |
| 1724 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | ASSERT(status == 0); |
| 1726 | msbp--; |
| 1727 | } |
| 1728 | } |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 1729 | spin_unlock(&mp->m_sb_lock); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1730 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | } |
| 1732 | |
| 1733 | /* |
| 1734 | * xfs_getsb() is called to obtain the buffer for the superblock. |
| 1735 | * The buffer is returned locked and read in from disk. |
| 1736 | * The buffer should be released with a call to xfs_brelse(). |
| 1737 | * |
| 1738 | * If the flags parameter is BUF_TRYLOCK, then we'll only return |
| 1739 | * the superblock buffer if it can be locked without sleeping. |
| 1740 | * If it can't then we'll return NULL. |
| 1741 | */ |
| 1742 | xfs_buf_t * |
| 1743 | xfs_getsb( |
| 1744 | xfs_mount_t *mp, |
| 1745 | int flags) |
| 1746 | { |
| 1747 | xfs_buf_t *bp; |
| 1748 | |
| 1749 | ASSERT(mp->m_sb_bp != NULL); |
| 1750 | bp = mp->m_sb_bp; |
| 1751 | if (flags & XFS_BUF_TRYLOCK) { |
| 1752 | if (!XFS_BUF_CPSEMA(bp)) { |
| 1753 | return NULL; |
| 1754 | } |
| 1755 | } else { |
| 1756 | XFS_BUF_PSEMA(bp, PRIBIO); |
| 1757 | } |
| 1758 | XFS_BUF_HOLD(bp); |
| 1759 | ASSERT(XFS_BUF_ISDONE(bp)); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1760 | return bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | } |
| 1762 | |
| 1763 | /* |
| 1764 | * Used to free the superblock along various error paths. |
| 1765 | */ |
| 1766 | void |
| 1767 | xfs_freesb( |
| 1768 | xfs_mount_t *mp) |
| 1769 | { |
| 1770 | xfs_buf_t *bp; |
| 1771 | |
| 1772 | /* |
| 1773 | * Use xfs_getsb() so that the buffer will be locked |
| 1774 | * when we call xfs_buf_relse(). |
| 1775 | */ |
| 1776 | bp = xfs_getsb(mp, 0); |
| 1777 | XFS_BUF_UNMANAGE(bp); |
| 1778 | xfs_buf_relse(bp); |
| 1779 | mp->m_sb_bp = NULL; |
| 1780 | } |
| 1781 | |
| 1782 | /* |
| 1783 | * See if the UUID is unique among mounted XFS filesystems. |
| 1784 | * Mount fails if UUID is nil or a FS with the same UUID is already mounted. |
| 1785 | */ |
| 1786 | STATIC int |
| 1787 | xfs_uuid_mount( |
| 1788 | xfs_mount_t *mp) |
| 1789 | { |
| 1790 | if (uuid_is_nil(&mp->m_sb.sb_uuid)) { |
| 1791 | cmn_err(CE_WARN, |
| 1792 | "XFS: Filesystem %s has nil UUID - can't mount", |
| 1793 | mp->m_fsname); |
| 1794 | return -1; |
| 1795 | } |
| 1796 | if (!uuid_table_insert(&mp->m_sb.sb_uuid)) { |
| 1797 | cmn_err(CE_WARN, |
| 1798 | "XFS: Filesystem %s has duplicate UUID - can't mount", |
| 1799 | mp->m_fsname); |
| 1800 | return -1; |
| 1801 | } |
| 1802 | return 0; |
| 1803 | } |
| 1804 | |
| 1805 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | * Used to log changes to the superblock unit and width fields which could |
Eric Sandeen | e6957ea | 2008-04-10 12:19:34 +1000 | [diff] [blame] | 1807 | * be altered by the mount options, as well as any potential sb_features2 |
| 1808 | * fixup. Only the first superblock is updated. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | */ |
Christoph Hellwig | 7884bc8 | 2009-01-19 02:04:07 +0100 | [diff] [blame] | 1810 | int |
David Chinner | ee1c090 | 2008-03-06 13:45:50 +1100 | [diff] [blame] | 1811 | xfs_mount_log_sb( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | xfs_mount_t *mp, |
| 1813 | __int64_t fields) |
| 1814 | { |
| 1815 | xfs_trans_t *tp; |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1816 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | |
David Chinner | ee1c090 | 2008-03-06 13:45:50 +1100 | [diff] [blame] | 1818 | ASSERT(fields & (XFS_SB_UNIT | XFS_SB_WIDTH | XFS_SB_UUID | |
David Chinner | 4b166de | 2008-05-20 11:30:27 +1000 | [diff] [blame] | 1819 | XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2 | |
| 1820 | XFS_SB_VERSIONNUM)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | |
| 1822 | tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT); |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1823 | error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0, |
| 1824 | XFS_DEFAULT_LOG_COUNT); |
| 1825 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | xfs_trans_cancel(tp, 0); |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1827 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | } |
| 1829 | xfs_mod_sb(tp, fields); |
David Chinner | e5720ee | 2008-04-10 12:21:18 +1000 | [diff] [blame] | 1830 | error = xfs_trans_commit(tp, 0); |
| 1831 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | } |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 1833 | |
| 1834 | |
| 1835 | #ifdef HAVE_PERCPU_SB |
| 1836 | /* |
| 1837 | * Per-cpu incore superblock counters |
| 1838 | * |
| 1839 | * Simple concept, difficult implementation |
| 1840 | * |
| 1841 | * Basically, replace the incore superblock counters with a distributed per cpu |
| 1842 | * counter for contended fields (e.g. free block count). |
| 1843 | * |
| 1844 | * Difficulties arise in that the incore sb is used for ENOSPC checking, and |
| 1845 | * hence needs to be accurately read when we are running low on space. Hence |
| 1846 | * there is a method to enable and disable the per-cpu counters based on how |
| 1847 | * much "stuff" is available in them. |
| 1848 | * |
| 1849 | * Basically, a counter is enabled if there is enough free resource to justify |
| 1850 | * running a per-cpu fast-path. If the per-cpu counter runs out (i.e. a local |
| 1851 | * ENOSPC), then we disable the counters to synchronise all callers and |
| 1852 | * re-distribute the available resources. |
| 1853 | * |
| 1854 | * If, once we redistributed the available resources, we still get a failure, |
| 1855 | * we disable the per-cpu counter and go through the slow path. |
| 1856 | * |
| 1857 | * The slow path is the current xfs_mod_incore_sb() function. This means that |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 1858 | * when we disable a per-cpu counter, we need to drain its resources back to |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 1859 | * the global superblock. We do this after disabling the counter to prevent |
| 1860 | * more threads from queueing up on the counter. |
| 1861 | * |
| 1862 | * Essentially, this means that we still need a lock in the fast path to enable |
| 1863 | * synchronisation between the global counters and the per-cpu counters. This |
| 1864 | * is not a problem because the lock will be local to a CPU almost all the time |
| 1865 | * and have little contention except when we get to ENOSPC conditions. |
| 1866 | * |
| 1867 | * Basically, this lock becomes a barrier that enables us to lock out the fast |
| 1868 | * path while we do things like enabling and disabling counters and |
| 1869 | * synchronising the counters. |
| 1870 | * |
| 1871 | * Locking rules: |
| 1872 | * |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 1873 | * 1. m_sb_lock before picking up per-cpu locks |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 1874 | * 2. per-cpu locks always picked up via for_each_online_cpu() order |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 1875 | * 3. accurate counter sync requires m_sb_lock + per cpu locks |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 1876 | * 4. modifying per-cpu counters requires holding per-cpu lock |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 1877 | * 5. modifying global counters requires holding m_sb_lock |
| 1878 | * 6. enabling or disabling a counter requires holding the m_sb_lock |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 1879 | * and _none_ of the per-cpu locks. |
| 1880 | * |
| 1881 | * Disabled counters are only ever re-enabled by a balance operation |
| 1882 | * that results in more free resources per CPU than a given threshold. |
| 1883 | * To ensure counters don't remain disabled, they are rebalanced when |
| 1884 | * the global resource goes above a higher threshold (i.e. some hysteresis |
| 1885 | * is present to prevent thrashing). |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 1886 | */ |
David Chinner | e8234a6 | 2006-03-14 13:23:52 +1100 | [diff] [blame] | 1887 | |
Chandra Seetharaman | 5a67e4c | 2006-06-27 02:54:11 -0700 | [diff] [blame] | 1888 | #ifdef CONFIG_HOTPLUG_CPU |
David Chinner | e8234a6 | 2006-03-14 13:23:52 +1100 | [diff] [blame] | 1889 | /* |
| 1890 | * hot-plug CPU notifier support. |
| 1891 | * |
Chandra Seetharaman | 5a67e4c | 2006-06-27 02:54:11 -0700 | [diff] [blame] | 1892 | * We need a notifier per filesystem as we need to be able to identify |
| 1893 | * the filesystem to balance the counters out. This is achieved by |
| 1894 | * having a notifier block embedded in the xfs_mount_t and doing pointer |
| 1895 | * magic to get the mount pointer from the notifier block address. |
David Chinner | e8234a6 | 2006-03-14 13:23:52 +1100 | [diff] [blame] | 1896 | */ |
| 1897 | STATIC int |
| 1898 | xfs_icsb_cpu_notify( |
| 1899 | struct notifier_block *nfb, |
| 1900 | unsigned long action, |
| 1901 | void *hcpu) |
| 1902 | { |
| 1903 | xfs_icsb_cnts_t *cntp; |
| 1904 | xfs_mount_t *mp; |
David Chinner | e8234a6 | 2006-03-14 13:23:52 +1100 | [diff] [blame] | 1905 | |
| 1906 | mp = (xfs_mount_t *)container_of(nfb, xfs_mount_t, m_icsb_notifier); |
| 1907 | cntp = (xfs_icsb_cnts_t *) |
| 1908 | per_cpu_ptr(mp->m_sb_cnts, (unsigned long)hcpu); |
| 1909 | switch (action) { |
| 1910 | case CPU_UP_PREPARE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 1911 | case CPU_UP_PREPARE_FROZEN: |
David Chinner | e8234a6 | 2006-03-14 13:23:52 +1100 | [diff] [blame] | 1912 | /* Easy Case - initialize the area and locks, and |
| 1913 | * then rebalance when online does everything else for us. */ |
David Chinner | 01e1b69 | 2006-03-14 13:29:16 +1100 | [diff] [blame] | 1914 | memset(cntp, 0, sizeof(xfs_icsb_cnts_t)); |
David Chinner | e8234a6 | 2006-03-14 13:23:52 +1100 | [diff] [blame] | 1915 | break; |
| 1916 | case CPU_ONLINE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 1917 | case CPU_ONLINE_FROZEN: |
David Chinner | 03135cf | 2007-02-10 18:35:15 +1100 | [diff] [blame] | 1918 | xfs_icsb_lock(mp); |
Christoph Hellwig | 45af6c6 | 2008-04-22 17:34:44 +1000 | [diff] [blame] | 1919 | xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0); |
| 1920 | xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0); |
| 1921 | xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0); |
David Chinner | 03135cf | 2007-02-10 18:35:15 +1100 | [diff] [blame] | 1922 | xfs_icsb_unlock(mp); |
David Chinner | e8234a6 | 2006-03-14 13:23:52 +1100 | [diff] [blame] | 1923 | break; |
| 1924 | case CPU_DEAD: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 1925 | case CPU_DEAD_FROZEN: |
David Chinner | e8234a6 | 2006-03-14 13:23:52 +1100 | [diff] [blame] | 1926 | /* Disable all the counters, then fold the dead cpu's |
| 1927 | * count into the total on the global superblock and |
| 1928 | * re-enable the counters. */ |
David Chinner | 03135cf | 2007-02-10 18:35:15 +1100 | [diff] [blame] | 1929 | xfs_icsb_lock(mp); |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 1930 | spin_lock(&mp->m_sb_lock); |
David Chinner | e8234a6 | 2006-03-14 13:23:52 +1100 | [diff] [blame] | 1931 | xfs_icsb_disable_counter(mp, XFS_SBS_ICOUNT); |
| 1932 | xfs_icsb_disable_counter(mp, XFS_SBS_IFREE); |
| 1933 | xfs_icsb_disable_counter(mp, XFS_SBS_FDBLOCKS); |
| 1934 | |
| 1935 | mp->m_sb.sb_icount += cntp->icsb_icount; |
| 1936 | mp->m_sb.sb_ifree += cntp->icsb_ifree; |
| 1937 | mp->m_sb.sb_fdblocks += cntp->icsb_fdblocks; |
| 1938 | |
David Chinner | 01e1b69 | 2006-03-14 13:29:16 +1100 | [diff] [blame] | 1939 | memset(cntp, 0, sizeof(xfs_icsb_cnts_t)); |
David Chinner | e8234a6 | 2006-03-14 13:23:52 +1100 | [diff] [blame] | 1940 | |
Christoph Hellwig | 45af6c6 | 2008-04-22 17:34:44 +1000 | [diff] [blame] | 1941 | xfs_icsb_balance_counter_locked(mp, XFS_SBS_ICOUNT, 0); |
| 1942 | xfs_icsb_balance_counter_locked(mp, XFS_SBS_IFREE, 0); |
| 1943 | xfs_icsb_balance_counter_locked(mp, XFS_SBS_FDBLOCKS, 0); |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 1944 | spin_unlock(&mp->m_sb_lock); |
David Chinner | 03135cf | 2007-02-10 18:35:15 +1100 | [diff] [blame] | 1945 | xfs_icsb_unlock(mp); |
David Chinner | e8234a6 | 2006-03-14 13:23:52 +1100 | [diff] [blame] | 1946 | break; |
| 1947 | } |
| 1948 | |
| 1949 | return NOTIFY_OK; |
| 1950 | } |
Chandra Seetharaman | 5a67e4c | 2006-06-27 02:54:11 -0700 | [diff] [blame] | 1951 | #endif /* CONFIG_HOTPLUG_CPU */ |
David Chinner | e8234a6 | 2006-03-14 13:23:52 +1100 | [diff] [blame] | 1952 | |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 1953 | int |
| 1954 | xfs_icsb_init_counters( |
| 1955 | xfs_mount_t *mp) |
| 1956 | { |
| 1957 | xfs_icsb_cnts_t *cntp; |
| 1958 | int i; |
| 1959 | |
| 1960 | mp->m_sb_cnts = alloc_percpu(xfs_icsb_cnts_t); |
| 1961 | if (mp->m_sb_cnts == NULL) |
| 1962 | return -ENOMEM; |
| 1963 | |
Chandra Seetharaman | 5a67e4c | 2006-06-27 02:54:11 -0700 | [diff] [blame] | 1964 | #ifdef CONFIG_HOTPLUG_CPU |
David Chinner | e8234a6 | 2006-03-14 13:23:52 +1100 | [diff] [blame] | 1965 | mp->m_icsb_notifier.notifier_call = xfs_icsb_cpu_notify; |
| 1966 | mp->m_icsb_notifier.priority = 0; |
Chandra Seetharaman | 5a67e4c | 2006-06-27 02:54:11 -0700 | [diff] [blame] | 1967 | register_hotcpu_notifier(&mp->m_icsb_notifier); |
| 1968 | #endif /* CONFIG_HOTPLUG_CPU */ |
David Chinner | e8234a6 | 2006-03-14 13:23:52 +1100 | [diff] [blame] | 1969 | |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 1970 | for_each_online_cpu(i) { |
| 1971 | cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i); |
David Chinner | 01e1b69 | 2006-03-14 13:29:16 +1100 | [diff] [blame] | 1972 | memset(cntp, 0, sizeof(xfs_icsb_cnts_t)); |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 1973 | } |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 1974 | |
| 1975 | mutex_init(&mp->m_icsb_mutex); |
| 1976 | |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 1977 | /* |
| 1978 | * start with all counters disabled so that the |
| 1979 | * initial balance kicks us off correctly |
| 1980 | */ |
| 1981 | mp->m_icsb_counters = -1; |
| 1982 | return 0; |
| 1983 | } |
| 1984 | |
Lachlan McIlroy | 5478eea | 2007-02-10 18:36:29 +1100 | [diff] [blame] | 1985 | void |
| 1986 | xfs_icsb_reinit_counters( |
| 1987 | xfs_mount_t *mp) |
| 1988 | { |
| 1989 | xfs_icsb_lock(mp); |
| 1990 | /* |
| 1991 | * start with all counters disabled so that the |
| 1992 | * initial balance kicks us off correctly |
| 1993 | */ |
| 1994 | mp->m_icsb_counters = -1; |
Christoph Hellwig | 45af6c6 | 2008-04-22 17:34:44 +1000 | [diff] [blame] | 1995 | xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0); |
| 1996 | xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0); |
| 1997 | xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0); |
Lachlan McIlroy | 5478eea | 2007-02-10 18:36:29 +1100 | [diff] [blame] | 1998 | xfs_icsb_unlock(mp); |
| 1999 | } |
| 2000 | |
Christoph Hellwig | c962fb7 | 2008-05-20 15:10:52 +1000 | [diff] [blame] | 2001 | void |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2002 | xfs_icsb_destroy_counters( |
| 2003 | xfs_mount_t *mp) |
| 2004 | { |
David Chinner | e8234a6 | 2006-03-14 13:23:52 +1100 | [diff] [blame] | 2005 | if (mp->m_sb_cnts) { |
Chandra Seetharaman | 5a67e4c | 2006-06-27 02:54:11 -0700 | [diff] [blame] | 2006 | unregister_hotcpu_notifier(&mp->m_icsb_notifier); |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2007 | free_percpu(mp->m_sb_cnts); |
David Chinner | e8234a6 | 2006-03-14 13:23:52 +1100 | [diff] [blame] | 2008 | } |
David Chinner | 03135cf | 2007-02-10 18:35:15 +1100 | [diff] [blame] | 2009 | mutex_destroy(&mp->m_icsb_mutex); |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2010 | } |
| 2011 | |
David Chinner | 7989cb8 | 2007-02-10 18:34:56 +1100 | [diff] [blame] | 2012 | STATIC_INLINE void |
David Chinner | 01e1b69 | 2006-03-14 13:29:16 +1100 | [diff] [blame] | 2013 | xfs_icsb_lock_cntr( |
| 2014 | xfs_icsb_cnts_t *icsbp) |
| 2015 | { |
| 2016 | while (test_and_set_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags)) { |
| 2017 | ndelay(1000); |
| 2018 | } |
| 2019 | } |
| 2020 | |
David Chinner | 7989cb8 | 2007-02-10 18:34:56 +1100 | [diff] [blame] | 2021 | STATIC_INLINE void |
David Chinner | 01e1b69 | 2006-03-14 13:29:16 +1100 | [diff] [blame] | 2022 | xfs_icsb_unlock_cntr( |
| 2023 | xfs_icsb_cnts_t *icsbp) |
| 2024 | { |
| 2025 | clear_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags); |
| 2026 | } |
| 2027 | |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2028 | |
David Chinner | 7989cb8 | 2007-02-10 18:34:56 +1100 | [diff] [blame] | 2029 | STATIC_INLINE void |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2030 | xfs_icsb_lock_all_counters( |
| 2031 | xfs_mount_t *mp) |
| 2032 | { |
| 2033 | xfs_icsb_cnts_t *cntp; |
| 2034 | int i; |
| 2035 | |
| 2036 | for_each_online_cpu(i) { |
| 2037 | cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i); |
David Chinner | 01e1b69 | 2006-03-14 13:29:16 +1100 | [diff] [blame] | 2038 | xfs_icsb_lock_cntr(cntp); |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2039 | } |
| 2040 | } |
| 2041 | |
David Chinner | 7989cb8 | 2007-02-10 18:34:56 +1100 | [diff] [blame] | 2042 | STATIC_INLINE void |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2043 | xfs_icsb_unlock_all_counters( |
| 2044 | xfs_mount_t *mp) |
| 2045 | { |
| 2046 | xfs_icsb_cnts_t *cntp; |
| 2047 | int i; |
| 2048 | |
| 2049 | for_each_online_cpu(i) { |
| 2050 | cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i); |
David Chinner | 01e1b69 | 2006-03-14 13:29:16 +1100 | [diff] [blame] | 2051 | xfs_icsb_unlock_cntr(cntp); |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2052 | } |
| 2053 | } |
| 2054 | |
| 2055 | STATIC void |
| 2056 | xfs_icsb_count( |
| 2057 | xfs_mount_t *mp, |
| 2058 | xfs_icsb_cnts_t *cnt, |
| 2059 | int flags) |
| 2060 | { |
| 2061 | xfs_icsb_cnts_t *cntp; |
| 2062 | int i; |
| 2063 | |
| 2064 | memset(cnt, 0, sizeof(xfs_icsb_cnts_t)); |
| 2065 | |
| 2066 | if (!(flags & XFS_ICSB_LAZY_COUNT)) |
| 2067 | xfs_icsb_lock_all_counters(mp); |
| 2068 | |
| 2069 | for_each_online_cpu(i) { |
| 2070 | cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i); |
| 2071 | cnt->icsb_icount += cntp->icsb_icount; |
| 2072 | cnt->icsb_ifree += cntp->icsb_ifree; |
| 2073 | cnt->icsb_fdblocks += cntp->icsb_fdblocks; |
| 2074 | } |
| 2075 | |
| 2076 | if (!(flags & XFS_ICSB_LAZY_COUNT)) |
| 2077 | xfs_icsb_unlock_all_counters(mp); |
| 2078 | } |
| 2079 | |
| 2080 | STATIC int |
| 2081 | xfs_icsb_counter_disabled( |
| 2082 | xfs_mount_t *mp, |
| 2083 | xfs_sb_field_t field) |
| 2084 | { |
| 2085 | ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS)); |
| 2086 | return test_bit(field, &mp->m_icsb_counters); |
| 2087 | } |
| 2088 | |
David Chinner | 36fbe6e | 2008-04-10 12:19:56 +1000 | [diff] [blame] | 2089 | STATIC void |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2090 | xfs_icsb_disable_counter( |
| 2091 | xfs_mount_t *mp, |
| 2092 | xfs_sb_field_t field) |
| 2093 | { |
| 2094 | xfs_icsb_cnts_t cnt; |
| 2095 | |
| 2096 | ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS)); |
| 2097 | |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 2098 | /* |
| 2099 | * If we are already disabled, then there is nothing to do |
| 2100 | * here. We check before locking all the counters to avoid |
| 2101 | * the expensive lock operation when being called in the |
| 2102 | * slow path and the counter is already disabled. This is |
| 2103 | * safe because the only time we set or clear this state is under |
| 2104 | * the m_icsb_mutex. |
| 2105 | */ |
| 2106 | if (xfs_icsb_counter_disabled(mp, field)) |
David Chinner | 36fbe6e | 2008-04-10 12:19:56 +1000 | [diff] [blame] | 2107 | return; |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 2108 | |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2109 | xfs_icsb_lock_all_counters(mp); |
| 2110 | if (!test_and_set_bit(field, &mp->m_icsb_counters)) { |
| 2111 | /* drain back to superblock */ |
| 2112 | |
Christoph Hellwig | ce46193 | 2008-04-22 17:34:50 +1000 | [diff] [blame] | 2113 | xfs_icsb_count(mp, &cnt, XFS_ICSB_LAZY_COUNT); |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2114 | switch(field) { |
| 2115 | case XFS_SBS_ICOUNT: |
| 2116 | mp->m_sb.sb_icount = cnt.icsb_icount; |
| 2117 | break; |
| 2118 | case XFS_SBS_IFREE: |
| 2119 | mp->m_sb.sb_ifree = cnt.icsb_ifree; |
| 2120 | break; |
| 2121 | case XFS_SBS_FDBLOCKS: |
| 2122 | mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks; |
| 2123 | break; |
| 2124 | default: |
| 2125 | BUG(); |
| 2126 | } |
| 2127 | } |
| 2128 | |
| 2129 | xfs_icsb_unlock_all_counters(mp); |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2130 | } |
| 2131 | |
| 2132 | STATIC void |
| 2133 | xfs_icsb_enable_counter( |
| 2134 | xfs_mount_t *mp, |
| 2135 | xfs_sb_field_t field, |
| 2136 | uint64_t count, |
| 2137 | uint64_t resid) |
| 2138 | { |
| 2139 | xfs_icsb_cnts_t *cntp; |
| 2140 | int i; |
| 2141 | |
| 2142 | ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS)); |
| 2143 | |
| 2144 | xfs_icsb_lock_all_counters(mp); |
| 2145 | for_each_online_cpu(i) { |
| 2146 | cntp = per_cpu_ptr(mp->m_sb_cnts, i); |
| 2147 | switch (field) { |
| 2148 | case XFS_SBS_ICOUNT: |
| 2149 | cntp->icsb_icount = count + resid; |
| 2150 | break; |
| 2151 | case XFS_SBS_IFREE: |
| 2152 | cntp->icsb_ifree = count + resid; |
| 2153 | break; |
| 2154 | case XFS_SBS_FDBLOCKS: |
| 2155 | cntp->icsb_fdblocks = count + resid; |
| 2156 | break; |
| 2157 | default: |
| 2158 | BUG(); |
| 2159 | break; |
| 2160 | } |
| 2161 | resid = 0; |
| 2162 | } |
| 2163 | clear_bit(field, &mp->m_icsb_counters); |
| 2164 | xfs_icsb_unlock_all_counters(mp); |
| 2165 | } |
| 2166 | |
David Chinner | dbcabad | 2007-02-10 18:36:17 +1100 | [diff] [blame] | 2167 | void |
Christoph Hellwig | d4d90b5 | 2008-04-22 17:34:37 +1000 | [diff] [blame] | 2168 | xfs_icsb_sync_counters_locked( |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2169 | xfs_mount_t *mp, |
| 2170 | int flags) |
| 2171 | { |
| 2172 | xfs_icsb_cnts_t cnt; |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2173 | |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2174 | xfs_icsb_count(mp, &cnt, flags); |
| 2175 | |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2176 | if (!xfs_icsb_counter_disabled(mp, XFS_SBS_ICOUNT)) |
| 2177 | mp->m_sb.sb_icount = cnt.icsb_icount; |
| 2178 | if (!xfs_icsb_counter_disabled(mp, XFS_SBS_IFREE)) |
| 2179 | mp->m_sb.sb_ifree = cnt.icsb_ifree; |
| 2180 | if (!xfs_icsb_counter_disabled(mp, XFS_SBS_FDBLOCKS)) |
| 2181 | mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks; |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2182 | } |
| 2183 | |
| 2184 | /* |
| 2185 | * Accurate update of per-cpu counters to incore superblock |
| 2186 | */ |
Christoph Hellwig | d4d90b5 | 2008-04-22 17:34:37 +1000 | [diff] [blame] | 2187 | void |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2188 | xfs_icsb_sync_counters( |
Christoph Hellwig | d4d90b5 | 2008-04-22 17:34:37 +1000 | [diff] [blame] | 2189 | xfs_mount_t *mp, |
| 2190 | int flags) |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2191 | { |
Christoph Hellwig | d4d90b5 | 2008-04-22 17:34:37 +1000 | [diff] [blame] | 2192 | spin_lock(&mp->m_sb_lock); |
| 2193 | xfs_icsb_sync_counters_locked(mp, flags); |
| 2194 | spin_unlock(&mp->m_sb_lock); |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2195 | } |
| 2196 | |
| 2197 | /* |
| 2198 | * Balance and enable/disable counters as necessary. |
| 2199 | * |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 2200 | * Thresholds for re-enabling counters are somewhat magic. inode counts are |
| 2201 | * chosen to be the same number as single on disk allocation chunk per CPU, and |
| 2202 | * free blocks is something far enough zero that we aren't going thrash when we |
| 2203 | * get near ENOSPC. We also need to supply a minimum we require per cpu to |
| 2204 | * prevent looping endlessly when xfs_alloc_space asks for more than will |
| 2205 | * be distributed to a single CPU but each CPU has enough blocks to be |
| 2206 | * reenabled. |
| 2207 | * |
| 2208 | * Note that we can be called when counters are already disabled. |
| 2209 | * xfs_icsb_disable_counter() optimises the counter locking in this case to |
| 2210 | * prevent locking every per-cpu counter needlessly. |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2211 | */ |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 2212 | |
| 2213 | #define XFS_ICSB_INO_CNTR_REENABLE (uint64_t)64 |
David Chinner | 4be536d | 2006-09-07 14:26:50 +1000 | [diff] [blame] | 2214 | #define XFS_ICSB_FDBLK_CNTR_REENABLE(mp) \ |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 2215 | (uint64_t)(512 + XFS_ALLOC_SET_ASIDE(mp)) |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2216 | STATIC void |
Christoph Hellwig | 45af6c6 | 2008-04-22 17:34:44 +1000 | [diff] [blame] | 2217 | xfs_icsb_balance_counter_locked( |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2218 | xfs_mount_t *mp, |
| 2219 | xfs_sb_field_t field, |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 2220 | int min_per_cpu) |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2221 | { |
Nathan Scott | 6fdf8cc | 2006-06-28 10:13:52 +1000 | [diff] [blame] | 2222 | uint64_t count, resid; |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2223 | int weight = num_online_cpus(); |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 2224 | uint64_t min = (uint64_t)min_per_cpu; |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2225 | |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2226 | /* disable counter and sync counter */ |
| 2227 | xfs_icsb_disable_counter(mp, field); |
| 2228 | |
| 2229 | /* update counters - first CPU gets residual*/ |
| 2230 | switch (field) { |
| 2231 | case XFS_SBS_ICOUNT: |
| 2232 | count = mp->m_sb.sb_icount; |
| 2233 | resid = do_div(count, weight); |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 2234 | if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE)) |
Christoph Hellwig | 45af6c6 | 2008-04-22 17:34:44 +1000 | [diff] [blame] | 2235 | return; |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2236 | break; |
| 2237 | case XFS_SBS_IFREE: |
| 2238 | count = mp->m_sb.sb_ifree; |
| 2239 | resid = do_div(count, weight); |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 2240 | if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE)) |
Christoph Hellwig | 45af6c6 | 2008-04-22 17:34:44 +1000 | [diff] [blame] | 2241 | return; |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2242 | break; |
| 2243 | case XFS_SBS_FDBLOCKS: |
| 2244 | count = mp->m_sb.sb_fdblocks; |
| 2245 | resid = do_div(count, weight); |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 2246 | if (count < max(min, XFS_ICSB_FDBLK_CNTR_REENABLE(mp))) |
Christoph Hellwig | 45af6c6 | 2008-04-22 17:34:44 +1000 | [diff] [blame] | 2247 | return; |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2248 | break; |
| 2249 | default: |
| 2250 | BUG(); |
Nathan Scott | 6fdf8cc | 2006-06-28 10:13:52 +1000 | [diff] [blame] | 2251 | count = resid = 0; /* quiet, gcc */ |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2252 | break; |
| 2253 | } |
| 2254 | |
| 2255 | xfs_icsb_enable_counter(mp, field, count, resid); |
Christoph Hellwig | 45af6c6 | 2008-04-22 17:34:44 +1000 | [diff] [blame] | 2256 | } |
| 2257 | |
| 2258 | STATIC void |
| 2259 | xfs_icsb_balance_counter( |
| 2260 | xfs_mount_t *mp, |
| 2261 | xfs_sb_field_t fields, |
| 2262 | int min_per_cpu) |
| 2263 | { |
| 2264 | spin_lock(&mp->m_sb_lock); |
| 2265 | xfs_icsb_balance_counter_locked(mp, fields, min_per_cpu); |
| 2266 | spin_unlock(&mp->m_sb_lock); |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2267 | } |
| 2268 | |
David Chinner | a8272ce | 2007-11-23 16:28:09 +1100 | [diff] [blame] | 2269 | STATIC int |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 2270 | xfs_icsb_modify_counters( |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2271 | xfs_mount_t *mp, |
| 2272 | xfs_sb_field_t field, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 2273 | int64_t delta, |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 2274 | int rsvd) |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2275 | { |
| 2276 | xfs_icsb_cnts_t *icsbp; |
| 2277 | long long lcounter; /* long counter for 64 bit fields */ |
Eric Sandeen | 007c61c | 2007-10-11 17:43:56 +1000 | [diff] [blame] | 2278 | int cpu, ret = 0; |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2279 | |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 2280 | might_sleep(); |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2281 | again: |
| 2282 | cpu = get_cpu(); |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 2283 | icsbp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, cpu); |
| 2284 | |
| 2285 | /* |
| 2286 | * if the counter is disabled, go to slow path |
| 2287 | */ |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2288 | if (unlikely(xfs_icsb_counter_disabled(mp, field))) |
| 2289 | goto slow_path; |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 2290 | xfs_icsb_lock_cntr(icsbp); |
| 2291 | if (unlikely(xfs_icsb_counter_disabled(mp, field))) { |
| 2292 | xfs_icsb_unlock_cntr(icsbp); |
| 2293 | goto slow_path; |
| 2294 | } |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2295 | |
| 2296 | switch (field) { |
| 2297 | case XFS_SBS_ICOUNT: |
| 2298 | lcounter = icsbp->icsb_icount; |
| 2299 | lcounter += delta; |
| 2300 | if (unlikely(lcounter < 0)) |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 2301 | goto balance_counter; |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2302 | icsbp->icsb_icount = lcounter; |
| 2303 | break; |
| 2304 | |
| 2305 | case XFS_SBS_IFREE: |
| 2306 | lcounter = icsbp->icsb_ifree; |
| 2307 | lcounter += delta; |
| 2308 | if (unlikely(lcounter < 0)) |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 2309 | goto balance_counter; |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2310 | icsbp->icsb_ifree = lcounter; |
| 2311 | break; |
| 2312 | |
| 2313 | case XFS_SBS_FDBLOCKS: |
| 2314 | BUG_ON((mp->m_resblks - mp->m_resblks_avail) != 0); |
| 2315 | |
David Chinner | 4be536d | 2006-09-07 14:26:50 +1000 | [diff] [blame] | 2316 | lcounter = icsbp->icsb_fdblocks - XFS_ALLOC_SET_ASIDE(mp); |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2317 | lcounter += delta; |
| 2318 | if (unlikely(lcounter < 0)) |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 2319 | goto balance_counter; |
David Chinner | 4be536d | 2006-09-07 14:26:50 +1000 | [diff] [blame] | 2320 | icsbp->icsb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp); |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2321 | break; |
| 2322 | default: |
| 2323 | BUG(); |
| 2324 | break; |
| 2325 | } |
David Chinner | 01e1b69 | 2006-03-14 13:29:16 +1100 | [diff] [blame] | 2326 | xfs_icsb_unlock_cntr(icsbp); |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2327 | put_cpu(); |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2328 | return 0; |
| 2329 | |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2330 | slow_path: |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 2331 | put_cpu(); |
| 2332 | |
| 2333 | /* |
| 2334 | * serialise with a mutex so we don't burn lots of cpu on |
| 2335 | * the superblock lock. We still need to hold the superblock |
| 2336 | * lock, however, when we modify the global structures. |
| 2337 | */ |
David Chinner | 03135cf | 2007-02-10 18:35:15 +1100 | [diff] [blame] | 2338 | xfs_icsb_lock(mp); |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 2339 | |
| 2340 | /* |
| 2341 | * Now running atomically. |
| 2342 | * |
| 2343 | * If the counter is enabled, someone has beaten us to rebalancing. |
| 2344 | * Drop the lock and try again in the fast path.... |
| 2345 | */ |
| 2346 | if (!(xfs_icsb_counter_disabled(mp, field))) { |
David Chinner | 03135cf | 2007-02-10 18:35:15 +1100 | [diff] [blame] | 2347 | xfs_icsb_unlock(mp); |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 2348 | goto again; |
| 2349 | } |
| 2350 | |
| 2351 | /* |
| 2352 | * The counter is currently disabled. Because we are |
| 2353 | * running atomically here, we know a rebalance cannot |
| 2354 | * be in progress. Hence we can go straight to operating |
| 2355 | * on the global superblock. We do not call xfs_mod_incore_sb() |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 2356 | * here even though we need to get the m_sb_lock. Doing so |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 2357 | * will cause us to re-enter this function and deadlock. |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 2358 | * Hence we get the m_sb_lock ourselves and then call |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 2359 | * xfs_mod_incore_sb_unlocked() as the unlocked path operates |
| 2360 | * directly on the global counters. |
| 2361 | */ |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 2362 | spin_lock(&mp->m_sb_lock); |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 2363 | ret = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd); |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 2364 | spin_unlock(&mp->m_sb_lock); |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 2365 | |
| 2366 | /* |
| 2367 | * Now that we've modified the global superblock, we |
| 2368 | * may be able to re-enable the distributed counters |
| 2369 | * (e.g. lots of space just got freed). After that |
| 2370 | * we are done. |
| 2371 | */ |
| 2372 | if (ret != ENOSPC) |
Christoph Hellwig | 45af6c6 | 2008-04-22 17:34:44 +1000 | [diff] [blame] | 2373 | xfs_icsb_balance_counter(mp, field, 0); |
David Chinner | 03135cf | 2007-02-10 18:35:15 +1100 | [diff] [blame] | 2374 | xfs_icsb_unlock(mp); |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 2375 | return ret; |
| 2376 | |
| 2377 | balance_counter: |
David Chinner | 01e1b69 | 2006-03-14 13:29:16 +1100 | [diff] [blame] | 2378 | xfs_icsb_unlock_cntr(icsbp); |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2379 | put_cpu(); |
| 2380 | |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 2381 | /* |
| 2382 | * We may have multiple threads here if multiple per-cpu |
| 2383 | * counters run dry at the same time. This will mean we can |
| 2384 | * do more balances than strictly necessary but it is not |
| 2385 | * the common slowpath case. |
| 2386 | */ |
David Chinner | 03135cf | 2007-02-10 18:35:15 +1100 | [diff] [blame] | 2387 | xfs_icsb_lock(mp); |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2388 | |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 2389 | /* |
| 2390 | * running atomically. |
| 2391 | * |
| 2392 | * This will leave the counter in the correct state for future |
| 2393 | * accesses. After the rebalance, we simply try again and our retry |
| 2394 | * will either succeed through the fast path or slow path without |
| 2395 | * another balance operation being required. |
| 2396 | */ |
Christoph Hellwig | 45af6c6 | 2008-04-22 17:34:44 +1000 | [diff] [blame] | 2397 | xfs_icsb_balance_counter(mp, field, delta); |
David Chinner | 03135cf | 2007-02-10 18:35:15 +1100 | [diff] [blame] | 2398 | xfs_icsb_unlock(mp); |
David Chinner | 20b6428 | 2007-02-10 18:35:09 +1100 | [diff] [blame] | 2399 | goto again; |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2400 | } |
| 2401 | |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 2402 | #endif |