~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/xfs/xfs_mount.c

Version: ~ [ linux-5.9 ] ~ [ linux-5.8.14 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.70 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.150 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.200 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.238 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.238 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp