xfs_mount.c revision 153323
1/* 2 * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved. 3 * 4 * This program is free software; you can redistribute it and/or modify it 5 * under the terms of version 2 of the GNU General Public License as 6 * published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it would be useful, but 9 * WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 11 * 12 * Further, this software is distributed without any warranty that it is 13 * free of the rightful claim of any third person regarding infringement 14 * or the like. Any license provided herein, whether implied or 15 * otherwise, applies only to this software file. Patent licenses, if 16 * any, provided herein do not apply to combinations of this program with 17 * other software, or any other product whatsoever. 18 * 19 * You should have received a copy of the GNU General Public License along 20 * with this program; if not, write the Free Software Foundation, Inc., 59 21 * Temple Place - Suite 330, Boston MA 02111-1307, USA. 22 * 23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, 24 * Mountain View, CA 94043, or: 25 * 26 * http://www.sgi.com 27 * 28 * For further information regarding this notice, see: 29 * 30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/ 31 */ 32 33#include "xfs.h" 34#include "xfs_macros.h" 35#include "xfs_types.h" 36#include "xfs_inum.h" 37#include "xfs_log.h" 38#include "xfs_trans.h" 39#include "xfs_sb.h" 40#include "xfs_ag.h" 41#include "xfs_dir.h" 42#include "xfs_dir2.h" 43#include "xfs_dmapi.h" 44#include "xfs_mount.h" 45#include "xfs_alloc_btree.h" 46#include "xfs_bmap_btree.h" 47#include "xfs_ialloc_btree.h" 48#include "xfs_btree.h" 49#include "xfs_ialloc.h" 50#include "xfs_attr_sf.h" 51#include "xfs_dir_sf.h" 52#include "xfs_dir2_sf.h" 53#include "xfs_dinode.h" 54#include "xfs_inode.h" 55#include "xfs_alloc.h" 56#include "xfs_rtalloc.h" 57#include "xfs_bmap.h" 58#include "xfs_error.h" 59#include "xfs_bit.h" 60#include "xfs_rw.h" 61#include "xfs_quota.h" 62#include "xfs_fsops.h" 63 64STATIC void xfs_mount_log_sbunit(xfs_mount_t *, __int64_t); 65STATIC int xfs_uuid_mount(xfs_mount_t *); 66STATIC void xfs_uuid_unmount(xfs_mount_t *mp); 67 68 69 70static struct { 71 short offset; 72 short type; /* 0 = integer 73 * 1 = binary / string (no translation) 74 */ 75} xfs_sb_info[] = { 76 { offsetof(xfs_sb_t, sb_magicnum), 0 }, 77 { offsetof(xfs_sb_t, sb_blocksize), 0 }, 78 { offsetof(xfs_sb_t, sb_dblocks), 0 }, 79 { offsetof(xfs_sb_t, sb_rblocks), 0 }, 80 { offsetof(xfs_sb_t, sb_rextents), 0 }, 81 { offsetof(xfs_sb_t, sb_uuid), 1 }, 82 { offsetof(xfs_sb_t, sb_logstart), 0 }, 83 { offsetof(xfs_sb_t, sb_rootino), 0 }, 84 { offsetof(xfs_sb_t, sb_rbmino), 0 }, 85 { offsetof(xfs_sb_t, sb_rsumino), 0 }, 86 { offsetof(xfs_sb_t, sb_rextsize), 0 }, 87 { offsetof(xfs_sb_t, sb_agblocks), 0 }, 88 { offsetof(xfs_sb_t, sb_agcount), 0 }, 89 { offsetof(xfs_sb_t, sb_rbmblocks), 0 }, 90 { offsetof(xfs_sb_t, sb_logblocks), 0 }, 91 { offsetof(xfs_sb_t, sb_versionnum), 0 }, 92 { offsetof(xfs_sb_t, sb_sectsize), 0 }, 93 { offsetof(xfs_sb_t, sb_inodesize), 0 }, 94 { offsetof(xfs_sb_t, sb_inopblock), 0 }, 95 { offsetof(xfs_sb_t, sb_fname[0]), 1 }, 96 { offsetof(xfs_sb_t, sb_blocklog), 0 }, 97 { offsetof(xfs_sb_t, sb_sectlog), 0 }, 98 { offsetof(xfs_sb_t, sb_inodelog), 0 }, 99 { offsetof(xfs_sb_t, sb_inopblog), 0 }, 100 { offsetof(xfs_sb_t, sb_agblklog), 0 }, 101 { offsetof(xfs_sb_t, sb_rextslog), 0 }, 102 { offsetof(xfs_sb_t, sb_inprogress), 0 }, 103 { offsetof(xfs_sb_t, sb_imax_pct), 0 }, 104 { offsetof(xfs_sb_t, sb_icount), 0 }, 105 { offsetof(xfs_sb_t, sb_ifree), 0 }, 106 { offsetof(xfs_sb_t, sb_fdblocks), 0 }, 107 { offsetof(xfs_sb_t, sb_frextents), 0 }, 108 { offsetof(xfs_sb_t, sb_uquotino), 0 }, 109 { offsetof(xfs_sb_t, sb_gquotino), 0 }, 110 { offsetof(xfs_sb_t, sb_qflags), 0 }, 111 { offsetof(xfs_sb_t, sb_flags), 0 }, 112 { offsetof(xfs_sb_t, sb_shared_vn), 0 }, 113 { offsetof(xfs_sb_t, sb_inoalignmt), 0 }, 114 { offsetof(xfs_sb_t, sb_unit), 0 }, 115 { offsetof(xfs_sb_t, sb_width), 0 }, 116 { offsetof(xfs_sb_t, sb_dirblklog), 0 }, 117 { offsetof(xfs_sb_t, sb_logsectlog), 0 }, 118 { offsetof(xfs_sb_t, sb_logsectsize),0 }, 119 { offsetof(xfs_sb_t, sb_logsunit), 0 }, 120 { sizeof(xfs_sb_t), 0 } 121}; 122 123/* 124 * Return a pointer to an initialized xfs_mount structure. 125 */ 126xfs_mount_t * 127xfs_mount_init(void) 128{ 129 xfs_mount_t *mp; 130 131 mp = kmem_zalloc(sizeof(*mp), KM_SLEEP); 132 133 AIL_LOCKINIT(&mp->m_ail_lock, "xfs_ail"); 134 spinlock_init(&mp->m_sb_lock, "xfs_sb"); 135 mutex_init(&mp->m_ilock, MUTEX_DEFAULT, "xfs_ilock"); 136 initnsema(&mp->m_growlock, 1, "xfs_grow"); 137 /* 138 * Initialize the AIL. 139 */ 140 xfs_trans_ail_init(mp); 141 142 /* Init freeze sync structures */ 143 spinlock_init(&mp->m_freeze_lock, "xfs_freeze"); 144 init_sv(&mp->m_wait_unfreeze, SV_DEFAULT, "xfs_freeze", 0); 145 atomic_set(&mp->m_active_trans, 0); 146 147 /* 148 * Moved from elsewhere. Linux initializes those 'on demand' part 149 * way through mount code. This spells trouble for FreeBSD and 150 * WITNESS because the mount can fail and we hit lock destruction 151 * code for locks that might not even been initialized yet. 152 */ 153 spinlock_init(&mp->m_agirotor_lock, "m_agirotor_lock"); 154 155 return mp; 156} 157 158/* 159 * Free up the resources associated with a mount structure. Assume that 160 * the structure was initially zeroed, so we can tell which fields got 161 * initialized. 162 */ 163void 164xfs_mount_free( 165 xfs_mount_t *mp, 166 int remove_bhv) 167{ 168 if (mp->m_ihash) 169 xfs_ihash_free(mp); 170 if (mp->m_chash) 171 xfs_chash_free(mp); 172 173 if (mp->m_perag) { 174 int agno; 175 176 for (agno = 0; agno < mp->m_maxagi; agno++) { 177 if (mp->m_perag[agno].pagf_init) 178 spinlock_destroy(&mp->m_perag[agno].pagb_lock); 179 if (mp->m_perag[agno].pagb_list) 180 kmem_free(mp->m_perag[agno].pagb_list, 181 sizeof(xfs_perag_busy_t) * 182 XFS_PAGB_NUM_SLOTS); 183 } 184 kmem_free(mp->m_perag, 185 sizeof(xfs_perag_t) * mp->m_sb.sb_agcount); 186 187 free_rwsem(&mp->m_peraglock); 188 } 189 190 AIL_LOCK_DESTROY(&mp->m_ail_lock); 191 spinlock_destroy(&mp->m_sb_lock); 192 spinlock_destroy(&mp->m_agirotor_lock); 193 mutex_destroy(&mp->m_ilock); 194 freesema(&mp->m_growlock); 195 if (mp->m_quotainfo) 196 XFS_QM_DONE(mp); 197 198 if (mp->m_fsname != NULL) 199 kmem_free(mp->m_fsname, mp->m_fsname_len); 200 201 if (remove_bhv) { 202 struct xfs_vfs *vfsp = XFS_MTOVFS(mp); 203 204 bhv_remove_all_vfsops(vfsp, 0); 205 VFS_REMOVEBHV(vfsp, &mp->m_bhv); 206 } 207 208 spinlock_destroy(&mp->m_freeze_lock); 209 sv_destroy(&mp->m_wait_unfreeze); 210 kmem_free(mp, sizeof(xfs_mount_t)); 211} 212 213 214/* 215 * Check the validity of the SB found. 216 */ 217STATIC int 218xfs_mount_validate_sb( 219 xfs_mount_t *mp, 220 xfs_sb_t *sbp) 221{ 222 /* 223 * If the log device and data device have the 224 * same device number, the log is internal. 225 * Consequently, the sb_logstart should be non-zero. If 226 * we have a zero sb_logstart in this case, we may be trying to mount 227 * a volume filesystem in a non-volume manner. 228 */ 229 if (sbp->sb_magicnum != XFS_SB_MAGIC) { 230 cmn_err(CE_WARN, "XFS: bad magic number"); 231 return XFS_ERROR(EWRONGFS); 232 } 233 234 if (!XFS_SB_GOOD_VERSION(sbp)) { 235 cmn_err(CE_WARN, "XFS: bad version"); 236 return XFS_ERROR(EWRONGFS); 237 } 238 239 if (unlikely( 240 sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) { 241 cmn_err(CE_WARN, 242 "XFS: filesystem is marked as having an external log; " 243 "specify logdev on the\nmount command line."); 244 XFS_CORRUPTION_ERROR("xfs_mount_validate_sb(1)", 245 XFS_ERRLEVEL_HIGH, mp, sbp); 246 return XFS_ERROR(EFSCORRUPTED); 247 } 248 249 if (unlikely( 250 sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) { 251 cmn_err(CE_WARN, 252 "XFS: filesystem is marked as having an internal log; " 253 "don't specify logdev on\nthe mount command line."); 254 XFS_CORRUPTION_ERROR("xfs_mount_validate_sb(2)", 255 XFS_ERRLEVEL_HIGH, mp, sbp); 256 return XFS_ERROR(EFSCORRUPTED); 257 } 258 259 /* 260 * More sanity checking. These were stolen directly from 261 * xfs_repair. 262 */ 263 if (unlikely( 264 sbp->sb_agcount <= 0 || 265 sbp->sb_sectsize < XFS_MIN_SECTORSIZE || 266 sbp->sb_sectsize > XFS_MAX_SECTORSIZE || 267 sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG || 268 sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG || 269 sbp->sb_blocksize < XFS_MIN_BLOCKSIZE || 270 sbp->sb_blocksize > XFS_MAX_BLOCKSIZE || 271 sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG || 272 sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG || 273 sbp->sb_inodesize < XFS_DINODE_MIN_SIZE || 274 sbp->sb_inodesize > XFS_DINODE_MAX_SIZE || 275 (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) || 276 (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) || 277 sbp->sb_imax_pct > 100)) { 278 cmn_err(CE_WARN, "XFS: SB sanity check 1 failed"); 279 XFS_CORRUPTION_ERROR("xfs_mount_validate_sb(3)", 280 XFS_ERRLEVEL_LOW, mp, sbp); 281 return XFS_ERROR(EFSCORRUPTED); 282 } 283 284 /* 285 * Sanity check AG count, size fields against data size field 286 */ 287 if (unlikely( 288 sbp->sb_dblocks == 0 || 289 sbp->sb_dblocks > 290 (xfs_drfsbno_t)sbp->sb_agcount * sbp->sb_agblocks || 291 sbp->sb_dblocks < (xfs_drfsbno_t)(sbp->sb_agcount - 1) * 292 sbp->sb_agblocks + XFS_MIN_AG_BLOCKS)) { 293 cmn_err(CE_WARN, "XFS: SB sanity check 2 failed"); 294 XFS_ERROR_REPORT("xfs_mount_validate_sb(4)", 295 XFS_ERRLEVEL_LOW, mp); 296 return XFS_ERROR(EFSCORRUPTED); 297 } 298 299#if !XFS_BIG_BLKNOS 300 if (unlikely( 301 (sbp->sb_dblocks << (__uint64_t)(sbp->sb_blocklog - BBSHIFT)) 302 > UINT_MAX || 303 (sbp->sb_rblocks << (__uint64_t)(sbp->sb_blocklog - BBSHIFT)) 304 > UINT_MAX)) { 305 cmn_err(CE_WARN, 306 "XFS: File system is too large to be mounted on this system."); 307 return XFS_ERROR(E2BIG); 308 } 309#endif 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 /* 319 * Until this is fixed only page-sized or smaller data blocks work. 320 */ 321 if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) { 322 cmn_err(CE_WARN, 323 "XFS: Attempted to mount file system with blocksize %d bytes", 324 sbp->sb_blocksize); 325 cmn_err(CE_WARN, 326 "XFS: Only page-sized (%d) or less blocksizes currently work.", 327 PAGE_SIZE); 328 return XFS_ERROR(ENOSYS); 329 } 330 331 return 0; 332} 333 334void 335xfs_initialize_perag(xfs_mount_t *mp, int agcount) 336{ 337 int index, max_metadata; 338 xfs_perag_t *pag; 339 xfs_agino_t agino; 340 xfs_ino_t ino; 341 xfs_sb_t *sbp = &mp->m_sb; 342 xfs_ino_t max_inum = XFS_MAXINUMBER_32; 343 344 /* Check to see if the filesystem can overflow 32 bit inodes */ 345 agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0); 346 ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino); 347 348 /* Clear the mount flag if no inode can overflow 32 bits 349 * on this filesystem, or if specifically requested.. 350 */ 351 if ((mp->m_flags & XFS_MOUNT_32BITINOOPT) && ino > max_inum) { 352 mp->m_flags |= XFS_MOUNT_32BITINODES; 353 } else { 354 mp->m_flags &= ~XFS_MOUNT_32BITINODES; 355 } 356 357 /* If we can overflow then setup the ag headers accordingly */ 358 if (mp->m_flags & XFS_MOUNT_32BITINODES) { 359 /* Calculate how much should be reserved for inodes to 360 * meet the max inode percentage. 361 */ 362 if (mp->m_maxicount) { 363 __uint64_t icount; 364 365 icount = sbp->sb_dblocks * sbp->sb_imax_pct; 366 do_div(icount, 100); 367 icount += sbp->sb_agblocks - 1; 368 do_div(icount, mp->m_ialloc_blks); 369 max_metadata = icount; 370 } else { 371 max_metadata = agcount; 372 } 373 for (index = 0; index < agcount; index++) { 374 ino = XFS_AGINO_TO_INO(mp, index, agino); 375 if (ino > max_inum) { 376 index++; 377 break; 378 } 379 380 /* This ag is prefered for inodes */ 381 pag = &mp->m_perag[index]; 382 pag->pagi_inodeok = 1; 383 if (index < max_metadata) 384 pag->pagf_metadata = 1; 385 } 386 } else { 387 /* Setup default behavior for smaller filesystems */ 388 for (index = 0; index < agcount; index++) { 389 pag = &mp->m_perag[index]; 390 pag->pagi_inodeok = 1; 391 } 392 } 393 mp->m_maxagi = index; 394} 395 396/* 397 * xfs_xlatesb 398 * 399 * data - on disk version of sb 400 * sb - a superblock 401 * dir - conversion direction: <0 - convert sb to buf 402 * >0 - convert buf to sb 403 * arch - architecture to read/write from/to buf 404 * fields - which fields to copy (bitmask) 405 */ 406void 407xfs_xlatesb( 408 void *data, 409 xfs_sb_t *sb, 410 int dir, 411 xfs_arch_t arch, 412 __int64_t fields) 413{ 414 xfs_caddr_t buf_ptr; 415 xfs_caddr_t mem_ptr; 416 xfs_sb_field_t f; 417 int first; 418 int size; 419 420 ASSERT(dir); 421 ASSERT(fields); 422 423 if (!fields) 424 return; 425 426 buf_ptr = (xfs_caddr_t)data; 427 mem_ptr = (xfs_caddr_t)sb; 428 429 while (fields) { 430 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields); 431 first = xfs_sb_info[f].offset; 432 size = xfs_sb_info[f + 1].offset - first; 433 434 ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1); 435 436 if (arch == ARCH_NOCONVERT || 437 size == 1 || 438 xfs_sb_info[f].type == 1) { 439 if (dir > 0) { 440 memcpy(mem_ptr + first, buf_ptr + first, size); 441 } else { 442 memcpy(buf_ptr + first, mem_ptr + first, size); 443 } 444 } else { 445 switch (size) { 446 case 2: 447 INT_XLATE(*(__uint16_t*)(buf_ptr+first), 448 *(__uint16_t*)(mem_ptr+first), 449 dir, arch); 450 break; 451 case 4: 452 INT_XLATE(*(__uint32_t*)(buf_ptr+first), 453 *(__uint32_t*)(mem_ptr+first), 454 dir, arch); 455 break; 456 case 8: 457 INT_XLATE(*(__uint64_t*)(buf_ptr+first), 458 *(__uint64_t*)(mem_ptr+first), dir, arch); 459 break; 460 default: 461 ASSERT(0); 462 } 463 } 464 465 fields &= ~(1LL << f); 466 } 467} 468 469/* 470 * xfs_readsb 471 * 472 * Does the initial read of the superblock. 473 */ 474int 475xfs_readsb(xfs_mount_t *mp) 476{ 477 unsigned int sector_size; 478 unsigned int extra_flags; 479 xfs_buf_t *bp; 480 xfs_sb_t *sbp; 481 int error; 482 483 ASSERT(mp->m_sb_bp == NULL); 484 ASSERT(mp->m_ddev_targp != NULL); 485 486 /* 487 * Allocate a (locked) buffer to hold the superblock. 488 * This will be kept around at all times to optimize 489 * access to the superblock. 490 */ 491 sector_size = xfs_getsize_buftarg(mp->m_ddev_targp); 492 extra_flags = XFS_BUF_LOCK | XFS_BUF_MANAGE | XFS_BUF_MAPPED; 493 494 bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR, 495 BTOBB(sector_size), extra_flags); 496 if (!bp || XFS_BUF_ISERROR(bp)) { 497 cmn_err(CE_WARN, "XFS: SB read failed"); 498 error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM; 499 goto fail; 500 } 501 ASSERT(XFS_BUF_ISBUSY(bp)); 502 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0); 503 504 /* 505 * Initialize the mount structure from the superblock. 506 * But first do some basic consistency checking. 507 */ 508 sbp = XFS_BUF_TO_SBP(bp); 509 xfs_xlatesb(XFS_BUF_PTR(bp), &(mp->m_sb), 1, 510 ARCH_CONVERT, XFS_SB_ALL_BITS); 511 512 error = xfs_mount_validate_sb(mp, &(mp->m_sb)); 513 if (error) { 514 cmn_err(CE_WARN, "XFS: SB validate failed"); 515 goto fail; 516 } 517 518 /* 519 * We must be able to do sector-sized and sector-aligned IO. 520 */ 521 if (sector_size > mp->m_sb.sb_sectsize) { 522 cmn_err(CE_WARN, 523 "XFS: device supports only %u byte sectors (not %u)", 524 sector_size, mp->m_sb.sb_sectsize); 525 error = ENOSYS; 526 goto fail; 527 } 528 529 /* 530 * If device sector size is smaller than the superblock size, 531 * re-read the superblock so the buffer is correctly sized. 532 */ 533 if (sector_size < mp->m_sb.sb_sectsize) { 534 XFS_BUF_UNMANAGE(bp); 535 xfs_buf_relse(bp); 536 sector_size = mp->m_sb.sb_sectsize; 537 bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR, 538 BTOBB(sector_size), extra_flags); 539 if (!bp || XFS_BUF_ISERROR(bp)) { 540 cmn_err(CE_WARN, "XFS: SB re-read failed"); 541 error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM; 542 goto fail; 543 } 544 ASSERT(XFS_BUF_ISBUSY(bp)); 545 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0); 546 } 547 548 mp->m_sb_bp = bp; 549 xfs_buf_relse(bp); 550 ASSERT(XFS_BUF_VALUSEMA(bp) > 0); 551 return 0; 552 553 fail: 554 if (bp) { 555 XFS_BUF_UNMANAGE(bp); 556 xfs_buf_relse(bp); 557 } 558 return error; 559} 560 561 562/* 563 * xfs_mount_common 564 * 565 * Mount initialization code establishing various mount 566 * fields from the superblock associated with the given 567 * mount structure 568 */ 569STATIC void 570xfs_mount_common(xfs_mount_t *mp, xfs_sb_t *sbp) 571{ 572 int i; 573 574 mp->m_agfrotor = mp->m_agirotor = 0; 575 mp->m_maxagi = mp->m_sb.sb_agcount; 576 mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG; 577 mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT; 578 mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT; 579 mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1; 580 mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog; 581 mp->m_litino = sbp->sb_inodesize - 582 ((uint)sizeof(xfs_dinode_core_t) + (uint)sizeof(xfs_agino_t)); 583 mp->m_blockmask = sbp->sb_blocksize - 1; 584 mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG; 585 mp->m_blockwmask = mp->m_blockwsize - 1; 586 587 TAILQ_INIT(&mp->m_del_inodes); 588 589 /* 590 * Setup for attributes, in case they get created. 591 * This value is for inodes getting attributes for the first time, 592 * the per-inode value is for old attribute values. 593 */ 594 ASSERT(sbp->sb_inodesize >= 256 && sbp->sb_inodesize <= 2048); 595 switch (sbp->sb_inodesize) { 596 case 256: 597 mp->m_attroffset = XFS_LITINO(mp) - XFS_BMDR_SPACE_CALC(2); 598 break; 599 case 512: 600 case 1024: 601 case 2048: 602 mp->m_attroffset = XFS_BMDR_SPACE_CALC(12); 603 break; 604 default: 605 ASSERT(0); 606 } 607 ASSERT(mp->m_attroffset < XFS_LITINO(mp)); 608 609 for (i = 0; i < 2; i++) { 610 mp->m_alloc_mxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize, 611 xfs_alloc, i == 0); 612 mp->m_alloc_mnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize, 613 xfs_alloc, i == 0); 614 } 615 for (i = 0; i < 2; i++) { 616 mp->m_bmap_dmxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize, 617 xfs_bmbt, i == 0); 618 mp->m_bmap_dmnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize, 619 xfs_bmbt, i == 0); 620 } 621 for (i = 0; i < 2; i++) { 622 mp->m_inobt_mxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize, 623 xfs_inobt, i == 0); 624 mp->m_inobt_mnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize, 625 xfs_inobt, i == 0); 626 } 627 628 mp->m_bsize = XFS_FSB_TO_BB(mp, 1); 629 mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK, 630 sbp->sb_inopblock); 631 mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog; 632} 633/* 634 * xfs_mountfs 635 * 636 * This function does the following on an initial mount of a file system: 637 * - reads the superblock from disk and init the mount struct 638 * - if we're a 32-bit kernel, do a size check on the superblock 639 * so we don't mount terabyte filesystems 640 * - init mount struct realtime fields 641 * - allocate inode hash table for fs 642 * - init directory manager 643 * - perform recovery and init the log manager 644 */ 645int 646xfs_mountfs( 647 xfs_vfs_t *vfsp, 648 xfs_mount_t *mp, 649 int mfsi_flags) 650{ 651 xfs_buf_t *bp; 652 xfs_sb_t *sbp = &(mp->m_sb); 653 xfs_inode_t *rip; 654 xfs_vnode_t *rvp = 0; 655 int readio_log, writeio_log; 656 vmap_t vmap; 657 xfs_daddr_t d; 658 __uint64_t ret64; 659 __int64_t update_flags; 660 uint quotamount, quotaflags; 661 int agno; 662 int uuid_mounted = 0; 663 int error = 0; 664 665 if (mp->m_sb_bp == NULL) { 666 if ((error = xfs_readsb(mp))) { 667 return (error); 668 } 669 } 670 xfs_mount_common(mp, sbp); 671 672 /* 673 * Check if sb_agblocks is aligned at stripe boundary 674 * If sb_agblocks is NOT aligned turn off m_dalign since 675 * allocator alignment is within an ag, therefore ag has 676 * to be aligned at stripe boundary. 677 */ 678 update_flags = 0LL; 679 if (mp->m_dalign && !(mfsi_flags & XFS_MFSI_SECOND)) { 680 /* 681 * If stripe unit and stripe width are not multiples 682 * of the fs blocksize turn off alignment. 683 */ 684 if ((BBTOB(mp->m_dalign) & mp->m_blockmask) || 685 (BBTOB(mp->m_swidth) & mp->m_blockmask)) { 686 if (mp->m_flags & XFS_MOUNT_RETERR) { 687 cmn_err(CE_WARN, 688 "XFS: alignment check 1 failed"); 689 error = XFS_ERROR(EINVAL); 690 goto error1; 691 } 692 mp->m_dalign = mp->m_swidth = 0; 693 } else { 694 /* 695 * Convert the stripe unit and width to FSBs. 696 */ 697 mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign); 698 if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) { 699 if (mp->m_flags & XFS_MOUNT_RETERR) { 700 error = XFS_ERROR(EINVAL); 701 goto error1; 702 } 703 mp->m_dalign = 0; 704 mp->m_swidth = 0; 705 } else if (mp->m_dalign) { 706 mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth); 707 } else { 708 if (mp->m_flags & XFS_MOUNT_RETERR) { 709 cmn_err(CE_WARN, 710 "XFS: alignment check 3 failed"); 711 error = XFS_ERROR(EINVAL); 712 goto error1; 713 } 714 mp->m_swidth = 0; 715 } 716 } 717 718 /* 719 * Update superblock with new values 720 * and log changes 721 */ 722 if (XFS_SB_VERSION_HASDALIGN(sbp)) { 723 if (sbp->sb_unit != mp->m_dalign) { 724 sbp->sb_unit = mp->m_dalign; 725 update_flags |= XFS_SB_UNIT; 726 } 727 if (sbp->sb_width != mp->m_swidth) { 728 sbp->sb_width = mp->m_swidth; 729 update_flags |= XFS_SB_WIDTH; 730 } 731 } 732 } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN && 733 XFS_SB_VERSION_HASDALIGN(&mp->m_sb)) { 734 mp->m_dalign = sbp->sb_unit; 735 mp->m_swidth = sbp->sb_width; 736 } 737 738 xfs_alloc_compute_maxlevels(mp); 739 xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK); 740 xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK); 741 xfs_ialloc_compute_maxlevels(mp); 742 743 if (sbp->sb_imax_pct) { 744 __uint64_t icount; 745 746 /* Make sure the maximum inode count is a multiple of the 747 * units we allocate inodes in. 748 */ 749 750 icount = sbp->sb_dblocks * sbp->sb_imax_pct; 751 do_div(icount, 100); 752 do_div(icount, mp->m_ialloc_blks); 753 mp->m_maxicount = (icount * mp->m_ialloc_blks) << 754 sbp->sb_inopblog; 755 } else 756 mp->m_maxicount = 0; 757 758 mp->m_maxioffset = xfs_max_file_offset(sbp->sb_blocklog); 759 760 /* 761 * XFS uses the uuid from the superblock as the unique 762 * identifier for fsid. We can not use the uuid from the volume 763 * since a single partition filesystem is identical to a single 764 * partition volume/filesystem. 765 */ 766 if ((mfsi_flags & XFS_MFSI_SECOND) == 0 && 767 (mp->m_flags & XFS_MOUNT_NOUUID) == 0) { 768 if (xfs_uuid_mount(mp)) { 769 error = XFS_ERROR(EINVAL); 770 goto error1; 771 } 772 uuid_mounted=1; 773 ret64 = uuid_hash64(&sbp->sb_uuid); 774 memcpy(&vfsp->vfs_fsid, &ret64, sizeof(ret64)); 775 } 776 777 /* 778 * Set the default minimum read and write sizes unless 779 * already specified in a mount option. 780 * We use smaller I/O sizes when the file system 781 * is being used for NFS service (wsync mount option). 782 */ 783 if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) { 784 if (mp->m_flags & XFS_MOUNT_WSYNC) { 785 readio_log = XFS_WSYNC_READIO_LOG; 786 writeio_log = XFS_WSYNC_WRITEIO_LOG; 787 } else { 788 readio_log = XFS_READIO_LOG_LARGE; 789 writeio_log = XFS_WRITEIO_LOG_LARGE; 790 } 791 } else { 792 readio_log = mp->m_readio_log; 793 writeio_log = mp->m_writeio_log; 794 } 795 796 /* 797 * Set the number of readahead buffers to use based on 798 * physical memory size. 799 */ 800 if (xfs_physmem <= 4096) /* <= 16MB */ 801 mp->m_nreadaheads = XFS_RW_NREADAHEAD_16MB; 802 else if (xfs_physmem <= 8192) /* <= 32MB */ 803 mp->m_nreadaheads = XFS_RW_NREADAHEAD_32MB; 804 else 805 mp->m_nreadaheads = XFS_RW_NREADAHEAD_K32; 806 if (sbp->sb_blocklog > readio_log) { 807 mp->m_readio_log = sbp->sb_blocklog; 808 } else { 809 mp->m_readio_log = readio_log; 810 } 811 mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog); 812 if (sbp->sb_blocklog > writeio_log) { 813 mp->m_writeio_log = sbp->sb_blocklog; 814 } else { 815 mp->m_writeio_log = writeio_log; 816 } 817 mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog); 818 819 /* 820 * Set the inode cluster size based on the physical memory 821 * size. This may still be overridden by the file system 822 * block size if it is larger than the chosen cluster size. 823 */ 824 if (xfs_physmem <= btoc(32 * 1024 * 1024)) { /* <= 32 MB */ 825 mp->m_inode_cluster_size = XFS_INODE_SMALL_CLUSTER_SIZE; 826 } else { 827 mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE; 828 } 829 /* 830 * Set whether we're using inode alignment. 831 */ 832 if (XFS_SB_VERSION_HASALIGN(&mp->m_sb) && 833 mp->m_sb.sb_inoalignmt >= 834 XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size)) 835 mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1; 836 else 837 mp->m_inoalign_mask = 0; 838 /* 839 * If we are using stripe alignment, check whether 840 * the stripe unit is a multiple of the inode alignment 841 */ 842 if (mp->m_dalign && mp->m_inoalign_mask && 843 !(mp->m_dalign & mp->m_inoalign_mask)) 844 mp->m_sinoalign = mp->m_dalign; 845 else 846 mp->m_sinoalign = 0; 847 /* 848 * Check that the data (and log if separate) are an ok size. 849 */ 850 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks); 851 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) { 852 cmn_err(CE_WARN, "XFS: size check 1 failed"); 853 error = XFS_ERROR(E2BIG); 854 goto error1; 855 } 856 error = xfs_read_buf(mp, mp->m_ddev_targp, 857 d - XFS_FSS_TO_BB(mp, 1), 858 XFS_FSS_TO_BB(mp, 1), 0, &bp); 859 if (!error) { 860 xfs_buf_relse(bp); 861 } else { 862 cmn_err(CE_WARN, "XFS: size check 2 failed"); 863 if (error == ENOSPC) { 864 error = XFS_ERROR(E2BIG); 865 } 866 goto error1; 867 } 868 869 if (((mfsi_flags & XFS_MFSI_CLIENT) == 0) && 870 mp->m_logdev_targp != mp->m_ddev_targp) { 871 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks); 872 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) { 873 cmn_err(CE_WARN, "XFS: size check 3 failed"); 874 error = XFS_ERROR(E2BIG); 875 goto error1; 876 } 877 error = xfs_read_buf(mp, mp->m_logdev_targp, 878 d - XFS_FSB_TO_BB(mp, 1), 879 XFS_FSB_TO_BB(mp, 1), 0, &bp); 880 if (!error) { 881 xfs_buf_relse(bp); 882 } else { 883 cmn_err(CE_WARN, "XFS: size check 3 failed"); 884 if (error == ENOSPC) { 885 error = XFS_ERROR(E2BIG); 886 } 887 goto error1; 888 } 889 } 890 891 /* 892 * Initialize realtime fields in the mount structure 893 */ 894 if ((error = xfs_rtmount_init(mp))) { 895 cmn_err(CE_WARN, "XFS: RT mount failed"); 896 goto error1; 897 } 898 899 /* 900 * For client case we are done now 901 */ 902 if (mfsi_flags & XFS_MFSI_CLIENT) { 903 return(0); 904 } 905 906 /* 907 * Copies the low order bits of the timestamp and the randomly 908 * set "sequence" number out of a UUID. 909 */ 910 uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid); 911 912 /* 913 * The vfs structure needs to have a file system independent 914 * way of checking for the invariant file system ID. Since it 915 * can't look at mount structures it has a pointer to the data 916 * in the mount structure. 917 * 918 * File systems that don't support user level file handles (i.e. 919 * all of them except for XFS) will leave vfs_altfsid as NULL. 920 */ 921 vfsp->vfs_altfsid = (xfs_fsid_t *)mp->m_fixedfsid; 922 mp->m_dmevmask = 0; /* not persistent; set after each mount */ 923 924 /* 925 * Select the right directory manager. 926 */ 927 mp->m_dirops = 928 XFS_SB_VERSION_HASDIRV2(&mp->m_sb) ? 929 xfsv2_dirops : 930 xfsv1_dirops; 931 932 /* 933 * Initialize directory manager's entries. 934 */ 935 XFS_DIR_MOUNT(mp); 936 937 /* 938 * Initialize the attribute manager's entries. 939 */ 940 mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100; 941 942 /* 943 * Initialize the precomputed transaction reservations values. 944 */ 945 xfs_trans_init(mp); 946 947 /* 948 * Allocate and initialize the inode hash table for this 949 * file system. 950 */ 951 xfs_ihash_init(mp); 952 xfs_chash_init(mp); 953 954 /* 955 * Allocate and initialize the per-ag data. 956 */ 957 init_rwsem(&mp->m_peraglock); 958 mp->m_perag = 959 kmem_zalloc(sbp->sb_agcount * sizeof(xfs_perag_t), KM_SLEEP); 960 961 xfs_initialize_perag(mp, sbp->sb_agcount); 962 963 /* 964 * log's mount-time initialization. Perform 1st part recovery if needed 965 */ 966 if (likely(sbp->sb_logblocks > 0)) { /* check for volume case */ 967 error = xfs_log_mount(mp, mp->m_logdev_targp, 968 XFS_FSB_TO_DADDR(mp, sbp->sb_logstart), 969 XFS_FSB_TO_BB(mp, sbp->sb_logblocks)); 970 if (error) { 971 cmn_err(CE_WARN, "XFS: log mount failed"); 972 goto error2; 973 } 974 } else { /* No log has been defined */ 975 cmn_err(CE_WARN, "XFS: no log defined"); 976 XFS_ERROR_REPORT("xfs_mountfs_int(1)", XFS_ERRLEVEL_LOW, mp); 977 error = XFS_ERROR(EFSCORRUPTED); 978 goto error2; 979 } 980 981 /* 982 * Get and sanity-check the root inode. 983 * Save the pointer to it in the mount structure. 984 */ 985 error = xfs_iget(mp, NULL, sbp->sb_rootino, XFS_ILOCK_EXCL, &rip, 0); 986 if (error) { 987 cmn_err(CE_WARN, "XFS: failed to read root inode"); 988 goto error3; 989 } 990 991 ASSERT(rip != NULL); 992 rvp = XFS_ITOV(rip); 993 VMAP(rvp, vmap); 994 995 if (unlikely((rip->i_d.di_mode & S_IFMT) != S_IFDIR)) { 996 cmn_err(CE_WARN, "XFS: corrupted root inode"); 997 prdev("Root inode %llu is not a directory", 998 mp->m_ddev_targp, (unsigned long long)rip->i_ino); 999 xfs_iunlock(rip, XFS_ILOCK_EXCL); 1000 XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW, 1001 mp); 1002 error = XFS_ERROR(EFSCORRUPTED); 1003 goto error4; 1004 } 1005 mp->m_rootip = rip; /* save it */ 1006 1007 xfs_iunlock(rip, XFS_ILOCK_EXCL); 1008 1009 /* 1010 * Initialize realtime inode pointers in the mount structure 1011 */ 1012 if ((error = xfs_rtmount_inodes(mp))) { 1013 /* 1014 * Free up the root inode. 1015 */ 1016 cmn_err(CE_WARN, "XFS: failed to read RT inodes"); 1017 goto error4; 1018 } 1019 1020 /* 1021 * If fs is not mounted readonly, then update the superblock 1022 * unit and width changes. 1023 */ 1024 if (update_flags && !(vfsp->vfs_flag & VFS_RDONLY)) 1025 xfs_mount_log_sbunit(mp, update_flags); 1026 1027 /* 1028 * Initialise the XFS quota management subsystem for this mount 1029 */ 1030 if ((error = XFS_QM_INIT(mp, "amount, "aflags))) 1031 goto error4; 1032 1033 /* 1034 * Finish recovering the file system. This part needed to be 1035 * delayed until after the root and real-time bitmap inodes 1036 * were consistently read in. 1037 */ 1038 error = xfs_log_mount_finish(mp, mfsi_flags); 1039 if (error) { 1040 cmn_err(CE_WARN, "XFS: log mount finish failed"); 1041 goto error4; 1042 } 1043 1044 /* 1045 * Complete the quota initialisation, post-log-replay component. 1046 */ 1047 if ((error = XFS_QM_MOUNT(mp, quotamount, quotaflags))) 1048 goto error4; 1049 1050 return 0; 1051 1052 error4: 1053 /* 1054 * Free up the root inode. 1055 */ 1056 VN_RELE(rvp); 1057 vn_purge(rvp, &vmap); 1058 error3: 1059 xfs_log_unmount_dealloc(mp); 1060 error2: 1061 xfs_ihash_free(mp); 1062 xfs_chash_free(mp); 1063 for (agno = 0; agno < sbp->sb_agcount; agno++) 1064 if (mp->m_perag[agno].pagb_list) 1065 kmem_free(mp->m_perag[agno].pagb_list, 1066 sizeof(xfs_perag_busy_t) * XFS_PAGB_NUM_SLOTS); 1067 kmem_free(mp->m_perag, sbp->sb_agcount * sizeof(xfs_perag_t)); 1068 mp->m_perag = NULL; 1069 /* FALLTHROUGH */ 1070 error1: 1071 if (uuid_mounted) 1072 xfs_uuid_unmount(mp); 1073 xfs_freesb(mp); 1074 return error; 1075} 1076 1077/* 1078 * xfs_unmountfs 1079 * 1080 * This flushes out the inodes,dquots and the superblock, unmounts the 1081 * log and makes sure that incore structures are freed. 1082 */ 1083int 1084xfs_unmountfs(xfs_mount_t *mp, struct cred *cr) 1085{ 1086 struct xfs_vfs *vfsp = XFS_MTOVFS(mp); 1087#if defined(DEBUG) || defined(INDUCE_IO_ERROR) 1088 int64_t fsid; 1089#endif 1090 1091 xfs_iflush_all(mp, XFS_FLUSH_ALL); 1092 1093 XFS_QM_DQPURGEALL(mp, 1094 XFS_QMOPT_UQUOTA | XFS_QMOPT_GQUOTA | XFS_QMOPT_UMOUNTING); 1095 1096 /* 1097 * Flush out the log synchronously so that we know for sure 1098 * that nothing is pinned. This is important because bflush() 1099 * will skip pinned buffers. 1100 */ 1101 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC); 1102 1103 xfs_binval(mp->m_ddev_targp); 1104 if (mp->m_rtdev_targp) { 1105 xfs_binval(mp->m_rtdev_targp); 1106 } 1107 1108 xfs_unmountfs_writesb(mp); 1109 1110 xfs_log_unmount(mp); /* Done! No more fs ops. */ 1111 1112 xfs_freesb(mp); 1113 1114 /* 1115 * All inodes from this mount point should be freed. 1116 */ 1117 ASSERT(mp->m_inodes == NULL); 1118 1119 /* 1120 * We may have bufs that are in the process of getting written still. 1121 * We must wait for the I/O completion of those. The sync flag here 1122 * does a two pass iteration thru the bufcache. 1123 */ 1124 if (XFS_FORCED_SHUTDOWN(mp)) { 1125 xfs_incore_relse(mp->m_ddev_targp, 0, 1); /* synchronous */ 1126 } 1127 1128 xfs_unmountfs_close(mp, cr); 1129 if ((mp->m_flags & XFS_MOUNT_NOUUID) == 0) 1130 xfs_uuid_unmount(mp); 1131 1132#if defined(DEBUG) || defined(INDUCE_IO_ERROR) 1133 /* 1134 * clear all error tags on this filesystem 1135 */ 1136 memcpy(&fsid, &vfsp->vfs_fsid, sizeof(int64_t)); 1137 xfs_errortag_clearall_umount(fsid, mp->m_fsname, 0); 1138#endif 1139 XFS_IODONE(vfsp); 1140 xfs_mount_free(mp, 1); 1141 return 0; 1142} 1143 1144void 1145xfs_unmountfs_close(xfs_mount_t *mp, struct cred *cr) 1146{ 1147 int have_logdev = (mp->m_logdev_targp != mp->m_ddev_targp); 1148 1149 if (mp->m_ddev_targp) { 1150 xfs_blkdev_put(mp->m_ddev_targp->specvp); /* FreeBSD non-portable */ 1151 xfs_free_buftarg(mp->m_ddev_targp); 1152 mp->m_ddev_targp = NULL; 1153 } 1154 if (mp->m_rtdev_targp) { 1155 xfs_blkdev_put(mp->m_rtdev_targp->specvp); /* FreeBSD non-portable */ 1156 xfs_free_buftarg(mp->m_rtdev_targp); 1157 mp->m_rtdev_targp = NULL; 1158 } 1159 if (mp->m_logdev_targp && have_logdev) { 1160 xfs_blkdev_put(mp->m_logdev_targp->specvp); /* FreeBSD non-portable */ 1161 xfs_free_buftarg(mp->m_logdev_targp); 1162 mp->m_logdev_targp = NULL; 1163 } 1164} 1165 1166int 1167xfs_unmountfs_writesb(xfs_mount_t *mp) 1168{ 1169 xfs_buf_t *sbp; 1170 xfs_sb_t *sb; 1171 int error = 0; 1172 1173 /* 1174 * skip superblock write if fs is read-only, or 1175 * if we are doing a forced umount. 1176 */ 1177 sbp = xfs_getsb(mp, 0); 1178 if (!(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY || 1179 XFS_FORCED_SHUTDOWN(mp))) { 1180 /* 1181 * mark shared-readonly if desired 1182 */ 1183 sb = XFS_BUF_TO_SBP(sbp); 1184 if (mp->m_mk_sharedro) { 1185 if (!(sb->sb_flags & XFS_SBF_READONLY)) 1186 sb->sb_flags |= XFS_SBF_READONLY; 1187 if (!XFS_SB_VERSION_HASSHARED(sb)) 1188 XFS_SB_VERSION_ADDSHARED(sb); 1189 xfs_fs_cmn_err(CE_NOTE, mp, 1190 "Unmounting, marking shared read-only"); 1191 } 1192 /* Not on FreeBSD XFS_BUF_UNDONE(sbp); */ 1193 XFS_BUF_UNREAD(sbp); 1194 XFS_BUF_UNDELAYWRITE(sbp); 1195 XFS_BUF_WRITE(sbp); 1196 XFS_BUF_UNASYNC(sbp); 1197 ASSERT(XFS_BUF_TARGET(sbp) == mp->m_ddev_targp); 1198 xfsbdstrat(mp, sbp); 1199 /* Nevermind errors we might get here. */ 1200 error = xfs_iowait(sbp); 1201 if (error) 1202 xfs_ioerror_alert("xfs_unmountfs_writesb", 1203 mp, sbp, XFS_BUF_ADDR(sbp)); 1204 if (error && mp->m_mk_sharedro) 1205 xfs_fs_cmn_err(CE_ALERT, mp, "Superblock write error detected while unmounting. Filesystem may not be marked shared readonly"); 1206 } 1207 xfs_buf_relse(sbp); 1208 return (error); 1209} 1210 1211/* 1212 * xfs_mod_sb() can be used to copy arbitrary changes to the 1213 * in-core superblock into the superblock buffer to be logged. 1214 * It does not provide the higher level of locking that is 1215 * needed to protect the in-core superblock from concurrent 1216 * access. 1217 */ 1218void 1219xfs_mod_sb(xfs_trans_t *tp, __int64_t fields) 1220{ 1221 xfs_buf_t *bp; 1222 int first; 1223 int last; 1224 xfs_mount_t *mp; 1225 xfs_sb_t *sbp; 1226 xfs_sb_field_t f; 1227 1228 ASSERT(fields); 1229 if (!fields) 1230 return; 1231 mp = tp->t_mountp; 1232 bp = xfs_trans_getsb(tp, mp, 0); 1233 sbp = XFS_BUF_TO_SBP(bp); 1234 first = sizeof(xfs_sb_t); 1235 last = 0; 1236 1237 /* translate/copy */ 1238 1239 xfs_xlatesb(XFS_BUF_PTR(bp), &(mp->m_sb), -1, ARCH_CONVERT, fields); 1240 1241 /* find modified range */ 1242 1243 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields); 1244 ASSERT((1LL << f) & XFS_SB_MOD_BITS); 1245 first = xfs_sb_info[f].offset; 1246 1247 f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields); 1248 ASSERT((1LL << f) & XFS_SB_MOD_BITS); 1249 last = xfs_sb_info[f + 1].offset - 1; 1250 1251 xfs_trans_log_buf(tp, bp, first, last); 1252} 1253 1254/* 1255 * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply 1256 * a delta to a specified field in the in-core superblock. Simply 1257 * switch on the field indicated and apply the delta to that field. 1258 * Fields are not allowed to dip below zero, so if the delta would 1259 * do this do not apply it and return EINVAL. 1260 * 1261 * The SB_LOCK must be held when this routine is called. 1262 */ 1263STATIC int 1264xfs_mod_incore_sb_unlocked(xfs_mount_t *mp, xfs_sb_field_t field, 1265 int delta, int rsvd) 1266{ 1267 int scounter; /* short counter for 32 bit fields */ 1268 long long lcounter; /* long counter for 64 bit fields */ 1269 long long res_used, rem; 1270 1271 /* 1272 * With the in-core superblock spin lock held, switch 1273 * on the indicated field. Apply the delta to the 1274 * proper field. If the fields value would dip below 1275 * 0, then do not apply the delta and return EINVAL. 1276 */ 1277 switch (field) { 1278 case XFS_SBS_ICOUNT: 1279 lcounter = (long long)mp->m_sb.sb_icount; 1280 lcounter += delta; 1281 if (lcounter < 0) { 1282 ASSERT(0); 1283 return (XFS_ERROR(EINVAL)); 1284 } 1285 mp->m_sb.sb_icount = lcounter; 1286 return (0); 1287 case XFS_SBS_IFREE: 1288 lcounter = (long long)mp->m_sb.sb_ifree; 1289 lcounter += delta; 1290 if (lcounter < 0) { 1291 ASSERT(0); 1292 return (XFS_ERROR(EINVAL)); 1293 } 1294 mp->m_sb.sb_ifree = lcounter; 1295 return (0); 1296 case XFS_SBS_FDBLOCKS: 1297 1298 lcounter = (long long)mp->m_sb.sb_fdblocks; 1299 res_used = (long long)(mp->m_resblks - mp->m_resblks_avail); 1300 1301 if (delta > 0) { /* Putting blocks back */ 1302 if (res_used > delta) { 1303 mp->m_resblks_avail += delta; 1304 } else { 1305 rem = delta - res_used; 1306 mp->m_resblks_avail = mp->m_resblks; 1307 lcounter += rem; 1308 } 1309 } else { /* Taking blocks away */ 1310 1311 lcounter += delta; 1312 1313 /* 1314 * If were out of blocks, use any available reserved blocks if 1315 * were allowed to. 1316 */ 1317 1318 if (lcounter < 0) { 1319 if (rsvd) { 1320 lcounter = (long long)mp->m_resblks_avail + delta; 1321 if (lcounter < 0) { 1322 return (XFS_ERROR(ENOSPC)); 1323 } 1324 mp->m_resblks_avail = lcounter; 1325 return (0); 1326 } else { /* not reserved */ 1327 return (XFS_ERROR(ENOSPC)); 1328 } 1329 } 1330 } 1331 1332 mp->m_sb.sb_fdblocks = lcounter; 1333 return (0); 1334 case XFS_SBS_FREXTENTS: 1335 lcounter = (long long)mp->m_sb.sb_frextents; 1336 lcounter += delta; 1337 if (lcounter < 0) { 1338 return (XFS_ERROR(ENOSPC)); 1339 } 1340 mp->m_sb.sb_frextents = lcounter; 1341 return (0); 1342 case XFS_SBS_DBLOCKS: 1343 lcounter = (long long)mp->m_sb.sb_dblocks; 1344 lcounter += delta; 1345 if (lcounter < 0) { 1346 ASSERT(0); 1347 return (XFS_ERROR(EINVAL)); 1348 } 1349 mp->m_sb.sb_dblocks = lcounter; 1350 return (0); 1351 case XFS_SBS_AGCOUNT: 1352 scounter = mp->m_sb.sb_agcount; 1353 scounter += delta; 1354 if (scounter < 0) { 1355 ASSERT(0); 1356 return (XFS_ERROR(EINVAL)); 1357 } 1358 mp->m_sb.sb_agcount = scounter; 1359 return (0); 1360 case XFS_SBS_IMAX_PCT: 1361 scounter = mp->m_sb.sb_imax_pct; 1362 scounter += delta; 1363 if (scounter < 0) { 1364 ASSERT(0); 1365 return (XFS_ERROR(EINVAL)); 1366 } 1367 mp->m_sb.sb_imax_pct = scounter; 1368 return (0); 1369 case XFS_SBS_REXTSIZE: 1370 scounter = mp->m_sb.sb_rextsize; 1371 scounter += delta; 1372 if (scounter < 0) { 1373 ASSERT(0); 1374 return (XFS_ERROR(EINVAL)); 1375 } 1376 mp->m_sb.sb_rextsize = scounter; 1377 return (0); 1378 case XFS_SBS_RBMBLOCKS: 1379 scounter = mp->m_sb.sb_rbmblocks; 1380 scounter += delta; 1381 if (scounter < 0) { 1382 ASSERT(0); 1383 return (XFS_ERROR(EINVAL)); 1384 } 1385 mp->m_sb.sb_rbmblocks = scounter; 1386 return (0); 1387 case XFS_SBS_RBLOCKS: 1388 lcounter = (long long)mp->m_sb.sb_rblocks; 1389 lcounter += delta; 1390 if (lcounter < 0) { 1391 ASSERT(0); 1392 return (XFS_ERROR(EINVAL)); 1393 } 1394 mp->m_sb.sb_rblocks = lcounter; 1395 return (0); 1396 case XFS_SBS_REXTENTS: 1397 lcounter = (long long)mp->m_sb.sb_rextents; 1398 lcounter += delta; 1399 if (lcounter < 0) { 1400 ASSERT(0); 1401 return (XFS_ERROR(EINVAL)); 1402 } 1403 mp->m_sb.sb_rextents = lcounter; 1404 return (0); 1405 case XFS_SBS_REXTSLOG: 1406 scounter = mp->m_sb.sb_rextslog; 1407 scounter += delta; 1408 if (scounter < 0) { 1409 ASSERT(0); 1410 return (XFS_ERROR(EINVAL)); 1411 } 1412 mp->m_sb.sb_rextslog = scounter; 1413 return (0); 1414 default: 1415 ASSERT(0); 1416 return (XFS_ERROR(EINVAL)); 1417 } 1418} 1419 1420/* 1421 * xfs_mod_incore_sb() is used to change a field in the in-core 1422 * superblock structure by the specified delta. This modification 1423 * is protected by the SB_LOCK. Just use the xfs_mod_incore_sb_unlocked() 1424 * routine to do the work. 1425 */ 1426int 1427xfs_mod_incore_sb(xfs_mount_t *mp, xfs_sb_field_t field, int delta, int rsvd) 1428{ 1429 unsigned long s; 1430 int status; 1431 1432 s = XFS_SB_LOCK(mp); 1433 status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd); 1434 XFS_SB_UNLOCK(mp, s); 1435 return (status); 1436} 1437 1438/* 1439 * xfs_mod_incore_sb_batch() is used to change more than one field 1440 * in the in-core superblock structure at a time. This modification 1441 * is protected by a lock internal to this module. The fields and 1442 * changes to those fields are specified in the array of xfs_mod_sb 1443 * structures passed in. 1444 * 1445 * Either all of the specified deltas will be applied or none of 1446 * them will. If any modified field dips below 0, then all modifications 1447 * will be backed out and EINVAL will be returned. 1448 */ 1449int 1450xfs_mod_incore_sb_batch(xfs_mount_t *mp, xfs_mod_sb_t *msb, uint nmsb, int rsvd) 1451{ 1452 unsigned long s; 1453 int status=0; 1454 xfs_mod_sb_t *msbp; 1455 1456 /* 1457 * Loop through the array of mod structures and apply each 1458 * individually. If any fail, then back out all those 1459 * which have already been applied. Do all of this within 1460 * the scope of the SB_LOCK so that all of the changes will 1461 * be atomic. 1462 */ 1463 s = XFS_SB_LOCK(mp); 1464 msbp = &msb[0]; 1465 for (msbp = &msbp[0]; msbp < (msb + nmsb); msbp++) { 1466 /* 1467 * Apply the delta at index n. If it fails, break 1468 * from the loop so we'll fall into the undo loop 1469 * below. 1470 */ 1471 status = xfs_mod_incore_sb_unlocked(mp, msbp->msb_field, 1472 msbp->msb_delta, rsvd); 1473 if (status != 0) { 1474 break; 1475 } 1476 } 1477 1478 /* 1479 * If we didn't complete the loop above, then back out 1480 * any changes made to the superblock. If you add code 1481 * between the loop above and here, make sure that you 1482 * preserve the value of status. Loop back until 1483 * we step below the beginning of the array. Make sure 1484 * we don't touch anything back there. 1485 */ 1486 if (status != 0) { 1487 msbp--; 1488 while (msbp >= msb) { 1489 status = xfs_mod_incore_sb_unlocked(mp, 1490 msbp->msb_field, -(msbp->msb_delta), rsvd); 1491 ASSERT(status == 0); 1492 msbp--; 1493 } 1494 } 1495 XFS_SB_UNLOCK(mp, s); 1496 return (status); 1497} 1498 1499/* 1500 * xfs_getsb() is called to obtain the buffer for the superblock. 1501 * The buffer is returned locked and read in from disk. 1502 * The buffer should be released with a call to xfs_brelse(). 1503 * 1504 * If the flags parameter is BUF_TRYLOCK, then we'll only return 1505 * the superblock buffer if it can be locked without sleeping. 1506 * If it can't then we'll return NULL. 1507 */ 1508xfs_buf_t * 1509xfs_getsb( 1510 xfs_mount_t *mp, 1511 int flags) 1512{ 1513 xfs_buf_t *bp; 1514 1515 ASSERT(mp->m_sb_bp != NULL); 1516 bp = mp->m_sb_bp; 1517 if (flags & XFS_BUF_TRYLOCK) { 1518 if (!XFS_BUF_CPSEMA(bp)) { 1519 return NULL; 1520 } 1521 } else { 1522 XFS_BUF_PSEMA(bp, PRIBIO); 1523 } 1524 XFS_BUF_HOLD(bp); 1525 ASSERT(XFS_BUF_ISDONE(bp)); 1526 return (bp); 1527} 1528 1529/* 1530 * Used to free the superblock along various error paths. 1531 */ 1532void 1533xfs_freesb( 1534 xfs_mount_t *mp) 1535{ 1536 xfs_buf_t *bp; 1537 1538 /* 1539 * Use xfs_getsb() so that the buffer will be locked 1540 * when we call xfs_buf_relse(). 1541 */ 1542 bp = xfs_getsb(mp, 0); 1543 XFS_BUF_UNMANAGE(bp); 1544 xfs_buf_relse(bp); 1545 mp->m_sb_bp = NULL; 1546} 1547 1548/* 1549 * See if the UUID is unique among mounted XFS filesystems. 1550 * Mount fails if UUID is nil or a FS with the same UUID is already mounted. 1551 */ 1552STATIC int 1553xfs_uuid_mount( 1554 xfs_mount_t *mp) 1555{ 1556 if (uuid_is_nil(&mp->m_sb.sb_uuid)) { 1557 cmn_err(CE_WARN, 1558 "XFS: Filesystem %s has nil UUID - can't mount", 1559 mp->m_fsname); 1560 return -1; 1561 } 1562 if (!uuid_table_insert(&mp->m_sb.sb_uuid)) { 1563 cmn_err(CE_WARN, 1564 "XFS: Filesystem %s has duplicate UUID - can't mount", 1565 mp->m_fsname); 1566 return -1; 1567 } 1568 return 0; 1569} 1570 1571/* 1572 * Remove filesystem from the UUID table. 1573 */ 1574STATIC void 1575xfs_uuid_unmount( 1576 xfs_mount_t *mp) 1577{ 1578 uuid_table_remove(&mp->m_sb.sb_uuid); 1579} 1580 1581/* 1582 * Used to log changes to the superblock unit and width fields which could 1583 * be altered by the mount options. Only the first superblock is updated. 1584 */ 1585STATIC void 1586xfs_mount_log_sbunit( 1587 xfs_mount_t *mp, 1588 __int64_t fields) 1589{ 1590 xfs_trans_t *tp; 1591 1592 ASSERT(fields & (XFS_SB_UNIT|XFS_SB_WIDTH|XFS_SB_UUID)); 1593 1594 tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT); 1595 if (xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0, 1596 XFS_DEFAULT_LOG_COUNT)) { 1597 xfs_trans_cancel(tp, 0); 1598 return; 1599 } 1600 xfs_mod_sb(tp, fields); 1601 xfs_trans_commit(tp, 0, NULL); 1602} 1603 1604/* Functions to lock access out of the filesystem for forced 1605 * shutdown or snapshot. 1606 */ 1607 1608void 1609xfs_start_freeze( 1610 xfs_mount_t *mp, 1611 int level) 1612{ 1613 unsigned long s = mutex_spinlock(&mp->m_freeze_lock); 1614 1615 mp->m_frozen = level; 1616 mutex_spinunlock(&mp->m_freeze_lock, s); 1617 1618 if (level == XFS_FREEZE_TRANS) { 1619 while (atomic_read(&mp->m_active_trans) > 0) 1620 delay(100); 1621 } 1622} 1623 1624void 1625xfs_finish_freeze( 1626 xfs_mount_t *mp) 1627{ 1628 unsigned long s = mutex_spinlock(&mp->m_freeze_lock); 1629 1630 if (mp->m_frozen) { 1631 mp->m_frozen = 0; 1632 sv_broadcast(&mp->m_wait_unfreeze); 1633 } 1634 1635 mutex_spinunlock(&mp->m_freeze_lock, s); 1636} 1637 1638void 1639xfs_check_frozen( 1640 xfs_mount_t *mp, 1641 bhv_desc_t *bdp, 1642 int level) 1643{ 1644 unsigned long s; 1645 1646 if (mp->m_frozen) { 1647 s = mutex_spinlock(&mp->m_freeze_lock); 1648 1649 if (mp->m_frozen < level) { 1650 mutex_spinunlock(&mp->m_freeze_lock, s); 1651 } else { 1652 sv_wait(&mp->m_wait_unfreeze, 0, &mp->m_freeze_lock, s); 1653 } 1654 } 1655 1656 if (level == XFS_FREEZE_TRANS) 1657 atomic_inc(&mp->m_active_trans); 1658} 1659 1660