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