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