blob: 664961e45e0240d87153e238f068f3b26f988799 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_dinode.h"
36#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_btree.h"
38#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_alloc.h"
40#include "xfs_rtalloc.h"
41#include "xfs_bmap.h"
42#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_rw.h"
44#include "xfs_quota.h"
45#include "xfs_fsops.h"
Christoph Hellwig43355092008-03-27 18:01:08 +110046#include "xfs_utils.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048STATIC int xfs_uuid_mount(xfs_mount_t *);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100049STATIC void xfs_unmountfs_wait(xfs_mount_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
David Chinner8d280b92006-03-14 13:13:09 +110051
52#ifdef HAVE_PERCPU_SB
David Chinner20f4ebf2007-02-10 18:36:10 +110053STATIC void xfs_icsb_balance_counter(xfs_mount_t *, xfs_sb_field_t,
Christoph Hellwig45af6c62008-04-22 17:34:44 +100054 int);
55STATIC void xfs_icsb_balance_counter_locked(xfs_mount_t *, xfs_sb_field_t,
56 int);
David Chinner8d280b92006-03-14 13:13:09 +110057STATIC int xfs_icsb_modify_counters(xfs_mount_t *, xfs_sb_field_t,
David Chinner20f4ebf2007-02-10 18:36:10 +110058 int64_t, int);
David Chinner36fbe6e2008-04-10 12:19:56 +100059STATIC void xfs_icsb_disable_counter(xfs_mount_t *, xfs_sb_field_t);
David Chinner8d280b92006-03-14 13:13:09 +110060
61#else
62
Christoph Hellwig45af6c62008-04-22 17:34:44 +100063#define xfs_icsb_balance_counter(mp, a, b) do { } while (0)
64#define xfs_icsb_balance_counter_locked(mp, a, b) do { } while (0)
David Chinner8d280b92006-03-14 13:13:09 +110065#define xfs_icsb_modify_counters(mp, a, b, c) do { } while (0)
David Chinner8d280b92006-03-14 13:13:09 +110066
67#endif
68
Christoph Hellwig1df84c92006-01-11 15:29:52 +110069static const struct {
David Chinner8d280b92006-03-14 13:13:09 +110070 short offset;
71 short type; /* 0 = integer
72 * 1 = binary / string (no translation)
73 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074} 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 },
David Chinneree1c0902008-03-06 13:45:50 +1100120 { offsetof(xfs_sb_t, sb_bad_features2), 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 { sizeof(xfs_sb_t), 0 }
122};
123
124/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 * 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 */
Christoph Hellwigc962fb72008-05-20 15:10:52 +1000129STATIC void
Christoph Hellwigff4f0382008-08-13 16:50:47 +1000130xfs_free_perag(
Christoph Hellwig745f6912007-08-30 17:20:39 +1000131 xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 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)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000138 kmem_free(mp->m_perag[agno].pagb_list);
139 kmem_free(mp->m_perag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Nathan Scott4cc929e2007-05-14 18:24:02 +1000143/*
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 */
147int
148xfs_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}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165/*
166 * Check the validity of the SB found.
167 */
168STATIC int
169xfs_mount_validate_sb(
170 xfs_mount_t *mp,
Nathan Scott764d1f82006-03-31 13:04:17 +1000171 xfs_sb_t *sbp,
172 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
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) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000182 xfs_fs_mount_cmn_err(flags, "bad magic number");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return XFS_ERROR(EWRONGFS);
184 }
185
Eric Sandeen62118702008-03-06 13:44:28 +1100186 if (!xfs_sb_good_version(sbp)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000187 xfs_fs_mount_cmn_err(flags, "bad version");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return XFS_ERROR(EWRONGFS);
189 }
190
191 if (unlikely(
192 sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000193 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
198
199 if (unlikely(
200 sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000201 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
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 ||
Nathan Scott9f989c92006-03-14 13:29:32 +1100223 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) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
227 (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
Nathan Scotte50bd162006-04-11 15:10:45 +1000228 (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */))) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000229 xfs_fs_mount_cmn_err(flags, "SB sanity check 1 failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 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)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000242 xfs_fs_mount_cmn_err(flags, "SB sanity check 2 failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return XFS_ERROR(EFSCORRUPTED);
244 }
245
Lachlan McIlroy2edbddd2008-06-27 13:34:34 +1000246 /*
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
Nathan Scott4cc929e2007-05-14 18:24:02 +1000259 if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) ||
260 xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000261 xfs_fs_mount_cmn_err(flags,
262 "file system too large to be mounted on this system.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return XFS_ERROR(E2BIG);
264 }
265
266 if (unlikely(sbp->sb_inprogress)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000267 xfs_fs_mount_cmn_err(flags, "file system busy");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return XFS_ERROR(EFSCORRUPTED);
269 }
270
271 /*
Nathan Scottde206142005-05-05 13:24:13 -0700272 * Version 1 directory format has never worked on Linux.
273 */
Eric Sandeen62118702008-03-06 13:44:28 +1100274 if (unlikely(!xfs_sb_version_hasdirv2(sbp))) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000275 xfs_fs_mount_cmn_err(flags,
276 "file system using version 1 directory format");
Nathan Scottde206142005-05-05 13:24:13 -0700277 return XFS_ERROR(ENOSYS);
278 }
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return 0;
281}
282
David Chinnerda353b02007-08-28 14:00:13 +1000283STATIC void
284xfs_initialize_perag_icache(
285 xfs_perag_t *pag)
286{
287 if (!pag->pag_ici_init) {
288 rwlock_init(&pag->pag_ici_lock);
289 INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC);
290 pag->pag_ici_init = 1;
291 }
292}
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294xfs_agnumber_t
Nathan Scottc11e2c32005-11-02 15:11:45 +1100295xfs_initialize_perag(
Nathan Scottc11e2c32005-11-02 15:11:45 +1100296 xfs_mount_t *mp,
297 xfs_agnumber_t agcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 xfs_agnumber_t index, max_metadata;
300 xfs_perag_t *pag;
301 xfs_agino_t agino;
302 xfs_ino_t ino;
303 xfs_sb_t *sbp = &mp->m_sb;
304 xfs_ino_t max_inum = XFS_MAXINUMBER_32;
305
306 /* Check to see if the filesystem can overflow 32 bit inodes */
307 agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
308 ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
309
310 /* Clear the mount flag if no inode can overflow 32 bits
311 * on this filesystem, or if specifically requested..
312 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000313 if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) && ino > max_inum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 mp->m_flags |= XFS_MOUNT_32BITINODES;
315 } else {
316 mp->m_flags &= ~XFS_MOUNT_32BITINODES;
317 }
318
319 /* If we can overflow then setup the ag headers accordingly */
320 if (mp->m_flags & XFS_MOUNT_32BITINODES) {
321 /* Calculate how much should be reserved for inodes to
322 * meet the max inode percentage.
323 */
324 if (mp->m_maxicount) {
325 __uint64_t icount;
326
327 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
328 do_div(icount, 100);
329 icount += sbp->sb_agblocks - 1;
Eric Sandeena749ee82005-11-02 15:13:42 +1100330 do_div(icount, sbp->sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 max_metadata = icount;
332 } else {
333 max_metadata = agcount;
334 }
335 for (index = 0; index < agcount; index++) {
336 ino = XFS_AGINO_TO_INO(mp, index, agino);
337 if (ino > max_inum) {
338 index++;
339 break;
340 }
341
Nathan Scottc41564b2006-03-29 08:55:14 +1000342 /* This ag is preferred for inodes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 pag = &mp->m_perag[index];
344 pag->pagi_inodeok = 1;
345 if (index < max_metadata)
346 pag->pagf_metadata = 1;
David Chinnerda353b02007-08-28 14:00:13 +1000347 xfs_initialize_perag_icache(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349 } else {
350 /* Setup default behavior for smaller filesystems */
351 for (index = 0; index < agcount; index++) {
352 pag = &mp->m_perag[index];
353 pag->pagi_inodeok = 1;
David Chinnerda353b02007-08-28 14:00:13 +1000354 xfs_initialize_perag_icache(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356 }
357 return index;
358}
359
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000360void
361xfs_sb_from_disk(
362 xfs_sb_t *to,
363 xfs_dsb_t *from)
364{
365 to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
366 to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
367 to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
368 to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
369 to->sb_rextents = be64_to_cpu(from->sb_rextents);
370 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
371 to->sb_logstart = be64_to_cpu(from->sb_logstart);
372 to->sb_rootino = be64_to_cpu(from->sb_rootino);
373 to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
374 to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
375 to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
376 to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
377 to->sb_agcount = be32_to_cpu(from->sb_agcount);
378 to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
379 to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
380 to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
381 to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
382 to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
383 to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
384 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
385 to->sb_blocklog = from->sb_blocklog;
386 to->sb_sectlog = from->sb_sectlog;
387 to->sb_inodelog = from->sb_inodelog;
388 to->sb_inopblog = from->sb_inopblog;
389 to->sb_agblklog = from->sb_agblklog;
390 to->sb_rextslog = from->sb_rextslog;
391 to->sb_inprogress = from->sb_inprogress;
392 to->sb_imax_pct = from->sb_imax_pct;
393 to->sb_icount = be64_to_cpu(from->sb_icount);
394 to->sb_ifree = be64_to_cpu(from->sb_ifree);
395 to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
396 to->sb_frextents = be64_to_cpu(from->sb_frextents);
397 to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
398 to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
399 to->sb_qflags = be16_to_cpu(from->sb_qflags);
400 to->sb_flags = from->sb_flags;
401 to->sb_shared_vn = from->sb_shared_vn;
402 to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
403 to->sb_unit = be32_to_cpu(from->sb_unit);
404 to->sb_width = be32_to_cpu(from->sb_width);
405 to->sb_dirblklog = from->sb_dirblklog;
406 to->sb_logsectlog = from->sb_logsectlog;
407 to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
408 to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
409 to->sb_features2 = be32_to_cpu(from->sb_features2);
David Chinneree1c0902008-03-06 13:45:50 +1100410 to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000411}
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413/*
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000414 * Copy in core superblock to ondisk one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 *
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000416 * The fields argument is mask of superblock fields to copy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 */
418void
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000419xfs_sb_to_disk(
420 xfs_dsb_t *to,
421 xfs_sb_t *from,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 __int64_t fields)
423{
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000424 xfs_caddr_t to_ptr = (xfs_caddr_t)to;
425 xfs_caddr_t from_ptr = (xfs_caddr_t)from;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 xfs_sb_field_t f;
427 int first;
428 int size;
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 ASSERT(fields);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (!fields)
432 return;
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 while (fields) {
435 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
436 first = xfs_sb_info[f].offset;
437 size = xfs_sb_info[f + 1].offset - first;
438
439 ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1);
440
441 if (size == 1 || xfs_sb_info[f].type == 1) {
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000442 memcpy(to_ptr + first, from_ptr + first, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 } else {
444 switch (size) {
445 case 2:
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000446 *(__be16 *)(to_ptr + first) =
447 cpu_to_be16(*(__u16 *)(from_ptr + first));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 break;
449 case 4:
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000450 *(__be32 *)(to_ptr + first) =
451 cpu_to_be32(*(__u32 *)(from_ptr + first));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 break;
453 case 8:
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000454 *(__be64 *)(to_ptr + first) =
455 cpu_to_be64(*(__u64 *)(from_ptr + first));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 break;
457 default:
458 ASSERT(0);
459 }
460 }
461
462 fields &= ~(1LL << f);
463 }
464}
465
466/*
467 * xfs_readsb
468 *
469 * Does the initial read of the superblock.
470 */
471int
Nathan Scott764d1f82006-03-31 13:04:17 +1000472xfs_readsb(xfs_mount_t *mp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 unsigned int sector_size;
475 unsigned int extra_flags;
476 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 int error;
478
479 ASSERT(mp->m_sb_bp == NULL);
480 ASSERT(mp->m_ddev_targp != NULL);
481
482 /*
483 * Allocate a (locked) buffer to hold the superblock.
484 * This will be kept around at all times to optimize
485 * access to the superblock.
486 */
487 sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
488 extra_flags = XFS_BUF_LOCK | XFS_BUF_MANAGE | XFS_BUF_MAPPED;
489
490 bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR,
491 BTOBB(sector_size), extra_flags);
492 if (!bp || XFS_BUF_ISERROR(bp)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000493 xfs_fs_mount_cmn_err(flags, "SB read failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
495 goto fail;
496 }
497 ASSERT(XFS_BUF_ISBUSY(bp));
498 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
499
500 /*
501 * Initialize the mount structure from the superblock.
502 * But first do some basic consistency checking.
503 */
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000504 xfs_sb_from_disk(&mp->m_sb, XFS_BUF_TO_SBP(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Nathan Scott764d1f82006-03-31 13:04:17 +1000506 error = xfs_mount_validate_sb(mp, &(mp->m_sb), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (error) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000508 xfs_fs_mount_cmn_err(flags, "SB validate failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 goto fail;
510 }
511
512 /*
513 * We must be able to do sector-sized and sector-aligned IO.
514 */
515 if (sector_size > mp->m_sb.sb_sectsize) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000516 xfs_fs_mount_cmn_err(flags,
517 "device supports only %u byte sectors (not %u)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 sector_size, mp->m_sb.sb_sectsize);
519 error = ENOSYS;
520 goto fail;
521 }
522
523 /*
524 * If device sector size is smaller than the superblock size,
525 * re-read the superblock so the buffer is correctly sized.
526 */
527 if (sector_size < mp->m_sb.sb_sectsize) {
528 XFS_BUF_UNMANAGE(bp);
529 xfs_buf_relse(bp);
530 sector_size = mp->m_sb.sb_sectsize;
531 bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR,
532 BTOBB(sector_size), extra_flags);
533 if (!bp || XFS_BUF_ISERROR(bp)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000534 xfs_fs_mount_cmn_err(flags, "SB re-read failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
536 goto fail;
537 }
538 ASSERT(XFS_BUF_ISBUSY(bp));
539 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
540 }
541
Lachlan McIlroy5478eea2007-02-10 18:36:29 +1100542 /* Initialize per-cpu counters */
543 xfs_icsb_reinit_counters(mp);
David Chinner8d280b92006-03-14 13:13:09 +1100544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 mp->m_sb_bp = bp;
546 xfs_buf_relse(bp);
547 ASSERT(XFS_BUF_VALUSEMA(bp) > 0);
548 return 0;
549
550 fail:
551 if (bp) {
552 XFS_BUF_UNMANAGE(bp);
553 xfs_buf_relse(bp);
554 }
555 return error;
556}
557
558
559/*
560 * xfs_mount_common
561 *
562 * Mount initialization code establishing various mount
563 * fields from the superblock associated with the given
564 * mount structure
565 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000566STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567xfs_mount_common(xfs_mount_t *mp, xfs_sb_t *sbp)
568{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 mp->m_agfrotor = mp->m_agirotor = 0;
Eric Sandeen007c61c2007-10-11 17:43:56 +1000570 spin_lock_init(&mp->m_agirotor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 mp->m_maxagi = mp->m_sb.sb_agcount;
572 mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
573 mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
574 mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
575 mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
576 mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100577 mp->m_litino = sbp->sb_inodesize - sizeof(struct xfs_dinode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 mp->m_blockmask = sbp->sb_blocksize - 1;
579 mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
580 mp->m_blockwmask = mp->m_blockwsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 /*
583 * Setup for attributes, in case they get created.
584 * This value is for inodes getting attributes for the first time,
585 * the per-inode value is for old attribute values.
586 */
587 ASSERT(sbp->sb_inodesize >= 256 && sbp->sb_inodesize <= 2048);
588 switch (sbp->sb_inodesize) {
589 case 256:
Nathan Scottd8cc8902005-11-02 10:34:53 +1100590 mp->m_attroffset = XFS_LITINO(mp) -
591 XFS_BMDR_SPACE_CALC(MINABTPTRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 break;
593 case 512:
594 case 1024:
595 case 2048:
Nathan Scottd8cc8902005-11-02 10:34:53 +1100596 mp->m_attroffset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 break;
598 default:
599 ASSERT(0);
600 }
601 ASSERT(mp->m_attroffset < XFS_LITINO(mp));
602
Christoph Hellwig60197e82008-10-30 17:11:19 +1100603 mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
604 mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
605 mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
606 mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
607
608 mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
609 mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
610 mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2;
611 mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2;
612
613 mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
614 mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
615 mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
616 mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
619 mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
620 sbp->sb_inopblock);
621 mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
622}
David Chinner92821e22007-05-24 15:26:31 +1000623
624/*
625 * xfs_initialize_perag_data
626 *
627 * Read in each per-ag structure so we can count up the number of
628 * allocated inodes, free inodes and used filesystem blocks as this
629 * information is no longer persistent in the superblock. Once we have
630 * this information, write it into the in-core superblock structure.
631 */
632STATIC int
633xfs_initialize_perag_data(xfs_mount_t *mp, xfs_agnumber_t agcount)
634{
635 xfs_agnumber_t index;
636 xfs_perag_t *pag;
637 xfs_sb_t *sbp = &mp->m_sb;
638 uint64_t ifree = 0;
639 uint64_t ialloc = 0;
640 uint64_t bfree = 0;
641 uint64_t bfreelst = 0;
642 uint64_t btree = 0;
643 int error;
David Chinner92821e22007-05-24 15:26:31 +1000644
645 for (index = 0; index < agcount; index++) {
646 /*
647 * read the agf, then the agi. This gets us
648 * all the inforamtion we need and populates the
649 * per-ag structures for us.
650 */
651 error = xfs_alloc_pagf_init(mp, NULL, index, 0);
652 if (error)
653 return error;
654
655 error = xfs_ialloc_pagi_init(mp, NULL, index);
656 if (error)
657 return error;
658 pag = &mp->m_perag[index];
659 ifree += pag->pagi_freecount;
660 ialloc += pag->pagi_count;
661 bfree += pag->pagf_freeblks;
662 bfreelst += pag->pagf_flcount;
663 btree += pag->pagf_btreeblks;
664 }
665 /*
666 * Overwrite incore superblock counters with just-read data
667 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000668 spin_lock(&mp->m_sb_lock);
David Chinner92821e22007-05-24 15:26:31 +1000669 sbp->sb_ifree = ifree;
670 sbp->sb_icount = ialloc;
671 sbp->sb_fdblocks = bfree + bfreelst + btree;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000672 spin_unlock(&mp->m_sb_lock);
David Chinner92821e22007-05-24 15:26:31 +1000673
674 /* Fixup the per-cpu counters as well. */
675 xfs_icsb_reinit_counters(mp);
676
677 return 0;
678}
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680/*
Eric Sandeen0771fb42007-10-12 11:03:40 +1000681 * Update alignment values based on mount options and sb values
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 */
Eric Sandeen0771fb42007-10-12 11:03:40 +1000683STATIC int
Christoph Hellwig7884bc82009-01-19 02:04:07 +0100684xfs_update_alignment(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 xfs_sb_t *sbp = &(mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Christoph Hellwig42490232008-08-13 16:49:32 +1000688 if (mp->m_dalign) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 /*
690 * If stripe unit and stripe width are not multiples
691 * of the fs blocksize turn off alignment.
692 */
693 if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
694 (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
695 if (mp->m_flags & XFS_MOUNT_RETERR) {
696 cmn_err(CE_WARN,
697 "XFS: alignment check 1 failed");
Eric Sandeen0771fb42007-10-12 11:03:40 +1000698 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
700 mp->m_dalign = mp->m_swidth = 0;
701 } else {
702 /*
703 * Convert the stripe unit and width to FSBs.
704 */
705 mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
706 if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
707 if (mp->m_flags & XFS_MOUNT_RETERR) {
Eric Sandeen0771fb42007-10-12 11:03:40 +1000708 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710 xfs_fs_cmn_err(CE_WARN, mp,
711"stripe alignment turned off: sunit(%d)/swidth(%d) incompatible with agsize(%d)",
712 mp->m_dalign, mp->m_swidth,
713 sbp->sb_agblocks);
714
715 mp->m_dalign = 0;
716 mp->m_swidth = 0;
717 } else if (mp->m_dalign) {
718 mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
719 } else {
720 if (mp->m_flags & XFS_MOUNT_RETERR) {
721 xfs_fs_cmn_err(CE_WARN, mp,
722"stripe alignment turned off: sunit(%d) less than bsize(%d)",
723 mp->m_dalign,
724 mp->m_blockmask +1);
Eric Sandeen0771fb42007-10-12 11:03:40 +1000725 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727 mp->m_swidth = 0;
728 }
729 }
730
731 /*
732 * Update superblock with new values
733 * and log changes
734 */
Eric Sandeen62118702008-03-06 13:44:28 +1100735 if (xfs_sb_version_hasdalign(sbp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (sbp->sb_unit != mp->m_dalign) {
737 sbp->sb_unit = mp->m_dalign;
Christoph Hellwig7884bc82009-01-19 02:04:07 +0100738 mp->m_update_flags |= XFS_SB_UNIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
740 if (sbp->sb_width != mp->m_swidth) {
741 sbp->sb_width = mp->m_swidth;
Christoph Hellwig7884bc82009-01-19 02:04:07 +0100742 mp->m_update_flags |= XFS_SB_WIDTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744 }
745 } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
Eric Sandeen62118702008-03-06 13:44:28 +1100746 xfs_sb_version_hasdalign(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 mp->m_dalign = sbp->sb_unit;
748 mp->m_swidth = sbp->sb_width;
749 }
750
Eric Sandeen0771fb42007-10-12 11:03:40 +1000751 return 0;
752}
753
754/*
755 * Set the maximum inode count for this filesystem
756 */
757STATIC void
758xfs_set_maxicount(xfs_mount_t *mp)
759{
760 xfs_sb_t *sbp = &(mp->m_sb);
761 __uint64_t icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 if (sbp->sb_imax_pct) {
Eric Sandeen0771fb42007-10-12 11:03:40 +1000764 /*
765 * Make sure the maximum inode count is a multiple
766 * of the units we allocate inodes in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
769 do_div(icount, 100);
770 do_div(icount, mp->m_ialloc_blks);
771 mp->m_maxicount = (icount * mp->m_ialloc_blks) <<
772 sbp->sb_inopblog;
Eric Sandeen0771fb42007-10-12 11:03:40 +1000773 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 mp->m_maxicount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
Eric Sandeen0771fb42007-10-12 11:03:40 +1000776}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Eric Sandeen0771fb42007-10-12 11:03:40 +1000778/*
779 * Set the default minimum read and write sizes unless
780 * already specified in a mount option.
781 * We use smaller I/O sizes when the file system
782 * is being used for NFS service (wsync mount option).
783 */
784STATIC void
785xfs_set_rw_sizes(xfs_mount_t *mp)
786{
787 xfs_sb_t *sbp = &(mp->m_sb);
788 int readio_log, writeio_log;
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
791 if (mp->m_flags & XFS_MOUNT_WSYNC) {
792 readio_log = XFS_WSYNC_READIO_LOG;
793 writeio_log = XFS_WSYNC_WRITEIO_LOG;
794 } else {
795 readio_log = XFS_READIO_LOG_LARGE;
796 writeio_log = XFS_WRITEIO_LOG_LARGE;
797 }
798 } else {
799 readio_log = mp->m_readio_log;
800 writeio_log = mp->m_writeio_log;
801 }
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (sbp->sb_blocklog > readio_log) {
804 mp->m_readio_log = sbp->sb_blocklog;
805 } else {
806 mp->m_readio_log = readio_log;
807 }
808 mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
809 if (sbp->sb_blocklog > writeio_log) {
810 mp->m_writeio_log = sbp->sb_blocklog;
811 } else {
812 mp->m_writeio_log = writeio_log;
813 }
814 mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
Eric Sandeen0771fb42007-10-12 11:03:40 +1000815}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Eric Sandeen0771fb42007-10-12 11:03:40 +1000817/*
818 * Set whether we're using inode alignment.
819 */
820STATIC void
821xfs_set_inoalignment(xfs_mount_t *mp)
822{
Eric Sandeen62118702008-03-06 13:44:28 +1100823 if (xfs_sb_version_hasalign(&mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 mp->m_sb.sb_inoalignmt >=
825 XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
826 mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
827 else
828 mp->m_inoalign_mask = 0;
829 /*
830 * If we are using stripe alignment, check whether
831 * the stripe unit is a multiple of the inode alignment
832 */
833 if (mp->m_dalign && mp->m_inoalign_mask &&
834 !(mp->m_dalign & mp->m_inoalign_mask))
835 mp->m_sinoalign = mp->m_dalign;
836 else
837 mp->m_sinoalign = 0;
Eric Sandeen0771fb42007-10-12 11:03:40 +1000838}
839
840/*
841 * Check that the data (and log if separate) are an ok size.
842 */
843STATIC int
Christoph Hellwig42490232008-08-13 16:49:32 +1000844xfs_check_sizes(xfs_mount_t *mp)
Eric Sandeen0771fb42007-10-12 11:03:40 +1000845{
846 xfs_buf_t *bp;
847 xfs_daddr_t d;
848 int error;
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
851 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
852 cmn_err(CE_WARN, "XFS: size check 1 failed");
Eric Sandeen0771fb42007-10-12 11:03:40 +1000853 return XFS_ERROR(E2BIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855 error = xfs_read_buf(mp, mp->m_ddev_targp,
856 d - XFS_FSS_TO_BB(mp, 1),
857 XFS_FSS_TO_BB(mp, 1), 0, &bp);
858 if (!error) {
859 xfs_buf_relse(bp);
860 } else {
861 cmn_err(CE_WARN, "XFS: size check 2 failed");
Eric Sandeen0771fb42007-10-12 11:03:40 +1000862 if (error == ENOSPC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 error = XFS_ERROR(E2BIG);
Eric Sandeen0771fb42007-10-12 11:03:40 +1000864 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866
Christoph Hellwig42490232008-08-13 16:49:32 +1000867 if (mp->m_logdev_targp != mp->m_ddev_targp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
869 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
870 cmn_err(CE_WARN, "XFS: size check 3 failed");
Eric Sandeen0771fb42007-10-12 11:03:40 +1000871 return XFS_ERROR(E2BIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
873 error = xfs_read_buf(mp, mp->m_logdev_targp,
874 d - XFS_FSB_TO_BB(mp, 1),
875 XFS_FSB_TO_BB(mp, 1), 0, &bp);
876 if (!error) {
877 xfs_buf_relse(bp);
878 } else {
879 cmn_err(CE_WARN, "XFS: size check 3 failed");
Eric Sandeen0771fb42007-10-12 11:03:40 +1000880 if (error == ENOSPC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 error = XFS_ERROR(E2BIG);
Eric Sandeen0771fb42007-10-12 11:03:40 +1000882 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
884 }
Eric Sandeen0771fb42007-10-12 11:03:40 +1000885 return 0;
886}
887
888/*
Eric Sandeen0771fb42007-10-12 11:03:40 +1000889 * This function does the following on an initial mount of a file system:
890 * - reads the superblock from disk and init the mount struct
891 * - if we're a 32-bit kernel, do a size check on the superblock
892 * so we don't mount terabyte filesystems
893 * - init mount struct realtime fields
894 * - allocate inode hash table for fs
895 * - init directory manager
896 * - perform recovery and init the log manager
897 */
898int
899xfs_mountfs(
Christoph Hellwig42490232008-08-13 16:49:32 +1000900 xfs_mount_t *mp)
Eric Sandeen0771fb42007-10-12 11:03:40 +1000901{
902 xfs_sb_t *sbp = &(mp->m_sb);
903 xfs_inode_t *rip;
Eric Sandeen0771fb42007-10-12 11:03:40 +1000904 __uint64_t resblks;
Eric Sandeen0771fb42007-10-12 11:03:40 +1000905 uint quotamount, quotaflags;
Eric Sandeen0771fb42007-10-12 11:03:40 +1000906 int error = 0;
907
Eric Sandeen0771fb42007-10-12 11:03:40 +1000908 xfs_mount_common(mp, sbp);
909
910 /*
Eric Sandeene6957ea2008-04-10 12:19:34 +1000911 * Check for a mismatched features2 values. Older kernels
912 * read & wrote into the wrong sb offset for sb_features2
913 * on some platforms due to xfs_sb_t not being 64bit size aligned
914 * when sb_features2 was added, which made older superblock
915 * reading/writing routines swap it as a 64-bit value.
David Chinneree1c0902008-03-06 13:45:50 +1100916 *
Eric Sandeene6957ea2008-04-10 12:19:34 +1000917 * For backwards compatibility, we make both slots equal.
918 *
919 * If we detect a mismatched field, we OR the set bits into the
920 * existing features2 field in case it has already been modified; we
921 * don't want to lose any features. We then update the bad location
922 * with the ORed value so that older kernels will see any features2
923 * flags, and mark the two fields as needing updates once the
924 * transaction subsystem is online.
David Chinneree1c0902008-03-06 13:45:50 +1100925 */
Eric Sandeene6957ea2008-04-10 12:19:34 +1000926 if (xfs_sb_has_mismatched_features2(sbp)) {
David Chinneree1c0902008-03-06 13:45:50 +1100927 cmn_err(CE_WARN,
928 "XFS: correcting sb_features alignment problem");
929 sbp->sb_features2 |= sbp->sb_bad_features2;
Eric Sandeene6957ea2008-04-10 12:19:34 +1000930 sbp->sb_bad_features2 = sbp->sb_features2;
Christoph Hellwig7884bc82009-01-19 02:04:07 +0100931 mp->m_update_flags |= XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2;
Eric Sandeene6957ea2008-04-10 12:19:34 +1000932
933 /*
934 * Re-check for ATTR2 in case it was found in bad_features2
935 * slot.
936 */
Tim Shimmin7c12f292008-04-30 18:15:28 +1000937 if (xfs_sb_version_hasattr2(&mp->m_sb) &&
938 !(mp->m_flags & XFS_MOUNT_NOATTR2))
Eric Sandeene6957ea2008-04-10 12:19:34 +1000939 mp->m_flags |= XFS_MOUNT_ATTR2;
Tim Shimmin7c12f292008-04-30 18:15:28 +1000940 }
Eric Sandeene6957ea2008-04-10 12:19:34 +1000941
Tim Shimmin7c12f292008-04-30 18:15:28 +1000942 if (xfs_sb_version_hasattr2(&mp->m_sb) &&
943 (mp->m_flags & XFS_MOUNT_NOATTR2)) {
944 xfs_sb_version_removeattr2(&mp->m_sb);
Christoph Hellwig7884bc82009-01-19 02:04:07 +0100945 mp->m_update_flags |= XFS_SB_FEATURES2;
Tim Shimmin7c12f292008-04-30 18:15:28 +1000946
947 /* update sb_versionnum for the clearing of the morebits */
948 if (!sbp->sb_features2)
Christoph Hellwig7884bc82009-01-19 02:04:07 +0100949 mp->m_update_flags |= XFS_SB_VERSIONNUM;
David Chinneree1c0902008-03-06 13:45:50 +1100950 }
951
952 /*
Eric Sandeen0771fb42007-10-12 11:03:40 +1000953 * Check if sb_agblocks is aligned at stripe boundary
954 * If sb_agblocks is NOT aligned turn off m_dalign since
955 * allocator alignment is within an ag, therefore ag has
956 * to be aligned at stripe boundary.
957 */
Christoph Hellwig7884bc82009-01-19 02:04:07 +0100958 error = xfs_update_alignment(mp);
Eric Sandeen0771fb42007-10-12 11:03:40 +1000959 if (error)
Christoph Hellwigf9057e32009-02-04 09:31:52 +0100960 goto out;
Eric Sandeen0771fb42007-10-12 11:03:40 +1000961
962 xfs_alloc_compute_maxlevels(mp);
963 xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
964 xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
965 xfs_ialloc_compute_maxlevels(mp);
966
967 xfs_set_maxicount(mp);
968
969 mp->m_maxioffset = xfs_max_file_offset(sbp->sb_blocklog);
970
971 /*
972 * XFS uses the uuid from the superblock as the unique
973 * identifier for fsid. We can not use the uuid from the volume
974 * since a single partition filesystem is identical to a single
975 * partition volume/filesystem.
976 */
Christoph Hellwigf9057e32009-02-04 09:31:52 +0100977 if (!(mp->m_flags & XFS_MOUNT_NOUUID)) {
Eric Sandeen0771fb42007-10-12 11:03:40 +1000978 if (xfs_uuid_mount(mp)) {
979 error = XFS_ERROR(EINVAL);
Christoph Hellwigf9057e32009-02-04 09:31:52 +0100980 goto out;
Eric Sandeen0771fb42007-10-12 11:03:40 +1000981 }
Eric Sandeen0771fb42007-10-12 11:03:40 +1000982 }
983
984 /*
985 * Set the minimum read and write sizes
986 */
987 xfs_set_rw_sizes(mp);
988
989 /*
990 * Set the inode cluster size.
991 * This may still be overridden by the file system
992 * block size if it is larger than the chosen cluster size.
993 */
994 mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
995
996 /*
997 * Set inode alignment fields
998 */
999 xfs_set_inoalignment(mp);
1000
1001 /*
1002 * Check that the data (and log if separate) are an ok size.
1003 */
Christoph Hellwig42490232008-08-13 16:49:32 +10001004 error = xfs_check_sizes(mp);
Eric Sandeen0771fb42007-10-12 11:03:40 +10001005 if (error)
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001006 goto out_remove_uuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 /*
1009 * Initialize realtime fields in the mount structure
1010 */
Eric Sandeen0771fb42007-10-12 11:03:40 +10001011 error = xfs_rtmount_init(mp);
1012 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 cmn_err(CE_WARN, "XFS: RT mount failed");
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001014 goto out_remove_uuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016
1017 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 * Copies the low order bits of the timestamp and the randomly
1019 * set "sequence" number out of a UUID.
1020 */
1021 uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid);
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 mp->m_dmevmask = 0; /* not persistent; set after each mount */
1024
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001025 xfs_dir_mount(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027 /*
1028 * Initialize the attribute manager's entries.
1029 */
1030 mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100;
1031
1032 /*
1033 * Initialize the precomputed transaction reservations values.
1034 */
1035 xfs_trans_init(mp);
1036
1037 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 * Allocate and initialize the per-ag data.
1039 */
1040 init_rwsem(&mp->m_peraglock);
Christoph Hellwigd62c2512008-08-13 16:51:29 +10001041 mp->m_perag = kmem_zalloc(sbp->sb_agcount * sizeof(xfs_perag_t),
1042 KM_MAYFAIL);
1043 if (!mp->m_perag)
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001044 goto out_remove_uuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Christoph Hellwigb267ce92007-08-30 17:21:30 +10001046 mp->m_maxagi = xfs_initialize_perag(mp, sbp->sb_agcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001048 if (!sbp->sb_logblocks) {
1049 cmn_err(CE_WARN, "XFS: no log defined");
1050 XFS_ERROR_REPORT("xfs_mountfs", XFS_ERRLEVEL_LOW, mp);
1051 error = XFS_ERROR(EFSCORRUPTED);
1052 goto out_free_perag;
1053 }
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 /*
1056 * log's mount-time initialization. Perform 1st part recovery if needed
1057 */
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001058 error = xfs_log_mount(mp, mp->m_logdev_targp,
1059 XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
1060 XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
1061 if (error) {
1062 cmn_err(CE_WARN, "XFS: log mount failed");
1063 goto out_free_perag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065
1066 /*
David Chinner92821e22007-05-24 15:26:31 +10001067 * Now the log is mounted, we know if it was an unclean shutdown or
1068 * not. If it was, with the first phase of recovery has completed, we
1069 * have consistent AG blocks on disk. We have not recovered EFIs yet,
1070 * but they are recovered transactionally in the second recovery phase
1071 * later.
1072 *
1073 * Hence we can safely re-initialise incore superblock counters from
1074 * the per-ag data. These may not be correct if the filesystem was not
1075 * cleanly unmounted, so we need to wait for recovery to finish before
1076 * doing this.
1077 *
1078 * If the filesystem was cleanly unmounted, then we can trust the
1079 * values in the superblock to be correct and we don't need to do
1080 * anything here.
1081 *
1082 * If we are currently making the filesystem, the initialisation will
1083 * fail as the perag data is in an undefined state.
1084 */
David Chinner92821e22007-05-24 15:26:31 +10001085 if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
1086 !XFS_LAST_UNMOUNT_WAS_CLEAN(mp) &&
1087 !mp->m_sb.sb_inprogress) {
1088 error = xfs_initialize_perag_data(mp, sbp->sb_agcount);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001089 if (error)
1090 goto out_free_perag;
David Chinner92821e22007-05-24 15:26:31 +10001091 }
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001092
David Chinner92821e22007-05-24 15:26:31 +10001093 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 * Get and sanity-check the root inode.
1095 * Save the pointer to it in the mount structure.
1096 */
1097 error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip, 0);
1098 if (error) {
1099 cmn_err(CE_WARN, "XFS: failed to read root inode");
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001100 goto out_log_dealloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
1102
1103 ASSERT(rip != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 if (unlikely((rip->i_d.di_mode & S_IFMT) != S_IFDIR)) {
1106 cmn_err(CE_WARN, "XFS: corrupted root inode");
Nathan Scottb6574522006-06-09 15:29:40 +10001107 cmn_err(CE_WARN, "Device %s - root %llu is not a directory",
1108 XFS_BUFTARG_NAME(mp->m_ddev_targp),
1109 (unsigned long long)rip->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 xfs_iunlock(rip, XFS_ILOCK_EXCL);
1111 XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
1112 mp);
1113 error = XFS_ERROR(EFSCORRUPTED);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001114 goto out_rele_rip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
1116 mp->m_rootip = rip; /* save it */
1117
1118 xfs_iunlock(rip, XFS_ILOCK_EXCL);
1119
1120 /*
1121 * Initialize realtime inode pointers in the mount structure
1122 */
Eric Sandeen0771fb42007-10-12 11:03:40 +10001123 error = xfs_rtmount_inodes(mp);
1124 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 /*
1126 * Free up the root inode.
1127 */
1128 cmn_err(CE_WARN, "XFS: failed to read RT inodes");
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001129 goto out_rele_rip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131
1132 /*
Christoph Hellwig7884bc82009-01-19 02:04:07 +01001133 * If this is a read-only mount defer the superblock updates until
1134 * the next remount into writeable mode. Otherwise we would never
1135 * perform the update e.g. for the root filesystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 */
Christoph Hellwig7884bc82009-01-19 02:04:07 +01001137 if (mp->m_update_flags && !(mp->m_flags & XFS_MOUNT_RDONLY)) {
1138 error = xfs_mount_log_sb(mp, mp->m_update_flags);
David Chinnere5720ee2008-04-10 12:21:18 +10001139 if (error) {
1140 cmn_err(CE_WARN, "XFS: failed to write sb changes");
Christoph Hellwigb93b6e42009-02-04 09:33:58 +01001141 goto out_rtunmount;
David Chinnere5720ee2008-04-10 12:21:18 +10001142 }
1143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 /*
1146 * Initialise the XFS quota management subsystem for this mount
1147 */
Eric Sandeen0771fb42007-10-12 11:03:40 +10001148 error = XFS_QM_INIT(mp, &quotamount, &quotaflags);
1149 if (error)
Christoph Hellwigb93b6e42009-02-04 09:33:58 +01001150 goto out_rtunmount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 /*
1153 * Finish recovering the file system. This part needed to be
1154 * delayed until after the root and real-time bitmap inodes
1155 * were consistently read in.
1156 */
Christoph Hellwig42490232008-08-13 16:49:32 +10001157 error = xfs_log_mount_finish(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 if (error) {
1159 cmn_err(CE_WARN, "XFS: log mount finish failed");
Christoph Hellwigb93b6e42009-02-04 09:33:58 +01001160 goto out_rtunmount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
1162
1163 /*
1164 * Complete the quota initialisation, post-log-replay component.
1165 */
Christoph Hellwig42490232008-08-13 16:49:32 +10001166 error = XFS_QM_MOUNT(mp, quotamount, quotaflags);
Eric Sandeen0771fb42007-10-12 11:03:40 +10001167 if (error)
Christoph Hellwigb93b6e42009-02-04 09:33:58 +01001168 goto out_rtunmount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
David Chinner84e1e992007-06-18 16:50:27 +10001170 /*
1171 * Now we are mounted, reserve a small amount of unused space for
1172 * privileged transactions. This is needed so that transaction
1173 * space required for critical operations can dip into this pool
1174 * when at ENOSPC. This is needed for operations like create with
1175 * attr, unwritten extent conversion at ENOSPC, etc. Data allocations
1176 * are not allowed to use this reserved space.
1177 *
1178 * We default to 5% or 1024 fsbs of space reserved, whichever is smaller.
1179 * This may drive us straight to ENOSPC on mount, but that implies
David Chinner714082b2008-04-10 12:20:03 +10001180 * we were already there on the last unmount. Warn if this occurs.
David Chinner84e1e992007-06-18 16:50:27 +10001181 */
Christoph Hellwig39726be2007-06-18 17:57:45 +10001182 resblks = mp->m_sb.sb_dblocks;
1183 do_div(resblks, 20);
1184 resblks = min_t(__uint64_t, resblks, 1024);
David Chinner714082b2008-04-10 12:20:03 +10001185 error = xfs_reserve_blocks(mp, &resblks, NULL);
1186 if (error)
1187 cmn_err(CE_WARN, "XFS: Unable to allocate reserve blocks. "
1188 "Continuing without a reserve pool.");
David Chinner84e1e992007-06-18 16:50:27 +10001189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return 0;
1191
Christoph Hellwigb93b6e42009-02-04 09:33:58 +01001192 out_rtunmount:
1193 xfs_rtunmount_inodes(mp);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001194 out_rele_rip:
Christoph Hellwig43355092008-03-27 18:01:08 +11001195 IRELE(rip);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001196 out_log_dealloc:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 xfs_log_unmount_dealloc(mp);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001198 out_free_perag:
Christoph Hellwigff4f0382008-08-13 16:50:47 +10001199 xfs_free_perag(mp);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001200 out_remove_uuid:
1201 if (!(mp->m_flags & XFS_MOUNT_NOUUID))
Christoph Hellwigfa6adbe2008-05-20 11:30:33 +10001202 uuid_table_remove(&mp->m_sb.sb_uuid);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001203 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 return error;
1205}
1206
1207/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 * This flushes out the inodes,dquots and the superblock, unmounts the
1209 * log and makes sure that incore structures are freed.
1210 */
Christoph Hellwig41b5c2e2008-08-13 16:49:57 +10001211void
1212xfs_unmountfs(
1213 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Christoph Hellwig41b5c2e2008-08-13 16:49:57 +10001215 __uint64_t resblks;
1216 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Christoph Hellwigf95099b2008-12-03 12:20:37 +01001218 /*
1219 * Release dquot that rootinode, rbmino and rsumino might be holding,
1220 * and release the quota inodes.
1221 */
1222 XFS_QM_UNMOUNT(mp);
1223
Christoph Hellwigb93b6e42009-02-04 09:33:58 +01001224 xfs_rtunmount_inodes(mp);
Christoph Hellwig77508ec2008-08-13 16:49:04 +10001225 IRELE(mp->m_rootip);
1226
David Chinner641c56f2007-06-18 16:50:17 +10001227 /*
1228 * We can potentially deadlock here if we have an inode cluster
1229 * that has been freed has it's buffer still pinned in memory because
1230 * the transaction is still sitting in a iclog. The stale inodes
1231 * on that buffer will have their flush locks held until the
1232 * transaction hits the disk and the callbacks run. the inode
1233 * flush takes the flush lock unconditionally and with nothing to
1234 * push out the iclog we will never get that unlocked. hence we
1235 * need to force the log first.
1236 */
1237 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
David Chinner1dc33182008-10-30 17:37:15 +11001238 xfs_reclaim_inodes(mp, 0, XFS_IFLUSH_ASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Nathan Scottee2a4f72006-01-11 15:33:36 +11001240 XFS_QM_DQPURGEALL(mp, XFS_QMOPT_QUOTALL | XFS_QMOPT_UMOUNTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Lachlan McIlroya357a122008-10-30 16:53:25 +11001242 if (mp->m_quotainfo)
1243 XFS_QM_DONE(mp);
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 /*
1246 * Flush out the log synchronously so that we know for sure
1247 * that nothing is pinned. This is important because bflush()
1248 * will skip pinned buffers.
1249 */
1250 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1251
1252 xfs_binval(mp->m_ddev_targp);
1253 if (mp->m_rtdev_targp) {
1254 xfs_binval(mp->m_rtdev_targp);
1255 }
1256
David Chinner84e1e992007-06-18 16:50:27 +10001257 /*
1258 * Unreserve any blocks we have so that when we unmount we don't account
1259 * the reserved free space as used. This is really only necessary for
1260 * lazy superblock counting because it trusts the incore superblock
1261 * counters to be aboslutely correct on clean unmount.
1262 *
1263 * We don't bother correcting this elsewhere for lazy superblock
1264 * counting because on mount of an unclean filesystem we reconstruct the
1265 * correct counter value and this is irrelevant.
1266 *
1267 * For non-lazy counter filesystems, this doesn't matter at all because
1268 * we only every apply deltas to the superblock and hence the incore
1269 * value does not matter....
1270 */
1271 resblks = 0;
David Chinner714082b2008-04-10 12:20:03 +10001272 error = xfs_reserve_blocks(mp, &resblks, NULL);
1273 if (error)
1274 cmn_err(CE_WARN, "XFS: Unable to free reserved block pool. "
1275 "Freespace may not be correct on next mount.");
1276
David Chinnere5720ee2008-04-10 12:21:18 +10001277 error = xfs_log_sbcount(mp, 1);
1278 if (error)
1279 cmn_err(CE_WARN, "XFS: Unable to update superblock counters. "
1280 "Freespace may not be correct on next mount.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 xfs_unmountfs_writesb(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 xfs_unmountfs_wait(mp); /* wait for async bufs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 xfs_log_unmount(mp); /* Done! No more fs ops. */
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 if ((mp->m_flags & XFS_MOUNT_NOUUID) == 0)
Christoph Hellwigfa6adbe2008-05-20 11:30:33 +10001286 uuid_table_remove(&mp->m_sb.sb_uuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Christoph Hellwig1550d0b2008-08-13 16:17:37 +10001288#if defined(DEBUG)
Christoph Hellwig0ce4cfd2007-08-30 17:20:53 +10001289 xfs_errortag_clearall(mp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290#endif
Christoph Hellwigff4f0382008-08-13 16:50:47 +10001291 xfs_free_perag(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001294STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295xfs_unmountfs_wait(xfs_mount_t *mp)
1296{
1297 if (mp->m_logdev_targp != mp->m_ddev_targp)
1298 xfs_wait_buftarg(mp->m_logdev_targp);
1299 if (mp->m_rtdev_targp)
1300 xfs_wait_buftarg(mp->m_rtdev_targp);
1301 xfs_wait_buftarg(mp->m_ddev_targp);
1302}
1303
1304int
David Chinner92821e22007-05-24 15:26:31 +10001305xfs_fs_writable(xfs_mount_t *mp)
1306{
Christoph Hellwigb267ce92007-08-30 17:21:30 +10001307 return !(xfs_test_for_freeze(mp) || XFS_FORCED_SHUTDOWN(mp) ||
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001308 (mp->m_flags & XFS_MOUNT_RDONLY));
David Chinner92821e22007-05-24 15:26:31 +10001309}
1310
1311/*
1312 * xfs_log_sbcount
1313 *
1314 * Called either periodically to keep the on disk superblock values
1315 * roughly up to date or from unmount to make sure the values are
1316 * correct on a clean unmount.
1317 *
1318 * Note this code can be called during the process of freezing, so
1319 * we may need to use the transaction allocator which does not not
1320 * block when the transaction subsystem is in its frozen state.
1321 */
1322int
1323xfs_log_sbcount(
1324 xfs_mount_t *mp,
1325 uint sync)
1326{
1327 xfs_trans_t *tp;
1328 int error;
1329
1330 if (!xfs_fs_writable(mp))
1331 return 0;
1332
Christoph Hellwigd4d90b52008-04-22 17:34:37 +10001333 xfs_icsb_sync_counters(mp, 0);
David Chinner92821e22007-05-24 15:26:31 +10001334
1335 /*
1336 * we don't need to do this if we are updating the superblock
1337 * counters on every modification.
1338 */
1339 if (!xfs_sb_version_haslazysbcount(&mp->m_sb))
1340 return 0;
1341
1342 tp = _xfs_trans_alloc(mp, XFS_TRANS_SB_COUNT);
1343 error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
1344 XFS_DEFAULT_LOG_COUNT);
1345 if (error) {
1346 xfs_trans_cancel(tp, 0);
1347 return error;
1348 }
1349
1350 xfs_mod_sb(tp, XFS_SB_IFREE | XFS_SB_ICOUNT | XFS_SB_FDBLOCKS);
1351 if (sync)
1352 xfs_trans_set_sync(tp);
David Chinnere5720ee2008-04-10 12:21:18 +10001353 error = xfs_trans_commit(tp, 0);
1354 return error;
David Chinner92821e22007-05-24 15:26:31 +10001355}
1356
1357int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358xfs_unmountfs_writesb(xfs_mount_t *mp)
1359{
1360 xfs_buf_t *sbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 int error = 0;
1362
1363 /*
1364 * skip superblock write if fs is read-only, or
1365 * if we are doing a forced umount.
1366 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001367 if (!((mp->m_flags & XFS_MOUNT_RDONLY) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 XFS_FORCED_SHUTDOWN(mp))) {
David Chinner8d280b92006-03-14 13:13:09 +11001369
David Chinner92821e22007-05-24 15:26:31 +10001370 sbp = xfs_getsb(mp, 0);
David Chinner8d280b92006-03-14 13:13:09 +11001371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 XFS_BUF_UNDONE(sbp);
1373 XFS_BUF_UNREAD(sbp);
1374 XFS_BUF_UNDELAYWRITE(sbp);
1375 XFS_BUF_WRITE(sbp);
1376 XFS_BUF_UNASYNC(sbp);
1377 ASSERT(XFS_BUF_TARGET(sbp) == mp->m_ddev_targp);
1378 xfsbdstrat(mp, sbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 error = xfs_iowait(sbp);
1380 if (error)
1381 xfs_ioerror_alert("xfs_unmountfs_writesb",
1382 mp, sbp, XFS_BUF_ADDR(sbp));
David Chinner92821e22007-05-24 15:26:31 +10001383 xfs_buf_relse(sbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001385 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
1388/*
1389 * xfs_mod_sb() can be used to copy arbitrary changes to the
1390 * in-core superblock into the superblock buffer to be logged.
1391 * It does not provide the higher level of locking that is
1392 * needed to protect the in-core superblock from concurrent
1393 * access.
1394 */
1395void
1396xfs_mod_sb(xfs_trans_t *tp, __int64_t fields)
1397{
1398 xfs_buf_t *bp;
1399 int first;
1400 int last;
1401 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 xfs_sb_field_t f;
1403
1404 ASSERT(fields);
1405 if (!fields)
1406 return;
1407 mp = tp->t_mountp;
1408 bp = xfs_trans_getsb(tp, mp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 first = sizeof(xfs_sb_t);
1410 last = 0;
1411
1412 /* translate/copy */
1413
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +10001414 xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb, fields);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 /* find modified range */
1417
1418 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
1419 ASSERT((1LL << f) & XFS_SB_MOD_BITS);
1420 first = xfs_sb_info[f].offset;
1421
1422 f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields);
1423 ASSERT((1LL << f) & XFS_SB_MOD_BITS);
1424 last = xfs_sb_info[f + 1].offset - 1;
1425
1426 xfs_trans_log_buf(tp, bp, first, last);
1427}
Yingping Lud210a282006-06-09 14:55:18 +10001428
Yingping Lud210a282006-06-09 14:55:18 +10001429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430/*
1431 * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply
1432 * a delta to a specified field in the in-core superblock. Simply
1433 * switch on the field indicated and apply the delta to that field.
1434 * Fields are not allowed to dip below zero, so if the delta would
1435 * do this do not apply it and return EINVAL.
1436 *
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001437 * The m_sb_lock must be held when this routine is called.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 */
David Chinner8d280b92006-03-14 13:13:09 +11001439int
David Chinner20f4ebf2007-02-10 18:36:10 +11001440xfs_mod_incore_sb_unlocked(
1441 xfs_mount_t *mp,
1442 xfs_sb_field_t field,
1443 int64_t delta,
1444 int rsvd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
1446 int scounter; /* short counter for 32 bit fields */
1447 long long lcounter; /* long counter for 64 bit fields */
1448 long long res_used, rem;
1449
1450 /*
1451 * With the in-core superblock spin lock held, switch
1452 * on the indicated field. Apply the delta to the
1453 * proper field. If the fields value would dip below
1454 * 0, then do not apply the delta and return EINVAL.
1455 */
1456 switch (field) {
1457 case XFS_SBS_ICOUNT:
1458 lcounter = (long long)mp->m_sb.sb_icount;
1459 lcounter += delta;
1460 if (lcounter < 0) {
1461 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001462 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
1464 mp->m_sb.sb_icount = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001465 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 case XFS_SBS_IFREE:
1467 lcounter = (long long)mp->m_sb.sb_ifree;
1468 lcounter += delta;
1469 if (lcounter < 0) {
1470 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001471 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 }
1473 mp->m_sb.sb_ifree = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001474 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 case XFS_SBS_FDBLOCKS:
David Chinner4be536d2006-09-07 14:26:50 +10001476 lcounter = (long long)
1477 mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
1479
1480 if (delta > 0) { /* Putting blocks back */
1481 if (res_used > delta) {
1482 mp->m_resblks_avail += delta;
1483 } else {
1484 rem = delta - res_used;
1485 mp->m_resblks_avail = mp->m_resblks;
1486 lcounter += rem;
1487 }
1488 } else { /* Taking blocks away */
1489
1490 lcounter += delta;
1491
1492 /*
1493 * If were out of blocks, use any available reserved blocks if
1494 * were allowed to.
1495 */
1496
1497 if (lcounter < 0) {
1498 if (rsvd) {
1499 lcounter = (long long)mp->m_resblks_avail + delta;
1500 if (lcounter < 0) {
Jesper Juhl014c2542006-01-15 02:37:08 +01001501 return XFS_ERROR(ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
1503 mp->m_resblks_avail = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001504 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 } else { /* not reserved */
Jesper Juhl014c2542006-01-15 02:37:08 +01001506 return XFS_ERROR(ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 }
1508 }
1509 }
1510
David Chinner4be536d2006-09-07 14:26:50 +10001511 mp->m_sb.sb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
Jesper Juhl014c2542006-01-15 02:37:08 +01001512 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 case XFS_SBS_FREXTENTS:
1514 lcounter = (long long)mp->m_sb.sb_frextents;
1515 lcounter += delta;
1516 if (lcounter < 0) {
Jesper Juhl014c2542006-01-15 02:37:08 +01001517 return XFS_ERROR(ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 }
1519 mp->m_sb.sb_frextents = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001520 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 case XFS_SBS_DBLOCKS:
1522 lcounter = (long long)mp->m_sb.sb_dblocks;
1523 lcounter += delta;
1524 if (lcounter < 0) {
1525 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001526 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
1528 mp->m_sb.sb_dblocks = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001529 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 case XFS_SBS_AGCOUNT:
1531 scounter = mp->m_sb.sb_agcount;
1532 scounter += delta;
1533 if (scounter < 0) {
1534 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001535 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 }
1537 mp->m_sb.sb_agcount = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001538 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 case XFS_SBS_IMAX_PCT:
1540 scounter = mp->m_sb.sb_imax_pct;
1541 scounter += delta;
1542 if (scounter < 0) {
1543 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001544 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 }
1546 mp->m_sb.sb_imax_pct = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001547 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 case XFS_SBS_REXTSIZE:
1549 scounter = mp->m_sb.sb_rextsize;
1550 scounter += delta;
1551 if (scounter < 0) {
1552 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001553 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 }
1555 mp->m_sb.sb_rextsize = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001556 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 case XFS_SBS_RBMBLOCKS:
1558 scounter = mp->m_sb.sb_rbmblocks;
1559 scounter += delta;
1560 if (scounter < 0) {
1561 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001562 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
1564 mp->m_sb.sb_rbmblocks = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001565 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 case XFS_SBS_RBLOCKS:
1567 lcounter = (long long)mp->m_sb.sb_rblocks;
1568 lcounter += delta;
1569 if (lcounter < 0) {
1570 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001571 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 }
1573 mp->m_sb.sb_rblocks = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001574 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 case XFS_SBS_REXTENTS:
1576 lcounter = (long long)mp->m_sb.sb_rextents;
1577 lcounter += delta;
1578 if (lcounter < 0) {
1579 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001580 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 }
1582 mp->m_sb.sb_rextents = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001583 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 case XFS_SBS_REXTSLOG:
1585 scounter = mp->m_sb.sb_rextslog;
1586 scounter += delta;
1587 if (scounter < 0) {
1588 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001589 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
1591 mp->m_sb.sb_rextslog = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001592 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 default:
1594 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001595 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 }
1597}
1598
1599/*
1600 * xfs_mod_incore_sb() is used to change a field in the in-core
1601 * superblock structure by the specified delta. This modification
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001602 * is protected by the m_sb_lock. Just use the xfs_mod_incore_sb_unlocked()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 * routine to do the work.
1604 */
1605int
David Chinner20f4ebf2007-02-10 18:36:10 +11001606xfs_mod_incore_sb(
1607 xfs_mount_t *mp,
1608 xfs_sb_field_t field,
1609 int64_t delta,
1610 int rsvd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 int status;
1613
David Chinner8d280b92006-03-14 13:13:09 +11001614 /* check for per-cpu counters */
1615 switch (field) {
1616#ifdef HAVE_PERCPU_SB
1617 case XFS_SBS_ICOUNT:
1618 case XFS_SBS_IFREE:
1619 case XFS_SBS_FDBLOCKS:
1620 if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
1621 status = xfs_icsb_modify_counters(mp, field,
1622 delta, rsvd);
1623 break;
1624 }
1625 /* FALLTHROUGH */
1626#endif
1627 default:
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001628 spin_lock(&mp->m_sb_lock);
David Chinner8d280b92006-03-14 13:13:09 +11001629 status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001630 spin_unlock(&mp->m_sb_lock);
David Chinner8d280b92006-03-14 13:13:09 +11001631 break;
1632 }
1633
Jesper Juhl014c2542006-01-15 02:37:08 +01001634 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635}
1636
1637/*
1638 * xfs_mod_incore_sb_batch() is used to change more than one field
1639 * in the in-core superblock structure at a time. This modification
1640 * is protected by a lock internal to this module. The fields and
1641 * changes to those fields are specified in the array of xfs_mod_sb
1642 * structures passed in.
1643 *
1644 * Either all of the specified deltas will be applied or none of
1645 * them will. If any modified field dips below 0, then all modifications
1646 * will be backed out and EINVAL will be returned.
1647 */
1648int
1649xfs_mod_incore_sb_batch(xfs_mount_t *mp, xfs_mod_sb_t *msb, uint nmsb, int rsvd)
1650{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 int status=0;
1652 xfs_mod_sb_t *msbp;
1653
1654 /*
1655 * Loop through the array of mod structures and apply each
1656 * individually. If any fail, then back out all those
1657 * which have already been applied. Do all of this within
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001658 * the scope of the m_sb_lock so that all of the changes will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 * be atomic.
1660 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001661 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 msbp = &msb[0];
1663 for (msbp = &msbp[0]; msbp < (msb + nmsb); msbp++) {
1664 /*
1665 * Apply the delta at index n. If it fails, break
1666 * from the loop so we'll fall into the undo loop
1667 * below.
1668 */
David Chinner8d280b92006-03-14 13:13:09 +11001669 switch (msbp->msb_field) {
1670#ifdef HAVE_PERCPU_SB
1671 case XFS_SBS_ICOUNT:
1672 case XFS_SBS_IFREE:
1673 case XFS_SBS_FDBLOCKS:
1674 if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001675 spin_unlock(&mp->m_sb_lock);
David Chinner20b64282007-02-10 18:35:09 +11001676 status = xfs_icsb_modify_counters(mp,
David Chinner8d280b92006-03-14 13:13:09 +11001677 msbp->msb_field,
1678 msbp->msb_delta, rsvd);
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001679 spin_lock(&mp->m_sb_lock);
David Chinner8d280b92006-03-14 13:13:09 +11001680 break;
1681 }
1682 /* FALLTHROUGH */
1683#endif
1684 default:
1685 status = xfs_mod_incore_sb_unlocked(mp,
1686 msbp->msb_field,
1687 msbp->msb_delta, rsvd);
1688 break;
1689 }
1690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 if (status != 0) {
1692 break;
1693 }
1694 }
1695
1696 /*
1697 * If we didn't complete the loop above, then back out
1698 * any changes made to the superblock. If you add code
1699 * between the loop above and here, make sure that you
1700 * preserve the value of status. Loop back until
1701 * we step below the beginning of the array. Make sure
1702 * we don't touch anything back there.
1703 */
1704 if (status != 0) {
1705 msbp--;
1706 while (msbp >= msb) {
David Chinner8d280b92006-03-14 13:13:09 +11001707 switch (msbp->msb_field) {
1708#ifdef HAVE_PERCPU_SB
1709 case XFS_SBS_ICOUNT:
1710 case XFS_SBS_IFREE:
1711 case XFS_SBS_FDBLOCKS:
1712 if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001713 spin_unlock(&mp->m_sb_lock);
David Chinner20b64282007-02-10 18:35:09 +11001714 status = xfs_icsb_modify_counters(mp,
David Chinner8d280b92006-03-14 13:13:09 +11001715 msbp->msb_field,
1716 -(msbp->msb_delta),
1717 rsvd);
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001718 spin_lock(&mp->m_sb_lock);
David Chinner8d280b92006-03-14 13:13:09 +11001719 break;
1720 }
1721 /* FALLTHROUGH */
1722#endif
1723 default:
1724 status = xfs_mod_incore_sb_unlocked(mp,
1725 msbp->msb_field,
1726 -(msbp->msb_delta),
1727 rsvd);
1728 break;
1729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 ASSERT(status == 0);
1731 msbp--;
1732 }
1733 }
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001734 spin_unlock(&mp->m_sb_lock);
Jesper Juhl014c2542006-01-15 02:37:08 +01001735 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736}
1737
1738/*
1739 * xfs_getsb() is called to obtain the buffer for the superblock.
1740 * The buffer is returned locked and read in from disk.
1741 * The buffer should be released with a call to xfs_brelse().
1742 *
1743 * If the flags parameter is BUF_TRYLOCK, then we'll only return
1744 * the superblock buffer if it can be locked without sleeping.
1745 * If it can't then we'll return NULL.
1746 */
1747xfs_buf_t *
1748xfs_getsb(
1749 xfs_mount_t *mp,
1750 int flags)
1751{
1752 xfs_buf_t *bp;
1753
1754 ASSERT(mp->m_sb_bp != NULL);
1755 bp = mp->m_sb_bp;
1756 if (flags & XFS_BUF_TRYLOCK) {
1757 if (!XFS_BUF_CPSEMA(bp)) {
1758 return NULL;
1759 }
1760 } else {
1761 XFS_BUF_PSEMA(bp, PRIBIO);
1762 }
1763 XFS_BUF_HOLD(bp);
1764 ASSERT(XFS_BUF_ISDONE(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001765 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766}
1767
1768/*
1769 * Used to free the superblock along various error paths.
1770 */
1771void
1772xfs_freesb(
1773 xfs_mount_t *mp)
1774{
1775 xfs_buf_t *bp;
1776
1777 /*
1778 * Use xfs_getsb() so that the buffer will be locked
1779 * when we call xfs_buf_relse().
1780 */
1781 bp = xfs_getsb(mp, 0);
1782 XFS_BUF_UNMANAGE(bp);
1783 xfs_buf_relse(bp);
1784 mp->m_sb_bp = NULL;
1785}
1786
1787/*
1788 * See if the UUID is unique among mounted XFS filesystems.
1789 * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
1790 */
1791STATIC int
1792xfs_uuid_mount(
1793 xfs_mount_t *mp)
1794{
1795 if (uuid_is_nil(&mp->m_sb.sb_uuid)) {
1796 cmn_err(CE_WARN,
1797 "XFS: Filesystem %s has nil UUID - can't mount",
1798 mp->m_fsname);
1799 return -1;
1800 }
1801 if (!uuid_table_insert(&mp->m_sb.sb_uuid)) {
1802 cmn_err(CE_WARN,
1803 "XFS: Filesystem %s has duplicate UUID - can't mount",
1804 mp->m_fsname);
1805 return -1;
1806 }
1807 return 0;
1808}
1809
1810/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 * Used to log changes to the superblock unit and width fields which could
Eric Sandeene6957ea2008-04-10 12:19:34 +10001812 * be altered by the mount options, as well as any potential sb_features2
1813 * fixup. Only the first superblock is updated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 */
Christoph Hellwig7884bc82009-01-19 02:04:07 +01001815int
David Chinneree1c0902008-03-06 13:45:50 +11001816xfs_mount_log_sb(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 xfs_mount_t *mp,
1818 __int64_t fields)
1819{
1820 xfs_trans_t *tp;
David Chinnere5720ee2008-04-10 12:21:18 +10001821 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
David Chinneree1c0902008-03-06 13:45:50 +11001823 ASSERT(fields & (XFS_SB_UNIT | XFS_SB_WIDTH | XFS_SB_UUID |
David Chinner4b166de2008-05-20 11:30:27 +10001824 XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2 |
1825 XFS_SB_VERSIONNUM));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827 tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT);
David Chinnere5720ee2008-04-10 12:21:18 +10001828 error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
1829 XFS_DEFAULT_LOG_COUNT);
1830 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 xfs_trans_cancel(tp, 0);
David Chinnere5720ee2008-04-10 12:21:18 +10001832 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 }
1834 xfs_mod_sb(tp, fields);
David Chinnere5720ee2008-04-10 12:21:18 +10001835 error = xfs_trans_commit(tp, 0);
1836 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837}
David Chinner8d280b92006-03-14 13:13:09 +11001838
1839
1840#ifdef HAVE_PERCPU_SB
1841/*
1842 * Per-cpu incore superblock counters
1843 *
1844 * Simple concept, difficult implementation
1845 *
1846 * Basically, replace the incore superblock counters with a distributed per cpu
1847 * counter for contended fields (e.g. free block count).
1848 *
1849 * Difficulties arise in that the incore sb is used for ENOSPC checking, and
1850 * hence needs to be accurately read when we are running low on space. Hence
1851 * there is a method to enable and disable the per-cpu counters based on how
1852 * much "stuff" is available in them.
1853 *
1854 * Basically, a counter is enabled if there is enough free resource to justify
1855 * running a per-cpu fast-path. If the per-cpu counter runs out (i.e. a local
1856 * ENOSPC), then we disable the counters to synchronise all callers and
1857 * re-distribute the available resources.
1858 *
1859 * If, once we redistributed the available resources, we still get a failure,
1860 * we disable the per-cpu counter and go through the slow path.
1861 *
1862 * The slow path is the current xfs_mod_incore_sb() function. This means that
1863 * when we disable a per-cpu counter, we need to drain it's resources back to
1864 * the global superblock. We do this after disabling the counter to prevent
1865 * more threads from queueing up on the counter.
1866 *
1867 * Essentially, this means that we still need a lock in the fast path to enable
1868 * synchronisation between the global counters and the per-cpu counters. This
1869 * is not a problem because the lock will be local to a CPU almost all the time
1870 * and have little contention except when we get to ENOSPC conditions.
1871 *
1872 * Basically, this lock becomes a barrier that enables us to lock out the fast
1873 * path while we do things like enabling and disabling counters and
1874 * synchronising the counters.
1875 *
1876 * Locking rules:
1877 *
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001878 * 1. m_sb_lock before picking up per-cpu locks
David Chinner8d280b92006-03-14 13:13:09 +11001879 * 2. per-cpu locks always picked up via for_each_online_cpu() order
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001880 * 3. accurate counter sync requires m_sb_lock + per cpu locks
David Chinner8d280b92006-03-14 13:13:09 +11001881 * 4. modifying per-cpu counters requires holding per-cpu lock
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001882 * 5. modifying global counters requires holding m_sb_lock
1883 * 6. enabling or disabling a counter requires holding the m_sb_lock
David Chinner8d280b92006-03-14 13:13:09 +11001884 * and _none_ of the per-cpu locks.
1885 *
1886 * Disabled counters are only ever re-enabled by a balance operation
1887 * that results in more free resources per CPU than a given threshold.
1888 * To ensure counters don't remain disabled, they are rebalanced when
1889 * the global resource goes above a higher threshold (i.e. some hysteresis
1890 * is present to prevent thrashing).
David Chinner8d280b92006-03-14 13:13:09 +11001891 */
David Chinnere8234a62006-03-14 13:23:52 +11001892
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07001893#ifdef CONFIG_HOTPLUG_CPU
David Chinnere8234a62006-03-14 13:23:52 +11001894/*
1895 * hot-plug CPU notifier support.
1896 *
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07001897 * We need a notifier per filesystem as we need to be able to identify
1898 * the filesystem to balance the counters out. This is achieved by
1899 * having a notifier block embedded in the xfs_mount_t and doing pointer
1900 * magic to get the mount pointer from the notifier block address.
David Chinnere8234a62006-03-14 13:23:52 +11001901 */
1902STATIC int
1903xfs_icsb_cpu_notify(
1904 struct notifier_block *nfb,
1905 unsigned long action,
1906 void *hcpu)
1907{
1908 xfs_icsb_cnts_t *cntp;
1909 xfs_mount_t *mp;
David Chinnere8234a62006-03-14 13:23:52 +11001910
1911 mp = (xfs_mount_t *)container_of(nfb, xfs_mount_t, m_icsb_notifier);
1912 cntp = (xfs_icsb_cnts_t *)
1913 per_cpu_ptr(mp->m_sb_cnts, (unsigned long)hcpu);
1914 switch (action) {
1915 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001916 case CPU_UP_PREPARE_FROZEN:
David Chinnere8234a62006-03-14 13:23:52 +11001917 /* Easy Case - initialize the area and locks, and
1918 * then rebalance when online does everything else for us. */
David Chinner01e1b692006-03-14 13:29:16 +11001919 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
David Chinnere8234a62006-03-14 13:23:52 +11001920 break;
1921 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001922 case CPU_ONLINE_FROZEN:
David Chinner03135cf2007-02-10 18:35:15 +11001923 xfs_icsb_lock(mp);
Christoph Hellwig45af6c62008-04-22 17:34:44 +10001924 xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
1925 xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
1926 xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
David Chinner03135cf2007-02-10 18:35:15 +11001927 xfs_icsb_unlock(mp);
David Chinnere8234a62006-03-14 13:23:52 +11001928 break;
1929 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001930 case CPU_DEAD_FROZEN:
David Chinnere8234a62006-03-14 13:23:52 +11001931 /* Disable all the counters, then fold the dead cpu's
1932 * count into the total on the global superblock and
1933 * re-enable the counters. */
David Chinner03135cf2007-02-10 18:35:15 +11001934 xfs_icsb_lock(mp);
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001935 spin_lock(&mp->m_sb_lock);
David Chinnere8234a62006-03-14 13:23:52 +11001936 xfs_icsb_disable_counter(mp, XFS_SBS_ICOUNT);
1937 xfs_icsb_disable_counter(mp, XFS_SBS_IFREE);
1938 xfs_icsb_disable_counter(mp, XFS_SBS_FDBLOCKS);
1939
1940 mp->m_sb.sb_icount += cntp->icsb_icount;
1941 mp->m_sb.sb_ifree += cntp->icsb_ifree;
1942 mp->m_sb.sb_fdblocks += cntp->icsb_fdblocks;
1943
David Chinner01e1b692006-03-14 13:29:16 +11001944 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
David Chinnere8234a62006-03-14 13:23:52 +11001945
Christoph Hellwig45af6c62008-04-22 17:34:44 +10001946 xfs_icsb_balance_counter_locked(mp, XFS_SBS_ICOUNT, 0);
1947 xfs_icsb_balance_counter_locked(mp, XFS_SBS_IFREE, 0);
1948 xfs_icsb_balance_counter_locked(mp, XFS_SBS_FDBLOCKS, 0);
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001949 spin_unlock(&mp->m_sb_lock);
David Chinner03135cf2007-02-10 18:35:15 +11001950 xfs_icsb_unlock(mp);
David Chinnere8234a62006-03-14 13:23:52 +11001951 break;
1952 }
1953
1954 return NOTIFY_OK;
1955}
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07001956#endif /* CONFIG_HOTPLUG_CPU */
David Chinnere8234a62006-03-14 13:23:52 +11001957
David Chinner8d280b92006-03-14 13:13:09 +11001958int
1959xfs_icsb_init_counters(
1960 xfs_mount_t *mp)
1961{
1962 xfs_icsb_cnts_t *cntp;
1963 int i;
1964
1965 mp->m_sb_cnts = alloc_percpu(xfs_icsb_cnts_t);
1966 if (mp->m_sb_cnts == NULL)
1967 return -ENOMEM;
1968
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07001969#ifdef CONFIG_HOTPLUG_CPU
David Chinnere8234a62006-03-14 13:23:52 +11001970 mp->m_icsb_notifier.notifier_call = xfs_icsb_cpu_notify;
1971 mp->m_icsb_notifier.priority = 0;
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07001972 register_hotcpu_notifier(&mp->m_icsb_notifier);
1973#endif /* CONFIG_HOTPLUG_CPU */
David Chinnere8234a62006-03-14 13:23:52 +11001974
David Chinner8d280b92006-03-14 13:13:09 +11001975 for_each_online_cpu(i) {
1976 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
David Chinner01e1b692006-03-14 13:29:16 +11001977 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
David Chinner8d280b92006-03-14 13:13:09 +11001978 }
David Chinner20b64282007-02-10 18:35:09 +11001979
1980 mutex_init(&mp->m_icsb_mutex);
1981
David Chinner8d280b92006-03-14 13:13:09 +11001982 /*
1983 * start with all counters disabled so that the
1984 * initial balance kicks us off correctly
1985 */
1986 mp->m_icsb_counters = -1;
1987 return 0;
1988}
1989
Lachlan McIlroy5478eea2007-02-10 18:36:29 +11001990void
1991xfs_icsb_reinit_counters(
1992 xfs_mount_t *mp)
1993{
1994 xfs_icsb_lock(mp);
1995 /*
1996 * start with all counters disabled so that the
1997 * initial balance kicks us off correctly
1998 */
1999 mp->m_icsb_counters = -1;
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002000 xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
2001 xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
2002 xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
Lachlan McIlroy5478eea2007-02-10 18:36:29 +11002003 xfs_icsb_unlock(mp);
2004}
2005
Christoph Hellwigc962fb72008-05-20 15:10:52 +10002006void
David Chinner8d280b92006-03-14 13:13:09 +11002007xfs_icsb_destroy_counters(
2008 xfs_mount_t *mp)
2009{
David Chinnere8234a62006-03-14 13:23:52 +11002010 if (mp->m_sb_cnts) {
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07002011 unregister_hotcpu_notifier(&mp->m_icsb_notifier);
David Chinner8d280b92006-03-14 13:13:09 +11002012 free_percpu(mp->m_sb_cnts);
David Chinnere8234a62006-03-14 13:23:52 +11002013 }
David Chinner03135cf2007-02-10 18:35:15 +11002014 mutex_destroy(&mp->m_icsb_mutex);
David Chinner8d280b92006-03-14 13:13:09 +11002015}
2016
David Chinner7989cb82007-02-10 18:34:56 +11002017STATIC_INLINE void
David Chinner01e1b692006-03-14 13:29:16 +11002018xfs_icsb_lock_cntr(
2019 xfs_icsb_cnts_t *icsbp)
2020{
2021 while (test_and_set_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags)) {
2022 ndelay(1000);
2023 }
2024}
2025
David Chinner7989cb82007-02-10 18:34:56 +11002026STATIC_INLINE void
David Chinner01e1b692006-03-14 13:29:16 +11002027xfs_icsb_unlock_cntr(
2028 xfs_icsb_cnts_t *icsbp)
2029{
2030 clear_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags);
2031}
2032
David Chinner8d280b92006-03-14 13:13:09 +11002033
David Chinner7989cb82007-02-10 18:34:56 +11002034STATIC_INLINE void
David Chinner8d280b92006-03-14 13:13:09 +11002035xfs_icsb_lock_all_counters(
2036 xfs_mount_t *mp)
2037{
2038 xfs_icsb_cnts_t *cntp;
2039 int i;
2040
2041 for_each_online_cpu(i) {
2042 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
David Chinner01e1b692006-03-14 13:29:16 +11002043 xfs_icsb_lock_cntr(cntp);
David Chinner8d280b92006-03-14 13:13:09 +11002044 }
2045}
2046
David Chinner7989cb82007-02-10 18:34:56 +11002047STATIC_INLINE void
David Chinner8d280b92006-03-14 13:13:09 +11002048xfs_icsb_unlock_all_counters(
2049 xfs_mount_t *mp)
2050{
2051 xfs_icsb_cnts_t *cntp;
2052 int i;
2053
2054 for_each_online_cpu(i) {
2055 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
David Chinner01e1b692006-03-14 13:29:16 +11002056 xfs_icsb_unlock_cntr(cntp);
David Chinner8d280b92006-03-14 13:13:09 +11002057 }
2058}
2059
2060STATIC void
2061xfs_icsb_count(
2062 xfs_mount_t *mp,
2063 xfs_icsb_cnts_t *cnt,
2064 int flags)
2065{
2066 xfs_icsb_cnts_t *cntp;
2067 int i;
2068
2069 memset(cnt, 0, sizeof(xfs_icsb_cnts_t));
2070
2071 if (!(flags & XFS_ICSB_LAZY_COUNT))
2072 xfs_icsb_lock_all_counters(mp);
2073
2074 for_each_online_cpu(i) {
2075 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
2076 cnt->icsb_icount += cntp->icsb_icount;
2077 cnt->icsb_ifree += cntp->icsb_ifree;
2078 cnt->icsb_fdblocks += cntp->icsb_fdblocks;
2079 }
2080
2081 if (!(flags & XFS_ICSB_LAZY_COUNT))
2082 xfs_icsb_unlock_all_counters(mp);
2083}
2084
2085STATIC int
2086xfs_icsb_counter_disabled(
2087 xfs_mount_t *mp,
2088 xfs_sb_field_t field)
2089{
2090 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
2091 return test_bit(field, &mp->m_icsb_counters);
2092}
2093
David Chinner36fbe6e2008-04-10 12:19:56 +10002094STATIC void
David Chinner8d280b92006-03-14 13:13:09 +11002095xfs_icsb_disable_counter(
2096 xfs_mount_t *mp,
2097 xfs_sb_field_t field)
2098{
2099 xfs_icsb_cnts_t cnt;
2100
2101 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
2102
David Chinner20b64282007-02-10 18:35:09 +11002103 /*
2104 * If we are already disabled, then there is nothing to do
2105 * here. We check before locking all the counters to avoid
2106 * the expensive lock operation when being called in the
2107 * slow path and the counter is already disabled. This is
2108 * safe because the only time we set or clear this state is under
2109 * the m_icsb_mutex.
2110 */
2111 if (xfs_icsb_counter_disabled(mp, field))
David Chinner36fbe6e2008-04-10 12:19:56 +10002112 return;
David Chinner20b64282007-02-10 18:35:09 +11002113
David Chinner8d280b92006-03-14 13:13:09 +11002114 xfs_icsb_lock_all_counters(mp);
2115 if (!test_and_set_bit(field, &mp->m_icsb_counters)) {
2116 /* drain back to superblock */
2117
Christoph Hellwigce461932008-04-22 17:34:50 +10002118 xfs_icsb_count(mp, &cnt, XFS_ICSB_LAZY_COUNT);
David Chinner8d280b92006-03-14 13:13:09 +11002119 switch(field) {
2120 case XFS_SBS_ICOUNT:
2121 mp->m_sb.sb_icount = cnt.icsb_icount;
2122 break;
2123 case XFS_SBS_IFREE:
2124 mp->m_sb.sb_ifree = cnt.icsb_ifree;
2125 break;
2126 case XFS_SBS_FDBLOCKS:
2127 mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
2128 break;
2129 default:
2130 BUG();
2131 }
2132 }
2133
2134 xfs_icsb_unlock_all_counters(mp);
David Chinner8d280b92006-03-14 13:13:09 +11002135}
2136
2137STATIC void
2138xfs_icsb_enable_counter(
2139 xfs_mount_t *mp,
2140 xfs_sb_field_t field,
2141 uint64_t count,
2142 uint64_t resid)
2143{
2144 xfs_icsb_cnts_t *cntp;
2145 int i;
2146
2147 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
2148
2149 xfs_icsb_lock_all_counters(mp);
2150 for_each_online_cpu(i) {
2151 cntp = per_cpu_ptr(mp->m_sb_cnts, i);
2152 switch (field) {
2153 case XFS_SBS_ICOUNT:
2154 cntp->icsb_icount = count + resid;
2155 break;
2156 case XFS_SBS_IFREE:
2157 cntp->icsb_ifree = count + resid;
2158 break;
2159 case XFS_SBS_FDBLOCKS:
2160 cntp->icsb_fdblocks = count + resid;
2161 break;
2162 default:
2163 BUG();
2164 break;
2165 }
2166 resid = 0;
2167 }
2168 clear_bit(field, &mp->m_icsb_counters);
2169 xfs_icsb_unlock_all_counters(mp);
2170}
2171
David Chinnerdbcabad2007-02-10 18:36:17 +11002172void
Christoph Hellwigd4d90b52008-04-22 17:34:37 +10002173xfs_icsb_sync_counters_locked(
David Chinner8d280b92006-03-14 13:13:09 +11002174 xfs_mount_t *mp,
2175 int flags)
2176{
2177 xfs_icsb_cnts_t cnt;
David Chinner8d280b92006-03-14 13:13:09 +11002178
David Chinner8d280b92006-03-14 13:13:09 +11002179 xfs_icsb_count(mp, &cnt, flags);
2180
David Chinner8d280b92006-03-14 13:13:09 +11002181 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_ICOUNT))
2182 mp->m_sb.sb_icount = cnt.icsb_icount;
2183 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_IFREE))
2184 mp->m_sb.sb_ifree = cnt.icsb_ifree;
2185 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_FDBLOCKS))
2186 mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
David Chinner8d280b92006-03-14 13:13:09 +11002187}
2188
2189/*
2190 * Accurate update of per-cpu counters to incore superblock
2191 */
Christoph Hellwigd4d90b52008-04-22 17:34:37 +10002192void
David Chinner8d280b92006-03-14 13:13:09 +11002193xfs_icsb_sync_counters(
Christoph Hellwigd4d90b52008-04-22 17:34:37 +10002194 xfs_mount_t *mp,
2195 int flags)
David Chinner8d280b92006-03-14 13:13:09 +11002196{
Christoph Hellwigd4d90b52008-04-22 17:34:37 +10002197 spin_lock(&mp->m_sb_lock);
2198 xfs_icsb_sync_counters_locked(mp, flags);
2199 spin_unlock(&mp->m_sb_lock);
David Chinner8d280b92006-03-14 13:13:09 +11002200}
2201
2202/*
2203 * Balance and enable/disable counters as necessary.
2204 *
David Chinner20b64282007-02-10 18:35:09 +11002205 * Thresholds for re-enabling counters are somewhat magic. inode counts are
2206 * chosen to be the same number as single on disk allocation chunk per CPU, and
2207 * free blocks is something far enough zero that we aren't going thrash when we
2208 * get near ENOSPC. We also need to supply a minimum we require per cpu to
2209 * prevent looping endlessly when xfs_alloc_space asks for more than will
2210 * be distributed to a single CPU but each CPU has enough blocks to be
2211 * reenabled.
2212 *
2213 * Note that we can be called when counters are already disabled.
2214 * xfs_icsb_disable_counter() optimises the counter locking in this case to
2215 * prevent locking every per-cpu counter needlessly.
David Chinner8d280b92006-03-14 13:13:09 +11002216 */
David Chinner20b64282007-02-10 18:35:09 +11002217
2218#define XFS_ICSB_INO_CNTR_REENABLE (uint64_t)64
David Chinner4be536d2006-09-07 14:26:50 +10002219#define XFS_ICSB_FDBLK_CNTR_REENABLE(mp) \
David Chinner20b64282007-02-10 18:35:09 +11002220 (uint64_t)(512 + XFS_ALLOC_SET_ASIDE(mp))
David Chinner8d280b92006-03-14 13:13:09 +11002221STATIC void
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002222xfs_icsb_balance_counter_locked(
David Chinner8d280b92006-03-14 13:13:09 +11002223 xfs_mount_t *mp,
2224 xfs_sb_field_t field,
David Chinner20b64282007-02-10 18:35:09 +11002225 int min_per_cpu)
David Chinner8d280b92006-03-14 13:13:09 +11002226{
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10002227 uint64_t count, resid;
David Chinner8d280b92006-03-14 13:13:09 +11002228 int weight = num_online_cpus();
David Chinner20b64282007-02-10 18:35:09 +11002229 uint64_t min = (uint64_t)min_per_cpu;
David Chinner8d280b92006-03-14 13:13:09 +11002230
David Chinner8d280b92006-03-14 13:13:09 +11002231 /* disable counter and sync counter */
2232 xfs_icsb_disable_counter(mp, field);
2233
2234 /* update counters - first CPU gets residual*/
2235 switch (field) {
2236 case XFS_SBS_ICOUNT:
2237 count = mp->m_sb.sb_icount;
2238 resid = do_div(count, weight);
David Chinner20b64282007-02-10 18:35:09 +11002239 if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002240 return;
David Chinner8d280b92006-03-14 13:13:09 +11002241 break;
2242 case XFS_SBS_IFREE:
2243 count = mp->m_sb.sb_ifree;
2244 resid = do_div(count, weight);
David Chinner20b64282007-02-10 18:35:09 +11002245 if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002246 return;
David Chinner8d280b92006-03-14 13:13:09 +11002247 break;
2248 case XFS_SBS_FDBLOCKS:
2249 count = mp->m_sb.sb_fdblocks;
2250 resid = do_div(count, weight);
David Chinner20b64282007-02-10 18:35:09 +11002251 if (count < max(min, XFS_ICSB_FDBLK_CNTR_REENABLE(mp)))
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002252 return;
David Chinner8d280b92006-03-14 13:13:09 +11002253 break;
2254 default:
2255 BUG();
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10002256 count = resid = 0; /* quiet, gcc */
David Chinner8d280b92006-03-14 13:13:09 +11002257 break;
2258 }
2259
2260 xfs_icsb_enable_counter(mp, field, count, resid);
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002261}
2262
2263STATIC void
2264xfs_icsb_balance_counter(
2265 xfs_mount_t *mp,
2266 xfs_sb_field_t fields,
2267 int min_per_cpu)
2268{
2269 spin_lock(&mp->m_sb_lock);
2270 xfs_icsb_balance_counter_locked(mp, fields, min_per_cpu);
2271 spin_unlock(&mp->m_sb_lock);
David Chinner8d280b92006-03-14 13:13:09 +11002272}
2273
David Chinnera8272ce2007-11-23 16:28:09 +11002274STATIC int
David Chinner20b64282007-02-10 18:35:09 +11002275xfs_icsb_modify_counters(
David Chinner8d280b92006-03-14 13:13:09 +11002276 xfs_mount_t *mp,
2277 xfs_sb_field_t field,
David Chinner20f4ebf2007-02-10 18:36:10 +11002278 int64_t delta,
David Chinner20b64282007-02-10 18:35:09 +11002279 int rsvd)
David Chinner8d280b92006-03-14 13:13:09 +11002280{
2281 xfs_icsb_cnts_t *icsbp;
2282 long long lcounter; /* long counter for 64 bit fields */
Eric Sandeen007c61c2007-10-11 17:43:56 +10002283 int cpu, ret = 0;
David Chinner8d280b92006-03-14 13:13:09 +11002284
David Chinner20b64282007-02-10 18:35:09 +11002285 might_sleep();
David Chinner8d280b92006-03-14 13:13:09 +11002286again:
2287 cpu = get_cpu();
David Chinner20b64282007-02-10 18:35:09 +11002288 icsbp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, cpu);
2289
2290 /*
2291 * if the counter is disabled, go to slow path
2292 */
David Chinner8d280b92006-03-14 13:13:09 +11002293 if (unlikely(xfs_icsb_counter_disabled(mp, field)))
2294 goto slow_path;
David Chinner20b64282007-02-10 18:35:09 +11002295 xfs_icsb_lock_cntr(icsbp);
2296 if (unlikely(xfs_icsb_counter_disabled(mp, field))) {
2297 xfs_icsb_unlock_cntr(icsbp);
2298 goto slow_path;
2299 }
David Chinner8d280b92006-03-14 13:13:09 +11002300
2301 switch (field) {
2302 case XFS_SBS_ICOUNT:
2303 lcounter = icsbp->icsb_icount;
2304 lcounter += delta;
2305 if (unlikely(lcounter < 0))
David Chinner20b64282007-02-10 18:35:09 +11002306 goto balance_counter;
David Chinner8d280b92006-03-14 13:13:09 +11002307 icsbp->icsb_icount = lcounter;
2308 break;
2309
2310 case XFS_SBS_IFREE:
2311 lcounter = icsbp->icsb_ifree;
2312 lcounter += delta;
2313 if (unlikely(lcounter < 0))
David Chinner20b64282007-02-10 18:35:09 +11002314 goto balance_counter;
David Chinner8d280b92006-03-14 13:13:09 +11002315 icsbp->icsb_ifree = lcounter;
2316 break;
2317
2318 case XFS_SBS_FDBLOCKS:
2319 BUG_ON((mp->m_resblks - mp->m_resblks_avail) != 0);
2320
David Chinner4be536d2006-09-07 14:26:50 +10002321 lcounter = icsbp->icsb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
David Chinner8d280b92006-03-14 13:13:09 +11002322 lcounter += delta;
2323 if (unlikely(lcounter < 0))
David Chinner20b64282007-02-10 18:35:09 +11002324 goto balance_counter;
David Chinner4be536d2006-09-07 14:26:50 +10002325 icsbp->icsb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
David Chinner8d280b92006-03-14 13:13:09 +11002326 break;
2327 default:
2328 BUG();
2329 break;
2330 }
David Chinner01e1b692006-03-14 13:29:16 +11002331 xfs_icsb_unlock_cntr(icsbp);
David Chinner8d280b92006-03-14 13:13:09 +11002332 put_cpu();
David Chinner8d280b92006-03-14 13:13:09 +11002333 return 0;
2334
David Chinner8d280b92006-03-14 13:13:09 +11002335slow_path:
David Chinner20b64282007-02-10 18:35:09 +11002336 put_cpu();
2337
2338 /*
2339 * serialise with a mutex so we don't burn lots of cpu on
2340 * the superblock lock. We still need to hold the superblock
2341 * lock, however, when we modify the global structures.
2342 */
David Chinner03135cf2007-02-10 18:35:15 +11002343 xfs_icsb_lock(mp);
David Chinner20b64282007-02-10 18:35:09 +11002344
2345 /*
2346 * Now running atomically.
2347 *
2348 * If the counter is enabled, someone has beaten us to rebalancing.
2349 * Drop the lock and try again in the fast path....
2350 */
2351 if (!(xfs_icsb_counter_disabled(mp, field))) {
David Chinner03135cf2007-02-10 18:35:15 +11002352 xfs_icsb_unlock(mp);
David Chinner20b64282007-02-10 18:35:09 +11002353 goto again;
2354 }
2355
2356 /*
2357 * The counter is currently disabled. Because we are
2358 * running atomically here, we know a rebalance cannot
2359 * be in progress. Hence we can go straight to operating
2360 * on the global superblock. We do not call xfs_mod_incore_sb()
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002361 * here even though we need to get the m_sb_lock. Doing so
David Chinner20b64282007-02-10 18:35:09 +11002362 * will cause us to re-enter this function and deadlock.
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002363 * Hence we get the m_sb_lock ourselves and then call
David Chinner20b64282007-02-10 18:35:09 +11002364 * xfs_mod_incore_sb_unlocked() as the unlocked path operates
2365 * directly on the global counters.
2366 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002367 spin_lock(&mp->m_sb_lock);
David Chinner20b64282007-02-10 18:35:09 +11002368 ret = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002369 spin_unlock(&mp->m_sb_lock);
David Chinner20b64282007-02-10 18:35:09 +11002370
2371 /*
2372 * Now that we've modified the global superblock, we
2373 * may be able to re-enable the distributed counters
2374 * (e.g. lots of space just got freed). After that
2375 * we are done.
2376 */
2377 if (ret != ENOSPC)
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002378 xfs_icsb_balance_counter(mp, field, 0);
David Chinner03135cf2007-02-10 18:35:15 +11002379 xfs_icsb_unlock(mp);
David Chinner20b64282007-02-10 18:35:09 +11002380 return ret;
2381
2382balance_counter:
David Chinner01e1b692006-03-14 13:29:16 +11002383 xfs_icsb_unlock_cntr(icsbp);
David Chinner8d280b92006-03-14 13:13:09 +11002384 put_cpu();
2385
David Chinner20b64282007-02-10 18:35:09 +11002386 /*
2387 * We may have multiple threads here if multiple per-cpu
2388 * counters run dry at the same time. This will mean we can
2389 * do more balances than strictly necessary but it is not
2390 * the common slowpath case.
2391 */
David Chinner03135cf2007-02-10 18:35:15 +11002392 xfs_icsb_lock(mp);
David Chinner8d280b92006-03-14 13:13:09 +11002393
David Chinner20b64282007-02-10 18:35:09 +11002394 /*
2395 * running atomically.
2396 *
2397 * This will leave the counter in the correct state for future
2398 * accesses. After the rebalance, we simply try again and our retry
2399 * will either succeed through the fast path or slow path without
2400 * another balance operation being required.
2401 */
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002402 xfs_icsb_balance_counter(mp, field, delta);
David Chinner03135cf2007-02-10 18:35:15 +11002403 xfs_icsb_unlock(mp);
David Chinner20b64282007-02-10 18:35:09 +11002404 goto again;
David Chinner8d280b92006-03-14 13:13:09 +11002405}
2406
David Chinner8d280b92006-03-14 13:13:09 +11002407#endif