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