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" |
| 27 | #include "xfs_dir.h" |
| 28 | #include "xfs_dir2.h" |
| 29 | #include "xfs_dmapi.h" |
| 30 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_bmap_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 32 | #include "xfs_alloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "xfs_ialloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_dir_sf.h" |
| 35 | #include "xfs_dir2_sf.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 36 | #include "xfs_attr_sf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include "xfs_dinode.h" |
| 38 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 39 | #include "xfs_btree.h" |
| 40 | #include "xfs_ialloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include "xfs_alloc.h" |
| 42 | #include "xfs_rtalloc.h" |
| 43 | #include "xfs_bmap.h" |
| 44 | #include "xfs_error.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include "xfs_rw.h" |
| 46 | #include "xfs_quota.h" |
| 47 | #include "xfs_fsops.h" |
| 48 | |
| 49 | STATIC void xfs_mount_log_sbunit(xfs_mount_t *, __int64_t); |
| 50 | STATIC int xfs_uuid_mount(xfs_mount_t *); |
| 51 | STATIC void xfs_uuid_unmount(xfs_mount_t *mp); |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 52 | STATIC void xfs_unmountfs_wait(xfs_mount_t *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 54 | |
| 55 | #ifdef HAVE_PERCPU_SB |
| 56 | STATIC void xfs_icsb_destroy_counters(xfs_mount_t *); |
| 57 | STATIC void xfs_icsb_balance_counter(xfs_mount_t *, xfs_sb_field_t, int); |
| 58 | STATIC void xfs_icsb_sync_counters(xfs_mount_t *); |
| 59 | STATIC int xfs_icsb_modify_counters(xfs_mount_t *, xfs_sb_field_t, |
| 60 | int, int); |
| 61 | STATIC int xfs_icsb_modify_counters_locked(xfs_mount_t *, xfs_sb_field_t, |
| 62 | int, int); |
| 63 | |
| 64 | #else |
| 65 | |
| 66 | #define xfs_icsb_destroy_counters(mp) do { } while (0) |
| 67 | #define xfs_icsb_balance_counter(mp, a, b) do { } while (0) |
| 68 | #define xfs_icsb_sync_counters(mp) do { } while (0) |
| 69 | #define xfs_icsb_modify_counters(mp, a, b, c) do { } while (0) |
| 70 | #define xfs_icsb_modify_counters_locked(mp, a, b, c) do { } while (0) |
| 71 | |
| 72 | #endif |
| 73 | |
Christoph Hellwig | 1df84c9 | 2006-01-11 15:29:52 +1100 | [diff] [blame] | 74 | static const struct { |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 75 | short offset; |
| 76 | short type; /* 0 = integer |
| 77 | * 1 = binary / string (no translation) |
| 78 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | } xfs_sb_info[] = { |
| 80 | { offsetof(xfs_sb_t, sb_magicnum), 0 }, |
| 81 | { offsetof(xfs_sb_t, sb_blocksize), 0 }, |
| 82 | { offsetof(xfs_sb_t, sb_dblocks), 0 }, |
| 83 | { offsetof(xfs_sb_t, sb_rblocks), 0 }, |
| 84 | { offsetof(xfs_sb_t, sb_rextents), 0 }, |
| 85 | { offsetof(xfs_sb_t, sb_uuid), 1 }, |
| 86 | { offsetof(xfs_sb_t, sb_logstart), 0 }, |
| 87 | { offsetof(xfs_sb_t, sb_rootino), 0 }, |
| 88 | { offsetof(xfs_sb_t, sb_rbmino), 0 }, |
| 89 | { offsetof(xfs_sb_t, sb_rsumino), 0 }, |
| 90 | { offsetof(xfs_sb_t, sb_rextsize), 0 }, |
| 91 | { offsetof(xfs_sb_t, sb_agblocks), 0 }, |
| 92 | { offsetof(xfs_sb_t, sb_agcount), 0 }, |
| 93 | { offsetof(xfs_sb_t, sb_rbmblocks), 0 }, |
| 94 | { offsetof(xfs_sb_t, sb_logblocks), 0 }, |
| 95 | { offsetof(xfs_sb_t, sb_versionnum), 0 }, |
| 96 | { offsetof(xfs_sb_t, sb_sectsize), 0 }, |
| 97 | { offsetof(xfs_sb_t, sb_inodesize), 0 }, |
| 98 | { offsetof(xfs_sb_t, sb_inopblock), 0 }, |
| 99 | { offsetof(xfs_sb_t, sb_fname[0]), 1 }, |
| 100 | { offsetof(xfs_sb_t, sb_blocklog), 0 }, |
| 101 | { offsetof(xfs_sb_t, sb_sectlog), 0 }, |
| 102 | { offsetof(xfs_sb_t, sb_inodelog), 0 }, |
| 103 | { offsetof(xfs_sb_t, sb_inopblog), 0 }, |
| 104 | { offsetof(xfs_sb_t, sb_agblklog), 0 }, |
| 105 | { offsetof(xfs_sb_t, sb_rextslog), 0 }, |
| 106 | { offsetof(xfs_sb_t, sb_inprogress), 0 }, |
| 107 | { offsetof(xfs_sb_t, sb_imax_pct), 0 }, |
| 108 | { offsetof(xfs_sb_t, sb_icount), 0 }, |
| 109 | { offsetof(xfs_sb_t, sb_ifree), 0 }, |
| 110 | { offsetof(xfs_sb_t, sb_fdblocks), 0 }, |
| 111 | { offsetof(xfs_sb_t, sb_frextents), 0 }, |
| 112 | { offsetof(xfs_sb_t, sb_uquotino), 0 }, |
| 113 | { offsetof(xfs_sb_t, sb_gquotino), 0 }, |
| 114 | { offsetof(xfs_sb_t, sb_qflags), 0 }, |
| 115 | { offsetof(xfs_sb_t, sb_flags), 0 }, |
| 116 | { offsetof(xfs_sb_t, sb_shared_vn), 0 }, |
| 117 | { offsetof(xfs_sb_t, sb_inoalignmt), 0 }, |
| 118 | { offsetof(xfs_sb_t, sb_unit), 0 }, |
| 119 | { offsetof(xfs_sb_t, sb_width), 0 }, |
| 120 | { offsetof(xfs_sb_t, sb_dirblklog), 0 }, |
| 121 | { offsetof(xfs_sb_t, sb_logsectlog), 0 }, |
| 122 | { offsetof(xfs_sb_t, sb_logsectsize),0 }, |
| 123 | { offsetof(xfs_sb_t, sb_logsunit), 0 }, |
| 124 | { offsetof(xfs_sb_t, sb_features2), 0 }, |
| 125 | { sizeof(xfs_sb_t), 0 } |
| 126 | }; |
| 127 | |
| 128 | /* |
| 129 | * Return a pointer to an initialized xfs_mount structure. |
| 130 | */ |
| 131 | xfs_mount_t * |
| 132 | xfs_mount_init(void) |
| 133 | { |
| 134 | xfs_mount_t *mp; |
| 135 | |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 136 | mp = kmem_zalloc(sizeof(xfs_mount_t), KM_SLEEP); |
| 137 | |
| 138 | if (xfs_icsb_init_counters(mp)) { |
| 139 | mp->m_flags |= XFS_MOUNT_NO_PERCPU_SB; |
| 140 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | AIL_LOCKINIT(&mp->m_ail_lock, "xfs_ail"); |
| 143 | spinlock_init(&mp->m_sb_lock, "xfs_sb"); |
Jes Sorensen | 794ee1b | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 144 | mutex_init(&mp->m_ilock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | initnsema(&mp->m_growlock, 1, "xfs_grow"); |
| 146 | /* |
| 147 | * Initialize the AIL. |
| 148 | */ |
| 149 | xfs_trans_ail_init(mp); |
| 150 | |
| 151 | atomic_set(&mp->m_active_trans, 0); |
| 152 | |
| 153 | return mp; |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * Free up the resources associated with a mount structure. Assume that |
| 158 | * the structure was initially zeroed, so we can tell which fields got |
| 159 | * initialized. |
| 160 | */ |
| 161 | void |
| 162 | xfs_mount_free( |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 163 | xfs_mount_t *mp, |
| 164 | int remove_bhv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | { |
| 166 | if (mp->m_ihash) |
| 167 | xfs_ihash_free(mp); |
| 168 | if (mp->m_chash) |
| 169 | xfs_chash_free(mp); |
| 170 | |
| 171 | if (mp->m_perag) { |
| 172 | int agno; |
| 173 | |
| 174 | for (agno = 0; agno < mp->m_maxagi; agno++) |
| 175 | if (mp->m_perag[agno].pagb_list) |
| 176 | kmem_free(mp->m_perag[agno].pagb_list, |
| 177 | sizeof(xfs_perag_busy_t) * |
| 178 | XFS_PAGB_NUM_SLOTS); |
| 179 | kmem_free(mp->m_perag, |
| 180 | sizeof(xfs_perag_t) * mp->m_sb.sb_agcount); |
| 181 | } |
| 182 | |
| 183 | AIL_LOCK_DESTROY(&mp->m_ail_lock); |
| 184 | spinlock_destroy(&mp->m_sb_lock); |
| 185 | mutex_destroy(&mp->m_ilock); |
| 186 | freesema(&mp->m_growlock); |
| 187 | if (mp->m_quotainfo) |
| 188 | XFS_QM_DONE(mp); |
| 189 | |
| 190 | if (mp->m_fsname != NULL) |
| 191 | kmem_free(mp->m_fsname, mp->m_fsname_len); |
Nathan Scott | fc1f8c1 | 2005-11-02 11:44:33 +1100 | [diff] [blame] | 192 | if (mp->m_rtname != NULL) |
| 193 | kmem_free(mp->m_rtname, strlen(mp->m_rtname) + 1); |
| 194 | if (mp->m_logname != NULL) |
| 195 | kmem_free(mp->m_logname, strlen(mp->m_logname) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
| 197 | if (remove_bhv) { |
| 198 | struct vfs *vfsp = XFS_MTOVFS(mp); |
| 199 | |
| 200 | bhv_remove_all_vfsops(vfsp, 0); |
| 201 | VFS_REMOVEBHV(vfsp, &mp->m_bhv); |
| 202 | } |
| 203 | |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 204 | xfs_icsb_destroy_counters(mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | kmem_free(mp, sizeof(xfs_mount_t)); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | /* |
| 210 | * Check the validity of the SB found. |
| 211 | */ |
| 212 | STATIC int |
| 213 | xfs_mount_validate_sb( |
| 214 | xfs_mount_t *mp, |
| 215 | xfs_sb_t *sbp) |
| 216 | { |
| 217 | /* |
| 218 | * If the log device and data device have the |
| 219 | * same device number, the log is internal. |
| 220 | * Consequently, the sb_logstart should be non-zero. If |
| 221 | * we have a zero sb_logstart in this case, we may be trying to mount |
| 222 | * a volume filesystem in a non-volume manner. |
| 223 | */ |
| 224 | if (sbp->sb_magicnum != XFS_SB_MAGIC) { |
| 225 | cmn_err(CE_WARN, "XFS: bad magic number"); |
| 226 | return XFS_ERROR(EWRONGFS); |
| 227 | } |
| 228 | |
| 229 | if (!XFS_SB_GOOD_VERSION(sbp)) { |
| 230 | cmn_err(CE_WARN, "XFS: bad version"); |
| 231 | return XFS_ERROR(EWRONGFS); |
| 232 | } |
| 233 | |
| 234 | if (unlikely( |
| 235 | sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) { |
| 236 | cmn_err(CE_WARN, |
| 237 | "XFS: filesystem is marked as having an external log; " |
| 238 | "specify logdev on the\nmount command line."); |
| 239 | XFS_CORRUPTION_ERROR("xfs_mount_validate_sb(1)", |
| 240 | XFS_ERRLEVEL_HIGH, mp, sbp); |
| 241 | return XFS_ERROR(EFSCORRUPTED); |
| 242 | } |
| 243 | |
| 244 | if (unlikely( |
| 245 | sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) { |
| 246 | cmn_err(CE_WARN, |
| 247 | "XFS: filesystem is marked as having an internal log; " |
| 248 | "don't specify logdev on\nthe mount command line."); |
| 249 | XFS_CORRUPTION_ERROR("xfs_mount_validate_sb(2)", |
| 250 | XFS_ERRLEVEL_HIGH, mp, sbp); |
| 251 | return XFS_ERROR(EFSCORRUPTED); |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * More sanity checking. These were stolen directly from |
| 256 | * xfs_repair. |
| 257 | */ |
| 258 | if (unlikely( |
| 259 | sbp->sb_agcount <= 0 || |
| 260 | sbp->sb_sectsize < XFS_MIN_SECTORSIZE || |
| 261 | sbp->sb_sectsize > XFS_MAX_SECTORSIZE || |
| 262 | sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG || |
| 263 | sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG || |
| 264 | sbp->sb_blocksize < XFS_MIN_BLOCKSIZE || |
| 265 | sbp->sb_blocksize > XFS_MAX_BLOCKSIZE || |
| 266 | sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG || |
| 267 | sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG || |
| 268 | sbp->sb_inodesize < XFS_DINODE_MIN_SIZE || |
| 269 | sbp->sb_inodesize > XFS_DINODE_MAX_SIZE || |
| 270 | (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) || |
| 271 | (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) || |
| 272 | sbp->sb_imax_pct > 100)) { |
| 273 | cmn_err(CE_WARN, "XFS: SB sanity check 1 failed"); |
| 274 | XFS_CORRUPTION_ERROR("xfs_mount_validate_sb(3)", |
| 275 | XFS_ERRLEVEL_LOW, mp, sbp); |
| 276 | return XFS_ERROR(EFSCORRUPTED); |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * Sanity check AG count, size fields against data size field |
| 281 | */ |
| 282 | if (unlikely( |
| 283 | sbp->sb_dblocks == 0 || |
| 284 | sbp->sb_dblocks > |
| 285 | (xfs_drfsbno_t)sbp->sb_agcount * sbp->sb_agblocks || |
| 286 | sbp->sb_dblocks < (xfs_drfsbno_t)(sbp->sb_agcount - 1) * |
| 287 | sbp->sb_agblocks + XFS_MIN_AG_BLOCKS)) { |
| 288 | cmn_err(CE_WARN, "XFS: SB sanity check 2 failed"); |
| 289 | XFS_ERROR_REPORT("xfs_mount_validate_sb(4)", |
| 290 | XFS_ERRLEVEL_LOW, mp); |
| 291 | return XFS_ERROR(EFSCORRUPTED); |
| 292 | } |
| 293 | |
| 294 | ASSERT(PAGE_SHIFT >= sbp->sb_blocklog); |
| 295 | ASSERT(sbp->sb_blocklog >= BBSHIFT); |
| 296 | |
| 297 | #if XFS_BIG_BLKNOS /* Limited by ULONG_MAX of page cache index */ |
| 298 | if (unlikely( |
| 299 | (sbp->sb_dblocks >> (PAGE_SHIFT - sbp->sb_blocklog)) > ULONG_MAX || |
| 300 | (sbp->sb_rblocks >> (PAGE_SHIFT - sbp->sb_blocklog)) > ULONG_MAX)) { |
| 301 | #else /* Limited by UINT_MAX of sectors */ |
| 302 | if (unlikely( |
| 303 | (sbp->sb_dblocks << (sbp->sb_blocklog - BBSHIFT)) > UINT_MAX || |
| 304 | (sbp->sb_rblocks << (sbp->sb_blocklog - BBSHIFT)) > UINT_MAX)) { |
| 305 | #endif |
| 306 | cmn_err(CE_WARN, |
| 307 | "XFS: File system is too large to be mounted on this system."); |
| 308 | return XFS_ERROR(E2BIG); |
| 309 | } |
| 310 | |
| 311 | if (unlikely(sbp->sb_inprogress)) { |
| 312 | cmn_err(CE_WARN, "XFS: file system busy"); |
| 313 | XFS_ERROR_REPORT("xfs_mount_validate_sb(5)", |
| 314 | XFS_ERRLEVEL_LOW, mp); |
| 315 | return XFS_ERROR(EFSCORRUPTED); |
| 316 | } |
| 317 | |
| 318 | /* |
Nathan Scott | de20614 | 2005-05-05 13:24:13 -0700 | [diff] [blame] | 319 | * Version 1 directory format has never worked on Linux. |
| 320 | */ |
| 321 | if (unlikely(!XFS_SB_VERSION_HASDIRV2(sbp))) { |
| 322 | cmn_err(CE_WARN, |
| 323 | "XFS: Attempted to mount file system using version 1 directory format"); |
| 324 | return XFS_ERROR(ENOSYS); |
| 325 | } |
| 326 | |
| 327 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | * Until this is fixed only page-sized or smaller data blocks work. |
| 329 | */ |
| 330 | if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) { |
| 331 | cmn_err(CE_WARN, |
| 332 | "XFS: Attempted to mount file system with blocksize %d bytes", |
| 333 | sbp->sb_blocksize); |
| 334 | cmn_err(CE_WARN, |
Christoph Hellwig | da1650a | 2005-11-02 10:21:35 +1100 | [diff] [blame] | 335 | "XFS: Only page-sized (%ld) or less blocksizes currently work.", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | PAGE_SIZE); |
| 337 | return XFS_ERROR(ENOSYS); |
| 338 | } |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | xfs_agnumber_t |
Nathan Scott | c11e2c3 | 2005-11-02 15:11:45 +1100 | [diff] [blame] | 344 | xfs_initialize_perag( |
| 345 | struct vfs *vfs, |
| 346 | xfs_mount_t *mp, |
| 347 | xfs_agnumber_t agcount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | { |
| 349 | xfs_agnumber_t index, max_metadata; |
| 350 | xfs_perag_t *pag; |
| 351 | xfs_agino_t agino; |
| 352 | xfs_ino_t ino; |
| 353 | xfs_sb_t *sbp = &mp->m_sb; |
| 354 | xfs_ino_t max_inum = XFS_MAXINUMBER_32; |
| 355 | |
| 356 | /* Check to see if the filesystem can overflow 32 bit inodes */ |
| 357 | agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0); |
| 358 | ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino); |
| 359 | |
| 360 | /* Clear the mount flag if no inode can overflow 32 bits |
| 361 | * on this filesystem, or if specifically requested.. |
| 362 | */ |
Nathan Scott | c11e2c3 | 2005-11-02 15:11:45 +1100 | [diff] [blame] | 363 | if ((vfs->vfs_flag & VFS_32BITINODES) && ino > max_inum) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | mp->m_flags |= XFS_MOUNT_32BITINODES; |
| 365 | } else { |
| 366 | mp->m_flags &= ~XFS_MOUNT_32BITINODES; |
| 367 | } |
| 368 | |
| 369 | /* If we can overflow then setup the ag headers accordingly */ |
| 370 | if (mp->m_flags & XFS_MOUNT_32BITINODES) { |
| 371 | /* Calculate how much should be reserved for inodes to |
| 372 | * meet the max inode percentage. |
| 373 | */ |
| 374 | if (mp->m_maxicount) { |
| 375 | __uint64_t icount; |
| 376 | |
| 377 | icount = sbp->sb_dblocks * sbp->sb_imax_pct; |
| 378 | do_div(icount, 100); |
| 379 | icount += sbp->sb_agblocks - 1; |
Eric Sandeen | a749ee8 | 2005-11-02 15:13:42 +1100 | [diff] [blame] | 380 | do_div(icount, sbp->sb_agblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | max_metadata = icount; |
| 382 | } else { |
| 383 | max_metadata = agcount; |
| 384 | } |
| 385 | for (index = 0; index < agcount; index++) { |
| 386 | ino = XFS_AGINO_TO_INO(mp, index, agino); |
| 387 | if (ino > max_inum) { |
| 388 | index++; |
| 389 | break; |
| 390 | } |
| 391 | |
| 392 | /* This ag is prefered for inodes */ |
| 393 | pag = &mp->m_perag[index]; |
| 394 | pag->pagi_inodeok = 1; |
| 395 | if (index < max_metadata) |
| 396 | pag->pagf_metadata = 1; |
| 397 | } |
| 398 | } else { |
| 399 | /* Setup default behavior for smaller filesystems */ |
| 400 | for (index = 0; index < agcount; index++) { |
| 401 | pag = &mp->m_perag[index]; |
| 402 | pag->pagi_inodeok = 1; |
| 403 | } |
| 404 | } |
| 405 | return index; |
| 406 | } |
| 407 | |
| 408 | /* |
| 409 | * xfs_xlatesb |
| 410 | * |
| 411 | * data - on disk version of sb |
| 412 | * sb - a superblock |
| 413 | * dir - conversion direction: <0 - convert sb to buf |
| 414 | * >0 - convert buf to sb |
| 415 | * fields - which fields to copy (bitmask) |
| 416 | */ |
| 417 | void |
| 418 | xfs_xlatesb( |
| 419 | void *data, |
| 420 | xfs_sb_t *sb, |
| 421 | int dir, |
| 422 | __int64_t fields) |
| 423 | { |
| 424 | xfs_caddr_t buf_ptr; |
| 425 | xfs_caddr_t mem_ptr; |
| 426 | xfs_sb_field_t f; |
| 427 | int first; |
| 428 | int size; |
| 429 | |
| 430 | ASSERT(dir); |
| 431 | ASSERT(fields); |
| 432 | |
| 433 | if (!fields) |
| 434 | return; |
| 435 | |
| 436 | buf_ptr = (xfs_caddr_t)data; |
| 437 | mem_ptr = (xfs_caddr_t)sb; |
| 438 | |
| 439 | while (fields) { |
| 440 | f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields); |
| 441 | first = xfs_sb_info[f].offset; |
| 442 | size = xfs_sb_info[f + 1].offset - first; |
| 443 | |
| 444 | ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1); |
| 445 | |
| 446 | if (size == 1 || xfs_sb_info[f].type == 1) { |
| 447 | if (dir > 0) { |
| 448 | memcpy(mem_ptr + first, buf_ptr + first, size); |
| 449 | } else { |
| 450 | memcpy(buf_ptr + first, mem_ptr + first, size); |
| 451 | } |
| 452 | } else { |
| 453 | switch (size) { |
| 454 | case 2: |
| 455 | INT_XLATE(*(__uint16_t*)(buf_ptr+first), |
| 456 | *(__uint16_t*)(mem_ptr+first), |
| 457 | dir, ARCH_CONVERT); |
| 458 | break; |
| 459 | case 4: |
| 460 | INT_XLATE(*(__uint32_t*)(buf_ptr+first), |
| 461 | *(__uint32_t*)(mem_ptr+first), |
| 462 | dir, ARCH_CONVERT); |
| 463 | break; |
| 464 | case 8: |
| 465 | INT_XLATE(*(__uint64_t*)(buf_ptr+first), |
| 466 | *(__uint64_t*)(mem_ptr+first), dir, ARCH_CONVERT); |
| 467 | break; |
| 468 | default: |
| 469 | ASSERT(0); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | fields &= ~(1LL << f); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | /* |
| 478 | * xfs_readsb |
| 479 | * |
| 480 | * Does the initial read of the superblock. |
| 481 | */ |
| 482 | int |
| 483 | xfs_readsb(xfs_mount_t *mp) |
| 484 | { |
| 485 | unsigned int sector_size; |
| 486 | unsigned int extra_flags; |
| 487 | xfs_buf_t *bp; |
| 488 | xfs_sb_t *sbp; |
| 489 | int error; |
| 490 | |
| 491 | ASSERT(mp->m_sb_bp == NULL); |
| 492 | ASSERT(mp->m_ddev_targp != NULL); |
| 493 | |
| 494 | /* |
| 495 | * Allocate a (locked) buffer to hold the superblock. |
| 496 | * This will be kept around at all times to optimize |
| 497 | * access to the superblock. |
| 498 | */ |
| 499 | sector_size = xfs_getsize_buftarg(mp->m_ddev_targp); |
| 500 | extra_flags = XFS_BUF_LOCK | XFS_BUF_MANAGE | XFS_BUF_MAPPED; |
| 501 | |
| 502 | bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR, |
| 503 | BTOBB(sector_size), extra_flags); |
| 504 | if (!bp || XFS_BUF_ISERROR(bp)) { |
| 505 | cmn_err(CE_WARN, "XFS: SB read failed"); |
| 506 | error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM; |
| 507 | goto fail; |
| 508 | } |
| 509 | ASSERT(XFS_BUF_ISBUSY(bp)); |
| 510 | ASSERT(XFS_BUF_VALUSEMA(bp) <= 0); |
| 511 | |
| 512 | /* |
| 513 | * Initialize the mount structure from the superblock. |
| 514 | * But first do some basic consistency checking. |
| 515 | */ |
| 516 | sbp = XFS_BUF_TO_SBP(bp); |
| 517 | xfs_xlatesb(XFS_BUF_PTR(bp), &(mp->m_sb), 1, XFS_SB_ALL_BITS); |
| 518 | |
| 519 | error = xfs_mount_validate_sb(mp, &(mp->m_sb)); |
| 520 | if (error) { |
| 521 | cmn_err(CE_WARN, "XFS: SB validate failed"); |
| 522 | goto fail; |
| 523 | } |
| 524 | |
| 525 | /* |
| 526 | * We must be able to do sector-sized and sector-aligned IO. |
| 527 | */ |
| 528 | if (sector_size > mp->m_sb.sb_sectsize) { |
| 529 | cmn_err(CE_WARN, |
| 530 | "XFS: device supports only %u byte sectors (not %u)", |
| 531 | sector_size, mp->m_sb.sb_sectsize); |
| 532 | error = ENOSYS; |
| 533 | goto fail; |
| 534 | } |
| 535 | |
| 536 | /* |
| 537 | * If device sector size is smaller than the superblock size, |
| 538 | * re-read the superblock so the buffer is correctly sized. |
| 539 | */ |
| 540 | if (sector_size < mp->m_sb.sb_sectsize) { |
| 541 | XFS_BUF_UNMANAGE(bp); |
| 542 | xfs_buf_relse(bp); |
| 543 | sector_size = mp->m_sb.sb_sectsize; |
| 544 | bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR, |
| 545 | BTOBB(sector_size), extra_flags); |
| 546 | if (!bp || XFS_BUF_ISERROR(bp)) { |
| 547 | cmn_err(CE_WARN, "XFS: SB re-read failed"); |
| 548 | error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM; |
| 549 | goto fail; |
| 550 | } |
| 551 | ASSERT(XFS_BUF_ISBUSY(bp)); |
| 552 | ASSERT(XFS_BUF_VALUSEMA(bp) <= 0); |
| 553 | } |
| 554 | |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 555 | xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0); |
| 556 | xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0); |
| 557 | xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0); |
| 558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | mp->m_sb_bp = bp; |
| 560 | xfs_buf_relse(bp); |
| 561 | ASSERT(XFS_BUF_VALUSEMA(bp) > 0); |
| 562 | return 0; |
| 563 | |
| 564 | fail: |
| 565 | if (bp) { |
| 566 | XFS_BUF_UNMANAGE(bp); |
| 567 | xfs_buf_relse(bp); |
| 568 | } |
| 569 | return error; |
| 570 | } |
| 571 | |
| 572 | |
| 573 | /* |
| 574 | * xfs_mount_common |
| 575 | * |
| 576 | * Mount initialization code establishing various mount |
| 577 | * fields from the superblock associated with the given |
| 578 | * mount structure |
| 579 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 580 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | xfs_mount_common(xfs_mount_t *mp, xfs_sb_t *sbp) |
| 582 | { |
| 583 | int i; |
| 584 | |
| 585 | mp->m_agfrotor = mp->m_agirotor = 0; |
| 586 | spinlock_init(&mp->m_agirotor_lock, "m_agirotor_lock"); |
| 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; |
| 593 | mp->m_litino = sbp->sb_inodesize - |
| 594 | ((uint)sizeof(xfs_dinode_core_t) + (uint)sizeof(xfs_agino_t)); |
| 595 | mp->m_blockmask = sbp->sb_blocksize - 1; |
| 596 | mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG; |
| 597 | mp->m_blockwmask = mp->m_blockwsize - 1; |
| 598 | INIT_LIST_HEAD(&mp->m_del_inodes); |
| 599 | |
| 600 | /* |
| 601 | * Setup for attributes, in case they get created. |
| 602 | * This value is for inodes getting attributes for the first time, |
| 603 | * the per-inode value is for old attribute values. |
| 604 | */ |
| 605 | ASSERT(sbp->sb_inodesize >= 256 && sbp->sb_inodesize <= 2048); |
| 606 | switch (sbp->sb_inodesize) { |
| 607 | case 256: |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 608 | mp->m_attroffset = XFS_LITINO(mp) - |
| 609 | XFS_BMDR_SPACE_CALC(MINABTPTRS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | break; |
| 611 | case 512: |
| 612 | case 1024: |
| 613 | case 2048: |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 614 | mp->m_attroffset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | break; |
| 616 | default: |
| 617 | ASSERT(0); |
| 618 | } |
| 619 | ASSERT(mp->m_attroffset < XFS_LITINO(mp)); |
| 620 | |
| 621 | for (i = 0; i < 2; i++) { |
| 622 | mp->m_alloc_mxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize, |
| 623 | xfs_alloc, i == 0); |
| 624 | mp->m_alloc_mnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize, |
| 625 | xfs_alloc, i == 0); |
| 626 | } |
| 627 | for (i = 0; i < 2; i++) { |
| 628 | mp->m_bmap_dmxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize, |
| 629 | xfs_bmbt, i == 0); |
| 630 | mp->m_bmap_dmnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize, |
| 631 | xfs_bmbt, i == 0); |
| 632 | } |
| 633 | for (i = 0; i < 2; i++) { |
| 634 | mp->m_inobt_mxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize, |
| 635 | xfs_inobt, i == 0); |
| 636 | mp->m_inobt_mnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize, |
| 637 | xfs_inobt, i == 0); |
| 638 | } |
| 639 | |
| 640 | mp->m_bsize = XFS_FSB_TO_BB(mp, 1); |
| 641 | mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK, |
| 642 | sbp->sb_inopblock); |
| 643 | mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog; |
| 644 | } |
| 645 | /* |
| 646 | * xfs_mountfs |
| 647 | * |
| 648 | * This function does the following on an initial mount of a file system: |
| 649 | * - reads the superblock from disk and init the mount struct |
| 650 | * - if we're a 32-bit kernel, do a size check on the superblock |
| 651 | * so we don't mount terabyte filesystems |
| 652 | * - init mount struct realtime fields |
| 653 | * - allocate inode hash table for fs |
| 654 | * - init directory manager |
| 655 | * - perform recovery and init the log manager |
| 656 | */ |
| 657 | int |
| 658 | xfs_mountfs( |
| 659 | vfs_t *vfsp, |
| 660 | xfs_mount_t *mp, |
| 661 | int mfsi_flags) |
| 662 | { |
| 663 | xfs_buf_t *bp; |
| 664 | xfs_sb_t *sbp = &(mp->m_sb); |
| 665 | xfs_inode_t *rip; |
| 666 | vnode_t *rvp = NULL; |
| 667 | int readio_log, writeio_log; |
| 668 | xfs_daddr_t d; |
| 669 | __uint64_t ret64; |
| 670 | __int64_t update_flags; |
| 671 | uint quotamount, quotaflags; |
| 672 | int agno; |
| 673 | int uuid_mounted = 0; |
| 674 | int error = 0; |
| 675 | |
| 676 | if (mp->m_sb_bp == NULL) { |
| 677 | if ((error = xfs_readsb(mp))) { |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 678 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | xfs_mount_common(mp, sbp); |
| 682 | |
| 683 | /* |
| 684 | * Check if sb_agblocks is aligned at stripe boundary |
| 685 | * If sb_agblocks is NOT aligned turn off m_dalign since |
| 686 | * allocator alignment is within an ag, therefore ag has |
| 687 | * to be aligned at stripe boundary. |
| 688 | */ |
| 689 | update_flags = 0LL; |
| 690 | if (mp->m_dalign && !(mfsi_flags & XFS_MFSI_SECOND)) { |
| 691 | /* |
| 692 | * If stripe unit and stripe width are not multiples |
| 693 | * of the fs blocksize turn off alignment. |
| 694 | */ |
| 695 | if ((BBTOB(mp->m_dalign) & mp->m_blockmask) || |
| 696 | (BBTOB(mp->m_swidth) & mp->m_blockmask)) { |
| 697 | if (mp->m_flags & XFS_MOUNT_RETERR) { |
| 698 | cmn_err(CE_WARN, |
| 699 | "XFS: alignment check 1 failed"); |
| 700 | error = XFS_ERROR(EINVAL); |
| 701 | goto error1; |
| 702 | } |
| 703 | mp->m_dalign = mp->m_swidth = 0; |
| 704 | } else { |
| 705 | /* |
| 706 | * Convert the stripe unit and width to FSBs. |
| 707 | */ |
| 708 | mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign); |
| 709 | if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) { |
| 710 | if (mp->m_flags & XFS_MOUNT_RETERR) { |
| 711 | error = XFS_ERROR(EINVAL); |
| 712 | goto error1; |
| 713 | } |
| 714 | xfs_fs_cmn_err(CE_WARN, mp, |
| 715 | "stripe alignment turned off: sunit(%d)/swidth(%d) incompatible with agsize(%d)", |
| 716 | mp->m_dalign, mp->m_swidth, |
| 717 | sbp->sb_agblocks); |
| 718 | |
| 719 | mp->m_dalign = 0; |
| 720 | mp->m_swidth = 0; |
| 721 | } else if (mp->m_dalign) { |
| 722 | mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth); |
| 723 | } else { |
| 724 | if (mp->m_flags & XFS_MOUNT_RETERR) { |
| 725 | xfs_fs_cmn_err(CE_WARN, mp, |
| 726 | "stripe alignment turned off: sunit(%d) less than bsize(%d)", |
| 727 | mp->m_dalign, |
| 728 | mp->m_blockmask +1); |
| 729 | error = XFS_ERROR(EINVAL); |
| 730 | goto error1; |
| 731 | } |
| 732 | mp->m_swidth = 0; |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | /* |
| 737 | * Update superblock with new values |
| 738 | * and log changes |
| 739 | */ |
| 740 | if (XFS_SB_VERSION_HASDALIGN(sbp)) { |
| 741 | if (sbp->sb_unit != mp->m_dalign) { |
| 742 | sbp->sb_unit = mp->m_dalign; |
| 743 | update_flags |= XFS_SB_UNIT; |
| 744 | } |
| 745 | if (sbp->sb_width != mp->m_swidth) { |
| 746 | sbp->sb_width = mp->m_swidth; |
| 747 | update_flags |= XFS_SB_WIDTH; |
| 748 | } |
| 749 | } |
| 750 | } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN && |
| 751 | XFS_SB_VERSION_HASDALIGN(&mp->m_sb)) { |
| 752 | mp->m_dalign = sbp->sb_unit; |
| 753 | mp->m_swidth = sbp->sb_width; |
| 754 | } |
| 755 | |
| 756 | xfs_alloc_compute_maxlevels(mp); |
| 757 | xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK); |
| 758 | xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK); |
| 759 | xfs_ialloc_compute_maxlevels(mp); |
| 760 | |
| 761 | if (sbp->sb_imax_pct) { |
| 762 | __uint64_t icount; |
| 763 | |
| 764 | /* Make sure the maximum inode count is a multiple of the |
| 765 | * units we allocate inodes in. |
| 766 | */ |
| 767 | |
| 768 | icount = sbp->sb_dblocks * sbp->sb_imax_pct; |
| 769 | do_div(icount, 100); |
| 770 | do_div(icount, mp->m_ialloc_blks); |
| 771 | mp->m_maxicount = (icount * mp->m_ialloc_blks) << |
| 772 | sbp->sb_inopblog; |
| 773 | } else |
| 774 | mp->m_maxicount = 0; |
| 775 | |
| 776 | mp->m_maxioffset = xfs_max_file_offset(sbp->sb_blocklog); |
| 777 | |
| 778 | /* |
| 779 | * XFS uses the uuid from the superblock as the unique |
| 780 | * identifier for fsid. We can not use the uuid from the volume |
| 781 | * since a single partition filesystem is identical to a single |
| 782 | * partition volume/filesystem. |
| 783 | */ |
| 784 | if ((mfsi_flags & XFS_MFSI_SECOND) == 0 && |
| 785 | (mp->m_flags & XFS_MOUNT_NOUUID) == 0) { |
| 786 | if (xfs_uuid_mount(mp)) { |
| 787 | error = XFS_ERROR(EINVAL); |
| 788 | goto error1; |
| 789 | } |
| 790 | uuid_mounted=1; |
| 791 | ret64 = uuid_hash64(&sbp->sb_uuid); |
| 792 | memcpy(&vfsp->vfs_fsid, &ret64, sizeof(ret64)); |
| 793 | } |
| 794 | |
| 795 | /* |
| 796 | * Set the default minimum read and write sizes unless |
| 797 | * already specified in a mount option. |
| 798 | * We use smaller I/O sizes when the file system |
| 799 | * is being used for NFS service (wsync mount option). |
| 800 | */ |
| 801 | if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) { |
| 802 | if (mp->m_flags & XFS_MOUNT_WSYNC) { |
| 803 | readio_log = XFS_WSYNC_READIO_LOG; |
| 804 | writeio_log = XFS_WSYNC_WRITEIO_LOG; |
| 805 | } else { |
| 806 | readio_log = XFS_READIO_LOG_LARGE; |
| 807 | writeio_log = XFS_WRITEIO_LOG_LARGE; |
| 808 | } |
| 809 | } else { |
| 810 | readio_log = mp->m_readio_log; |
| 811 | writeio_log = mp->m_writeio_log; |
| 812 | } |
| 813 | |
| 814 | /* |
| 815 | * Set the number of readahead buffers to use based on |
| 816 | * physical memory size. |
| 817 | */ |
| 818 | if (xfs_physmem <= 4096) /* <= 16MB */ |
| 819 | mp->m_nreadaheads = XFS_RW_NREADAHEAD_16MB; |
| 820 | else if (xfs_physmem <= 8192) /* <= 32MB */ |
| 821 | mp->m_nreadaheads = XFS_RW_NREADAHEAD_32MB; |
| 822 | else |
| 823 | mp->m_nreadaheads = XFS_RW_NREADAHEAD_K32; |
| 824 | if (sbp->sb_blocklog > readio_log) { |
| 825 | mp->m_readio_log = sbp->sb_blocklog; |
| 826 | } else { |
| 827 | mp->m_readio_log = readio_log; |
| 828 | } |
| 829 | mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog); |
| 830 | if (sbp->sb_blocklog > writeio_log) { |
| 831 | mp->m_writeio_log = sbp->sb_blocklog; |
| 832 | } else { |
| 833 | mp->m_writeio_log = writeio_log; |
| 834 | } |
| 835 | mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog); |
| 836 | |
| 837 | /* |
| 838 | * Set the inode cluster size based on the physical memory |
| 839 | * size. This may still be overridden by the file system |
| 840 | * block size if it is larger than the chosen cluster size. |
| 841 | */ |
| 842 | if (xfs_physmem <= btoc(32 * 1024 * 1024)) { /* <= 32 MB */ |
| 843 | mp->m_inode_cluster_size = XFS_INODE_SMALL_CLUSTER_SIZE; |
| 844 | } else { |
| 845 | mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE; |
| 846 | } |
| 847 | /* |
| 848 | * Set whether we're using inode alignment. |
| 849 | */ |
| 850 | if (XFS_SB_VERSION_HASALIGN(&mp->m_sb) && |
| 851 | mp->m_sb.sb_inoalignmt >= |
| 852 | XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size)) |
| 853 | mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1; |
| 854 | else |
| 855 | mp->m_inoalign_mask = 0; |
| 856 | /* |
| 857 | * If we are using stripe alignment, check whether |
| 858 | * the stripe unit is a multiple of the inode alignment |
| 859 | */ |
| 860 | if (mp->m_dalign && mp->m_inoalign_mask && |
| 861 | !(mp->m_dalign & mp->m_inoalign_mask)) |
| 862 | mp->m_sinoalign = mp->m_dalign; |
| 863 | else |
| 864 | mp->m_sinoalign = 0; |
| 865 | /* |
| 866 | * Check that the data (and log if separate) are an ok size. |
| 867 | */ |
| 868 | d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks); |
| 869 | if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) { |
| 870 | cmn_err(CE_WARN, "XFS: size check 1 failed"); |
| 871 | error = XFS_ERROR(E2BIG); |
| 872 | goto error1; |
| 873 | } |
| 874 | error = xfs_read_buf(mp, mp->m_ddev_targp, |
| 875 | d - XFS_FSS_TO_BB(mp, 1), |
| 876 | XFS_FSS_TO_BB(mp, 1), 0, &bp); |
| 877 | if (!error) { |
| 878 | xfs_buf_relse(bp); |
| 879 | } else { |
| 880 | cmn_err(CE_WARN, "XFS: size check 2 failed"); |
| 881 | if (error == ENOSPC) { |
| 882 | error = XFS_ERROR(E2BIG); |
| 883 | } |
| 884 | goto error1; |
| 885 | } |
| 886 | |
| 887 | if (((mfsi_flags & XFS_MFSI_CLIENT) == 0) && |
| 888 | mp->m_logdev_targp != mp->m_ddev_targp) { |
| 889 | d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks); |
| 890 | if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) { |
| 891 | cmn_err(CE_WARN, "XFS: size check 3 failed"); |
| 892 | error = XFS_ERROR(E2BIG); |
| 893 | goto error1; |
| 894 | } |
| 895 | error = xfs_read_buf(mp, mp->m_logdev_targp, |
| 896 | d - XFS_FSB_TO_BB(mp, 1), |
| 897 | XFS_FSB_TO_BB(mp, 1), 0, &bp); |
| 898 | if (!error) { |
| 899 | xfs_buf_relse(bp); |
| 900 | } else { |
| 901 | cmn_err(CE_WARN, "XFS: size check 3 failed"); |
| 902 | if (error == ENOSPC) { |
| 903 | error = XFS_ERROR(E2BIG); |
| 904 | } |
| 905 | goto error1; |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | /* |
| 910 | * Initialize realtime fields in the mount structure |
| 911 | */ |
| 912 | if ((error = xfs_rtmount_init(mp))) { |
| 913 | cmn_err(CE_WARN, "XFS: RT mount failed"); |
| 914 | goto error1; |
| 915 | } |
| 916 | |
| 917 | /* |
| 918 | * For client case we are done now |
| 919 | */ |
| 920 | if (mfsi_flags & XFS_MFSI_CLIENT) { |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 921 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | /* |
| 925 | * Copies the low order bits of the timestamp and the randomly |
| 926 | * set "sequence" number out of a UUID. |
| 927 | */ |
| 928 | uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid); |
| 929 | |
| 930 | /* |
| 931 | * The vfs structure needs to have a file system independent |
| 932 | * way of checking for the invariant file system ID. Since it |
| 933 | * can't look at mount structures it has a pointer to the data |
| 934 | * in the mount structure. |
| 935 | * |
| 936 | * File systems that don't support user level file handles (i.e. |
| 937 | * all of them except for XFS) will leave vfs_altfsid as NULL. |
| 938 | */ |
| 939 | vfsp->vfs_altfsid = (xfs_fsid_t *)mp->m_fixedfsid; |
| 940 | mp->m_dmevmask = 0; /* not persistent; set after each mount */ |
| 941 | |
| 942 | /* |
| 943 | * Select the right directory manager. |
| 944 | */ |
| 945 | mp->m_dirops = |
| 946 | XFS_SB_VERSION_HASDIRV2(&mp->m_sb) ? |
| 947 | xfsv2_dirops : |
| 948 | xfsv1_dirops; |
| 949 | |
| 950 | /* |
| 951 | * Initialize directory manager's entries. |
| 952 | */ |
| 953 | XFS_DIR_MOUNT(mp); |
| 954 | |
| 955 | /* |
| 956 | * Initialize the attribute manager's entries. |
| 957 | */ |
| 958 | mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100; |
| 959 | |
| 960 | /* |
| 961 | * Initialize the precomputed transaction reservations values. |
| 962 | */ |
| 963 | xfs_trans_init(mp); |
| 964 | |
| 965 | /* |
| 966 | * Allocate and initialize the inode hash table for this |
| 967 | * file system. |
| 968 | */ |
| 969 | xfs_ihash_init(mp); |
| 970 | xfs_chash_init(mp); |
| 971 | |
| 972 | /* |
| 973 | * Allocate and initialize the per-ag data. |
| 974 | */ |
| 975 | init_rwsem(&mp->m_peraglock); |
| 976 | mp->m_perag = |
| 977 | kmem_zalloc(sbp->sb_agcount * sizeof(xfs_perag_t), KM_SLEEP); |
| 978 | |
Nathan Scott | c11e2c3 | 2005-11-02 15:11:45 +1100 | [diff] [blame] | 979 | mp->m_maxagi = xfs_initialize_perag(vfsp, mp, sbp->sb_agcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | |
| 981 | /* |
| 982 | * log's mount-time initialization. Perform 1st part recovery if needed |
| 983 | */ |
| 984 | if (likely(sbp->sb_logblocks > 0)) { /* check for volume case */ |
| 985 | error = xfs_log_mount(mp, mp->m_logdev_targp, |
| 986 | XFS_FSB_TO_DADDR(mp, sbp->sb_logstart), |
| 987 | XFS_FSB_TO_BB(mp, sbp->sb_logblocks)); |
| 988 | if (error) { |
| 989 | cmn_err(CE_WARN, "XFS: log mount failed"); |
| 990 | goto error2; |
| 991 | } |
| 992 | } else { /* No log has been defined */ |
| 993 | cmn_err(CE_WARN, "XFS: no log defined"); |
| 994 | XFS_ERROR_REPORT("xfs_mountfs_int(1)", XFS_ERRLEVEL_LOW, mp); |
| 995 | error = XFS_ERROR(EFSCORRUPTED); |
| 996 | goto error2; |
| 997 | } |
| 998 | |
| 999 | /* |
| 1000 | * Get and sanity-check the root inode. |
| 1001 | * Save the pointer to it in the mount structure. |
| 1002 | */ |
| 1003 | error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip, 0); |
| 1004 | if (error) { |
| 1005 | cmn_err(CE_WARN, "XFS: failed to read root inode"); |
| 1006 | goto error3; |
| 1007 | } |
| 1008 | |
| 1009 | ASSERT(rip != NULL); |
| 1010 | rvp = XFS_ITOV(rip); |
| 1011 | |
| 1012 | if (unlikely((rip->i_d.di_mode & S_IFMT) != S_IFDIR)) { |
| 1013 | cmn_err(CE_WARN, "XFS: corrupted root inode"); |
| 1014 | prdev("Root inode %llu is not a directory", |
| 1015 | mp->m_ddev_targp, (unsigned long long)rip->i_ino); |
| 1016 | xfs_iunlock(rip, XFS_ILOCK_EXCL); |
| 1017 | XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW, |
| 1018 | mp); |
| 1019 | error = XFS_ERROR(EFSCORRUPTED); |
| 1020 | goto error4; |
| 1021 | } |
| 1022 | mp->m_rootip = rip; /* save it */ |
| 1023 | |
| 1024 | xfs_iunlock(rip, XFS_ILOCK_EXCL); |
| 1025 | |
| 1026 | /* |
| 1027 | * Initialize realtime inode pointers in the mount structure |
| 1028 | */ |
| 1029 | if ((error = xfs_rtmount_inodes(mp))) { |
| 1030 | /* |
| 1031 | * Free up the root inode. |
| 1032 | */ |
| 1033 | cmn_err(CE_WARN, "XFS: failed to read RT inodes"); |
| 1034 | goto error4; |
| 1035 | } |
| 1036 | |
| 1037 | /* |
| 1038 | * If fs is not mounted readonly, then update the superblock |
| 1039 | * unit and width changes. |
| 1040 | */ |
| 1041 | if (update_flags && !(vfsp->vfs_flag & VFS_RDONLY)) |
| 1042 | xfs_mount_log_sbunit(mp, update_flags); |
| 1043 | |
| 1044 | /* |
| 1045 | * Initialise the XFS quota management subsystem for this mount |
| 1046 | */ |
| 1047 | if ((error = XFS_QM_INIT(mp, "amount, "aflags))) |
| 1048 | goto error4; |
| 1049 | |
| 1050 | /* |
| 1051 | * Finish recovering the file system. This part needed to be |
| 1052 | * delayed until after the root and real-time bitmap inodes |
| 1053 | * were consistently read in. |
| 1054 | */ |
| 1055 | error = xfs_log_mount_finish(mp, mfsi_flags); |
| 1056 | if (error) { |
| 1057 | cmn_err(CE_WARN, "XFS: log mount finish failed"); |
| 1058 | goto error4; |
| 1059 | } |
| 1060 | |
| 1061 | /* |
| 1062 | * Complete the quota initialisation, post-log-replay component. |
| 1063 | */ |
| 1064 | if ((error = XFS_QM_MOUNT(mp, quotamount, quotaflags, mfsi_flags))) |
| 1065 | goto error4; |
| 1066 | |
| 1067 | return 0; |
| 1068 | |
| 1069 | error4: |
| 1070 | /* |
| 1071 | * Free up the root inode. |
| 1072 | */ |
| 1073 | VN_RELE(rvp); |
| 1074 | error3: |
| 1075 | xfs_log_unmount_dealloc(mp); |
| 1076 | error2: |
| 1077 | xfs_ihash_free(mp); |
| 1078 | xfs_chash_free(mp); |
| 1079 | for (agno = 0; agno < sbp->sb_agcount; agno++) |
| 1080 | if (mp->m_perag[agno].pagb_list) |
| 1081 | kmem_free(mp->m_perag[agno].pagb_list, |
| 1082 | sizeof(xfs_perag_busy_t) * XFS_PAGB_NUM_SLOTS); |
| 1083 | kmem_free(mp->m_perag, sbp->sb_agcount * sizeof(xfs_perag_t)); |
| 1084 | mp->m_perag = NULL; |
| 1085 | /* FALLTHROUGH */ |
| 1086 | error1: |
| 1087 | if (uuid_mounted) |
| 1088 | xfs_uuid_unmount(mp); |
| 1089 | xfs_freesb(mp); |
| 1090 | return error; |
| 1091 | } |
| 1092 | |
| 1093 | /* |
| 1094 | * xfs_unmountfs |
| 1095 | * |
| 1096 | * This flushes out the inodes,dquots and the superblock, unmounts the |
| 1097 | * log and makes sure that incore structures are freed. |
| 1098 | */ |
| 1099 | int |
| 1100 | xfs_unmountfs(xfs_mount_t *mp, struct cred *cr) |
| 1101 | { |
| 1102 | struct vfs *vfsp = XFS_MTOVFS(mp); |
| 1103 | #if defined(DEBUG) || defined(INDUCE_IO_ERROR) |
| 1104 | int64_t fsid; |
| 1105 | #endif |
| 1106 | |
Christoph Hellwig | efa8027 | 2005-06-21 15:37:17 +1000 | [diff] [blame] | 1107 | xfs_iflush_all(mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | |
Nathan Scott | ee2a4f7 | 2006-01-11 15:33:36 +1100 | [diff] [blame] | 1109 | XFS_QM_DQPURGEALL(mp, XFS_QMOPT_QUOTALL | XFS_QMOPT_UMOUNTING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | |
| 1111 | /* |
| 1112 | * Flush out the log synchronously so that we know for sure |
| 1113 | * that nothing is pinned. This is important because bflush() |
| 1114 | * will skip pinned buffers. |
| 1115 | */ |
| 1116 | xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC); |
| 1117 | |
| 1118 | xfs_binval(mp->m_ddev_targp); |
| 1119 | if (mp->m_rtdev_targp) { |
| 1120 | xfs_binval(mp->m_rtdev_targp); |
| 1121 | } |
| 1122 | |
| 1123 | xfs_unmountfs_writesb(mp); |
| 1124 | |
| 1125 | xfs_unmountfs_wait(mp); /* wait for async bufs */ |
| 1126 | |
| 1127 | xfs_log_unmount(mp); /* Done! No more fs ops. */ |
| 1128 | |
| 1129 | xfs_freesb(mp); |
| 1130 | |
| 1131 | /* |
| 1132 | * All inodes from this mount point should be freed. |
| 1133 | */ |
| 1134 | ASSERT(mp->m_inodes == NULL); |
| 1135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | xfs_unmountfs_close(mp, cr); |
| 1137 | if ((mp->m_flags & XFS_MOUNT_NOUUID) == 0) |
| 1138 | xfs_uuid_unmount(mp); |
| 1139 | |
| 1140 | #if defined(DEBUG) || defined(INDUCE_IO_ERROR) |
| 1141 | /* |
| 1142 | * clear all error tags on this filesystem |
| 1143 | */ |
| 1144 | memcpy(&fsid, &vfsp->vfs_fsid, sizeof(int64_t)); |
| 1145 | xfs_errortag_clearall_umount(fsid, mp->m_fsname, 0); |
| 1146 | #endif |
| 1147 | XFS_IODONE(vfsp); |
| 1148 | xfs_mount_free(mp, 1); |
| 1149 | return 0; |
| 1150 | } |
| 1151 | |
| 1152 | void |
| 1153 | xfs_unmountfs_close(xfs_mount_t *mp, struct cred *cr) |
| 1154 | { |
| 1155 | if (mp->m_logdev_targp != mp->m_ddev_targp) |
| 1156 | xfs_free_buftarg(mp->m_logdev_targp, 1); |
| 1157 | if (mp->m_rtdev_targp) |
| 1158 | xfs_free_buftarg(mp->m_rtdev_targp, 1); |
| 1159 | xfs_free_buftarg(mp->m_ddev_targp, 0); |
| 1160 | } |
| 1161 | |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 1162 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | xfs_unmountfs_wait(xfs_mount_t *mp) |
| 1164 | { |
| 1165 | if (mp->m_logdev_targp != mp->m_ddev_targp) |
| 1166 | xfs_wait_buftarg(mp->m_logdev_targp); |
| 1167 | if (mp->m_rtdev_targp) |
| 1168 | xfs_wait_buftarg(mp->m_rtdev_targp); |
| 1169 | xfs_wait_buftarg(mp->m_ddev_targp); |
| 1170 | } |
| 1171 | |
| 1172 | int |
| 1173 | xfs_unmountfs_writesb(xfs_mount_t *mp) |
| 1174 | { |
| 1175 | xfs_buf_t *sbp; |
| 1176 | xfs_sb_t *sb; |
| 1177 | int error = 0; |
| 1178 | |
| 1179 | /* |
| 1180 | * skip superblock write if fs is read-only, or |
| 1181 | * if we are doing a forced umount. |
| 1182 | */ |
| 1183 | sbp = xfs_getsb(mp, 0); |
| 1184 | if (!(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY || |
| 1185 | XFS_FORCED_SHUTDOWN(mp))) { |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 1186 | |
| 1187 | xfs_icsb_sync_counters(mp); |
| 1188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | /* |
| 1190 | * mark shared-readonly if desired |
| 1191 | */ |
| 1192 | sb = XFS_BUF_TO_SBP(sbp); |
| 1193 | if (mp->m_mk_sharedro) { |
| 1194 | if (!(sb->sb_flags & XFS_SBF_READONLY)) |
| 1195 | sb->sb_flags |= XFS_SBF_READONLY; |
| 1196 | if (!XFS_SB_VERSION_HASSHARED(sb)) |
| 1197 | XFS_SB_VERSION_ADDSHARED(sb); |
| 1198 | xfs_fs_cmn_err(CE_NOTE, mp, |
| 1199 | "Unmounting, marking shared read-only"); |
| 1200 | } |
| 1201 | XFS_BUF_UNDONE(sbp); |
| 1202 | XFS_BUF_UNREAD(sbp); |
| 1203 | XFS_BUF_UNDELAYWRITE(sbp); |
| 1204 | XFS_BUF_WRITE(sbp); |
| 1205 | XFS_BUF_UNASYNC(sbp); |
| 1206 | ASSERT(XFS_BUF_TARGET(sbp) == mp->m_ddev_targp); |
| 1207 | xfsbdstrat(mp, sbp); |
| 1208 | /* Nevermind errors we might get here. */ |
| 1209 | error = xfs_iowait(sbp); |
| 1210 | if (error) |
| 1211 | xfs_ioerror_alert("xfs_unmountfs_writesb", |
| 1212 | mp, sbp, XFS_BUF_ADDR(sbp)); |
| 1213 | if (error && mp->m_mk_sharedro) |
| 1214 | xfs_fs_cmn_err(CE_ALERT, mp, "Superblock write error detected while unmounting. Filesystem may not be marked shared readonly"); |
| 1215 | } |
| 1216 | xfs_buf_relse(sbp); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1217 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | /* |
| 1221 | * xfs_mod_sb() can be used to copy arbitrary changes to the |
| 1222 | * in-core superblock into the superblock buffer to be logged. |
| 1223 | * It does not provide the higher level of locking that is |
| 1224 | * needed to protect the in-core superblock from concurrent |
| 1225 | * access. |
| 1226 | */ |
| 1227 | void |
| 1228 | xfs_mod_sb(xfs_trans_t *tp, __int64_t fields) |
| 1229 | { |
| 1230 | xfs_buf_t *bp; |
| 1231 | int first; |
| 1232 | int last; |
| 1233 | xfs_mount_t *mp; |
| 1234 | xfs_sb_t *sbp; |
| 1235 | xfs_sb_field_t f; |
| 1236 | |
| 1237 | ASSERT(fields); |
| 1238 | if (!fields) |
| 1239 | return; |
| 1240 | mp = tp->t_mountp; |
| 1241 | bp = xfs_trans_getsb(tp, mp, 0); |
| 1242 | sbp = XFS_BUF_TO_SBP(bp); |
| 1243 | first = sizeof(xfs_sb_t); |
| 1244 | last = 0; |
| 1245 | |
| 1246 | /* translate/copy */ |
| 1247 | |
| 1248 | xfs_xlatesb(XFS_BUF_PTR(bp), &(mp->m_sb), -1, fields); |
| 1249 | |
| 1250 | /* find modified range */ |
| 1251 | |
| 1252 | f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields); |
| 1253 | ASSERT((1LL << f) & XFS_SB_MOD_BITS); |
| 1254 | first = xfs_sb_info[f].offset; |
| 1255 | |
| 1256 | f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields); |
| 1257 | ASSERT((1LL << f) & XFS_SB_MOD_BITS); |
| 1258 | last = xfs_sb_info[f + 1].offset - 1; |
| 1259 | |
| 1260 | xfs_trans_log_buf(tp, bp, first, last); |
| 1261 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | /* |
| 1263 | * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply |
| 1264 | * a delta to a specified field in the in-core superblock. Simply |
| 1265 | * switch on the field indicated and apply the delta to that field. |
| 1266 | * Fields are not allowed to dip below zero, so if the delta would |
| 1267 | * do this do not apply it and return EINVAL. |
| 1268 | * |
| 1269 | * The SB_LOCK must be held when this routine is called. |
| 1270 | */ |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 1271 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | xfs_mod_incore_sb_unlocked(xfs_mount_t *mp, xfs_sb_field_t field, |
| 1273 | int delta, int rsvd) |
| 1274 | { |
| 1275 | int scounter; /* short counter for 32 bit fields */ |
| 1276 | long long lcounter; /* long counter for 64 bit fields */ |
| 1277 | long long res_used, rem; |
| 1278 | |
| 1279 | /* |
| 1280 | * With the in-core superblock spin lock held, switch |
| 1281 | * on the indicated field. Apply the delta to the |
| 1282 | * proper field. If the fields value would dip below |
| 1283 | * 0, then do not apply the delta and return EINVAL. |
| 1284 | */ |
| 1285 | switch (field) { |
| 1286 | case XFS_SBS_ICOUNT: |
| 1287 | lcounter = (long long)mp->m_sb.sb_icount; |
| 1288 | lcounter += delta; |
| 1289 | if (lcounter < 0) { |
| 1290 | ASSERT(0); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1291 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | } |
| 1293 | mp->m_sb.sb_icount = lcounter; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1294 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | case XFS_SBS_IFREE: |
| 1296 | lcounter = (long long)mp->m_sb.sb_ifree; |
| 1297 | lcounter += delta; |
| 1298 | if (lcounter < 0) { |
| 1299 | ASSERT(0); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1300 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | } |
| 1302 | mp->m_sb.sb_ifree = lcounter; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1303 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | case XFS_SBS_FDBLOCKS: |
| 1305 | |
| 1306 | lcounter = (long long)mp->m_sb.sb_fdblocks; |
| 1307 | res_used = (long long)(mp->m_resblks - mp->m_resblks_avail); |
| 1308 | |
| 1309 | if (delta > 0) { /* Putting blocks back */ |
| 1310 | if (res_used > delta) { |
| 1311 | mp->m_resblks_avail += delta; |
| 1312 | } else { |
| 1313 | rem = delta - res_used; |
| 1314 | mp->m_resblks_avail = mp->m_resblks; |
| 1315 | lcounter += rem; |
| 1316 | } |
| 1317 | } else { /* Taking blocks away */ |
| 1318 | |
| 1319 | lcounter += delta; |
| 1320 | |
| 1321 | /* |
| 1322 | * If were out of blocks, use any available reserved blocks if |
| 1323 | * were allowed to. |
| 1324 | */ |
| 1325 | |
| 1326 | if (lcounter < 0) { |
| 1327 | if (rsvd) { |
| 1328 | lcounter = (long long)mp->m_resblks_avail + delta; |
| 1329 | if (lcounter < 0) { |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1330 | return XFS_ERROR(ENOSPC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | } |
| 1332 | mp->m_resblks_avail = lcounter; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1333 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | } else { /* not reserved */ |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1335 | return XFS_ERROR(ENOSPC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | } |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | mp->m_sb.sb_fdblocks = lcounter; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1341 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | case XFS_SBS_FREXTENTS: |
| 1343 | lcounter = (long long)mp->m_sb.sb_frextents; |
| 1344 | lcounter += delta; |
| 1345 | if (lcounter < 0) { |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1346 | return XFS_ERROR(ENOSPC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | } |
| 1348 | mp->m_sb.sb_frextents = lcounter; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1349 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | case XFS_SBS_DBLOCKS: |
| 1351 | lcounter = (long long)mp->m_sb.sb_dblocks; |
| 1352 | lcounter += delta; |
| 1353 | if (lcounter < 0) { |
| 1354 | ASSERT(0); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1355 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | } |
| 1357 | mp->m_sb.sb_dblocks = lcounter; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1358 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | case XFS_SBS_AGCOUNT: |
| 1360 | scounter = mp->m_sb.sb_agcount; |
| 1361 | scounter += delta; |
| 1362 | if (scounter < 0) { |
| 1363 | ASSERT(0); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1364 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | } |
| 1366 | mp->m_sb.sb_agcount = scounter; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1367 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | case XFS_SBS_IMAX_PCT: |
| 1369 | scounter = mp->m_sb.sb_imax_pct; |
| 1370 | scounter += delta; |
| 1371 | if (scounter < 0) { |
| 1372 | ASSERT(0); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1373 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | } |
| 1375 | mp->m_sb.sb_imax_pct = scounter; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1376 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | case XFS_SBS_REXTSIZE: |
| 1378 | scounter = mp->m_sb.sb_rextsize; |
| 1379 | scounter += delta; |
| 1380 | if (scounter < 0) { |
| 1381 | ASSERT(0); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1382 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | } |
| 1384 | mp->m_sb.sb_rextsize = scounter; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1385 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | case XFS_SBS_RBMBLOCKS: |
| 1387 | scounter = mp->m_sb.sb_rbmblocks; |
| 1388 | scounter += delta; |
| 1389 | if (scounter < 0) { |
| 1390 | ASSERT(0); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1391 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | } |
| 1393 | mp->m_sb.sb_rbmblocks = scounter; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1394 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | case XFS_SBS_RBLOCKS: |
| 1396 | lcounter = (long long)mp->m_sb.sb_rblocks; |
| 1397 | lcounter += delta; |
| 1398 | if (lcounter < 0) { |
| 1399 | ASSERT(0); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1400 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | } |
| 1402 | mp->m_sb.sb_rblocks = lcounter; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1403 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | case XFS_SBS_REXTENTS: |
| 1405 | lcounter = (long long)mp->m_sb.sb_rextents; |
| 1406 | lcounter += delta; |
| 1407 | if (lcounter < 0) { |
| 1408 | ASSERT(0); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1409 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | } |
| 1411 | mp->m_sb.sb_rextents = lcounter; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1412 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | case XFS_SBS_REXTSLOG: |
| 1414 | scounter = mp->m_sb.sb_rextslog; |
| 1415 | scounter += delta; |
| 1416 | if (scounter < 0) { |
| 1417 | ASSERT(0); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1418 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | } |
| 1420 | mp->m_sb.sb_rextslog = scounter; |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1421 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | default: |
| 1423 | ASSERT(0); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1424 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | /* |
| 1429 | * xfs_mod_incore_sb() is used to change a field in the in-core |
| 1430 | * superblock structure by the specified delta. This modification |
| 1431 | * is protected by the SB_LOCK. Just use the xfs_mod_incore_sb_unlocked() |
| 1432 | * routine to do the work. |
| 1433 | */ |
| 1434 | int |
| 1435 | xfs_mod_incore_sb(xfs_mount_t *mp, xfs_sb_field_t field, int delta, int rsvd) |
| 1436 | { |
| 1437 | unsigned long s; |
| 1438 | int status; |
| 1439 | |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 1440 | /* check for per-cpu counters */ |
| 1441 | switch (field) { |
| 1442 | #ifdef HAVE_PERCPU_SB |
| 1443 | case XFS_SBS_ICOUNT: |
| 1444 | case XFS_SBS_IFREE: |
| 1445 | case XFS_SBS_FDBLOCKS: |
| 1446 | if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) { |
| 1447 | status = xfs_icsb_modify_counters(mp, field, |
| 1448 | delta, rsvd); |
| 1449 | break; |
| 1450 | } |
| 1451 | /* FALLTHROUGH */ |
| 1452 | #endif |
| 1453 | default: |
| 1454 | s = XFS_SB_LOCK(mp); |
| 1455 | status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd); |
| 1456 | XFS_SB_UNLOCK(mp, s); |
| 1457 | break; |
| 1458 | } |
| 1459 | |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1460 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | /* |
| 1464 | * xfs_mod_incore_sb_batch() is used to change more than one field |
| 1465 | * in the in-core superblock structure at a time. This modification |
| 1466 | * is protected by a lock internal to this module. The fields and |
| 1467 | * changes to those fields are specified in the array of xfs_mod_sb |
| 1468 | * structures passed in. |
| 1469 | * |
| 1470 | * Either all of the specified deltas will be applied or none of |
| 1471 | * them will. If any modified field dips below 0, then all modifications |
| 1472 | * will be backed out and EINVAL will be returned. |
| 1473 | */ |
| 1474 | int |
| 1475 | xfs_mod_incore_sb_batch(xfs_mount_t *mp, xfs_mod_sb_t *msb, uint nmsb, int rsvd) |
| 1476 | { |
| 1477 | unsigned long s; |
| 1478 | int status=0; |
| 1479 | xfs_mod_sb_t *msbp; |
| 1480 | |
| 1481 | /* |
| 1482 | * Loop through the array of mod structures and apply each |
| 1483 | * individually. If any fail, then back out all those |
| 1484 | * which have already been applied. Do all of this within |
| 1485 | * the scope of the SB_LOCK so that all of the changes will |
| 1486 | * be atomic. |
| 1487 | */ |
| 1488 | s = XFS_SB_LOCK(mp); |
| 1489 | msbp = &msb[0]; |
| 1490 | for (msbp = &msbp[0]; msbp < (msb + nmsb); msbp++) { |
| 1491 | /* |
| 1492 | * Apply the delta at index n. If it fails, break |
| 1493 | * from the loop so we'll fall into the undo loop |
| 1494 | * below. |
| 1495 | */ |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 1496 | switch (msbp->msb_field) { |
| 1497 | #ifdef HAVE_PERCPU_SB |
| 1498 | case XFS_SBS_ICOUNT: |
| 1499 | case XFS_SBS_IFREE: |
| 1500 | case XFS_SBS_FDBLOCKS: |
| 1501 | if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) { |
| 1502 | status = xfs_icsb_modify_counters_locked(mp, |
| 1503 | msbp->msb_field, |
| 1504 | msbp->msb_delta, rsvd); |
| 1505 | break; |
| 1506 | } |
| 1507 | /* FALLTHROUGH */ |
| 1508 | #endif |
| 1509 | default: |
| 1510 | status = xfs_mod_incore_sb_unlocked(mp, |
| 1511 | msbp->msb_field, |
| 1512 | msbp->msb_delta, rsvd); |
| 1513 | break; |
| 1514 | } |
| 1515 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | if (status != 0) { |
| 1517 | break; |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | /* |
| 1522 | * If we didn't complete the loop above, then back out |
| 1523 | * any changes made to the superblock. If you add code |
| 1524 | * between the loop above and here, make sure that you |
| 1525 | * preserve the value of status. Loop back until |
| 1526 | * we step below the beginning of the array. Make sure |
| 1527 | * we don't touch anything back there. |
| 1528 | */ |
| 1529 | if (status != 0) { |
| 1530 | msbp--; |
| 1531 | while (msbp >= msb) { |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 1532 | switch (msbp->msb_field) { |
| 1533 | #ifdef HAVE_PERCPU_SB |
| 1534 | case XFS_SBS_ICOUNT: |
| 1535 | case XFS_SBS_IFREE: |
| 1536 | case XFS_SBS_FDBLOCKS: |
| 1537 | if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) { |
| 1538 | status = |
| 1539 | xfs_icsb_modify_counters_locked(mp, |
| 1540 | msbp->msb_field, |
| 1541 | -(msbp->msb_delta), |
| 1542 | rsvd); |
| 1543 | break; |
| 1544 | } |
| 1545 | /* FALLTHROUGH */ |
| 1546 | #endif |
| 1547 | default: |
| 1548 | status = xfs_mod_incore_sb_unlocked(mp, |
| 1549 | msbp->msb_field, |
| 1550 | -(msbp->msb_delta), |
| 1551 | rsvd); |
| 1552 | break; |
| 1553 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | ASSERT(status == 0); |
| 1555 | msbp--; |
| 1556 | } |
| 1557 | } |
| 1558 | XFS_SB_UNLOCK(mp, s); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1559 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | /* |
| 1563 | * xfs_getsb() is called to obtain the buffer for the superblock. |
| 1564 | * The buffer is returned locked and read in from disk. |
| 1565 | * The buffer should be released with a call to xfs_brelse(). |
| 1566 | * |
| 1567 | * If the flags parameter is BUF_TRYLOCK, then we'll only return |
| 1568 | * the superblock buffer if it can be locked without sleeping. |
| 1569 | * If it can't then we'll return NULL. |
| 1570 | */ |
| 1571 | xfs_buf_t * |
| 1572 | xfs_getsb( |
| 1573 | xfs_mount_t *mp, |
| 1574 | int flags) |
| 1575 | { |
| 1576 | xfs_buf_t *bp; |
| 1577 | |
| 1578 | ASSERT(mp->m_sb_bp != NULL); |
| 1579 | bp = mp->m_sb_bp; |
| 1580 | if (flags & XFS_BUF_TRYLOCK) { |
| 1581 | if (!XFS_BUF_CPSEMA(bp)) { |
| 1582 | return NULL; |
| 1583 | } |
| 1584 | } else { |
| 1585 | XFS_BUF_PSEMA(bp, PRIBIO); |
| 1586 | } |
| 1587 | XFS_BUF_HOLD(bp); |
| 1588 | ASSERT(XFS_BUF_ISDONE(bp)); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1589 | return bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | /* |
| 1593 | * Used to free the superblock along various error paths. |
| 1594 | */ |
| 1595 | void |
| 1596 | xfs_freesb( |
| 1597 | xfs_mount_t *mp) |
| 1598 | { |
| 1599 | xfs_buf_t *bp; |
| 1600 | |
| 1601 | /* |
| 1602 | * Use xfs_getsb() so that the buffer will be locked |
| 1603 | * when we call xfs_buf_relse(). |
| 1604 | */ |
| 1605 | bp = xfs_getsb(mp, 0); |
| 1606 | XFS_BUF_UNMANAGE(bp); |
| 1607 | xfs_buf_relse(bp); |
| 1608 | mp->m_sb_bp = NULL; |
| 1609 | } |
| 1610 | |
| 1611 | /* |
| 1612 | * See if the UUID is unique among mounted XFS filesystems. |
| 1613 | * Mount fails if UUID is nil or a FS with the same UUID is already mounted. |
| 1614 | */ |
| 1615 | STATIC int |
| 1616 | xfs_uuid_mount( |
| 1617 | xfs_mount_t *mp) |
| 1618 | { |
| 1619 | if (uuid_is_nil(&mp->m_sb.sb_uuid)) { |
| 1620 | cmn_err(CE_WARN, |
| 1621 | "XFS: Filesystem %s has nil UUID - can't mount", |
| 1622 | mp->m_fsname); |
| 1623 | return -1; |
| 1624 | } |
| 1625 | if (!uuid_table_insert(&mp->m_sb.sb_uuid)) { |
| 1626 | cmn_err(CE_WARN, |
| 1627 | "XFS: Filesystem %s has duplicate UUID - can't mount", |
| 1628 | mp->m_fsname); |
| 1629 | return -1; |
| 1630 | } |
| 1631 | return 0; |
| 1632 | } |
| 1633 | |
| 1634 | /* |
| 1635 | * Remove filesystem from the UUID table. |
| 1636 | */ |
| 1637 | STATIC void |
| 1638 | xfs_uuid_unmount( |
| 1639 | xfs_mount_t *mp) |
| 1640 | { |
| 1641 | uuid_table_remove(&mp->m_sb.sb_uuid); |
| 1642 | } |
| 1643 | |
| 1644 | /* |
| 1645 | * Used to log changes to the superblock unit and width fields which could |
| 1646 | * be altered by the mount options. Only the first superblock is updated. |
| 1647 | */ |
| 1648 | STATIC void |
| 1649 | xfs_mount_log_sbunit( |
| 1650 | xfs_mount_t *mp, |
| 1651 | __int64_t fields) |
| 1652 | { |
| 1653 | xfs_trans_t *tp; |
| 1654 | |
| 1655 | ASSERT(fields & (XFS_SB_UNIT|XFS_SB_WIDTH|XFS_SB_UUID)); |
| 1656 | |
| 1657 | tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT); |
| 1658 | if (xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0, |
| 1659 | XFS_DEFAULT_LOG_COUNT)) { |
| 1660 | xfs_trans_cancel(tp, 0); |
| 1661 | return; |
| 1662 | } |
| 1663 | xfs_mod_sb(tp, fields); |
| 1664 | xfs_trans_commit(tp, 0, NULL); |
| 1665 | } |
David Chinner | 8d280b9 | 2006-03-14 13:13:09 +1100 | [diff] [blame] | 1666 | |
| 1667 | |
| 1668 | #ifdef HAVE_PERCPU_SB |
| 1669 | /* |
| 1670 | * Per-cpu incore superblock counters |
| 1671 | * |
| 1672 | * Simple concept, difficult implementation |
| 1673 | * |
| 1674 | * Basically, replace the incore superblock counters with a distributed per cpu |
| 1675 | * counter for contended fields (e.g. free block count). |
| 1676 | * |
| 1677 | * Difficulties arise in that the incore sb is used for ENOSPC checking, and |
| 1678 | * hence needs to be accurately read when we are running low on space. Hence |
| 1679 | * there is a method to enable and disable the per-cpu counters based on how |
| 1680 | * much "stuff" is available in them. |
| 1681 | * |
| 1682 | * Basically, a counter is enabled if there is enough free resource to justify |
| 1683 | * running a per-cpu fast-path. If the per-cpu counter runs out (i.e. a local |
| 1684 | * ENOSPC), then we disable the counters to synchronise all callers and |
| 1685 | * re-distribute the available resources. |
| 1686 | * |
| 1687 | * If, once we redistributed the available resources, we still get a failure, |
| 1688 | * we disable the per-cpu counter and go through the slow path. |
| 1689 | * |
| 1690 | * The slow path is the current xfs_mod_incore_sb() function. This means that |
| 1691 | * when we disable a per-cpu counter, we need to drain it's resources back to |
| 1692 | * the global superblock. We do this after disabling the counter to prevent |
| 1693 | * more threads from queueing up on the counter. |
| 1694 | * |
| 1695 | * Essentially, this means that we still need a lock in the fast path to enable |
| 1696 | * synchronisation between the global counters and the per-cpu counters. This |
| 1697 | * is not a problem because the lock will be local to a CPU almost all the time |
| 1698 | * and have little contention except when we get to ENOSPC conditions. |
| 1699 | * |
| 1700 | * Basically, this lock becomes a barrier that enables us to lock out the fast |
| 1701 | * path while we do things like enabling and disabling counters and |
| 1702 | * synchronising the counters. |
| 1703 | * |
| 1704 | * Locking rules: |
| 1705 | * |
| 1706 | * 1. XFS_SB_LOCK() before picking up per-cpu locks |
| 1707 | * 2. per-cpu locks always picked up via for_each_online_cpu() order |
| 1708 | * 3. accurate counter sync requires XFS_SB_LOCK + per cpu locks |
| 1709 | * 4. modifying per-cpu counters requires holding per-cpu lock |
| 1710 | * 5. modifying global counters requires holding XFS_SB_LOCK |
| 1711 | * 6. enabling or disabling a counter requires holding the XFS_SB_LOCK |
| 1712 | * and _none_ of the per-cpu locks. |
| 1713 | * |
| 1714 | * Disabled counters are only ever re-enabled by a balance operation |
| 1715 | * that results in more free resources per CPU than a given threshold. |
| 1716 | * To ensure counters don't remain disabled, they are rebalanced when |
| 1717 | * the global resource goes above a higher threshold (i.e. some hysteresis |
| 1718 | * is present to prevent thrashing). |
| 1719 | * |
| 1720 | * Note: hotplug CPUs not yet supported |
| 1721 | */ |
| 1722 | int |
| 1723 | xfs_icsb_init_counters( |
| 1724 | xfs_mount_t *mp) |
| 1725 | { |
| 1726 | xfs_icsb_cnts_t *cntp; |
| 1727 | int i; |
| 1728 | |
| 1729 | mp->m_sb_cnts = alloc_percpu(xfs_icsb_cnts_t); |
| 1730 | if (mp->m_sb_cnts == NULL) |
| 1731 | return -ENOMEM; |
| 1732 | |
| 1733 | for_each_online_cpu(i) { |
| 1734 | cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i); |
| 1735 | spin_lock_init(&cntp->icsb_lock); |
| 1736 | } |
| 1737 | /* |
| 1738 | * start with all counters disabled so that the |
| 1739 | * initial balance kicks us off correctly |
| 1740 | */ |
| 1741 | mp->m_icsb_counters = -1; |
| 1742 | return 0; |
| 1743 | } |
| 1744 | |
| 1745 | STATIC void |
| 1746 | xfs_icsb_destroy_counters( |
| 1747 | xfs_mount_t *mp) |
| 1748 | { |
| 1749 | if (mp->m_sb_cnts) |
| 1750 | free_percpu(mp->m_sb_cnts); |
| 1751 | } |
| 1752 | |
| 1753 | |
| 1754 | STATIC inline void |
| 1755 | xfs_icsb_lock_all_counters( |
| 1756 | xfs_mount_t *mp) |
| 1757 | { |
| 1758 | xfs_icsb_cnts_t *cntp; |
| 1759 | int i; |
| 1760 | |
| 1761 | for_each_online_cpu(i) { |
| 1762 | cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i); |
| 1763 | spin_lock(&cntp->icsb_lock); |
| 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | STATIC inline void |
| 1768 | xfs_icsb_unlock_all_counters( |
| 1769 | xfs_mount_t *mp) |
| 1770 | { |
| 1771 | xfs_icsb_cnts_t *cntp; |
| 1772 | int i; |
| 1773 | |
| 1774 | for_each_online_cpu(i) { |
| 1775 | cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i); |
| 1776 | spin_unlock(&cntp->icsb_lock); |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | STATIC void |
| 1781 | xfs_icsb_count( |
| 1782 | xfs_mount_t *mp, |
| 1783 | xfs_icsb_cnts_t *cnt, |
| 1784 | int flags) |
| 1785 | { |
| 1786 | xfs_icsb_cnts_t *cntp; |
| 1787 | int i; |
| 1788 | |
| 1789 | memset(cnt, 0, sizeof(xfs_icsb_cnts_t)); |
| 1790 | |
| 1791 | if (!(flags & XFS_ICSB_LAZY_COUNT)) |
| 1792 | xfs_icsb_lock_all_counters(mp); |
| 1793 | |
| 1794 | for_each_online_cpu(i) { |
| 1795 | cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i); |
| 1796 | cnt->icsb_icount += cntp->icsb_icount; |
| 1797 | cnt->icsb_ifree += cntp->icsb_ifree; |
| 1798 | cnt->icsb_fdblocks += cntp->icsb_fdblocks; |
| 1799 | } |
| 1800 | |
| 1801 | if (!(flags & XFS_ICSB_LAZY_COUNT)) |
| 1802 | xfs_icsb_unlock_all_counters(mp); |
| 1803 | } |
| 1804 | |
| 1805 | STATIC int |
| 1806 | xfs_icsb_counter_disabled( |
| 1807 | xfs_mount_t *mp, |
| 1808 | xfs_sb_field_t field) |
| 1809 | { |
| 1810 | ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS)); |
| 1811 | return test_bit(field, &mp->m_icsb_counters); |
| 1812 | } |
| 1813 | |
| 1814 | STATIC int |
| 1815 | xfs_icsb_disable_counter( |
| 1816 | xfs_mount_t *mp, |
| 1817 | xfs_sb_field_t field) |
| 1818 | { |
| 1819 | xfs_icsb_cnts_t cnt; |
| 1820 | |
| 1821 | ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS)); |
| 1822 | |
| 1823 | xfs_icsb_lock_all_counters(mp); |
| 1824 | if (!test_and_set_bit(field, &mp->m_icsb_counters)) { |
| 1825 | /* drain back to superblock */ |
| 1826 | |
| 1827 | xfs_icsb_count(mp, &cnt, XFS_ICSB_SB_LOCKED|XFS_ICSB_LAZY_COUNT); |
| 1828 | switch(field) { |
| 1829 | case XFS_SBS_ICOUNT: |
| 1830 | mp->m_sb.sb_icount = cnt.icsb_icount; |
| 1831 | break; |
| 1832 | case XFS_SBS_IFREE: |
| 1833 | mp->m_sb.sb_ifree = cnt.icsb_ifree; |
| 1834 | break; |
| 1835 | case XFS_SBS_FDBLOCKS: |
| 1836 | mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks; |
| 1837 | break; |
| 1838 | default: |
| 1839 | BUG(); |
| 1840 | } |
| 1841 | } |
| 1842 | |
| 1843 | xfs_icsb_unlock_all_counters(mp); |
| 1844 | |
| 1845 | return 0; |
| 1846 | } |
| 1847 | |
| 1848 | STATIC void |
| 1849 | xfs_icsb_enable_counter( |
| 1850 | xfs_mount_t *mp, |
| 1851 | xfs_sb_field_t field, |
| 1852 | uint64_t count, |
| 1853 | uint64_t resid) |
| 1854 | { |
| 1855 | xfs_icsb_cnts_t *cntp; |
| 1856 | int i; |
| 1857 | |
| 1858 | ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS)); |
| 1859 | |
| 1860 | xfs_icsb_lock_all_counters(mp); |
| 1861 | for_each_online_cpu(i) { |
| 1862 | cntp = per_cpu_ptr(mp->m_sb_cnts, i); |
| 1863 | switch (field) { |
| 1864 | case XFS_SBS_ICOUNT: |
| 1865 | cntp->icsb_icount = count + resid; |
| 1866 | break; |
| 1867 | case XFS_SBS_IFREE: |
| 1868 | cntp->icsb_ifree = count + resid; |
| 1869 | break; |
| 1870 | case XFS_SBS_FDBLOCKS: |
| 1871 | cntp->icsb_fdblocks = count + resid; |
| 1872 | break; |
| 1873 | default: |
| 1874 | BUG(); |
| 1875 | break; |
| 1876 | } |
| 1877 | resid = 0; |
| 1878 | } |
| 1879 | clear_bit(field, &mp->m_icsb_counters); |
| 1880 | xfs_icsb_unlock_all_counters(mp); |
| 1881 | } |
| 1882 | |
| 1883 | STATIC void |
| 1884 | xfs_icsb_sync_counters_int( |
| 1885 | xfs_mount_t *mp, |
| 1886 | int flags) |
| 1887 | { |
| 1888 | xfs_icsb_cnts_t cnt; |
| 1889 | int s; |
| 1890 | |
| 1891 | /* Pass 1: lock all counters */ |
| 1892 | if ((flags & XFS_ICSB_SB_LOCKED) == 0) |
| 1893 | s = XFS_SB_LOCK(mp); |
| 1894 | |
| 1895 | xfs_icsb_count(mp, &cnt, flags); |
| 1896 | |
| 1897 | /* Step 3: update mp->m_sb fields */ |
| 1898 | if (!xfs_icsb_counter_disabled(mp, XFS_SBS_ICOUNT)) |
| 1899 | mp->m_sb.sb_icount = cnt.icsb_icount; |
| 1900 | if (!xfs_icsb_counter_disabled(mp, XFS_SBS_IFREE)) |
| 1901 | mp->m_sb.sb_ifree = cnt.icsb_ifree; |
| 1902 | if (!xfs_icsb_counter_disabled(mp, XFS_SBS_FDBLOCKS)) |
| 1903 | mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks; |
| 1904 | |
| 1905 | if ((flags & XFS_ICSB_SB_LOCKED) == 0) |
| 1906 | XFS_SB_UNLOCK(mp, s); |
| 1907 | } |
| 1908 | |
| 1909 | /* |
| 1910 | * Accurate update of per-cpu counters to incore superblock |
| 1911 | */ |
| 1912 | STATIC void |
| 1913 | xfs_icsb_sync_counters( |
| 1914 | xfs_mount_t *mp) |
| 1915 | { |
| 1916 | xfs_icsb_sync_counters_int(mp, 0); |
| 1917 | } |
| 1918 | |
| 1919 | /* |
| 1920 | * lazy addition used for things like df, background sb syncs, etc |
| 1921 | */ |
| 1922 | void |
| 1923 | xfs_icsb_sync_counters_lazy( |
| 1924 | xfs_mount_t *mp) |
| 1925 | { |
| 1926 | xfs_icsb_sync_counters_int(mp, XFS_ICSB_LAZY_COUNT); |
| 1927 | } |
| 1928 | |
| 1929 | /* |
| 1930 | * Balance and enable/disable counters as necessary. |
| 1931 | * |
| 1932 | * Thresholds for re-enabling counters are somewhat magic. |
| 1933 | * inode counts are chosen to be the same number as single |
| 1934 | * on disk allocation chunk per CPU, and free blocks is |
| 1935 | * something far enough zero that we aren't going thrash |
| 1936 | * when we get near ENOSPC. |
| 1937 | */ |
| 1938 | #define XFS_ICSB_INO_CNTR_REENABLE 64 |
| 1939 | #define XFS_ICSB_FDBLK_CNTR_REENABLE 512 |
| 1940 | STATIC void |
| 1941 | xfs_icsb_balance_counter( |
| 1942 | xfs_mount_t *mp, |
| 1943 | xfs_sb_field_t field, |
| 1944 | int flags) |
| 1945 | { |
| 1946 | uint64_t count, resid = 0; |
| 1947 | int weight = num_online_cpus(); |
| 1948 | int s; |
| 1949 | |
| 1950 | if (!(flags & XFS_ICSB_SB_LOCKED)) |
| 1951 | s = XFS_SB_LOCK(mp); |
| 1952 | |
| 1953 | /* disable counter and sync counter */ |
| 1954 | xfs_icsb_disable_counter(mp, field); |
| 1955 | |
| 1956 | /* update counters - first CPU gets residual*/ |
| 1957 | switch (field) { |
| 1958 | case XFS_SBS_ICOUNT: |
| 1959 | count = mp->m_sb.sb_icount; |
| 1960 | resid = do_div(count, weight); |
| 1961 | if (count < XFS_ICSB_INO_CNTR_REENABLE) |
| 1962 | goto out; |
| 1963 | break; |
| 1964 | case XFS_SBS_IFREE: |
| 1965 | count = mp->m_sb.sb_ifree; |
| 1966 | resid = do_div(count, weight); |
| 1967 | if (count < XFS_ICSB_INO_CNTR_REENABLE) |
| 1968 | goto out; |
| 1969 | break; |
| 1970 | case XFS_SBS_FDBLOCKS: |
| 1971 | count = mp->m_sb.sb_fdblocks; |
| 1972 | resid = do_div(count, weight); |
| 1973 | if (count < XFS_ICSB_FDBLK_CNTR_REENABLE) |
| 1974 | goto out; |
| 1975 | break; |
| 1976 | default: |
| 1977 | BUG(); |
| 1978 | break; |
| 1979 | } |
| 1980 | |
| 1981 | xfs_icsb_enable_counter(mp, field, count, resid); |
| 1982 | out: |
| 1983 | if (!(flags & XFS_ICSB_SB_LOCKED)) |
| 1984 | XFS_SB_UNLOCK(mp, s); |
| 1985 | } |
| 1986 | |
| 1987 | STATIC int |
| 1988 | xfs_icsb_modify_counters_int( |
| 1989 | xfs_mount_t *mp, |
| 1990 | xfs_sb_field_t field, |
| 1991 | int delta, |
| 1992 | int rsvd, |
| 1993 | int flags) |
| 1994 | { |
| 1995 | xfs_icsb_cnts_t *icsbp; |
| 1996 | long long lcounter; /* long counter for 64 bit fields */ |
| 1997 | int cpu, s, locked = 0; |
| 1998 | int ret = 0, balance_done = 0; |
| 1999 | |
| 2000 | again: |
| 2001 | cpu = get_cpu(); |
| 2002 | icsbp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, cpu), |
| 2003 | spin_lock(&icsbp->icsb_lock); |
| 2004 | if (unlikely(xfs_icsb_counter_disabled(mp, field))) |
| 2005 | goto slow_path; |
| 2006 | |
| 2007 | switch (field) { |
| 2008 | case XFS_SBS_ICOUNT: |
| 2009 | lcounter = icsbp->icsb_icount; |
| 2010 | lcounter += delta; |
| 2011 | if (unlikely(lcounter < 0)) |
| 2012 | goto slow_path; |
| 2013 | icsbp->icsb_icount = lcounter; |
| 2014 | break; |
| 2015 | |
| 2016 | case XFS_SBS_IFREE: |
| 2017 | lcounter = icsbp->icsb_ifree; |
| 2018 | lcounter += delta; |
| 2019 | if (unlikely(lcounter < 0)) |
| 2020 | goto slow_path; |
| 2021 | icsbp->icsb_ifree = lcounter; |
| 2022 | break; |
| 2023 | |
| 2024 | case XFS_SBS_FDBLOCKS: |
| 2025 | BUG_ON((mp->m_resblks - mp->m_resblks_avail) != 0); |
| 2026 | |
| 2027 | lcounter = icsbp->icsb_fdblocks; |
| 2028 | lcounter += delta; |
| 2029 | if (unlikely(lcounter < 0)) |
| 2030 | goto slow_path; |
| 2031 | icsbp->icsb_fdblocks = lcounter; |
| 2032 | break; |
| 2033 | default: |
| 2034 | BUG(); |
| 2035 | break; |
| 2036 | } |
| 2037 | spin_unlock(&icsbp->icsb_lock); |
| 2038 | put_cpu(); |
| 2039 | if (locked) |
| 2040 | XFS_SB_UNLOCK(mp, s); |
| 2041 | return 0; |
| 2042 | |
| 2043 | /* |
| 2044 | * The slow path needs to be run with the SBLOCK |
| 2045 | * held so that we prevent other threads from |
| 2046 | * attempting to run this path at the same time. |
| 2047 | * this provides exclusion for the balancing code, |
| 2048 | * and exclusive fallback if the balance does not |
| 2049 | * provide enough resources to continue in an unlocked |
| 2050 | * manner. |
| 2051 | */ |
| 2052 | slow_path: |
| 2053 | spin_unlock(&icsbp->icsb_lock); |
| 2054 | put_cpu(); |
| 2055 | |
| 2056 | /* need to hold superblock incase we need |
| 2057 | * to disable a counter */ |
| 2058 | if (!(flags & XFS_ICSB_SB_LOCKED)) { |
| 2059 | s = XFS_SB_LOCK(mp); |
| 2060 | locked = 1; |
| 2061 | flags |= XFS_ICSB_SB_LOCKED; |
| 2062 | } |
| 2063 | if (!balance_done) { |
| 2064 | xfs_icsb_balance_counter(mp, field, flags); |
| 2065 | balance_done = 1; |
| 2066 | goto again; |
| 2067 | } else { |
| 2068 | /* |
| 2069 | * we might not have enough on this local |
| 2070 | * cpu to allocate for a bulk request. |
| 2071 | * We need to drain this field from all CPUs |
| 2072 | * and disable the counter fastpath |
| 2073 | */ |
| 2074 | xfs_icsb_disable_counter(mp, field); |
| 2075 | } |
| 2076 | |
| 2077 | ret = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd); |
| 2078 | |
| 2079 | if (locked) |
| 2080 | XFS_SB_UNLOCK(mp, s); |
| 2081 | return ret; |
| 2082 | } |
| 2083 | |
| 2084 | STATIC int |
| 2085 | xfs_icsb_modify_counters( |
| 2086 | xfs_mount_t *mp, |
| 2087 | xfs_sb_field_t field, |
| 2088 | int delta, |
| 2089 | int rsvd) |
| 2090 | { |
| 2091 | return xfs_icsb_modify_counters_int(mp, field, delta, rsvd, 0); |
| 2092 | } |
| 2093 | |
| 2094 | /* |
| 2095 | * Called when superblock is already locked |
| 2096 | */ |
| 2097 | STATIC int |
| 2098 | xfs_icsb_modify_counters_locked( |
| 2099 | xfs_mount_t *mp, |
| 2100 | xfs_sb_field_t field, |
| 2101 | int delta, |
| 2102 | int rsvd) |
| 2103 | { |
| 2104 | return xfs_icsb_modify_counters_int(mp, field, delta, |
| 2105 | rsvd, XFS_ICSB_SB_LOCKED); |
| 2106 | } |
| 2107 | #endif |