]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/xfs/xfs_mount.c
xfs: remove m_attroffset
[karo-tx-linux.git] / 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_dir2.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir2_sf.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_btree.h"
38 #include "xfs_ialloc.h"
39 #include "xfs_alloc.h"
40 #include "xfs_rtalloc.h"
41 #include "xfs_bmap.h"
42 #include "xfs_error.h"
43 #include "xfs_rw.h"
44 #include "xfs_quota.h"
45 #include "xfs_fsops.h"
46 #include "xfs_utils.h"
47
48 STATIC int      xfs_uuid_mount(xfs_mount_t *);
49 STATIC void     xfs_unmountfs_wait(xfs_mount_t *);
50
51
52 #ifdef HAVE_PERCPU_SB
53 STATIC void     xfs_icsb_balance_counter(xfs_mount_t *, xfs_sb_field_t,
54                                                 int);
55 STATIC void     xfs_icsb_balance_counter_locked(xfs_mount_t *, xfs_sb_field_t,
56                                                 int);
57 STATIC int      xfs_icsb_modify_counters(xfs_mount_t *, xfs_sb_field_t,
58                                                 int64_t, int);
59 STATIC void     xfs_icsb_disable_counter(xfs_mount_t *, xfs_sb_field_t);
60
61 #else
62
63 #define xfs_icsb_balance_counter(mp, a, b)              do { } while (0)
64 #define xfs_icsb_balance_counter_locked(mp, a, b)       do { } while (0)
65 #define xfs_icsb_modify_counters(mp, a, b, c)           do { } while (0)
66
67 #endif
68
69 static const struct {
70         short offset;
71         short type;     /* 0 = integer
72                          * 1 = binary / string (no translation)
73                          */
74 } xfs_sb_info[] = {
75     { offsetof(xfs_sb_t, sb_magicnum),   0 },
76     { offsetof(xfs_sb_t, sb_blocksize),  0 },
77     { offsetof(xfs_sb_t, sb_dblocks),    0 },
78     { offsetof(xfs_sb_t, sb_rblocks),    0 },
79     { offsetof(xfs_sb_t, sb_rextents),   0 },
80     { offsetof(xfs_sb_t, sb_uuid),       1 },
81     { offsetof(xfs_sb_t, sb_logstart),   0 },
82     { offsetof(xfs_sb_t, sb_rootino),    0 },
83     { offsetof(xfs_sb_t, sb_rbmino),     0 },
84     { offsetof(xfs_sb_t, sb_rsumino),    0 },
85     { offsetof(xfs_sb_t, sb_rextsize),   0 },
86     { offsetof(xfs_sb_t, sb_agblocks),   0 },
87     { offsetof(xfs_sb_t, sb_agcount),    0 },
88     { offsetof(xfs_sb_t, sb_rbmblocks),  0 },
89     { offsetof(xfs_sb_t, sb_logblocks),  0 },
90     { offsetof(xfs_sb_t, sb_versionnum), 0 },
91     { offsetof(xfs_sb_t, sb_sectsize),   0 },
92     { offsetof(xfs_sb_t, sb_inodesize),  0 },
93     { offsetof(xfs_sb_t, sb_inopblock),  0 },
94     { offsetof(xfs_sb_t, sb_fname[0]),   1 },
95     { offsetof(xfs_sb_t, sb_blocklog),   0 },
96     { offsetof(xfs_sb_t, sb_sectlog),    0 },
97     { offsetof(xfs_sb_t, sb_inodelog),   0 },
98     { offsetof(xfs_sb_t, sb_inopblog),   0 },
99     { offsetof(xfs_sb_t, sb_agblklog),   0 },
100     { offsetof(xfs_sb_t, sb_rextslog),   0 },
101     { offsetof(xfs_sb_t, sb_inprogress), 0 },
102     { offsetof(xfs_sb_t, sb_imax_pct),   0 },
103     { offsetof(xfs_sb_t, sb_icount),     0 },
104     { offsetof(xfs_sb_t, sb_ifree),      0 },
105     { offsetof(xfs_sb_t, sb_fdblocks),   0 },
106     { offsetof(xfs_sb_t, sb_frextents),  0 },
107     { offsetof(xfs_sb_t, sb_uquotino),   0 },
108     { offsetof(xfs_sb_t, sb_gquotino),   0 },
109     { offsetof(xfs_sb_t, sb_qflags),     0 },
110     { offsetof(xfs_sb_t, sb_flags),      0 },
111     { offsetof(xfs_sb_t, sb_shared_vn),  0 },
112     { offsetof(xfs_sb_t, sb_inoalignmt), 0 },
113     { offsetof(xfs_sb_t, sb_unit),       0 },
114     { offsetof(xfs_sb_t, sb_width),      0 },
115     { offsetof(xfs_sb_t, sb_dirblklog),  0 },
116     { offsetof(xfs_sb_t, sb_logsectlog), 0 },
117     { offsetof(xfs_sb_t, sb_logsectsize),0 },
118     { offsetof(xfs_sb_t, sb_logsunit),   0 },
119     { offsetof(xfs_sb_t, sb_features2),  0 },
120     { offsetof(xfs_sb_t, sb_bad_features2), 0 },
121     { sizeof(xfs_sb_t),                  0 }
122 };
123
124 /*
125  * Free up the resources associated with a mount structure.  Assume that
126  * the structure was initially zeroed, so we can tell which fields got
127  * initialized.
128  */
129 STATIC void
130 xfs_free_perag(
131         xfs_mount_t     *mp)
132 {
133         if (mp->m_perag) {
134                 int     agno;
135
136                 for (agno = 0; agno < mp->m_maxagi; agno++)
137                         if (mp->m_perag[agno].pagb_list)
138                                 kmem_free(mp->m_perag[agno].pagb_list);
139                 kmem_free(mp->m_perag);
140         }
141 }
142
143 /*
144  * Check size of device based on the (data/realtime) block count.
145  * Note: this check is used by the growfs code as well as mount.
146  */
147 int
148 xfs_sb_validate_fsb_count(
149         xfs_sb_t        *sbp,
150         __uint64_t      nblocks)
151 {
152         ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
153         ASSERT(sbp->sb_blocklog >= BBSHIFT);
154
155 #if XFS_BIG_BLKNOS     /* Limited by ULONG_MAX of page cache index */
156         if (nblocks >> (PAGE_CACHE_SHIFT - sbp->sb_blocklog) > ULONG_MAX)
157                 return E2BIG;
158 #else                  /* Limited by UINT_MAX of sectors */
159         if (nblocks << (sbp->sb_blocklog - BBSHIFT) > UINT_MAX)
160                 return E2BIG;
161 #endif
162         return 0;
163 }
164
165 /*
166  * Check the validity of the SB found.
167  */
168 STATIC int
169 xfs_mount_validate_sb(
170         xfs_mount_t     *mp,
171         xfs_sb_t        *sbp,
172         int             flags)
173 {
174         /*
175          * If the log device and data device have the
176          * same device number, the log is internal.
177          * Consequently, the sb_logstart should be non-zero.  If
178          * we have a zero sb_logstart in this case, we may be trying to mount
179          * a volume filesystem in a non-volume manner.
180          */
181         if (sbp->sb_magicnum != XFS_SB_MAGIC) {
182                 xfs_fs_mount_cmn_err(flags, "bad magic number");
183                 return XFS_ERROR(EWRONGFS);
184         }
185
186         if (!xfs_sb_good_version(sbp)) {
187                 xfs_fs_mount_cmn_err(flags, "bad version");
188                 return XFS_ERROR(EWRONGFS);
189         }
190
191         if (unlikely(
192             sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
193                 xfs_fs_mount_cmn_err(flags,
194                         "filesystem is marked as having an external log; "
195                         "specify logdev on the\nmount command line.");
196                 return XFS_ERROR(EINVAL);
197         }
198
199         if (unlikely(
200             sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
201                 xfs_fs_mount_cmn_err(flags,
202                         "filesystem is marked as having an internal log; "
203                         "do not specify logdev on\nthe mount command line.");
204                 return XFS_ERROR(EINVAL);
205         }
206
207         /*
208          * More sanity checking. These were stolen directly from
209          * xfs_repair.
210          */
211         if (unlikely(
212             sbp->sb_agcount <= 0                                        ||
213             sbp->sb_sectsize < XFS_MIN_SECTORSIZE                       ||
214             sbp->sb_sectsize > XFS_MAX_SECTORSIZE                       ||
215             sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG                    ||
216             sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG                    ||
217             sbp->sb_blocksize < XFS_MIN_BLOCKSIZE                       ||
218             sbp->sb_blocksize > XFS_MAX_BLOCKSIZE                       ||
219             sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG                    ||
220             sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG                    ||
221             sbp->sb_inodesize < XFS_DINODE_MIN_SIZE                     ||
222             sbp->sb_inodesize > XFS_DINODE_MAX_SIZE                     ||
223             sbp->sb_inodelog < XFS_DINODE_MIN_LOG                       ||
224             sbp->sb_inodelog > XFS_DINODE_MAX_LOG                       ||
225             (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog)   ||
226             (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE)  ||
227             (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE)  ||
228             (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */))) {
229                 xfs_fs_mount_cmn_err(flags, "SB sanity check 1 failed");
230                 return XFS_ERROR(EFSCORRUPTED);
231         }
232
233         /*
234          * Sanity check AG count, size fields against data size field
235          */
236         if (unlikely(
237             sbp->sb_dblocks == 0 ||
238             sbp->sb_dblocks >
239              (xfs_drfsbno_t)sbp->sb_agcount * sbp->sb_agblocks ||
240             sbp->sb_dblocks < (xfs_drfsbno_t)(sbp->sb_agcount - 1) *
241                               sbp->sb_agblocks + XFS_MIN_AG_BLOCKS)) {
242                 xfs_fs_mount_cmn_err(flags, "SB sanity check 2 failed");
243                 return XFS_ERROR(EFSCORRUPTED);
244         }
245
246         /*
247          * Until this is fixed only page-sized or smaller data blocks work.
248          */
249         if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
250                 xfs_fs_mount_cmn_err(flags,
251                         "file system with blocksize %d bytes",
252                         sbp->sb_blocksize);
253                 xfs_fs_mount_cmn_err(flags,
254                         "only pagesize (%ld) or less will currently work.",
255                         PAGE_SIZE);
256                 return XFS_ERROR(ENOSYS);
257         }
258
259         /*
260          * Currently only very few inode sizes are supported.
261          */
262         switch (sbp->sb_inodesize) {
263         case 256:
264         case 512:
265         case 1024:
266         case 2048:
267                 break;
268         default:
269                 xfs_fs_mount_cmn_err(flags,
270                         "inode size of %d bytes not supported",
271                         sbp->sb_inodesize);
272                 return XFS_ERROR(ENOSYS);
273         }
274
275         if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) ||
276             xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) {
277                 xfs_fs_mount_cmn_err(flags,
278                         "file system too large to be mounted on this system.");
279                 return XFS_ERROR(E2BIG);
280         }
281
282         if (unlikely(sbp->sb_inprogress)) {
283                 xfs_fs_mount_cmn_err(flags, "file system busy");
284                 return XFS_ERROR(EFSCORRUPTED);
285         }
286
287         /*
288          * Version 1 directory format has never worked on Linux.
289          */
290         if (unlikely(!xfs_sb_version_hasdirv2(sbp))) {
291                 xfs_fs_mount_cmn_err(flags,
292                         "file system using version 1 directory format");
293                 return XFS_ERROR(ENOSYS);
294         }
295
296         return 0;
297 }
298
299 STATIC void
300 xfs_initialize_perag_icache(
301         xfs_perag_t     *pag)
302 {
303         if (!pag->pag_ici_init) {
304                 rwlock_init(&pag->pag_ici_lock);
305                 INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC);
306                 pag->pag_ici_init = 1;
307         }
308 }
309
310 xfs_agnumber_t
311 xfs_initialize_perag(
312         xfs_mount_t     *mp,
313         xfs_agnumber_t  agcount)
314 {
315         xfs_agnumber_t  index, max_metadata;
316         xfs_perag_t     *pag;
317         xfs_agino_t     agino;
318         xfs_ino_t       ino;
319         xfs_sb_t        *sbp = &mp->m_sb;
320         xfs_ino_t       max_inum = XFS_MAXINUMBER_32;
321
322         /* Check to see if the filesystem can overflow 32 bit inodes */
323         agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
324         ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
325
326         /* Clear the mount flag if no inode can overflow 32 bits
327          * on this filesystem, or if specifically requested..
328          */
329         if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) && ino > max_inum) {
330                 mp->m_flags |= XFS_MOUNT_32BITINODES;
331         } else {
332                 mp->m_flags &= ~XFS_MOUNT_32BITINODES;
333         }
334
335         /* If we can overflow then setup the ag headers accordingly */
336         if (mp->m_flags & XFS_MOUNT_32BITINODES) {
337                 /* Calculate how much should be reserved for inodes to
338                  * meet the max inode percentage.
339                  */
340                 if (mp->m_maxicount) {
341                         __uint64_t      icount;
342
343                         icount = sbp->sb_dblocks * sbp->sb_imax_pct;
344                         do_div(icount, 100);
345                         icount += sbp->sb_agblocks - 1;
346                         do_div(icount, sbp->sb_agblocks);
347                         max_metadata = icount;
348                 } else {
349                         max_metadata = agcount;
350                 }
351                 for (index = 0; index < agcount; index++) {
352                         ino = XFS_AGINO_TO_INO(mp, index, agino);
353                         if (ino > max_inum) {
354                                 index++;
355                                 break;
356                         }
357
358                         /* This ag is preferred for inodes */
359                         pag = &mp->m_perag[index];
360                         pag->pagi_inodeok = 1;
361                         if (index < max_metadata)
362                                 pag->pagf_metadata = 1;
363                         xfs_initialize_perag_icache(pag);
364                 }
365         } else {
366                 /* Setup default behavior for smaller filesystems */
367                 for (index = 0; index < agcount; index++) {
368                         pag = &mp->m_perag[index];
369                         pag->pagi_inodeok = 1;
370                         xfs_initialize_perag_icache(pag);
371                 }
372         }
373         return index;
374 }
375
376 void
377 xfs_sb_from_disk(
378         xfs_sb_t        *to,
379         xfs_dsb_t       *from)
380 {
381         to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
382         to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
383         to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
384         to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
385         to->sb_rextents = be64_to_cpu(from->sb_rextents);
386         memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
387         to->sb_logstart = be64_to_cpu(from->sb_logstart);
388         to->sb_rootino = be64_to_cpu(from->sb_rootino);
389         to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
390         to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
391         to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
392         to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
393         to->sb_agcount = be32_to_cpu(from->sb_agcount);
394         to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
395         to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
396         to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
397         to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
398         to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
399         to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
400         memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
401         to->sb_blocklog = from->sb_blocklog;
402         to->sb_sectlog = from->sb_sectlog;
403         to->sb_inodelog = from->sb_inodelog;
404         to->sb_inopblog = from->sb_inopblog;
405         to->sb_agblklog = from->sb_agblklog;
406         to->sb_rextslog = from->sb_rextslog;
407         to->sb_inprogress = from->sb_inprogress;
408         to->sb_imax_pct = from->sb_imax_pct;
409         to->sb_icount = be64_to_cpu(from->sb_icount);
410         to->sb_ifree = be64_to_cpu(from->sb_ifree);
411         to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
412         to->sb_frextents = be64_to_cpu(from->sb_frextents);
413         to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
414         to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
415         to->sb_qflags = be16_to_cpu(from->sb_qflags);
416         to->sb_flags = from->sb_flags;
417         to->sb_shared_vn = from->sb_shared_vn;
418         to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
419         to->sb_unit = be32_to_cpu(from->sb_unit);
420         to->sb_width = be32_to_cpu(from->sb_width);
421         to->sb_dirblklog = from->sb_dirblklog;
422         to->sb_logsectlog = from->sb_logsectlog;
423         to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
424         to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
425         to->sb_features2 = be32_to_cpu(from->sb_features2);
426         to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
427 }
428
429 /*
430  * Copy in core superblock to ondisk one.
431  *
432  * The fields argument is mask of superblock fields to copy.
433  */
434 void
435 xfs_sb_to_disk(
436         xfs_dsb_t       *to,
437         xfs_sb_t        *from,
438         __int64_t       fields)
439 {
440         xfs_caddr_t     to_ptr = (xfs_caddr_t)to;
441         xfs_caddr_t     from_ptr = (xfs_caddr_t)from;
442         xfs_sb_field_t  f;
443         int             first;
444         int             size;
445
446         ASSERT(fields);
447         if (!fields)
448                 return;
449
450         while (fields) {
451                 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
452                 first = xfs_sb_info[f].offset;
453                 size = xfs_sb_info[f + 1].offset - first;
454
455                 ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1);
456
457                 if (size == 1 || xfs_sb_info[f].type == 1) {
458                         memcpy(to_ptr + first, from_ptr + first, size);
459                 } else {
460                         switch (size) {
461                         case 2:
462                                 *(__be16 *)(to_ptr + first) =
463                                         cpu_to_be16(*(__u16 *)(from_ptr + first));
464                                 break;
465                         case 4:
466                                 *(__be32 *)(to_ptr + first) =
467                                         cpu_to_be32(*(__u32 *)(from_ptr + first));
468                                 break;
469                         case 8:
470                                 *(__be64 *)(to_ptr + first) =
471                                         cpu_to_be64(*(__u64 *)(from_ptr + first));
472                                 break;
473                         default:
474                                 ASSERT(0);
475                         }
476                 }
477
478                 fields &= ~(1LL << f);
479         }
480 }
481
482 /*
483  * xfs_readsb
484  *
485  * Does the initial read of the superblock.
486  */
487 int
488 xfs_readsb(xfs_mount_t *mp, int flags)
489 {
490         unsigned int    sector_size;
491         unsigned int    extra_flags;
492         xfs_buf_t       *bp;
493         int             error;
494
495         ASSERT(mp->m_sb_bp == NULL);
496         ASSERT(mp->m_ddev_targp != NULL);
497
498         /*
499          * Allocate a (locked) buffer to hold the superblock.
500          * This will be kept around at all times to optimize
501          * access to the superblock.
502          */
503         sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
504         extra_flags = XFS_BUF_LOCK | XFS_BUF_MANAGE | XFS_BUF_MAPPED;
505
506         bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR,
507                                 BTOBB(sector_size), extra_flags);
508         if (!bp || XFS_BUF_ISERROR(bp)) {
509                 xfs_fs_mount_cmn_err(flags, "SB read failed");
510                 error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
511                 goto fail;
512         }
513         ASSERT(XFS_BUF_ISBUSY(bp));
514         ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
515
516         /*
517          * Initialize the mount structure from the superblock.
518          * But first do some basic consistency checking.
519          */
520         xfs_sb_from_disk(&mp->m_sb, XFS_BUF_TO_SBP(bp));
521
522         error = xfs_mount_validate_sb(mp, &(mp->m_sb), flags);
523         if (error) {
524                 xfs_fs_mount_cmn_err(flags, "SB validate failed");
525                 goto fail;
526         }
527
528         /*
529          * We must be able to do sector-sized and sector-aligned IO.
530          */
531         if (sector_size > mp->m_sb.sb_sectsize) {
532                 xfs_fs_mount_cmn_err(flags,
533                         "device supports only %u byte sectors (not %u)",
534                         sector_size, mp->m_sb.sb_sectsize);
535                 error = ENOSYS;
536                 goto fail;
537         }
538
539         /*
540          * If device sector size is smaller than the superblock size,
541          * re-read the superblock so the buffer is correctly sized.
542          */
543         if (sector_size < mp->m_sb.sb_sectsize) {
544                 XFS_BUF_UNMANAGE(bp);
545                 xfs_buf_relse(bp);
546                 sector_size = mp->m_sb.sb_sectsize;
547                 bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR,
548                                         BTOBB(sector_size), extra_flags);
549                 if (!bp || XFS_BUF_ISERROR(bp)) {
550                         xfs_fs_mount_cmn_err(flags, "SB re-read failed");
551                         error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
552                         goto fail;
553                 }
554                 ASSERT(XFS_BUF_ISBUSY(bp));
555                 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
556         }
557
558         /* Initialize per-cpu counters */
559         xfs_icsb_reinit_counters(mp);
560
561         mp->m_sb_bp = bp;
562         xfs_buf_relse(bp);
563         ASSERT(XFS_BUF_VALUSEMA(bp) > 0);
564         return 0;
565
566  fail:
567         if (bp) {
568                 XFS_BUF_UNMANAGE(bp);
569                 xfs_buf_relse(bp);
570         }
571         return error;
572 }
573
574
575 /*
576  * xfs_mount_common
577  *
578  * Mount initialization code establishing various mount
579  * fields from the superblock associated with the given
580  * mount structure
581  */
582 STATIC void
583 xfs_mount_common(xfs_mount_t *mp, xfs_sb_t *sbp)
584 {
585         mp->m_agfrotor = mp->m_agirotor = 0;
586         spin_lock_init(&mp->m_agirotor_lock);
587         mp->m_maxagi = mp->m_sb.sb_agcount;
588         mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
589         mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
590         mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
591         mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
592         mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
593         mp->m_blockmask = sbp->sb_blocksize - 1;
594         mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
595         mp->m_blockwmask = mp->m_blockwsize - 1;
596
597         mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
598         mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
599         mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
600         mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
601
602         mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
603         mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
604         mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2;
605         mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2;
606
607         mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
608         mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
609         mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
610         mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
611
612         mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
613         mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
614                                         sbp->sb_inopblock);
615         mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
616 }
617
618 /*
619  * xfs_initialize_perag_data
620  *
621  * Read in each per-ag structure so we can count up the number of
622  * allocated inodes, free inodes and used filesystem blocks as this
623  * information is no longer persistent in the superblock. Once we have
624  * this information, write it into the in-core superblock structure.
625  */
626 STATIC int
627 xfs_initialize_perag_data(xfs_mount_t *mp, xfs_agnumber_t agcount)
628 {
629         xfs_agnumber_t  index;
630         xfs_perag_t     *pag;
631         xfs_sb_t        *sbp = &mp->m_sb;
632         uint64_t        ifree = 0;
633         uint64_t        ialloc = 0;
634         uint64_t        bfree = 0;
635         uint64_t        bfreelst = 0;
636         uint64_t        btree = 0;
637         int             error;
638
639         for (index = 0; index < agcount; index++) {
640                 /*
641                  * read the agf, then the agi. This gets us
642                  * all the information we need and populates the
643                  * per-ag structures for us.
644                  */
645                 error = xfs_alloc_pagf_init(mp, NULL, index, 0);
646                 if (error)
647                         return error;
648
649                 error = xfs_ialloc_pagi_init(mp, NULL, index);
650                 if (error)
651                         return error;
652                 pag = &mp->m_perag[index];
653                 ifree += pag->pagi_freecount;
654                 ialloc += pag->pagi_count;
655                 bfree += pag->pagf_freeblks;
656                 bfreelst += pag->pagf_flcount;
657                 btree += pag->pagf_btreeblks;
658         }
659         /*
660          * Overwrite incore superblock counters with just-read data
661          */
662         spin_lock(&mp->m_sb_lock);
663         sbp->sb_ifree = ifree;
664         sbp->sb_icount = ialloc;
665         sbp->sb_fdblocks = bfree + bfreelst + btree;
666         spin_unlock(&mp->m_sb_lock);
667
668         /* Fixup the per-cpu counters as well. */
669         xfs_icsb_reinit_counters(mp);
670
671         return 0;
672 }
673
674 /*
675  * Update alignment values based on mount options and sb values
676  */
677 STATIC int
678 xfs_update_alignment(xfs_mount_t *mp)
679 {
680         xfs_sb_t        *sbp = &(mp->m_sb);
681
682         if (mp->m_dalign) {
683                 /*
684                  * If stripe unit and stripe width are not multiples
685                  * of the fs blocksize turn off alignment.
686                  */
687                 if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
688                     (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
689                         if (mp->m_flags & XFS_MOUNT_RETERR) {
690                                 cmn_err(CE_WARN,
691                                         "XFS: alignment check 1 failed");
692                                 return XFS_ERROR(EINVAL);
693                         }
694                         mp->m_dalign = mp->m_swidth = 0;
695                 } else {
696                         /*
697                          * Convert the stripe unit and width to FSBs.
698                          */
699                         mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
700                         if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
701                                 if (mp->m_flags & XFS_MOUNT_RETERR) {
702                                         return XFS_ERROR(EINVAL);
703                                 }
704                                 xfs_fs_cmn_err(CE_WARN, mp,
705 "stripe alignment turned off: sunit(%d)/swidth(%d) incompatible with agsize(%d)",
706                                         mp->m_dalign, mp->m_swidth,
707                                         sbp->sb_agblocks);
708
709                                 mp->m_dalign = 0;
710                                 mp->m_swidth = 0;
711                         } else if (mp->m_dalign) {
712                                 mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
713                         } else {
714                                 if (mp->m_flags & XFS_MOUNT_RETERR) {
715                                         xfs_fs_cmn_err(CE_WARN, mp,
716 "stripe alignment turned off: sunit(%d) less than bsize(%d)",
717                                                 mp->m_dalign,
718                                                 mp->m_blockmask +1);
719                                         return XFS_ERROR(EINVAL);
720                                 }
721                                 mp->m_swidth = 0;
722                         }
723                 }
724
725                 /*
726                  * Update superblock with new values
727                  * and log changes
728                  */
729                 if (xfs_sb_version_hasdalign(sbp)) {
730                         if (sbp->sb_unit != mp->m_dalign) {
731                                 sbp->sb_unit = mp->m_dalign;
732                                 mp->m_update_flags |= XFS_SB_UNIT;
733                         }
734                         if (sbp->sb_width != mp->m_swidth) {
735                                 sbp->sb_width = mp->m_swidth;
736                                 mp->m_update_flags |= XFS_SB_WIDTH;
737                         }
738                 }
739         } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
740                     xfs_sb_version_hasdalign(&mp->m_sb)) {
741                         mp->m_dalign = sbp->sb_unit;
742                         mp->m_swidth = sbp->sb_width;
743         }
744
745         return 0;
746 }
747
748 /*
749  * Set the maximum inode count for this filesystem
750  */
751 STATIC void
752 xfs_set_maxicount(xfs_mount_t *mp)
753 {
754         xfs_sb_t        *sbp = &(mp->m_sb);
755         __uint64_t      icount;
756
757         if (sbp->sb_imax_pct) {
758                 /*
759                  * Make sure the maximum inode count is a multiple
760                  * of the units we allocate inodes in.
761                  */
762                 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
763                 do_div(icount, 100);
764                 do_div(icount, mp->m_ialloc_blks);
765                 mp->m_maxicount = (icount * mp->m_ialloc_blks)  <<
766                                    sbp->sb_inopblog;
767         } else {
768                 mp->m_maxicount = 0;
769         }
770 }
771
772 /*
773  * Set the default minimum read and write sizes unless
774  * already specified in a mount option.
775  * We use smaller I/O sizes when the file system
776  * is being used for NFS service (wsync mount option).
777  */
778 STATIC void
779 xfs_set_rw_sizes(xfs_mount_t *mp)
780 {
781         xfs_sb_t        *sbp = &(mp->m_sb);
782         int             readio_log, writeio_log;
783
784         if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
785                 if (mp->m_flags & XFS_MOUNT_WSYNC) {
786                         readio_log = XFS_WSYNC_READIO_LOG;
787                         writeio_log = XFS_WSYNC_WRITEIO_LOG;
788                 } else {
789                         readio_log = XFS_READIO_LOG_LARGE;
790                         writeio_log = XFS_WRITEIO_LOG_LARGE;
791                 }
792         } else {
793                 readio_log = mp->m_readio_log;
794                 writeio_log = mp->m_writeio_log;
795         }
796
797         if (sbp->sb_blocklog > readio_log) {
798                 mp->m_readio_log = sbp->sb_blocklog;
799         } else {
800                 mp->m_readio_log = readio_log;
801         }
802         mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
803         if (sbp->sb_blocklog > writeio_log) {
804                 mp->m_writeio_log = sbp->sb_blocklog;
805         } else {
806                 mp->m_writeio_log = writeio_log;
807         }
808         mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
809 }
810
811 /*
812  * Set whether we're using inode alignment.
813  */
814 STATIC void
815 xfs_set_inoalignment(xfs_mount_t *mp)
816 {
817         if (xfs_sb_version_hasalign(&mp->m_sb) &&
818             mp->m_sb.sb_inoalignmt >=
819             XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
820                 mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
821         else
822                 mp->m_inoalign_mask = 0;
823         /*
824          * If we are using stripe alignment, check whether
825          * the stripe unit is a multiple of the inode alignment
826          */
827         if (mp->m_dalign && mp->m_inoalign_mask &&
828             !(mp->m_dalign & mp->m_inoalign_mask))
829                 mp->m_sinoalign = mp->m_dalign;
830         else
831                 mp->m_sinoalign = 0;
832 }
833
834 /*
835  * Check that the data (and log if separate) are an ok size.
836  */
837 STATIC int
838 xfs_check_sizes(xfs_mount_t *mp)
839 {
840         xfs_buf_t       *bp;
841         xfs_daddr_t     d;
842         int             error;
843
844         d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
845         if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
846                 cmn_err(CE_WARN, "XFS: size check 1 failed");
847                 return XFS_ERROR(E2BIG);
848         }
849         error = xfs_read_buf(mp, mp->m_ddev_targp,
850                              d - XFS_FSS_TO_BB(mp, 1),
851                              XFS_FSS_TO_BB(mp, 1), 0, &bp);
852         if (!error) {
853                 xfs_buf_relse(bp);
854         } else {
855                 cmn_err(CE_WARN, "XFS: size check 2 failed");
856                 if (error == ENOSPC)
857                         error = XFS_ERROR(E2BIG);
858                 return error;
859         }
860
861         if (mp->m_logdev_targp != mp->m_ddev_targp) {
862                 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
863                 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
864                         cmn_err(CE_WARN, "XFS: size check 3 failed");
865                         return XFS_ERROR(E2BIG);
866                 }
867                 error = xfs_read_buf(mp, mp->m_logdev_targp,
868                                      d - XFS_FSB_TO_BB(mp, 1),
869                                      XFS_FSB_TO_BB(mp, 1), 0, &bp);
870                 if (!error) {
871                         xfs_buf_relse(bp);
872                 } else {
873                         cmn_err(CE_WARN, "XFS: size check 3 failed");
874                         if (error == ENOSPC)
875                                 error = XFS_ERROR(E2BIG);
876                         return error;
877                 }
878         }
879         return 0;
880 }
881
882 /*
883  * This function does the following on an initial mount of a file system:
884  *      - reads the superblock from disk and init the mount struct
885  *      - if we're a 32-bit kernel, do a size check on the superblock
886  *              so we don't mount terabyte filesystems
887  *      - init mount struct realtime fields
888  *      - allocate inode hash table for fs
889  *      - init directory manager
890  *      - perform recovery and init the log manager
891  */
892 int
893 xfs_mountfs(
894         xfs_mount_t     *mp)
895 {
896         xfs_sb_t        *sbp = &(mp->m_sb);
897         xfs_inode_t     *rip;
898         __uint64_t      resblks;
899         uint            quotamount, quotaflags;
900         int             error = 0;
901
902         xfs_mount_common(mp, sbp);
903
904         /*
905          * Check for a mismatched features2 values.  Older kernels
906          * read & wrote into the wrong sb offset for sb_features2
907          * on some platforms due to xfs_sb_t not being 64bit size aligned
908          * when sb_features2 was added, which made older superblock
909          * reading/writing routines swap it as a 64-bit value.
910          *
911          * For backwards compatibility, we make both slots equal.
912          *
913          * If we detect a mismatched field, we OR the set bits into the
914          * existing features2 field in case it has already been modified; we
915          * don't want to lose any features.  We then update the bad location
916          * with the ORed value so that older kernels will see any features2
917          * flags, and mark the two fields as needing updates once the
918          * transaction subsystem is online.
919          */
920         if (xfs_sb_has_mismatched_features2(sbp)) {
921                 cmn_err(CE_WARN,
922                         "XFS: correcting sb_features alignment problem");
923                 sbp->sb_features2 |= sbp->sb_bad_features2;
924                 sbp->sb_bad_features2 = sbp->sb_features2;
925                 mp->m_update_flags |= XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2;
926
927                 /*
928                  * Re-check for ATTR2 in case it was found in bad_features2
929                  * slot.
930                  */
931                 if (xfs_sb_version_hasattr2(&mp->m_sb) &&
932                    !(mp->m_flags & XFS_MOUNT_NOATTR2))
933                         mp->m_flags |= XFS_MOUNT_ATTR2;
934         }
935
936         if (xfs_sb_version_hasattr2(&mp->m_sb) &&
937            (mp->m_flags & XFS_MOUNT_NOATTR2)) {
938                 xfs_sb_version_removeattr2(&mp->m_sb);
939                 mp->m_update_flags |= XFS_SB_FEATURES2;
940
941                 /* update sb_versionnum for the clearing of the morebits */
942                 if (!sbp->sb_features2)
943                         mp->m_update_flags |= XFS_SB_VERSIONNUM;
944         }
945
946         /*
947          * Check if sb_agblocks is aligned at stripe boundary
948          * If sb_agblocks is NOT aligned turn off m_dalign since
949          * allocator alignment is within an ag, therefore ag has
950          * to be aligned at stripe boundary.
951          */
952         error = xfs_update_alignment(mp);
953         if (error)
954                 goto out;
955
956         xfs_alloc_compute_maxlevels(mp);
957         xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
958         xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
959         xfs_ialloc_compute_maxlevels(mp);
960
961         xfs_set_maxicount(mp);
962
963         mp->m_maxioffset = xfs_max_file_offset(sbp->sb_blocklog);
964
965         /*
966          * XFS uses the uuid from the superblock as the unique
967          * identifier for fsid.  We can not use the uuid from the volume
968          * since a single partition filesystem is identical to a single
969          * partition volume/filesystem.
970          */
971         if (!(mp->m_flags & XFS_MOUNT_NOUUID)) {
972                 if (xfs_uuid_mount(mp)) {
973                         error = XFS_ERROR(EINVAL);
974                         goto out;
975                 }
976         }
977
978         /*
979          * Set the minimum read and write sizes
980          */
981         xfs_set_rw_sizes(mp);
982
983         /*
984          * Set the inode cluster size.
985          * This may still be overridden by the file system
986          * block size if it is larger than the chosen cluster size.
987          */
988         mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
989
990         /*
991          * Set inode alignment fields
992          */
993         xfs_set_inoalignment(mp);
994
995         /*
996          * Check that the data (and log if separate) are an ok size.
997          */
998         error = xfs_check_sizes(mp);
999         if (error)
1000                 goto out_remove_uuid;
1001
1002         /*
1003          * Initialize realtime fields in the mount structure
1004          */
1005         error = xfs_rtmount_init(mp);
1006         if (error) {
1007                 cmn_err(CE_WARN, "XFS: RT mount failed");
1008                 goto out_remove_uuid;
1009         }
1010
1011         /*
1012          *  Copies the low order bits of the timestamp and the randomly
1013          *  set "sequence" number out of a UUID.
1014          */
1015         uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid);
1016
1017         mp->m_dmevmask = 0;     /* not persistent; set after each mount */
1018
1019         xfs_dir_mount(mp);
1020
1021         /*
1022          * Initialize the attribute manager's entries.
1023          */
1024         mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100;
1025
1026         /*
1027          * Initialize the precomputed transaction reservations values.
1028          */
1029         xfs_trans_init(mp);
1030
1031         /*
1032          * Allocate and initialize the per-ag data.
1033          */
1034         init_rwsem(&mp->m_peraglock);
1035         mp->m_perag = kmem_zalloc(sbp->sb_agcount * sizeof(xfs_perag_t),
1036                                   KM_MAYFAIL);
1037         if (!mp->m_perag)
1038                 goto out_remove_uuid;
1039
1040         mp->m_maxagi = xfs_initialize_perag(mp, sbp->sb_agcount);
1041
1042         if (!sbp->sb_logblocks) {
1043                 cmn_err(CE_WARN, "XFS: no log defined");
1044                 XFS_ERROR_REPORT("xfs_mountfs", XFS_ERRLEVEL_LOW, mp);
1045                 error = XFS_ERROR(EFSCORRUPTED);
1046                 goto out_free_perag;
1047         }
1048
1049         /*
1050          * log's mount-time initialization. Perform 1st part recovery if needed
1051          */
1052         error = xfs_log_mount(mp, mp->m_logdev_targp,
1053                               XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
1054                               XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
1055         if (error) {
1056                 cmn_err(CE_WARN, "XFS: log mount failed");
1057                 goto out_free_perag;
1058         }
1059
1060         /*
1061          * Now the log is mounted, we know if it was an unclean shutdown or
1062          * not. If it was, with the first phase of recovery has completed, we
1063          * have consistent AG blocks on disk. We have not recovered EFIs yet,
1064          * but they are recovered transactionally in the second recovery phase
1065          * later.
1066          *
1067          * Hence we can safely re-initialise incore superblock counters from
1068          * the per-ag data. These may not be correct if the filesystem was not
1069          * cleanly unmounted, so we need to wait for recovery to finish before
1070          * doing this.
1071          *
1072          * If the filesystem was cleanly unmounted, then we can trust the
1073          * values in the superblock to be correct and we don't need to do
1074          * anything here.
1075          *
1076          * If we are currently making the filesystem, the initialisation will
1077          * fail as the perag data is in an undefined state.
1078          */
1079         if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
1080             !XFS_LAST_UNMOUNT_WAS_CLEAN(mp) &&
1081              !mp->m_sb.sb_inprogress) {
1082                 error = xfs_initialize_perag_data(mp, sbp->sb_agcount);
1083                 if (error)
1084                         goto out_free_perag;
1085         }
1086
1087         /*
1088          * Get and sanity-check the root inode.
1089          * Save the pointer to it in the mount structure.
1090          */
1091         error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip, 0);
1092         if (error) {
1093                 cmn_err(CE_WARN, "XFS: failed to read root inode");
1094                 goto out_log_dealloc;
1095         }
1096
1097         ASSERT(rip != NULL);
1098
1099         if (unlikely((rip->i_d.di_mode & S_IFMT) != S_IFDIR)) {
1100                 cmn_err(CE_WARN, "XFS: corrupted root inode");
1101                 cmn_err(CE_WARN, "Device %s - root %llu is not a directory",
1102                         XFS_BUFTARG_NAME(mp->m_ddev_targp),
1103                         (unsigned long long)rip->i_ino);
1104                 xfs_iunlock(rip, XFS_ILOCK_EXCL);
1105                 XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
1106                                  mp);
1107                 error = XFS_ERROR(EFSCORRUPTED);
1108                 goto out_rele_rip;
1109         }
1110         mp->m_rootip = rip;     /* save it */
1111
1112         xfs_iunlock(rip, XFS_ILOCK_EXCL);
1113
1114         /*
1115          * Initialize realtime inode pointers in the mount structure
1116          */
1117         error = xfs_rtmount_inodes(mp);
1118         if (error) {
1119                 /*
1120                  * Free up the root inode.
1121                  */
1122                 cmn_err(CE_WARN, "XFS: failed to read RT inodes");
1123                 goto out_rele_rip;
1124         }
1125
1126         /*
1127          * If this is a read-only mount defer the superblock updates until
1128          * the next remount into writeable mode.  Otherwise we would never
1129          * perform the update e.g. for the root filesystem.
1130          */
1131         if (mp->m_update_flags && !(mp->m_flags & XFS_MOUNT_RDONLY)) {
1132                 error = xfs_mount_log_sb(mp, mp->m_update_flags);
1133                 if (error) {
1134                         cmn_err(CE_WARN, "XFS: failed to write sb changes");
1135                         goto out_rtunmount;
1136                 }
1137         }
1138
1139         /*
1140          * Initialise the XFS quota management subsystem for this mount
1141          */
1142         error = XFS_QM_INIT(mp, &quotamount, &quotaflags);
1143         if (error)
1144                 goto out_rtunmount;
1145
1146         /*
1147          * Finish recovering the file system.  This part needed to be
1148          * delayed until after the root and real-time bitmap inodes
1149          * were consistently read in.
1150          */
1151         error = xfs_log_mount_finish(mp);
1152         if (error) {
1153                 cmn_err(CE_WARN, "XFS: log mount finish failed");
1154                 goto out_rtunmount;
1155         }
1156
1157         /*
1158          * Complete the quota initialisation, post-log-replay component.
1159          */
1160         error = XFS_QM_MOUNT(mp, quotamount, quotaflags);
1161         if (error)
1162                 goto out_rtunmount;
1163
1164         /*
1165          * Now we are mounted, reserve a small amount of unused space for
1166          * privileged transactions. This is needed so that transaction
1167          * space required for critical operations can dip into this pool
1168          * when at ENOSPC. This is needed for operations like create with
1169          * attr, unwritten extent conversion at ENOSPC, etc. Data allocations
1170          * are not allowed to use this reserved space.
1171          *
1172          * We default to 5% or 1024 fsbs of space reserved, whichever is smaller.
1173          * This may drive us straight to ENOSPC on mount, but that implies
1174          * we were already there on the last unmount. Warn if this occurs.
1175          */
1176         resblks = mp->m_sb.sb_dblocks;
1177         do_div(resblks, 20);
1178         resblks = min_t(__uint64_t, resblks, 1024);
1179         error = xfs_reserve_blocks(mp, &resblks, NULL);
1180         if (error)
1181                 cmn_err(CE_WARN, "XFS: Unable to allocate reserve blocks. "
1182                                 "Continuing without a reserve pool.");
1183
1184         return 0;
1185
1186  out_rtunmount:
1187         xfs_rtunmount_inodes(mp);
1188  out_rele_rip:
1189         IRELE(rip);
1190  out_log_dealloc:
1191         xfs_log_unmount(mp);
1192  out_free_perag:
1193         xfs_free_perag(mp);
1194  out_remove_uuid:
1195         if (!(mp->m_flags & XFS_MOUNT_NOUUID))
1196                 uuid_table_remove(&mp->m_sb.sb_uuid);
1197  out:
1198         return error;
1199 }
1200
1201 /*
1202  * This flushes out the inodes,dquots and the superblock, unmounts the
1203  * log and makes sure that incore structures are freed.
1204  */
1205 void
1206 xfs_unmountfs(
1207         struct xfs_mount        *mp)
1208 {
1209         __uint64_t              resblks;
1210         int                     error;
1211
1212         /*
1213          * Release dquot that rootinode, rbmino and rsumino might be holding,
1214          * and release the quota inodes.
1215          */
1216         XFS_QM_UNMOUNT(mp);
1217
1218         xfs_rtunmount_inodes(mp);
1219         IRELE(mp->m_rootip);
1220
1221         /*
1222          * We can potentially deadlock here if we have an inode cluster
1223          * that has been freed has its buffer still pinned in memory because
1224          * the transaction is still sitting in a iclog. The stale inodes
1225          * on that buffer will have their flush locks held until the
1226          * transaction hits the disk and the callbacks run. the inode
1227          * flush takes the flush lock unconditionally and with nothing to
1228          * push out the iclog we will never get that unlocked. hence we
1229          * need to force the log first.
1230          */
1231         xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1232         xfs_reclaim_inodes(mp, 0, XFS_IFLUSH_ASYNC);
1233
1234         XFS_QM_DQPURGEALL(mp, XFS_QMOPT_QUOTALL | XFS_QMOPT_UMOUNTING);
1235
1236         if (mp->m_quotainfo)
1237                 XFS_QM_DONE(mp);
1238
1239         /*
1240          * Flush out the log synchronously so that we know for sure
1241          * that nothing is pinned.  This is important because bflush()
1242          * will skip pinned buffers.
1243          */
1244         xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1245
1246         xfs_binval(mp->m_ddev_targp);
1247         if (mp->m_rtdev_targp) {
1248                 xfs_binval(mp->m_rtdev_targp);
1249         }
1250
1251         /*
1252          * Unreserve any blocks we have so that when we unmount we don't account
1253          * the reserved free space as used. This is really only necessary for
1254          * lazy superblock counting because it trusts the incore superblock
1255          * counters to be absolutely correct on clean unmount.
1256          *
1257          * We don't bother correcting this elsewhere for lazy superblock
1258          * counting because on mount of an unclean filesystem we reconstruct the
1259          * correct counter value and this is irrelevant.
1260          *
1261          * For non-lazy counter filesystems, this doesn't matter at all because
1262          * we only every apply deltas to the superblock and hence the incore
1263          * value does not matter....
1264          */
1265         resblks = 0;
1266         error = xfs_reserve_blocks(mp, &resblks, NULL);
1267         if (error)
1268                 cmn_err(CE_WARN, "XFS: Unable to free reserved block pool. "
1269                                 "Freespace may not be correct on next mount.");
1270
1271         error = xfs_log_sbcount(mp, 1);
1272         if (error)
1273                 cmn_err(CE_WARN, "XFS: Unable to update superblock counters. "
1274                                 "Freespace may not be correct on next mount.");
1275         xfs_unmountfs_writesb(mp);
1276         xfs_unmountfs_wait(mp);                 /* wait for async bufs */
1277         xfs_log_unmount_write(mp);
1278         xfs_log_unmount(mp);
1279
1280         if ((mp->m_flags & XFS_MOUNT_NOUUID) == 0)
1281                 uuid_table_remove(&mp->m_sb.sb_uuid);
1282
1283 #if defined(DEBUG)
1284         xfs_errortag_clearall(mp, 0);
1285 #endif
1286         xfs_free_perag(mp);
1287 }
1288
1289 STATIC void
1290 xfs_unmountfs_wait(xfs_mount_t *mp)
1291 {
1292         if (mp->m_logdev_targp != mp->m_ddev_targp)
1293                 xfs_wait_buftarg(mp->m_logdev_targp);
1294         if (mp->m_rtdev_targp)
1295                 xfs_wait_buftarg(mp->m_rtdev_targp);
1296         xfs_wait_buftarg(mp->m_ddev_targp);
1297 }
1298
1299 int
1300 xfs_fs_writable(xfs_mount_t *mp)
1301 {
1302         return !(xfs_test_for_freeze(mp) || XFS_FORCED_SHUTDOWN(mp) ||
1303                 (mp->m_flags & XFS_MOUNT_RDONLY));
1304 }
1305
1306 /*
1307  * xfs_log_sbcount
1308  *
1309  * Called either periodically to keep the on disk superblock values
1310  * roughly up to date or from unmount to make sure the values are
1311  * correct on a clean unmount.
1312  *
1313  * Note this code can be called during the process of freezing, so
1314  * we may need to use the transaction allocator which does not not
1315  * block when the transaction subsystem is in its frozen state.
1316  */
1317 int
1318 xfs_log_sbcount(
1319         xfs_mount_t     *mp,
1320         uint            sync)
1321 {
1322         xfs_trans_t     *tp;
1323         int             error;
1324
1325         if (!xfs_fs_writable(mp))
1326                 return 0;
1327
1328         xfs_icsb_sync_counters(mp, 0);
1329
1330         /*
1331          * we don't need to do this if we are updating the superblock
1332          * counters on every modification.
1333          */
1334         if (!xfs_sb_version_haslazysbcount(&mp->m_sb))
1335                 return 0;
1336
1337         tp = _xfs_trans_alloc(mp, XFS_TRANS_SB_COUNT);
1338         error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
1339                                         XFS_DEFAULT_LOG_COUNT);
1340         if (error) {
1341                 xfs_trans_cancel(tp, 0);
1342                 return error;
1343         }
1344
1345         xfs_mod_sb(tp, XFS_SB_IFREE | XFS_SB_ICOUNT | XFS_SB_FDBLOCKS);
1346         if (sync)
1347                 xfs_trans_set_sync(tp);
1348         error = xfs_trans_commit(tp, 0);
1349         return error;
1350 }
1351
1352 int
1353 xfs_unmountfs_writesb(xfs_mount_t *mp)
1354 {
1355         xfs_buf_t       *sbp;
1356         int             error = 0;
1357
1358         /*
1359          * skip superblock write if fs is read-only, or
1360          * if we are doing a forced umount.
1361          */
1362         if (!((mp->m_flags & XFS_MOUNT_RDONLY) ||
1363                 XFS_FORCED_SHUTDOWN(mp))) {
1364
1365                 sbp = xfs_getsb(mp, 0);
1366
1367                 XFS_BUF_UNDONE(sbp);
1368                 XFS_BUF_UNREAD(sbp);
1369                 XFS_BUF_UNDELAYWRITE(sbp);
1370                 XFS_BUF_WRITE(sbp);
1371                 XFS_BUF_UNASYNC(sbp);
1372                 ASSERT(XFS_BUF_TARGET(sbp) == mp->m_ddev_targp);
1373                 xfsbdstrat(mp, sbp);
1374                 error = xfs_iowait(sbp);
1375                 if (error)
1376                         xfs_ioerror_alert("xfs_unmountfs_writesb",
1377                                           mp, sbp, XFS_BUF_ADDR(sbp));
1378                 xfs_buf_relse(sbp);
1379         }
1380         return error;
1381 }
1382
1383 /*
1384  * xfs_mod_sb() can be used to copy arbitrary changes to the
1385  * in-core superblock into the superblock buffer to be logged.
1386  * It does not provide the higher level of locking that is
1387  * needed to protect the in-core superblock from concurrent
1388  * access.
1389  */
1390 void
1391 xfs_mod_sb(xfs_trans_t *tp, __int64_t fields)
1392 {
1393         xfs_buf_t       *bp;
1394         int             first;
1395         int             last;
1396         xfs_mount_t     *mp;
1397         xfs_sb_field_t  f;
1398
1399         ASSERT(fields);
1400         if (!fields)
1401                 return;
1402         mp = tp->t_mountp;
1403         bp = xfs_trans_getsb(tp, mp, 0);
1404         first = sizeof(xfs_sb_t);
1405         last = 0;
1406
1407         /* translate/copy */
1408
1409         xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb, fields);
1410
1411         /* find modified range */
1412
1413         f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
1414         ASSERT((1LL << f) & XFS_SB_MOD_BITS);
1415         first = xfs_sb_info[f].offset;
1416
1417         f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields);
1418         ASSERT((1LL << f) & XFS_SB_MOD_BITS);
1419         last = xfs_sb_info[f + 1].offset - 1;
1420
1421         xfs_trans_log_buf(tp, bp, first, last);
1422 }
1423
1424
1425 /*
1426  * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply
1427  * a delta to a specified field in the in-core superblock.  Simply
1428  * switch on the field indicated and apply the delta to that field.
1429  * Fields are not allowed to dip below zero, so if the delta would
1430  * do this do not apply it and return EINVAL.
1431  *
1432  * The m_sb_lock must be held when this routine is called.
1433  */
1434 int
1435 xfs_mod_incore_sb_unlocked(
1436         xfs_mount_t     *mp,
1437         xfs_sb_field_t  field,
1438         int64_t         delta,
1439         int             rsvd)
1440 {
1441         int             scounter;       /* short counter for 32 bit fields */
1442         long long       lcounter;       /* long counter for 64 bit fields */
1443         long long       res_used, rem;
1444
1445         /*
1446          * With the in-core superblock spin lock held, switch
1447          * on the indicated field.  Apply the delta to the
1448          * proper field.  If the fields value would dip below
1449          * 0, then do not apply the delta and return EINVAL.
1450          */
1451         switch (field) {
1452         case XFS_SBS_ICOUNT:
1453                 lcounter = (long long)mp->m_sb.sb_icount;
1454                 lcounter += delta;
1455                 if (lcounter < 0) {
1456                         ASSERT(0);
1457                         return XFS_ERROR(EINVAL);
1458                 }
1459                 mp->m_sb.sb_icount = lcounter;
1460                 return 0;
1461         case XFS_SBS_IFREE:
1462                 lcounter = (long long)mp->m_sb.sb_ifree;
1463                 lcounter += delta;
1464                 if (lcounter < 0) {
1465                         ASSERT(0);
1466                         return XFS_ERROR(EINVAL);
1467                 }
1468                 mp->m_sb.sb_ifree = lcounter;
1469                 return 0;
1470         case XFS_SBS_FDBLOCKS:
1471                 lcounter = (long long)
1472                         mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
1473                 res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
1474
1475                 if (delta > 0) {                /* Putting blocks back */
1476                         if (res_used > delta) {
1477                                 mp->m_resblks_avail += delta;
1478                         } else {
1479                                 rem = delta - res_used;
1480                                 mp->m_resblks_avail = mp->m_resblks;
1481                                 lcounter += rem;
1482                         }
1483                 } else {                                /* Taking blocks away */
1484
1485                         lcounter += delta;
1486
1487                 /*
1488                  * If were out of blocks, use any available reserved blocks if
1489                  * were allowed to.
1490                  */
1491
1492                         if (lcounter < 0) {
1493                                 if (rsvd) {
1494                                         lcounter = (long long)mp->m_resblks_avail + delta;
1495                                         if (lcounter < 0) {
1496                                                 return XFS_ERROR(ENOSPC);
1497                                         }
1498                                         mp->m_resblks_avail = lcounter;
1499                                         return 0;
1500                                 } else {        /* not reserved */
1501                                         return XFS_ERROR(ENOSPC);
1502                                 }
1503                         }
1504                 }
1505
1506                 mp->m_sb.sb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
1507                 return 0;
1508         case XFS_SBS_FREXTENTS:
1509                 lcounter = (long long)mp->m_sb.sb_frextents;
1510                 lcounter += delta;
1511                 if (lcounter < 0) {
1512                         return XFS_ERROR(ENOSPC);
1513                 }
1514                 mp->m_sb.sb_frextents = lcounter;
1515                 return 0;
1516         case XFS_SBS_DBLOCKS:
1517                 lcounter = (long long)mp->m_sb.sb_dblocks;
1518                 lcounter += delta;
1519                 if (lcounter < 0) {
1520                         ASSERT(0);
1521                         return XFS_ERROR(EINVAL);
1522                 }
1523                 mp->m_sb.sb_dblocks = lcounter;
1524                 return 0;
1525         case XFS_SBS_AGCOUNT:
1526                 scounter = mp->m_sb.sb_agcount;
1527                 scounter += delta;
1528                 if (scounter < 0) {
1529                         ASSERT(0);
1530                         return XFS_ERROR(EINVAL);
1531                 }
1532                 mp->m_sb.sb_agcount = scounter;
1533                 return 0;
1534         case XFS_SBS_IMAX_PCT:
1535                 scounter = mp->m_sb.sb_imax_pct;
1536                 scounter += delta;
1537                 if (scounter < 0) {
1538                         ASSERT(0);
1539                         return XFS_ERROR(EINVAL);
1540                 }
1541                 mp->m_sb.sb_imax_pct = scounter;
1542                 return 0;
1543         case XFS_SBS_REXTSIZE:
1544                 scounter = mp->m_sb.sb_rextsize;
1545                 scounter += delta;
1546                 if (scounter < 0) {
1547                         ASSERT(0);
1548                         return XFS_ERROR(EINVAL);
1549                 }
1550                 mp->m_sb.sb_rextsize = scounter;
1551                 return 0;
1552         case XFS_SBS_RBMBLOCKS:
1553                 scounter = mp->m_sb.sb_rbmblocks;
1554                 scounter += delta;
1555                 if (scounter < 0) {
1556                         ASSERT(0);
1557                         return XFS_ERROR(EINVAL);
1558                 }
1559                 mp->m_sb.sb_rbmblocks = scounter;
1560                 return 0;
1561         case XFS_SBS_RBLOCKS:
1562                 lcounter = (long long)mp->m_sb.sb_rblocks;
1563                 lcounter += delta;
1564                 if (lcounter < 0) {
1565                         ASSERT(0);
1566                         return XFS_ERROR(EINVAL);
1567                 }
1568                 mp->m_sb.sb_rblocks = lcounter;
1569                 return 0;
1570         case XFS_SBS_REXTENTS:
1571                 lcounter = (long long)mp->m_sb.sb_rextents;
1572                 lcounter += delta;
1573                 if (lcounter < 0) {
1574                         ASSERT(0);
1575                         return XFS_ERROR(EINVAL);
1576                 }
1577                 mp->m_sb.sb_rextents = lcounter;
1578                 return 0;
1579         case XFS_SBS_REXTSLOG:
1580                 scounter = mp->m_sb.sb_rextslog;
1581                 scounter += delta;
1582                 if (scounter < 0) {
1583                         ASSERT(0);
1584                         return XFS_ERROR(EINVAL);
1585                 }
1586                 mp->m_sb.sb_rextslog = scounter;
1587                 return 0;
1588         default:
1589                 ASSERT(0);
1590                 return XFS_ERROR(EINVAL);
1591         }
1592 }
1593
1594 /*
1595  * xfs_mod_incore_sb() is used to change a field in the in-core
1596  * superblock structure by the specified delta.  This modification
1597  * is protected by the m_sb_lock.  Just use the xfs_mod_incore_sb_unlocked()
1598  * routine to do the work.
1599  */
1600 int
1601 xfs_mod_incore_sb(
1602         xfs_mount_t     *mp,
1603         xfs_sb_field_t  field,
1604         int64_t         delta,
1605         int             rsvd)
1606 {
1607         int     status;
1608
1609         /* check for per-cpu counters */
1610         switch (field) {
1611 #ifdef HAVE_PERCPU_SB
1612         case XFS_SBS_ICOUNT:
1613         case XFS_SBS_IFREE:
1614         case XFS_SBS_FDBLOCKS:
1615                 if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
1616                         status = xfs_icsb_modify_counters(mp, field,
1617                                                         delta, rsvd);
1618                         break;
1619                 }
1620                 /* FALLTHROUGH */
1621 #endif
1622         default:
1623                 spin_lock(&mp->m_sb_lock);
1624                 status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
1625                 spin_unlock(&mp->m_sb_lock);
1626                 break;
1627         }
1628
1629         return status;
1630 }
1631
1632 /*
1633  * xfs_mod_incore_sb_batch() is used to change more than one field
1634  * in the in-core superblock structure at a time.  This modification
1635  * is protected by a lock internal to this module.  The fields and
1636  * changes to those fields are specified in the array of xfs_mod_sb
1637  * structures passed in.
1638  *
1639  * Either all of the specified deltas will be applied or none of
1640  * them will.  If any modified field dips below 0, then all modifications
1641  * will be backed out and EINVAL will be returned.
1642  */
1643 int
1644 xfs_mod_incore_sb_batch(xfs_mount_t *mp, xfs_mod_sb_t *msb, uint nmsb, int rsvd)
1645 {
1646         int             status=0;
1647         xfs_mod_sb_t    *msbp;
1648
1649         /*
1650          * Loop through the array of mod structures and apply each
1651          * individually.  If any fail, then back out all those
1652          * which have already been applied.  Do all of this within
1653          * the scope of the m_sb_lock so that all of the changes will
1654          * be atomic.
1655          */
1656         spin_lock(&mp->m_sb_lock);
1657         msbp = &msb[0];
1658         for (msbp = &msbp[0]; msbp < (msb + nmsb); msbp++) {
1659                 /*
1660                  * Apply the delta at index n.  If it fails, break
1661                  * from the loop so we'll fall into the undo loop
1662                  * below.
1663                  */
1664                 switch (msbp->msb_field) {
1665 #ifdef HAVE_PERCPU_SB
1666                 case XFS_SBS_ICOUNT:
1667                 case XFS_SBS_IFREE:
1668                 case XFS_SBS_FDBLOCKS:
1669                         if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
1670                                 spin_unlock(&mp->m_sb_lock);
1671                                 status = xfs_icsb_modify_counters(mp,
1672                                                         msbp->msb_field,
1673                                                         msbp->msb_delta, rsvd);
1674                                 spin_lock(&mp->m_sb_lock);
1675                                 break;
1676                         }
1677                         /* FALLTHROUGH */
1678 #endif
1679                 default:
1680                         status = xfs_mod_incore_sb_unlocked(mp,
1681                                                 msbp->msb_field,
1682                                                 msbp->msb_delta, rsvd);
1683                         break;
1684                 }
1685
1686                 if (status != 0) {
1687                         break;
1688                 }
1689         }
1690
1691         /*
1692          * If we didn't complete the loop above, then back out
1693          * any changes made to the superblock.  If you add code
1694          * between the loop above and here, make sure that you
1695          * preserve the value of status. Loop back until
1696          * we step below the beginning of the array.  Make sure
1697          * we don't touch anything back there.
1698          */
1699         if (status != 0) {
1700                 msbp--;
1701                 while (msbp >= msb) {
1702                         switch (msbp->msb_field) {
1703 #ifdef HAVE_PERCPU_SB
1704                         case XFS_SBS_ICOUNT:
1705                         case XFS_SBS_IFREE:
1706                         case XFS_SBS_FDBLOCKS:
1707                                 if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
1708                                         spin_unlock(&mp->m_sb_lock);
1709                                         status = xfs_icsb_modify_counters(mp,
1710                                                         msbp->msb_field,
1711                                                         -(msbp->msb_delta),
1712                                                         rsvd);
1713                                         spin_lock(&mp->m_sb_lock);
1714                                         break;
1715                                 }
1716                                 /* FALLTHROUGH */
1717 #endif
1718                         default:
1719                                 status = xfs_mod_incore_sb_unlocked(mp,
1720                                                         msbp->msb_field,
1721                                                         -(msbp->msb_delta),
1722                                                         rsvd);
1723                                 break;
1724                         }
1725                         ASSERT(status == 0);
1726                         msbp--;
1727                 }
1728         }
1729         spin_unlock(&mp->m_sb_lock);
1730         return status;
1731 }
1732
1733 /*
1734  * xfs_getsb() is called to obtain the buffer for the superblock.
1735  * The buffer is returned locked and read in from disk.
1736  * The buffer should be released with a call to xfs_brelse().
1737  *
1738  * If the flags parameter is BUF_TRYLOCK, then we'll only return
1739  * the superblock buffer if it can be locked without sleeping.
1740  * If it can't then we'll return NULL.
1741  */
1742 xfs_buf_t *
1743 xfs_getsb(
1744         xfs_mount_t     *mp,
1745         int             flags)
1746 {
1747         xfs_buf_t       *bp;
1748
1749         ASSERT(mp->m_sb_bp != NULL);
1750         bp = mp->m_sb_bp;
1751         if (flags & XFS_BUF_TRYLOCK) {
1752                 if (!XFS_BUF_CPSEMA(bp)) {
1753                         return NULL;
1754                 }
1755         } else {
1756                 XFS_BUF_PSEMA(bp, PRIBIO);
1757         }
1758         XFS_BUF_HOLD(bp);
1759         ASSERT(XFS_BUF_ISDONE(bp));
1760         return bp;
1761 }
1762
1763 /*
1764  * Used to free the superblock along various error paths.
1765  */
1766 void
1767 xfs_freesb(
1768         xfs_mount_t     *mp)
1769 {
1770         xfs_buf_t       *bp;
1771
1772         /*
1773          * Use xfs_getsb() so that the buffer will be locked
1774          * when we call xfs_buf_relse().
1775          */
1776         bp = xfs_getsb(mp, 0);
1777         XFS_BUF_UNMANAGE(bp);
1778         xfs_buf_relse(bp);
1779         mp->m_sb_bp = NULL;
1780 }
1781
1782 /*
1783  * See if the UUID is unique among mounted XFS filesystems.
1784  * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
1785  */
1786 STATIC int
1787 xfs_uuid_mount(
1788         xfs_mount_t     *mp)
1789 {
1790         if (uuid_is_nil(&mp->m_sb.sb_uuid)) {
1791                 cmn_err(CE_WARN,
1792                         "XFS: Filesystem %s has nil UUID - can't mount",
1793                         mp->m_fsname);
1794                 return -1;
1795         }
1796         if (!uuid_table_insert(&mp->m_sb.sb_uuid)) {
1797                 cmn_err(CE_WARN,
1798                         "XFS: Filesystem %s has duplicate UUID - can't mount",
1799                         mp->m_fsname);
1800                 return -1;
1801         }
1802         return 0;
1803 }
1804
1805 /*
1806  * Used to log changes to the superblock unit and width fields which could
1807  * be altered by the mount options, as well as any potential sb_features2
1808  * fixup. Only the first superblock is updated.
1809  */
1810 int
1811 xfs_mount_log_sb(
1812         xfs_mount_t     *mp,
1813         __int64_t       fields)
1814 {
1815         xfs_trans_t     *tp;
1816         int             error;
1817
1818         ASSERT(fields & (XFS_SB_UNIT | XFS_SB_WIDTH | XFS_SB_UUID |
1819                          XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2 |
1820                          XFS_SB_VERSIONNUM));
1821
1822         tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT);
1823         error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
1824                                 XFS_DEFAULT_LOG_COUNT);
1825         if (error) {
1826                 xfs_trans_cancel(tp, 0);
1827                 return error;
1828         }
1829         xfs_mod_sb(tp, fields);
1830         error = xfs_trans_commit(tp, 0);
1831         return error;
1832 }
1833
1834
1835 #ifdef HAVE_PERCPU_SB
1836 /*
1837  * Per-cpu incore superblock counters
1838  *
1839  * Simple concept, difficult implementation
1840  *
1841  * Basically, replace the incore superblock counters with a distributed per cpu
1842  * counter for contended fields (e.g.  free block count).
1843  *
1844  * Difficulties arise in that the incore sb is used for ENOSPC checking, and
1845  * hence needs to be accurately read when we are running low on space. Hence
1846  * there is a method to enable and disable the per-cpu counters based on how
1847  * much "stuff" is available in them.
1848  *
1849  * Basically, a counter is enabled if there is enough free resource to justify
1850  * running a per-cpu fast-path. If the per-cpu counter runs out (i.e. a local
1851  * ENOSPC), then we disable the counters to synchronise all callers and
1852  * re-distribute the available resources.
1853  *
1854  * If, once we redistributed the available resources, we still get a failure,
1855  * we disable the per-cpu counter and go through the slow path.
1856  *
1857  * The slow path is the current xfs_mod_incore_sb() function.  This means that
1858  * when we disable a per-cpu counter, we need to drain its resources back to
1859  * the global superblock. We do this after disabling the counter to prevent
1860  * more threads from queueing up on the counter.
1861  *
1862  * Essentially, this means that we still need a lock in the fast path to enable
1863  * synchronisation between the global counters and the per-cpu counters. This
1864  * is not a problem because the lock will be local to a CPU almost all the time
1865  * and have little contention except when we get to ENOSPC conditions.
1866  *
1867  * Basically, this lock becomes a barrier that enables us to lock out the fast
1868  * path while we do things like enabling and disabling counters and
1869  * synchronising the counters.
1870  *
1871  * Locking rules:
1872  *
1873  *      1. m_sb_lock before picking up per-cpu locks
1874  *      2. per-cpu locks always picked up via for_each_online_cpu() order
1875  *      3. accurate counter sync requires m_sb_lock + per cpu locks
1876  *      4. modifying per-cpu counters requires holding per-cpu lock
1877  *      5. modifying global counters requires holding m_sb_lock
1878  *      6. enabling or disabling a counter requires holding the m_sb_lock 
1879  *         and _none_ of the per-cpu locks.
1880  *
1881  * Disabled counters are only ever re-enabled by a balance operation
1882  * that results in more free resources per CPU than a given threshold.
1883  * To ensure counters don't remain disabled, they are rebalanced when
1884  * the global resource goes above a higher threshold (i.e. some hysteresis
1885  * is present to prevent thrashing).
1886  */
1887
1888 #ifdef CONFIG_HOTPLUG_CPU
1889 /*
1890  * hot-plug CPU notifier support.
1891  *
1892  * We need a notifier per filesystem as we need to be able to identify
1893  * the filesystem to balance the counters out. This is achieved by
1894  * having a notifier block embedded in the xfs_mount_t and doing pointer
1895  * magic to get the mount pointer from the notifier block address.
1896  */
1897 STATIC int
1898 xfs_icsb_cpu_notify(
1899         struct notifier_block *nfb,
1900         unsigned long action,
1901         void *hcpu)
1902 {
1903         xfs_icsb_cnts_t *cntp;
1904         xfs_mount_t     *mp;
1905
1906         mp = (xfs_mount_t *)container_of(nfb, xfs_mount_t, m_icsb_notifier);
1907         cntp = (xfs_icsb_cnts_t *)
1908                         per_cpu_ptr(mp->m_sb_cnts, (unsigned long)hcpu);
1909         switch (action) {
1910         case CPU_UP_PREPARE:
1911         case CPU_UP_PREPARE_FROZEN:
1912                 /* Easy Case - initialize the area and locks, and
1913                  * then rebalance when online does everything else for us. */
1914                 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
1915                 break;
1916         case CPU_ONLINE:
1917         case CPU_ONLINE_FROZEN:
1918                 xfs_icsb_lock(mp);
1919                 xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
1920                 xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
1921                 xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
1922                 xfs_icsb_unlock(mp);
1923                 break;
1924         case CPU_DEAD:
1925         case CPU_DEAD_FROZEN:
1926                 /* Disable all the counters, then fold the dead cpu's
1927                  * count into the total on the global superblock and
1928                  * re-enable the counters. */
1929                 xfs_icsb_lock(mp);
1930                 spin_lock(&mp->m_sb_lock);
1931                 xfs_icsb_disable_counter(mp, XFS_SBS_ICOUNT);
1932                 xfs_icsb_disable_counter(mp, XFS_SBS_IFREE);
1933                 xfs_icsb_disable_counter(mp, XFS_SBS_FDBLOCKS);
1934
1935                 mp->m_sb.sb_icount += cntp->icsb_icount;
1936                 mp->m_sb.sb_ifree += cntp->icsb_ifree;
1937                 mp->m_sb.sb_fdblocks += cntp->icsb_fdblocks;
1938
1939                 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
1940
1941                 xfs_icsb_balance_counter_locked(mp, XFS_SBS_ICOUNT, 0);
1942                 xfs_icsb_balance_counter_locked(mp, XFS_SBS_IFREE, 0);
1943                 xfs_icsb_balance_counter_locked(mp, XFS_SBS_FDBLOCKS, 0);
1944                 spin_unlock(&mp->m_sb_lock);
1945                 xfs_icsb_unlock(mp);
1946                 break;
1947         }
1948
1949         return NOTIFY_OK;
1950 }
1951 #endif /* CONFIG_HOTPLUG_CPU */
1952
1953 int
1954 xfs_icsb_init_counters(
1955         xfs_mount_t     *mp)
1956 {
1957         xfs_icsb_cnts_t *cntp;
1958         int             i;
1959
1960         mp->m_sb_cnts = alloc_percpu(xfs_icsb_cnts_t);
1961         if (mp->m_sb_cnts == NULL)
1962                 return -ENOMEM;
1963
1964 #ifdef CONFIG_HOTPLUG_CPU
1965         mp->m_icsb_notifier.notifier_call = xfs_icsb_cpu_notify;
1966         mp->m_icsb_notifier.priority = 0;
1967         register_hotcpu_notifier(&mp->m_icsb_notifier);
1968 #endif /* CONFIG_HOTPLUG_CPU */
1969
1970         for_each_online_cpu(i) {
1971                 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
1972                 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
1973         }
1974
1975         mutex_init(&mp->m_icsb_mutex);
1976
1977         /*
1978          * start with all counters disabled so that the
1979          * initial balance kicks us off correctly
1980          */
1981         mp->m_icsb_counters = -1;
1982         return 0;
1983 }
1984
1985 void
1986 xfs_icsb_reinit_counters(
1987         xfs_mount_t     *mp)
1988 {
1989         xfs_icsb_lock(mp);
1990         /*
1991          * start with all counters disabled so that the
1992          * initial balance kicks us off correctly
1993          */
1994         mp->m_icsb_counters = -1;
1995         xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
1996         xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
1997         xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
1998         xfs_icsb_unlock(mp);
1999 }
2000
2001 void
2002 xfs_icsb_destroy_counters(
2003         xfs_mount_t     *mp)
2004 {
2005         if (mp->m_sb_cnts) {
2006                 unregister_hotcpu_notifier(&mp->m_icsb_notifier);
2007                 free_percpu(mp->m_sb_cnts);
2008         }
2009         mutex_destroy(&mp->m_icsb_mutex);
2010 }
2011
2012 STATIC_INLINE void
2013 xfs_icsb_lock_cntr(
2014         xfs_icsb_cnts_t *icsbp)
2015 {
2016         while (test_and_set_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags)) {
2017                 ndelay(1000);
2018         }
2019 }
2020
2021 STATIC_INLINE void
2022 xfs_icsb_unlock_cntr(
2023         xfs_icsb_cnts_t *icsbp)
2024 {
2025         clear_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags);
2026 }
2027
2028
2029 STATIC_INLINE void
2030 xfs_icsb_lock_all_counters(
2031         xfs_mount_t     *mp)
2032 {
2033         xfs_icsb_cnts_t *cntp;
2034         int             i;
2035
2036         for_each_online_cpu(i) {
2037                 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
2038                 xfs_icsb_lock_cntr(cntp);
2039         }
2040 }
2041
2042 STATIC_INLINE void
2043 xfs_icsb_unlock_all_counters(
2044         xfs_mount_t     *mp)
2045 {
2046         xfs_icsb_cnts_t *cntp;
2047         int             i;
2048
2049         for_each_online_cpu(i) {
2050                 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
2051                 xfs_icsb_unlock_cntr(cntp);
2052         }
2053 }
2054
2055 STATIC void
2056 xfs_icsb_count(
2057         xfs_mount_t     *mp,
2058         xfs_icsb_cnts_t *cnt,
2059         int             flags)
2060 {
2061         xfs_icsb_cnts_t *cntp;
2062         int             i;
2063
2064         memset(cnt, 0, sizeof(xfs_icsb_cnts_t));
2065
2066         if (!(flags & XFS_ICSB_LAZY_COUNT))
2067                 xfs_icsb_lock_all_counters(mp);
2068
2069         for_each_online_cpu(i) {
2070                 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
2071                 cnt->icsb_icount += cntp->icsb_icount;
2072                 cnt->icsb_ifree += cntp->icsb_ifree;
2073                 cnt->icsb_fdblocks += cntp->icsb_fdblocks;
2074         }
2075
2076         if (!(flags & XFS_ICSB_LAZY_COUNT))
2077                 xfs_icsb_unlock_all_counters(mp);
2078 }
2079
2080 STATIC int
2081 xfs_icsb_counter_disabled(
2082         xfs_mount_t     *mp,
2083         xfs_sb_field_t  field)
2084 {
2085         ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
2086         return test_bit(field, &mp->m_icsb_counters);
2087 }
2088
2089 STATIC void
2090 xfs_icsb_disable_counter(
2091         xfs_mount_t     *mp,
2092         xfs_sb_field_t  field)
2093 {
2094         xfs_icsb_cnts_t cnt;
2095
2096         ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
2097
2098         /*
2099          * If we are already disabled, then there is nothing to do
2100          * here. We check before locking all the counters to avoid
2101          * the expensive lock operation when being called in the
2102          * slow path and the counter is already disabled. This is
2103          * safe because the only time we set or clear this state is under
2104          * the m_icsb_mutex.
2105          */
2106         if (xfs_icsb_counter_disabled(mp, field))
2107                 return;
2108
2109         xfs_icsb_lock_all_counters(mp);
2110         if (!test_and_set_bit(field, &mp->m_icsb_counters)) {
2111                 /* drain back to superblock */
2112
2113                 xfs_icsb_count(mp, &cnt, XFS_ICSB_LAZY_COUNT);
2114                 switch(field) {
2115                 case XFS_SBS_ICOUNT:
2116                         mp->m_sb.sb_icount = cnt.icsb_icount;
2117                         break;
2118                 case XFS_SBS_IFREE:
2119                         mp->m_sb.sb_ifree = cnt.icsb_ifree;
2120                         break;
2121                 case XFS_SBS_FDBLOCKS:
2122                         mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
2123                         break;
2124                 default:
2125                         BUG();
2126                 }
2127         }
2128
2129         xfs_icsb_unlock_all_counters(mp);
2130 }
2131
2132 STATIC void
2133 xfs_icsb_enable_counter(
2134         xfs_mount_t     *mp,
2135         xfs_sb_field_t  field,
2136         uint64_t        count,
2137         uint64_t        resid)
2138 {
2139         xfs_icsb_cnts_t *cntp;
2140         int             i;
2141
2142         ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
2143
2144         xfs_icsb_lock_all_counters(mp);
2145         for_each_online_cpu(i) {
2146                 cntp = per_cpu_ptr(mp->m_sb_cnts, i);
2147                 switch (field) {
2148                 case XFS_SBS_ICOUNT:
2149                         cntp->icsb_icount = count + resid;
2150                         break;
2151                 case XFS_SBS_IFREE:
2152                         cntp->icsb_ifree = count + resid;
2153                         break;
2154                 case XFS_SBS_FDBLOCKS:
2155                         cntp->icsb_fdblocks = count + resid;
2156                         break;
2157                 default:
2158                         BUG();
2159                         break;
2160                 }
2161                 resid = 0;
2162         }
2163         clear_bit(field, &mp->m_icsb_counters);
2164         xfs_icsb_unlock_all_counters(mp);
2165 }
2166
2167 void
2168 xfs_icsb_sync_counters_locked(
2169         xfs_mount_t     *mp,
2170         int             flags)
2171 {
2172         xfs_icsb_cnts_t cnt;
2173
2174         xfs_icsb_count(mp, &cnt, flags);
2175
2176         if (!xfs_icsb_counter_disabled(mp, XFS_SBS_ICOUNT))
2177                 mp->m_sb.sb_icount = cnt.icsb_icount;
2178         if (!xfs_icsb_counter_disabled(mp, XFS_SBS_IFREE))
2179                 mp->m_sb.sb_ifree = cnt.icsb_ifree;
2180         if (!xfs_icsb_counter_disabled(mp, XFS_SBS_FDBLOCKS))
2181                 mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
2182 }
2183
2184 /*
2185  * Accurate update of per-cpu counters to incore superblock
2186  */
2187 void
2188 xfs_icsb_sync_counters(
2189         xfs_mount_t     *mp,
2190         int             flags)
2191 {
2192         spin_lock(&mp->m_sb_lock);
2193         xfs_icsb_sync_counters_locked(mp, flags);
2194         spin_unlock(&mp->m_sb_lock);
2195 }
2196
2197 /*
2198  * Balance and enable/disable counters as necessary.
2199  *
2200  * Thresholds for re-enabling counters are somewhat magic.  inode counts are
2201  * chosen to be the same number as single on disk allocation chunk per CPU, and
2202  * free blocks is something far enough zero that we aren't going thrash when we
2203  * get near ENOSPC. We also need to supply a minimum we require per cpu to
2204  * prevent looping endlessly when xfs_alloc_space asks for more than will
2205  * be distributed to a single CPU but each CPU has enough blocks to be
2206  * reenabled.
2207  *
2208  * Note that we can be called when counters are already disabled.
2209  * xfs_icsb_disable_counter() optimises the counter locking in this case to
2210  * prevent locking every per-cpu counter needlessly.
2211  */
2212
2213 #define XFS_ICSB_INO_CNTR_REENABLE      (uint64_t)64
2214 #define XFS_ICSB_FDBLK_CNTR_REENABLE(mp) \
2215                 (uint64_t)(512 + XFS_ALLOC_SET_ASIDE(mp))
2216 STATIC void
2217 xfs_icsb_balance_counter_locked(
2218         xfs_mount_t     *mp,
2219         xfs_sb_field_t  field,
2220         int             min_per_cpu)
2221 {
2222         uint64_t        count, resid;
2223         int             weight = num_online_cpus();
2224         uint64_t        min = (uint64_t)min_per_cpu;
2225
2226         /* disable counter and sync counter */
2227         xfs_icsb_disable_counter(mp, field);
2228
2229         /* update counters  - first CPU gets residual*/
2230         switch (field) {
2231         case XFS_SBS_ICOUNT:
2232                 count = mp->m_sb.sb_icount;
2233                 resid = do_div(count, weight);
2234                 if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
2235                         return;
2236                 break;
2237         case XFS_SBS_IFREE:
2238                 count = mp->m_sb.sb_ifree;
2239                 resid = do_div(count, weight);
2240                 if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
2241                         return;
2242                 break;
2243         case XFS_SBS_FDBLOCKS:
2244                 count = mp->m_sb.sb_fdblocks;
2245                 resid = do_div(count, weight);
2246                 if (count < max(min, XFS_ICSB_FDBLK_CNTR_REENABLE(mp)))
2247                         return;
2248                 break;
2249         default:
2250                 BUG();
2251                 count = resid = 0;      /* quiet, gcc */
2252                 break;
2253         }
2254
2255         xfs_icsb_enable_counter(mp, field, count, resid);
2256 }
2257
2258 STATIC void
2259 xfs_icsb_balance_counter(
2260         xfs_mount_t     *mp,
2261         xfs_sb_field_t  fields,
2262         int             min_per_cpu)
2263 {
2264         spin_lock(&mp->m_sb_lock);
2265         xfs_icsb_balance_counter_locked(mp, fields, min_per_cpu);
2266         spin_unlock(&mp->m_sb_lock);
2267 }
2268
2269 STATIC int
2270 xfs_icsb_modify_counters(
2271         xfs_mount_t     *mp,
2272         xfs_sb_field_t  field,
2273         int64_t         delta,
2274         int             rsvd)
2275 {
2276         xfs_icsb_cnts_t *icsbp;
2277         long long       lcounter;       /* long counter for 64 bit fields */
2278         int             cpu, ret = 0;
2279
2280         might_sleep();
2281 again:
2282         cpu = get_cpu();
2283         icsbp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, cpu);
2284
2285         /*
2286          * if the counter is disabled, go to slow path
2287          */
2288         if (unlikely(xfs_icsb_counter_disabled(mp, field)))
2289                 goto slow_path;
2290         xfs_icsb_lock_cntr(icsbp);
2291         if (unlikely(xfs_icsb_counter_disabled(mp, field))) {
2292                 xfs_icsb_unlock_cntr(icsbp);
2293                 goto slow_path;
2294         }
2295
2296         switch (field) {
2297         case XFS_SBS_ICOUNT:
2298                 lcounter = icsbp->icsb_icount;
2299                 lcounter += delta;
2300                 if (unlikely(lcounter < 0))
2301                         goto balance_counter;
2302                 icsbp->icsb_icount = lcounter;
2303                 break;
2304
2305         case XFS_SBS_IFREE:
2306                 lcounter = icsbp->icsb_ifree;
2307                 lcounter += delta;
2308                 if (unlikely(lcounter < 0))
2309                         goto balance_counter;
2310                 icsbp->icsb_ifree = lcounter;
2311                 break;
2312
2313         case XFS_SBS_FDBLOCKS:
2314                 BUG_ON((mp->m_resblks - mp->m_resblks_avail) != 0);
2315
2316                 lcounter = icsbp->icsb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
2317                 lcounter += delta;
2318                 if (unlikely(lcounter < 0))
2319                         goto balance_counter;
2320                 icsbp->icsb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
2321                 break;
2322         default:
2323                 BUG();
2324                 break;
2325         }
2326         xfs_icsb_unlock_cntr(icsbp);
2327         put_cpu();
2328         return 0;
2329
2330 slow_path:
2331         put_cpu();
2332
2333         /*
2334          * serialise with a mutex so we don't burn lots of cpu on
2335          * the superblock lock. We still need to hold the superblock
2336          * lock, however, when we modify the global structures.
2337          */
2338         xfs_icsb_lock(mp);
2339
2340         /*
2341          * Now running atomically.
2342          *
2343          * If the counter is enabled, someone has beaten us to rebalancing.
2344          * Drop the lock and try again in the fast path....
2345          */
2346         if (!(xfs_icsb_counter_disabled(mp, field))) {
2347                 xfs_icsb_unlock(mp);
2348                 goto again;
2349         }
2350
2351         /*
2352          * The counter is currently disabled. Because we are
2353          * running atomically here, we know a rebalance cannot
2354          * be in progress. Hence we can go straight to operating
2355          * on the global superblock. We do not call xfs_mod_incore_sb()
2356          * here even though we need to get the m_sb_lock. Doing so
2357          * will cause us to re-enter this function and deadlock.
2358          * Hence we get the m_sb_lock ourselves and then call
2359          * xfs_mod_incore_sb_unlocked() as the unlocked path operates
2360          * directly on the global counters.
2361          */
2362         spin_lock(&mp->m_sb_lock);
2363         ret = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
2364         spin_unlock(&mp->m_sb_lock);
2365
2366         /*
2367          * Now that we've modified the global superblock, we
2368          * may be able to re-enable the distributed counters
2369          * (e.g. lots of space just got freed). After that
2370          * we are done.
2371          */
2372         if (ret != ENOSPC)
2373                 xfs_icsb_balance_counter(mp, field, 0);
2374         xfs_icsb_unlock(mp);
2375         return ret;
2376
2377 balance_counter:
2378         xfs_icsb_unlock_cntr(icsbp);
2379         put_cpu();
2380
2381         /*
2382          * We may have multiple threads here if multiple per-cpu
2383          * counters run dry at the same time. This will mean we can
2384          * do more balances than strictly necessary but it is not
2385          * the common slowpath case.
2386          */
2387         xfs_icsb_lock(mp);
2388
2389         /*
2390          * running atomically.
2391          *
2392          * This will leave the counter in the correct state for future
2393          * accesses. After the rebalance, we simply try again and our retry
2394          * will either succeed through the fast path or slow path without
2395          * another balance operation being required.
2396          */
2397         xfs_icsb_balance_counter(mp, field, delta);
2398         xfs_icsb_unlock(mp);
2399         goto again;
2400 }
2401
2402 #endif