blob: 92fedfcc879abee2944a02751f2ecba2ea11e461 [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
Christoph Hellwig1a5902c2009-03-29 19:26:46 +0200259 /*
260 * Currently only very few inode sizes are supported.
261 */
262 switch (sbp->sb_inodesize) {
263 case 256:
264 case 512:
265 case 1024:
266 case 2048:
267 break;
268 default:
269 xfs_fs_mount_cmn_err(flags,
270 "inode size of %d bytes not supported",
271 sbp->sb_inodesize);
272 return XFS_ERROR(ENOSYS);
273 }
274
Nathan Scott4cc929e2007-05-14 18:24:02 +1000275 if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) ||
276 xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000277 xfs_fs_mount_cmn_err(flags,
278 "file system too large to be mounted on this system.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return XFS_ERROR(E2BIG);
280 }
281
282 if (unlikely(sbp->sb_inprogress)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000283 xfs_fs_mount_cmn_err(flags, "file system busy");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return XFS_ERROR(EFSCORRUPTED);
285 }
286
287 /*
Nathan Scottde206142005-05-05 13:24:13 -0700288 * Version 1 directory format has never worked on Linux.
289 */
Eric Sandeen62118702008-03-06 13:44:28 +1100290 if (unlikely(!xfs_sb_version_hasdirv2(sbp))) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000291 xfs_fs_mount_cmn_err(flags,
292 "file system using version 1 directory format");
Nathan Scottde206142005-05-05 13:24:13 -0700293 return XFS_ERROR(ENOSYS);
294 }
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return 0;
297}
298
David Chinnerda353b02007-08-28 14:00:13 +1000299STATIC void
300xfs_initialize_perag_icache(
301 xfs_perag_t *pag)
302{
303 if (!pag->pag_ici_init) {
304 rwlock_init(&pag->pag_ici_lock);
305 INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC);
306 pag->pag_ici_init = 1;
307 }
308}
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310xfs_agnumber_t
Nathan Scottc11e2c32005-11-02 15:11:45 +1100311xfs_initialize_perag(
Nathan Scottc11e2c32005-11-02 15:11:45 +1100312 xfs_mount_t *mp,
313 xfs_agnumber_t agcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 xfs_agnumber_t index, max_metadata;
316 xfs_perag_t *pag;
317 xfs_agino_t agino;
318 xfs_ino_t ino;
319 xfs_sb_t *sbp = &mp->m_sb;
320 xfs_ino_t max_inum = XFS_MAXINUMBER_32;
321
322 /* Check to see if the filesystem can overflow 32 bit inodes */
323 agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
324 ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
325
326 /* Clear the mount flag if no inode can overflow 32 bits
327 * on this filesystem, or if specifically requested..
328 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000329 if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) && ino > max_inum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 mp->m_flags |= XFS_MOUNT_32BITINODES;
331 } else {
332 mp->m_flags &= ~XFS_MOUNT_32BITINODES;
333 }
334
335 /* If we can overflow then setup the ag headers accordingly */
336 if (mp->m_flags & XFS_MOUNT_32BITINODES) {
337 /* Calculate how much should be reserved for inodes to
338 * meet the max inode percentage.
339 */
340 if (mp->m_maxicount) {
341 __uint64_t icount;
342
343 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
344 do_div(icount, 100);
345 icount += sbp->sb_agblocks - 1;
Eric Sandeena749ee82005-11-02 15:13:42 +1100346 do_div(icount, sbp->sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 max_metadata = icount;
348 } else {
349 max_metadata = agcount;
350 }
351 for (index = 0; index < agcount; index++) {
352 ino = XFS_AGINO_TO_INO(mp, index, agino);
353 if (ino > max_inum) {
354 index++;
355 break;
356 }
357
Nathan Scottc41564b2006-03-29 08:55:14 +1000358 /* This ag is preferred for inodes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 pag = &mp->m_perag[index];
360 pag->pagi_inodeok = 1;
361 if (index < max_metadata)
362 pag->pagf_metadata = 1;
David Chinnerda353b02007-08-28 14:00:13 +1000363 xfs_initialize_perag_icache(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365 } else {
366 /* Setup default behavior for smaller filesystems */
367 for (index = 0; index < agcount; index++) {
368 pag = &mp->m_perag[index];
369 pag->pagi_inodeok = 1;
David Chinnerda353b02007-08-28 14:00:13 +1000370 xfs_initialize_perag_icache(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372 }
373 return index;
374}
375
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000376void
377xfs_sb_from_disk(
378 xfs_sb_t *to,
379 xfs_dsb_t *from)
380{
381 to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
382 to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
383 to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
384 to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
385 to->sb_rextents = be64_to_cpu(from->sb_rextents);
386 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
387 to->sb_logstart = be64_to_cpu(from->sb_logstart);
388 to->sb_rootino = be64_to_cpu(from->sb_rootino);
389 to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
390 to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
391 to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
392 to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
393 to->sb_agcount = be32_to_cpu(from->sb_agcount);
394 to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
395 to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
396 to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
397 to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
398 to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
399 to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
400 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
401 to->sb_blocklog = from->sb_blocklog;
402 to->sb_sectlog = from->sb_sectlog;
403 to->sb_inodelog = from->sb_inodelog;
404 to->sb_inopblog = from->sb_inopblog;
405 to->sb_agblklog = from->sb_agblklog;
406 to->sb_rextslog = from->sb_rextslog;
407 to->sb_inprogress = from->sb_inprogress;
408 to->sb_imax_pct = from->sb_imax_pct;
409 to->sb_icount = be64_to_cpu(from->sb_icount);
410 to->sb_ifree = be64_to_cpu(from->sb_ifree);
411 to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
412 to->sb_frextents = be64_to_cpu(from->sb_frextents);
413 to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
414 to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
415 to->sb_qflags = be16_to_cpu(from->sb_qflags);
416 to->sb_flags = from->sb_flags;
417 to->sb_shared_vn = from->sb_shared_vn;
418 to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
419 to->sb_unit = be32_to_cpu(from->sb_unit);
420 to->sb_width = be32_to_cpu(from->sb_width);
421 to->sb_dirblklog = from->sb_dirblklog;
422 to->sb_logsectlog = from->sb_logsectlog;
423 to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
424 to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
425 to->sb_features2 = be32_to_cpu(from->sb_features2);
David Chinneree1c0902008-03-06 13:45:50 +1100426 to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000427}
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429/*
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000430 * Copy in core superblock to ondisk one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 *
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000432 * The fields argument is mask of superblock fields to copy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 */
434void
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000435xfs_sb_to_disk(
436 xfs_dsb_t *to,
437 xfs_sb_t *from,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 __int64_t fields)
439{
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000440 xfs_caddr_t to_ptr = (xfs_caddr_t)to;
441 xfs_caddr_t from_ptr = (xfs_caddr_t)from;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 xfs_sb_field_t f;
443 int first;
444 int size;
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 ASSERT(fields);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (!fields)
448 return;
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 while (fields) {
451 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
452 first = xfs_sb_info[f].offset;
453 size = xfs_sb_info[f + 1].offset - first;
454
455 ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1);
456
457 if (size == 1 || xfs_sb_info[f].type == 1) {
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000458 memcpy(to_ptr + first, from_ptr + first, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 } else {
460 switch (size) {
461 case 2:
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000462 *(__be16 *)(to_ptr + first) =
463 cpu_to_be16(*(__u16 *)(from_ptr + first));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 break;
465 case 4:
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000466 *(__be32 *)(to_ptr + first) =
467 cpu_to_be32(*(__u32 *)(from_ptr + first));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 break;
469 case 8:
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000470 *(__be64 *)(to_ptr + first) =
471 cpu_to_be64(*(__u64 *)(from_ptr + first));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 break;
473 default:
474 ASSERT(0);
475 }
476 }
477
478 fields &= ~(1LL << f);
479 }
480}
481
482/*
483 * xfs_readsb
484 *
485 * Does the initial read of the superblock.
486 */
487int
Nathan Scott764d1f82006-03-31 13:04:17 +1000488xfs_readsb(xfs_mount_t *mp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 unsigned int sector_size;
491 unsigned int extra_flags;
492 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 int error;
494
495 ASSERT(mp->m_sb_bp == NULL);
496 ASSERT(mp->m_ddev_targp != NULL);
497
498 /*
499 * Allocate a (locked) buffer to hold the superblock.
500 * This will be kept around at all times to optimize
501 * access to the superblock.
502 */
503 sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
504 extra_flags = XFS_BUF_LOCK | XFS_BUF_MANAGE | XFS_BUF_MAPPED;
505
506 bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR,
507 BTOBB(sector_size), extra_flags);
508 if (!bp || XFS_BUF_ISERROR(bp)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000509 xfs_fs_mount_cmn_err(flags, "SB read failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
511 goto fail;
512 }
513 ASSERT(XFS_BUF_ISBUSY(bp));
514 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
515
516 /*
517 * Initialize the mount structure from the superblock.
518 * But first do some basic consistency checking.
519 */
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000520 xfs_sb_from_disk(&mp->m_sb, XFS_BUF_TO_SBP(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Nathan Scott764d1f82006-03-31 13:04:17 +1000522 error = xfs_mount_validate_sb(mp, &(mp->m_sb), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (error) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000524 xfs_fs_mount_cmn_err(flags, "SB validate failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 goto fail;
526 }
527
528 /*
529 * We must be able to do sector-sized and sector-aligned IO.
530 */
531 if (sector_size > mp->m_sb.sb_sectsize) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000532 xfs_fs_mount_cmn_err(flags,
533 "device supports only %u byte sectors (not %u)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 sector_size, mp->m_sb.sb_sectsize);
535 error = ENOSYS;
536 goto fail;
537 }
538
539 /*
540 * If device sector size is smaller than the superblock size,
541 * re-read the superblock so the buffer is correctly sized.
542 */
543 if (sector_size < mp->m_sb.sb_sectsize) {
544 XFS_BUF_UNMANAGE(bp);
545 xfs_buf_relse(bp);
546 sector_size = mp->m_sb.sb_sectsize;
547 bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR,
548 BTOBB(sector_size), extra_flags);
549 if (!bp || XFS_BUF_ISERROR(bp)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000550 xfs_fs_mount_cmn_err(flags, "SB re-read failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
552 goto fail;
553 }
554 ASSERT(XFS_BUF_ISBUSY(bp));
555 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
556 }
557
Lachlan McIlroy5478eea2007-02-10 18:36:29 +1100558 /* Initialize per-cpu counters */
559 xfs_icsb_reinit_counters(mp);
David Chinner8d280b92006-03-14 13:13:09 +1100560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 mp->m_sb_bp = bp;
562 xfs_buf_relse(bp);
563 ASSERT(XFS_BUF_VALUSEMA(bp) > 0);
564 return 0;
565
566 fail:
567 if (bp) {
568 XFS_BUF_UNMANAGE(bp);
569 xfs_buf_relse(bp);
570 }
571 return error;
572}
573
574
575/*
576 * xfs_mount_common
577 *
578 * Mount initialization code establishing various mount
579 * fields from the superblock associated with the given
580 * mount structure
581 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000582STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583xfs_mount_common(xfs_mount_t *mp, xfs_sb_t *sbp)
584{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 mp->m_agfrotor = mp->m_agirotor = 0;
Eric Sandeen007c61c2007-10-11 17:43:56 +1000586 spin_lock_init(&mp->m_agirotor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 mp->m_maxagi = mp->m_sb.sb_agcount;
588 mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
589 mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
590 mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
591 mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
592 mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 mp->m_blockmask = sbp->sb_blocksize - 1;
594 mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
595 mp->m_blockwmask = mp->m_blockwsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Christoph Hellwig60197e82008-10-30 17:11:19 +1100597 mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
598 mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
599 mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
600 mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
601
602 mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
603 mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
604 mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2;
605 mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2;
606
607 mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
608 mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
609 mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
610 mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
613 mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
614 sbp->sb_inopblock);
615 mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
616}
David Chinner92821e22007-05-24 15:26:31 +1000617
618/*
619 * xfs_initialize_perag_data
620 *
621 * Read in each per-ag structure so we can count up the number of
622 * allocated inodes, free inodes and used filesystem blocks as this
623 * information is no longer persistent in the superblock. Once we have
624 * this information, write it into the in-core superblock structure.
625 */
626STATIC int
627xfs_initialize_perag_data(xfs_mount_t *mp, xfs_agnumber_t agcount)
628{
629 xfs_agnumber_t index;
630 xfs_perag_t *pag;
631 xfs_sb_t *sbp = &mp->m_sb;
632 uint64_t ifree = 0;
633 uint64_t ialloc = 0;
634 uint64_t bfree = 0;
635 uint64_t bfreelst = 0;
636 uint64_t btree = 0;
637 int error;
David Chinner92821e22007-05-24 15:26:31 +1000638
639 for (index = 0; index < agcount; index++) {
640 /*
641 * read the agf, then the agi. This gets us
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200642 * all the information we need and populates the
David Chinner92821e22007-05-24 15:26:31 +1000643 * per-ag structures for us.
644 */
645 error = xfs_alloc_pagf_init(mp, NULL, index, 0);
646 if (error)
647 return error;
648
649 error = xfs_ialloc_pagi_init(mp, NULL, index);
650 if (error)
651 return error;
652 pag = &mp->m_perag[index];
653 ifree += pag->pagi_freecount;
654 ialloc += pag->pagi_count;
655 bfree += pag->pagf_freeblks;
656 bfreelst += pag->pagf_flcount;
657 btree += pag->pagf_btreeblks;
658 }
659 /*
660 * Overwrite incore superblock counters with just-read data
661 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000662 spin_lock(&mp->m_sb_lock);
David Chinner92821e22007-05-24 15:26:31 +1000663 sbp->sb_ifree = ifree;
664 sbp->sb_icount = ialloc;
665 sbp->sb_fdblocks = bfree + bfreelst + btree;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000666 spin_unlock(&mp->m_sb_lock);
David Chinner92821e22007-05-24 15:26:31 +1000667
668 /* Fixup the per-cpu counters as well. */
669 xfs_icsb_reinit_counters(mp);
670
671 return 0;
672}
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674/*
Eric Sandeen0771fb42007-10-12 11:03:40 +1000675 * Update alignment values based on mount options and sb values
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 */
Eric Sandeen0771fb42007-10-12 11:03:40 +1000677STATIC int
Christoph Hellwig7884bc82009-01-19 02:04:07 +0100678xfs_update_alignment(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 xfs_sb_t *sbp = &(mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Christoph Hellwig42490232008-08-13 16:49:32 +1000682 if (mp->m_dalign) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 /*
684 * If stripe unit and stripe width are not multiples
685 * of the fs blocksize turn off alignment.
686 */
687 if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
688 (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
689 if (mp->m_flags & XFS_MOUNT_RETERR) {
690 cmn_err(CE_WARN,
691 "XFS: alignment check 1 failed");
Eric Sandeen0771fb42007-10-12 11:03:40 +1000692 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694 mp->m_dalign = mp->m_swidth = 0;
695 } else {
696 /*
697 * Convert the stripe unit and width to FSBs.
698 */
699 mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
700 if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
701 if (mp->m_flags & XFS_MOUNT_RETERR) {
Eric Sandeen0771fb42007-10-12 11:03:40 +1000702 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704 xfs_fs_cmn_err(CE_WARN, mp,
705"stripe alignment turned off: sunit(%d)/swidth(%d) incompatible with agsize(%d)",
706 mp->m_dalign, mp->m_swidth,
707 sbp->sb_agblocks);
708
709 mp->m_dalign = 0;
710 mp->m_swidth = 0;
711 } else if (mp->m_dalign) {
712 mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
713 } else {
714 if (mp->m_flags & XFS_MOUNT_RETERR) {
715 xfs_fs_cmn_err(CE_WARN, mp,
716"stripe alignment turned off: sunit(%d) less than bsize(%d)",
717 mp->m_dalign,
718 mp->m_blockmask +1);
Eric Sandeen0771fb42007-10-12 11:03:40 +1000719 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
721 mp->m_swidth = 0;
722 }
723 }
724
725 /*
726 * Update superblock with new values
727 * and log changes
728 */
Eric Sandeen62118702008-03-06 13:44:28 +1100729 if (xfs_sb_version_hasdalign(sbp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (sbp->sb_unit != mp->m_dalign) {
731 sbp->sb_unit = mp->m_dalign;
Christoph Hellwig7884bc82009-01-19 02:04:07 +0100732 mp->m_update_flags |= XFS_SB_UNIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734 if (sbp->sb_width != mp->m_swidth) {
735 sbp->sb_width = mp->m_swidth;
Christoph Hellwig7884bc82009-01-19 02:04:07 +0100736 mp->m_update_flags |= XFS_SB_WIDTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738 }
739 } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
Eric Sandeen62118702008-03-06 13:44:28 +1100740 xfs_sb_version_hasdalign(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 mp->m_dalign = sbp->sb_unit;
742 mp->m_swidth = sbp->sb_width;
743 }
744
Eric Sandeen0771fb42007-10-12 11:03:40 +1000745 return 0;
746}
747
748/*
749 * Set the maximum inode count for this filesystem
750 */
751STATIC void
752xfs_set_maxicount(xfs_mount_t *mp)
753{
754 xfs_sb_t *sbp = &(mp->m_sb);
755 __uint64_t icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 if (sbp->sb_imax_pct) {
Eric Sandeen0771fb42007-10-12 11:03:40 +1000758 /*
759 * Make sure the maximum inode count is a multiple
760 * of the units we allocate inodes in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
763 do_div(icount, 100);
764 do_div(icount, mp->m_ialloc_blks);
765 mp->m_maxicount = (icount * mp->m_ialloc_blks) <<
766 sbp->sb_inopblog;
Eric Sandeen0771fb42007-10-12 11:03:40 +1000767 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 mp->m_maxicount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
Eric Sandeen0771fb42007-10-12 11:03:40 +1000770}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Eric Sandeen0771fb42007-10-12 11:03:40 +1000772/*
773 * Set the default minimum read and write sizes unless
774 * already specified in a mount option.
775 * We use smaller I/O sizes when the file system
776 * is being used for NFS service (wsync mount option).
777 */
778STATIC void
779xfs_set_rw_sizes(xfs_mount_t *mp)
780{
781 xfs_sb_t *sbp = &(mp->m_sb);
782 int readio_log, writeio_log;
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
785 if (mp->m_flags & XFS_MOUNT_WSYNC) {
786 readio_log = XFS_WSYNC_READIO_LOG;
787 writeio_log = XFS_WSYNC_WRITEIO_LOG;
788 } else {
789 readio_log = XFS_READIO_LOG_LARGE;
790 writeio_log = XFS_WRITEIO_LOG_LARGE;
791 }
792 } else {
793 readio_log = mp->m_readio_log;
794 writeio_log = mp->m_writeio_log;
795 }
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (sbp->sb_blocklog > readio_log) {
798 mp->m_readio_log = sbp->sb_blocklog;
799 } else {
800 mp->m_readio_log = readio_log;
801 }
802 mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
803 if (sbp->sb_blocklog > writeio_log) {
804 mp->m_writeio_log = sbp->sb_blocklog;
805 } else {
806 mp->m_writeio_log = writeio_log;
807 }
808 mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
Eric Sandeen0771fb42007-10-12 11:03:40 +1000809}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Eric Sandeen0771fb42007-10-12 11:03:40 +1000811/*
812 * Set whether we're using inode alignment.
813 */
814STATIC void
815xfs_set_inoalignment(xfs_mount_t *mp)
816{
Eric Sandeen62118702008-03-06 13:44:28 +1100817 if (xfs_sb_version_hasalign(&mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 mp->m_sb.sb_inoalignmt >=
819 XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
820 mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
821 else
822 mp->m_inoalign_mask = 0;
823 /*
824 * If we are using stripe alignment, check whether
825 * the stripe unit is a multiple of the inode alignment
826 */
827 if (mp->m_dalign && mp->m_inoalign_mask &&
828 !(mp->m_dalign & mp->m_inoalign_mask))
829 mp->m_sinoalign = mp->m_dalign;
830 else
831 mp->m_sinoalign = 0;
Eric Sandeen0771fb42007-10-12 11:03:40 +1000832}
833
834/*
835 * Check that the data (and log if separate) are an ok size.
836 */
837STATIC int
Christoph Hellwig42490232008-08-13 16:49:32 +1000838xfs_check_sizes(xfs_mount_t *mp)
Eric Sandeen0771fb42007-10-12 11:03:40 +1000839{
840 xfs_buf_t *bp;
841 xfs_daddr_t d;
842 int error;
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
845 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
846 cmn_err(CE_WARN, "XFS: size check 1 failed");
Eric Sandeen0771fb42007-10-12 11:03:40 +1000847 return XFS_ERROR(E2BIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
849 error = xfs_read_buf(mp, mp->m_ddev_targp,
850 d - XFS_FSS_TO_BB(mp, 1),
851 XFS_FSS_TO_BB(mp, 1), 0, &bp);
852 if (!error) {
853 xfs_buf_relse(bp);
854 } else {
855 cmn_err(CE_WARN, "XFS: size check 2 failed");
Eric Sandeen0771fb42007-10-12 11:03:40 +1000856 if (error == ENOSPC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 error = XFS_ERROR(E2BIG);
Eric Sandeen0771fb42007-10-12 11:03:40 +1000858 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860
Christoph Hellwig42490232008-08-13 16:49:32 +1000861 if (mp->m_logdev_targp != mp->m_ddev_targp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
863 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
864 cmn_err(CE_WARN, "XFS: size check 3 failed");
Eric Sandeen0771fb42007-10-12 11:03:40 +1000865 return XFS_ERROR(E2BIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867 error = xfs_read_buf(mp, mp->m_logdev_targp,
868 d - XFS_FSB_TO_BB(mp, 1),
869 XFS_FSB_TO_BB(mp, 1), 0, &bp);
870 if (!error) {
871 xfs_buf_relse(bp);
872 } else {
873 cmn_err(CE_WARN, "XFS: size check 3 failed");
Eric Sandeen0771fb42007-10-12 11:03:40 +1000874 if (error == ENOSPC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 error = XFS_ERROR(E2BIG);
Eric Sandeen0771fb42007-10-12 11:03:40 +1000876 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878 }
Eric Sandeen0771fb42007-10-12 11:03:40 +1000879 return 0;
880}
881
882/*
Eric Sandeen0771fb42007-10-12 11:03:40 +1000883 * This function does the following on an initial mount of a file system:
884 * - reads the superblock from disk and init the mount struct
885 * - if we're a 32-bit kernel, do a size check on the superblock
886 * so we don't mount terabyte filesystems
887 * - init mount struct realtime fields
888 * - allocate inode hash table for fs
889 * - init directory manager
890 * - perform recovery and init the log manager
891 */
892int
893xfs_mountfs(
Christoph Hellwig42490232008-08-13 16:49:32 +1000894 xfs_mount_t *mp)
Eric Sandeen0771fb42007-10-12 11:03:40 +1000895{
896 xfs_sb_t *sbp = &(mp->m_sb);
897 xfs_inode_t *rip;
Eric Sandeen0771fb42007-10-12 11:03:40 +1000898 __uint64_t resblks;
Eric Sandeen0771fb42007-10-12 11:03:40 +1000899 uint quotamount, quotaflags;
Eric Sandeen0771fb42007-10-12 11:03:40 +1000900 int error = 0;
901
Eric Sandeen0771fb42007-10-12 11:03:40 +1000902 xfs_mount_common(mp, sbp);
903
904 /*
Eric Sandeene6957ea2008-04-10 12:19:34 +1000905 * Check for a mismatched features2 values. Older kernels
906 * read & wrote into the wrong sb offset for sb_features2
907 * on some platforms due to xfs_sb_t not being 64bit size aligned
908 * when sb_features2 was added, which made older superblock
909 * reading/writing routines swap it as a 64-bit value.
David Chinneree1c0902008-03-06 13:45:50 +1100910 *
Eric Sandeene6957ea2008-04-10 12:19:34 +1000911 * For backwards compatibility, we make both slots equal.
912 *
913 * If we detect a mismatched field, we OR the set bits into the
914 * existing features2 field in case it has already been modified; we
915 * don't want to lose any features. We then update the bad location
916 * with the ORed value so that older kernels will see any features2
917 * flags, and mark the two fields as needing updates once the
918 * transaction subsystem is online.
David Chinneree1c0902008-03-06 13:45:50 +1100919 */
Eric Sandeene6957ea2008-04-10 12:19:34 +1000920 if (xfs_sb_has_mismatched_features2(sbp)) {
David Chinneree1c0902008-03-06 13:45:50 +1100921 cmn_err(CE_WARN,
922 "XFS: correcting sb_features alignment problem");
923 sbp->sb_features2 |= sbp->sb_bad_features2;
Eric Sandeene6957ea2008-04-10 12:19:34 +1000924 sbp->sb_bad_features2 = sbp->sb_features2;
Christoph Hellwig7884bc82009-01-19 02:04:07 +0100925 mp->m_update_flags |= XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2;
Eric Sandeene6957ea2008-04-10 12:19:34 +1000926
927 /*
928 * Re-check for ATTR2 in case it was found in bad_features2
929 * slot.
930 */
Tim Shimmin7c12f292008-04-30 18:15:28 +1000931 if (xfs_sb_version_hasattr2(&mp->m_sb) &&
932 !(mp->m_flags & XFS_MOUNT_NOATTR2))
Eric Sandeene6957ea2008-04-10 12:19:34 +1000933 mp->m_flags |= XFS_MOUNT_ATTR2;
Tim Shimmin7c12f292008-04-30 18:15:28 +1000934 }
Eric Sandeene6957ea2008-04-10 12:19:34 +1000935
Tim Shimmin7c12f292008-04-30 18:15:28 +1000936 if (xfs_sb_version_hasattr2(&mp->m_sb) &&
937 (mp->m_flags & XFS_MOUNT_NOATTR2)) {
938 xfs_sb_version_removeattr2(&mp->m_sb);
Christoph Hellwig7884bc82009-01-19 02:04:07 +0100939 mp->m_update_flags |= XFS_SB_FEATURES2;
Tim Shimmin7c12f292008-04-30 18:15:28 +1000940
941 /* update sb_versionnum for the clearing of the morebits */
942 if (!sbp->sb_features2)
Christoph Hellwig7884bc82009-01-19 02:04:07 +0100943 mp->m_update_flags |= XFS_SB_VERSIONNUM;
David Chinneree1c0902008-03-06 13:45:50 +1100944 }
945
946 /*
Eric Sandeen0771fb42007-10-12 11:03:40 +1000947 * Check if sb_agblocks is aligned at stripe boundary
948 * If sb_agblocks is NOT aligned turn off m_dalign since
949 * allocator alignment is within an ag, therefore ag has
950 * to be aligned at stripe boundary.
951 */
Christoph Hellwig7884bc82009-01-19 02:04:07 +0100952 error = xfs_update_alignment(mp);
Eric Sandeen0771fb42007-10-12 11:03:40 +1000953 if (error)
Christoph Hellwigf9057e32009-02-04 09:31:52 +0100954 goto out;
Eric Sandeen0771fb42007-10-12 11:03:40 +1000955
956 xfs_alloc_compute_maxlevels(mp);
957 xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
958 xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
959 xfs_ialloc_compute_maxlevels(mp);
960
961 xfs_set_maxicount(mp);
962
963 mp->m_maxioffset = xfs_max_file_offset(sbp->sb_blocklog);
964
965 /*
966 * XFS uses the uuid from the superblock as the unique
967 * identifier for fsid. We can not use the uuid from the volume
968 * since a single partition filesystem is identical to a single
969 * partition volume/filesystem.
970 */
Christoph Hellwigf9057e32009-02-04 09:31:52 +0100971 if (!(mp->m_flags & XFS_MOUNT_NOUUID)) {
Eric Sandeen0771fb42007-10-12 11:03:40 +1000972 if (xfs_uuid_mount(mp)) {
973 error = XFS_ERROR(EINVAL);
Christoph Hellwigf9057e32009-02-04 09:31:52 +0100974 goto out;
Eric Sandeen0771fb42007-10-12 11:03:40 +1000975 }
Eric Sandeen0771fb42007-10-12 11:03:40 +1000976 }
977
978 /*
979 * Set the minimum read and write sizes
980 */
981 xfs_set_rw_sizes(mp);
982
983 /*
984 * Set the inode cluster size.
985 * This may still be overridden by the file system
986 * block size if it is larger than the chosen cluster size.
987 */
988 mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
989
990 /*
991 * Set inode alignment fields
992 */
993 xfs_set_inoalignment(mp);
994
995 /*
996 * Check that the data (and log if separate) are an ok size.
997 */
Christoph Hellwig42490232008-08-13 16:49:32 +1000998 error = xfs_check_sizes(mp);
Eric Sandeen0771fb42007-10-12 11:03:40 +1000999 if (error)
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001000 goto out_remove_uuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 /*
1003 * Initialize realtime fields in the mount structure
1004 */
Eric Sandeen0771fb42007-10-12 11:03:40 +10001005 error = xfs_rtmount_init(mp);
1006 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 cmn_err(CE_WARN, "XFS: RT mount failed");
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001008 goto out_remove_uuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010
1011 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 * Copies the low order bits of the timestamp and the randomly
1013 * set "sequence" number out of a UUID.
1014 */
1015 uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid);
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 mp->m_dmevmask = 0; /* not persistent; set after each mount */
1018
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001019 xfs_dir_mount(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 /*
1022 * Initialize the attribute manager's entries.
1023 */
1024 mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100;
1025
1026 /*
1027 * Initialize the precomputed transaction reservations values.
1028 */
1029 xfs_trans_init(mp);
1030
1031 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 * Allocate and initialize the per-ag data.
1033 */
1034 init_rwsem(&mp->m_peraglock);
Christoph Hellwigd62c2512008-08-13 16:51:29 +10001035 mp->m_perag = kmem_zalloc(sbp->sb_agcount * sizeof(xfs_perag_t),
1036 KM_MAYFAIL);
1037 if (!mp->m_perag)
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001038 goto out_remove_uuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Christoph Hellwigb267ce92007-08-30 17:21:30 +10001040 mp->m_maxagi = xfs_initialize_perag(mp, sbp->sb_agcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001042 if (!sbp->sb_logblocks) {
1043 cmn_err(CE_WARN, "XFS: no log defined");
1044 XFS_ERROR_REPORT("xfs_mountfs", XFS_ERRLEVEL_LOW, mp);
1045 error = XFS_ERROR(EFSCORRUPTED);
1046 goto out_free_perag;
1047 }
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 /*
1050 * log's mount-time initialization. Perform 1st part recovery if needed
1051 */
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001052 error = xfs_log_mount(mp, mp->m_logdev_targp,
1053 XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
1054 XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
1055 if (error) {
1056 cmn_err(CE_WARN, "XFS: log mount failed");
1057 goto out_free_perag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
1059
1060 /*
David Chinner92821e22007-05-24 15:26:31 +10001061 * Now the log is mounted, we know if it was an unclean shutdown or
1062 * not. If it was, with the first phase of recovery has completed, we
1063 * have consistent AG blocks on disk. We have not recovered EFIs yet,
1064 * but they are recovered transactionally in the second recovery phase
1065 * later.
1066 *
1067 * Hence we can safely re-initialise incore superblock counters from
1068 * the per-ag data. These may not be correct if the filesystem was not
1069 * cleanly unmounted, so we need to wait for recovery to finish before
1070 * doing this.
1071 *
1072 * If the filesystem was cleanly unmounted, then we can trust the
1073 * values in the superblock to be correct and we don't need to do
1074 * anything here.
1075 *
1076 * If we are currently making the filesystem, the initialisation will
1077 * fail as the perag data is in an undefined state.
1078 */
David Chinner92821e22007-05-24 15:26:31 +10001079 if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
1080 !XFS_LAST_UNMOUNT_WAS_CLEAN(mp) &&
1081 !mp->m_sb.sb_inprogress) {
1082 error = xfs_initialize_perag_data(mp, sbp->sb_agcount);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001083 if (error)
1084 goto out_free_perag;
David Chinner92821e22007-05-24 15:26:31 +10001085 }
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001086
David Chinner92821e22007-05-24 15:26:31 +10001087 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 * Get and sanity-check the root inode.
1089 * Save the pointer to it in the mount structure.
1090 */
1091 error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip, 0);
1092 if (error) {
1093 cmn_err(CE_WARN, "XFS: failed to read root inode");
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001094 goto out_log_dealloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
1096
1097 ASSERT(rip != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 if (unlikely((rip->i_d.di_mode & S_IFMT) != S_IFDIR)) {
1100 cmn_err(CE_WARN, "XFS: corrupted root inode");
Nathan Scottb6574522006-06-09 15:29:40 +10001101 cmn_err(CE_WARN, "Device %s - root %llu is not a directory",
1102 XFS_BUFTARG_NAME(mp->m_ddev_targp),
1103 (unsigned long long)rip->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 xfs_iunlock(rip, XFS_ILOCK_EXCL);
1105 XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
1106 mp);
1107 error = XFS_ERROR(EFSCORRUPTED);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001108 goto out_rele_rip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110 mp->m_rootip = rip; /* save it */
1111
1112 xfs_iunlock(rip, XFS_ILOCK_EXCL);
1113
1114 /*
1115 * Initialize realtime inode pointers in the mount structure
1116 */
Eric Sandeen0771fb42007-10-12 11:03:40 +10001117 error = xfs_rtmount_inodes(mp);
1118 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 /*
1120 * Free up the root inode.
1121 */
1122 cmn_err(CE_WARN, "XFS: failed to read RT inodes");
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001123 goto out_rele_rip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
1125
1126 /*
Christoph Hellwig7884bc82009-01-19 02:04:07 +01001127 * If this is a read-only mount defer the superblock updates until
1128 * the next remount into writeable mode. Otherwise we would never
1129 * perform the update e.g. for the root filesystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 */
Christoph Hellwig7884bc82009-01-19 02:04:07 +01001131 if (mp->m_update_flags && !(mp->m_flags & XFS_MOUNT_RDONLY)) {
1132 error = xfs_mount_log_sb(mp, mp->m_update_flags);
David Chinnere5720ee2008-04-10 12:21:18 +10001133 if (error) {
1134 cmn_err(CE_WARN, "XFS: failed to write sb changes");
Christoph Hellwigb93b6e42009-02-04 09:33:58 +01001135 goto out_rtunmount;
David Chinnere5720ee2008-04-10 12:21:18 +10001136 }
1137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 /*
1140 * Initialise the XFS quota management subsystem for this mount
1141 */
Eric Sandeen0771fb42007-10-12 11:03:40 +10001142 error = XFS_QM_INIT(mp, &quotamount, &quotaflags);
1143 if (error)
Christoph Hellwigb93b6e42009-02-04 09:33:58 +01001144 goto out_rtunmount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 /*
1147 * Finish recovering the file system. This part needed to be
1148 * delayed until after the root and real-time bitmap inodes
1149 * were consistently read in.
1150 */
Christoph Hellwig42490232008-08-13 16:49:32 +10001151 error = xfs_log_mount_finish(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (error) {
1153 cmn_err(CE_WARN, "XFS: log mount finish failed");
Christoph Hellwigb93b6e42009-02-04 09:33:58 +01001154 goto out_rtunmount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156
1157 /*
1158 * Complete the quota initialisation, post-log-replay component.
1159 */
Christoph Hellwig42490232008-08-13 16:49:32 +10001160 error = XFS_QM_MOUNT(mp, quotamount, quotaflags);
Eric Sandeen0771fb42007-10-12 11:03:40 +10001161 if (error)
Christoph Hellwigb93b6e42009-02-04 09:33:58 +01001162 goto out_rtunmount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
David Chinner84e1e992007-06-18 16:50:27 +10001164 /*
1165 * Now we are mounted, reserve a small amount of unused space for
1166 * privileged transactions. This is needed so that transaction
1167 * space required for critical operations can dip into this pool
1168 * when at ENOSPC. This is needed for operations like create with
1169 * attr, unwritten extent conversion at ENOSPC, etc. Data allocations
1170 * are not allowed to use this reserved space.
1171 *
1172 * We default to 5% or 1024 fsbs of space reserved, whichever is smaller.
1173 * This may drive us straight to ENOSPC on mount, but that implies
David Chinner714082b2008-04-10 12:20:03 +10001174 * we were already there on the last unmount. Warn if this occurs.
David Chinner84e1e992007-06-18 16:50:27 +10001175 */
Christoph Hellwig39726be2007-06-18 17:57:45 +10001176 resblks = mp->m_sb.sb_dblocks;
1177 do_div(resblks, 20);
1178 resblks = min_t(__uint64_t, resblks, 1024);
David Chinner714082b2008-04-10 12:20:03 +10001179 error = xfs_reserve_blocks(mp, &resblks, NULL);
1180 if (error)
1181 cmn_err(CE_WARN, "XFS: Unable to allocate reserve blocks. "
1182 "Continuing without a reserve pool.");
David Chinner84e1e992007-06-18 16:50:27 +10001183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 return 0;
1185
Christoph Hellwigb93b6e42009-02-04 09:33:58 +01001186 out_rtunmount:
1187 xfs_rtunmount_inodes(mp);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001188 out_rele_rip:
Christoph Hellwig43355092008-03-27 18:01:08 +11001189 IRELE(rip);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001190 out_log_dealloc:
Christoph Hellwig21b699c2009-03-16 08:19:29 +01001191 xfs_log_unmount(mp);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001192 out_free_perag:
Christoph Hellwigff4f0382008-08-13 16:50:47 +10001193 xfs_free_perag(mp);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001194 out_remove_uuid:
1195 if (!(mp->m_flags & XFS_MOUNT_NOUUID))
Christoph Hellwigfa6adbe2008-05-20 11:30:33 +10001196 uuid_table_remove(&mp->m_sb.sb_uuid);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001197 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return error;
1199}
1200
1201/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 * This flushes out the inodes,dquots and the superblock, unmounts the
1203 * log and makes sure that incore structures are freed.
1204 */
Christoph Hellwig41b5c2e2008-08-13 16:49:57 +10001205void
1206xfs_unmountfs(
1207 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Christoph Hellwig41b5c2e2008-08-13 16:49:57 +10001209 __uint64_t resblks;
1210 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Christoph Hellwigf95099b2008-12-03 12:20:37 +01001212 /*
1213 * Release dquot that rootinode, rbmino and rsumino might be holding,
1214 * and release the quota inodes.
1215 */
1216 XFS_QM_UNMOUNT(mp);
1217
Christoph Hellwigb93b6e42009-02-04 09:33:58 +01001218 xfs_rtunmount_inodes(mp);
Christoph Hellwig77508ec2008-08-13 16:49:04 +10001219 IRELE(mp->m_rootip);
1220
David Chinner641c56f2007-06-18 16:50:17 +10001221 /*
1222 * We can potentially deadlock here if we have an inode cluster
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001223 * that has been freed has its buffer still pinned in memory because
David Chinner641c56f2007-06-18 16:50:17 +10001224 * the transaction is still sitting in a iclog. The stale inodes
1225 * on that buffer will have their flush locks held until the
1226 * transaction hits the disk and the callbacks run. the inode
1227 * flush takes the flush lock unconditionally and with nothing to
1228 * push out the iclog we will never get that unlocked. hence we
1229 * need to force the log first.
1230 */
1231 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
David Chinner1dc33182008-10-30 17:37:15 +11001232 xfs_reclaim_inodes(mp, 0, XFS_IFLUSH_ASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Nathan Scottee2a4f72006-01-11 15:33:36 +11001234 XFS_QM_DQPURGEALL(mp, XFS_QMOPT_QUOTALL | XFS_QMOPT_UMOUNTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Lachlan McIlroya357a122008-10-30 16:53:25 +11001236 if (mp->m_quotainfo)
1237 XFS_QM_DONE(mp);
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 /*
1240 * Flush out the log synchronously so that we know for sure
1241 * that nothing is pinned. This is important because bflush()
1242 * will skip pinned buffers.
1243 */
1244 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1245
1246 xfs_binval(mp->m_ddev_targp);
1247 if (mp->m_rtdev_targp) {
1248 xfs_binval(mp->m_rtdev_targp);
1249 }
1250
David Chinner84e1e992007-06-18 16:50:27 +10001251 /*
1252 * Unreserve any blocks we have so that when we unmount we don't account
1253 * the reserved free space as used. This is really only necessary for
1254 * lazy superblock counting because it trusts the incore superblock
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001255 * counters to be absolutely correct on clean unmount.
David Chinner84e1e992007-06-18 16:50:27 +10001256 *
1257 * We don't bother correcting this elsewhere for lazy superblock
1258 * counting because on mount of an unclean filesystem we reconstruct the
1259 * correct counter value and this is irrelevant.
1260 *
1261 * For non-lazy counter filesystems, this doesn't matter at all because
1262 * we only every apply deltas to the superblock and hence the incore
1263 * value does not matter....
1264 */
1265 resblks = 0;
David Chinner714082b2008-04-10 12:20:03 +10001266 error = xfs_reserve_blocks(mp, &resblks, NULL);
1267 if (error)
1268 cmn_err(CE_WARN, "XFS: Unable to free reserved block pool. "
1269 "Freespace may not be correct on next mount.");
1270
David Chinnere5720ee2008-04-10 12:21:18 +10001271 error = xfs_log_sbcount(mp, 1);
1272 if (error)
1273 cmn_err(CE_WARN, "XFS: Unable to update superblock counters. "
1274 "Freespace may not be correct on next mount.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 xfs_unmountfs_writesb(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 xfs_unmountfs_wait(mp); /* wait for async bufs */
Christoph Hellwig21b699c2009-03-16 08:19:29 +01001277 xfs_log_unmount_write(mp);
1278 xfs_log_unmount(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if ((mp->m_flags & XFS_MOUNT_NOUUID) == 0)
Christoph Hellwigfa6adbe2008-05-20 11:30:33 +10001281 uuid_table_remove(&mp->m_sb.sb_uuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Christoph Hellwig1550d0b2008-08-13 16:17:37 +10001283#if defined(DEBUG)
Christoph Hellwig0ce4cfd2007-08-30 17:20:53 +10001284 xfs_errortag_clearall(mp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285#endif
Christoph Hellwigff4f0382008-08-13 16:50:47 +10001286 xfs_free_perag(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001289STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290xfs_unmountfs_wait(xfs_mount_t *mp)
1291{
1292 if (mp->m_logdev_targp != mp->m_ddev_targp)
1293 xfs_wait_buftarg(mp->m_logdev_targp);
1294 if (mp->m_rtdev_targp)
1295 xfs_wait_buftarg(mp->m_rtdev_targp);
1296 xfs_wait_buftarg(mp->m_ddev_targp);
1297}
1298
1299int
David Chinner92821e22007-05-24 15:26:31 +10001300xfs_fs_writable(xfs_mount_t *mp)
1301{
Christoph Hellwigb267ce92007-08-30 17:21:30 +10001302 return !(xfs_test_for_freeze(mp) || XFS_FORCED_SHUTDOWN(mp) ||
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001303 (mp->m_flags & XFS_MOUNT_RDONLY));
David Chinner92821e22007-05-24 15:26:31 +10001304}
1305
1306/*
1307 * xfs_log_sbcount
1308 *
1309 * Called either periodically to keep the on disk superblock values
1310 * roughly up to date or from unmount to make sure the values are
1311 * correct on a clean unmount.
1312 *
1313 * Note this code can be called during the process of freezing, so
1314 * we may need to use the transaction allocator which does not not
1315 * block when the transaction subsystem is in its frozen state.
1316 */
1317int
1318xfs_log_sbcount(
1319 xfs_mount_t *mp,
1320 uint sync)
1321{
1322 xfs_trans_t *tp;
1323 int error;
1324
1325 if (!xfs_fs_writable(mp))
1326 return 0;
1327
Christoph Hellwigd4d90b52008-04-22 17:34:37 +10001328 xfs_icsb_sync_counters(mp, 0);
David Chinner92821e22007-05-24 15:26:31 +10001329
1330 /*
1331 * we don't need to do this if we are updating the superblock
1332 * counters on every modification.
1333 */
1334 if (!xfs_sb_version_haslazysbcount(&mp->m_sb))
1335 return 0;
1336
1337 tp = _xfs_trans_alloc(mp, XFS_TRANS_SB_COUNT);
1338 error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
1339 XFS_DEFAULT_LOG_COUNT);
1340 if (error) {
1341 xfs_trans_cancel(tp, 0);
1342 return error;
1343 }
1344
1345 xfs_mod_sb(tp, XFS_SB_IFREE | XFS_SB_ICOUNT | XFS_SB_FDBLOCKS);
1346 if (sync)
1347 xfs_trans_set_sync(tp);
David Chinnere5720ee2008-04-10 12:21:18 +10001348 error = xfs_trans_commit(tp, 0);
1349 return error;
David Chinner92821e22007-05-24 15:26:31 +10001350}
1351
1352int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353xfs_unmountfs_writesb(xfs_mount_t *mp)
1354{
1355 xfs_buf_t *sbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 int error = 0;
1357
1358 /*
1359 * skip superblock write if fs is read-only, or
1360 * if we are doing a forced umount.
1361 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001362 if (!((mp->m_flags & XFS_MOUNT_RDONLY) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 XFS_FORCED_SHUTDOWN(mp))) {
David Chinner8d280b92006-03-14 13:13:09 +11001364
David Chinner92821e22007-05-24 15:26:31 +10001365 sbp = xfs_getsb(mp, 0);
David Chinner8d280b92006-03-14 13:13:09 +11001366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 XFS_BUF_UNDONE(sbp);
1368 XFS_BUF_UNREAD(sbp);
1369 XFS_BUF_UNDELAYWRITE(sbp);
1370 XFS_BUF_WRITE(sbp);
1371 XFS_BUF_UNASYNC(sbp);
1372 ASSERT(XFS_BUF_TARGET(sbp) == mp->m_ddev_targp);
1373 xfsbdstrat(mp, sbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 error = xfs_iowait(sbp);
1375 if (error)
1376 xfs_ioerror_alert("xfs_unmountfs_writesb",
1377 mp, sbp, XFS_BUF_ADDR(sbp));
David Chinner92821e22007-05-24 15:26:31 +10001378 xfs_buf_relse(sbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001380 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
1382
1383/*
1384 * xfs_mod_sb() can be used to copy arbitrary changes to the
1385 * in-core superblock into the superblock buffer to be logged.
1386 * It does not provide the higher level of locking that is
1387 * needed to protect the in-core superblock from concurrent
1388 * access.
1389 */
1390void
1391xfs_mod_sb(xfs_trans_t *tp, __int64_t fields)
1392{
1393 xfs_buf_t *bp;
1394 int first;
1395 int last;
1396 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 xfs_sb_field_t f;
1398
1399 ASSERT(fields);
1400 if (!fields)
1401 return;
1402 mp = tp->t_mountp;
1403 bp = xfs_trans_getsb(tp, mp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 first = sizeof(xfs_sb_t);
1405 last = 0;
1406
1407 /* translate/copy */
1408
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +10001409 xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb, fields);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 /* find modified range */
1412
1413 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
1414 ASSERT((1LL << f) & XFS_SB_MOD_BITS);
1415 first = xfs_sb_info[f].offset;
1416
1417 f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields);
1418 ASSERT((1LL << f) & XFS_SB_MOD_BITS);
1419 last = xfs_sb_info[f + 1].offset - 1;
1420
1421 xfs_trans_log_buf(tp, bp, first, last);
1422}
Yingping Lud210a282006-06-09 14:55:18 +10001423
Yingping Lud210a282006-06-09 14:55:18 +10001424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425/*
1426 * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply
1427 * a delta to a specified field in the in-core superblock. Simply
1428 * switch on the field indicated and apply the delta to that field.
1429 * Fields are not allowed to dip below zero, so if the delta would
1430 * do this do not apply it and return EINVAL.
1431 *
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001432 * The m_sb_lock must be held when this routine is called.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 */
David Chinner8d280b92006-03-14 13:13:09 +11001434int
David Chinner20f4ebf2007-02-10 18:36:10 +11001435xfs_mod_incore_sb_unlocked(
1436 xfs_mount_t *mp,
1437 xfs_sb_field_t field,
1438 int64_t delta,
1439 int rsvd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
1441 int scounter; /* short counter for 32 bit fields */
1442 long long lcounter; /* long counter for 64 bit fields */
1443 long long res_used, rem;
1444
1445 /*
1446 * With the in-core superblock spin lock held, switch
1447 * on the indicated field. Apply the delta to the
1448 * proper field. If the fields value would dip below
1449 * 0, then do not apply the delta and return EINVAL.
1450 */
1451 switch (field) {
1452 case XFS_SBS_ICOUNT:
1453 lcounter = (long long)mp->m_sb.sb_icount;
1454 lcounter += delta;
1455 if (lcounter < 0) {
1456 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001457 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
1459 mp->m_sb.sb_icount = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001460 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 case XFS_SBS_IFREE:
1462 lcounter = (long long)mp->m_sb.sb_ifree;
1463 lcounter += delta;
1464 if (lcounter < 0) {
1465 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001466 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 }
1468 mp->m_sb.sb_ifree = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001469 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 case XFS_SBS_FDBLOCKS:
David Chinner4be536d2006-09-07 14:26:50 +10001471 lcounter = (long long)
1472 mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
1474
1475 if (delta > 0) { /* Putting blocks back */
1476 if (res_used > delta) {
1477 mp->m_resblks_avail += delta;
1478 } else {
1479 rem = delta - res_used;
1480 mp->m_resblks_avail = mp->m_resblks;
1481 lcounter += rem;
1482 }
1483 } else { /* Taking blocks away */
1484
1485 lcounter += delta;
1486
1487 /*
1488 * If were out of blocks, use any available reserved blocks if
1489 * were allowed to.
1490 */
1491
1492 if (lcounter < 0) {
1493 if (rsvd) {
1494 lcounter = (long long)mp->m_resblks_avail + delta;
1495 if (lcounter < 0) {
Jesper Juhl014c2542006-01-15 02:37:08 +01001496 return XFS_ERROR(ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 }
1498 mp->m_resblks_avail = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001499 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 } else { /* not reserved */
Jesper Juhl014c2542006-01-15 02:37:08 +01001501 return XFS_ERROR(ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
1503 }
1504 }
1505
David Chinner4be536d2006-09-07 14:26:50 +10001506 mp->m_sb.sb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
Jesper Juhl014c2542006-01-15 02:37:08 +01001507 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 case XFS_SBS_FREXTENTS:
1509 lcounter = (long long)mp->m_sb.sb_frextents;
1510 lcounter += delta;
1511 if (lcounter < 0) {
Jesper Juhl014c2542006-01-15 02:37:08 +01001512 return XFS_ERROR(ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
1514 mp->m_sb.sb_frextents = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001515 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 case XFS_SBS_DBLOCKS:
1517 lcounter = (long long)mp->m_sb.sb_dblocks;
1518 lcounter += delta;
1519 if (lcounter < 0) {
1520 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001521 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
1523 mp->m_sb.sb_dblocks = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001524 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 case XFS_SBS_AGCOUNT:
1526 scounter = mp->m_sb.sb_agcount;
1527 scounter += delta;
1528 if (scounter < 0) {
1529 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001530 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
1532 mp->m_sb.sb_agcount = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001533 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 case XFS_SBS_IMAX_PCT:
1535 scounter = mp->m_sb.sb_imax_pct;
1536 scounter += delta;
1537 if (scounter < 0) {
1538 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001539 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 }
1541 mp->m_sb.sb_imax_pct = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 case XFS_SBS_REXTSIZE:
1544 scounter = mp->m_sb.sb_rextsize;
1545 scounter += delta;
1546 if (scounter < 0) {
1547 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001548 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
1550 mp->m_sb.sb_rextsize = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001551 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 case XFS_SBS_RBMBLOCKS:
1553 scounter = mp->m_sb.sb_rbmblocks;
1554 scounter += delta;
1555 if (scounter < 0) {
1556 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001557 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
1559 mp->m_sb.sb_rbmblocks = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001560 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 case XFS_SBS_RBLOCKS:
1562 lcounter = (long long)mp->m_sb.sb_rblocks;
1563 lcounter += delta;
1564 if (lcounter < 0) {
1565 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001566 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
1568 mp->m_sb.sb_rblocks = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001569 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 case XFS_SBS_REXTENTS:
1571 lcounter = (long long)mp->m_sb.sb_rextents;
1572 lcounter += delta;
1573 if (lcounter < 0) {
1574 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001575 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 }
1577 mp->m_sb.sb_rextents = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001578 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 case XFS_SBS_REXTSLOG:
1580 scounter = mp->m_sb.sb_rextslog;
1581 scounter += delta;
1582 if (scounter < 0) {
1583 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001584 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
1586 mp->m_sb.sb_rextslog = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001587 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 default:
1589 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001590 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 }
1592}
1593
1594/*
1595 * xfs_mod_incore_sb() is used to change a field in the in-core
1596 * superblock structure by the specified delta. This modification
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001597 * is protected by the m_sb_lock. Just use the xfs_mod_incore_sb_unlocked()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 * routine to do the work.
1599 */
1600int
David Chinner20f4ebf2007-02-10 18:36:10 +11001601xfs_mod_incore_sb(
1602 xfs_mount_t *mp,
1603 xfs_sb_field_t field,
1604 int64_t delta,
1605 int rsvd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 int status;
1608
David Chinner8d280b92006-03-14 13:13:09 +11001609 /* check for per-cpu counters */
1610 switch (field) {
1611#ifdef HAVE_PERCPU_SB
1612 case XFS_SBS_ICOUNT:
1613 case XFS_SBS_IFREE:
1614 case XFS_SBS_FDBLOCKS:
1615 if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
1616 status = xfs_icsb_modify_counters(mp, field,
1617 delta, rsvd);
1618 break;
1619 }
1620 /* FALLTHROUGH */
1621#endif
1622 default:
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001623 spin_lock(&mp->m_sb_lock);
David Chinner8d280b92006-03-14 13:13:09 +11001624 status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001625 spin_unlock(&mp->m_sb_lock);
David Chinner8d280b92006-03-14 13:13:09 +11001626 break;
1627 }
1628
Jesper Juhl014c2542006-01-15 02:37:08 +01001629 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630}
1631
1632/*
1633 * xfs_mod_incore_sb_batch() is used to change more than one field
1634 * in the in-core superblock structure at a time. This modification
1635 * is protected by a lock internal to this module. The fields and
1636 * changes to those fields are specified in the array of xfs_mod_sb
1637 * structures passed in.
1638 *
1639 * Either all of the specified deltas will be applied or none of
1640 * them will. If any modified field dips below 0, then all modifications
1641 * will be backed out and EINVAL will be returned.
1642 */
1643int
1644xfs_mod_incore_sb_batch(xfs_mount_t *mp, xfs_mod_sb_t *msb, uint nmsb, int rsvd)
1645{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 int status=0;
1647 xfs_mod_sb_t *msbp;
1648
1649 /*
1650 * Loop through the array of mod structures and apply each
1651 * individually. If any fail, then back out all those
1652 * which have already been applied. Do all of this within
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001653 * the scope of the m_sb_lock so that all of the changes will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 * be atomic.
1655 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001656 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 msbp = &msb[0];
1658 for (msbp = &msbp[0]; msbp < (msb + nmsb); msbp++) {
1659 /*
1660 * Apply the delta at index n. If it fails, break
1661 * from the loop so we'll fall into the undo loop
1662 * below.
1663 */
David Chinner8d280b92006-03-14 13:13:09 +11001664 switch (msbp->msb_field) {
1665#ifdef HAVE_PERCPU_SB
1666 case XFS_SBS_ICOUNT:
1667 case XFS_SBS_IFREE:
1668 case XFS_SBS_FDBLOCKS:
1669 if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001670 spin_unlock(&mp->m_sb_lock);
David Chinner20b64282007-02-10 18:35:09 +11001671 status = xfs_icsb_modify_counters(mp,
David Chinner8d280b92006-03-14 13:13:09 +11001672 msbp->msb_field,
1673 msbp->msb_delta, rsvd);
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001674 spin_lock(&mp->m_sb_lock);
David Chinner8d280b92006-03-14 13:13:09 +11001675 break;
1676 }
1677 /* FALLTHROUGH */
1678#endif
1679 default:
1680 status = xfs_mod_incore_sb_unlocked(mp,
1681 msbp->msb_field,
1682 msbp->msb_delta, rsvd);
1683 break;
1684 }
1685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 if (status != 0) {
1687 break;
1688 }
1689 }
1690
1691 /*
1692 * If we didn't complete the loop above, then back out
1693 * any changes made to the superblock. If you add code
1694 * between the loop above and here, make sure that you
1695 * preserve the value of status. Loop back until
1696 * we step below the beginning of the array. Make sure
1697 * we don't touch anything back there.
1698 */
1699 if (status != 0) {
1700 msbp--;
1701 while (msbp >= msb) {
David Chinner8d280b92006-03-14 13:13:09 +11001702 switch (msbp->msb_field) {
1703#ifdef HAVE_PERCPU_SB
1704 case XFS_SBS_ICOUNT:
1705 case XFS_SBS_IFREE:
1706 case XFS_SBS_FDBLOCKS:
1707 if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001708 spin_unlock(&mp->m_sb_lock);
David Chinner20b64282007-02-10 18:35:09 +11001709 status = xfs_icsb_modify_counters(mp,
David Chinner8d280b92006-03-14 13:13:09 +11001710 msbp->msb_field,
1711 -(msbp->msb_delta),
1712 rsvd);
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001713 spin_lock(&mp->m_sb_lock);
David Chinner8d280b92006-03-14 13:13:09 +11001714 break;
1715 }
1716 /* FALLTHROUGH */
1717#endif
1718 default:
1719 status = xfs_mod_incore_sb_unlocked(mp,
1720 msbp->msb_field,
1721 -(msbp->msb_delta),
1722 rsvd);
1723 break;
1724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 ASSERT(status == 0);
1726 msbp--;
1727 }
1728 }
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001729 spin_unlock(&mp->m_sb_lock);
Jesper Juhl014c2542006-01-15 02:37:08 +01001730 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
1733/*
1734 * xfs_getsb() is called to obtain the buffer for the superblock.
1735 * The buffer is returned locked and read in from disk.
1736 * The buffer should be released with a call to xfs_brelse().
1737 *
1738 * If the flags parameter is BUF_TRYLOCK, then we'll only return
1739 * the superblock buffer if it can be locked without sleeping.
1740 * If it can't then we'll return NULL.
1741 */
1742xfs_buf_t *
1743xfs_getsb(
1744 xfs_mount_t *mp,
1745 int flags)
1746{
1747 xfs_buf_t *bp;
1748
1749 ASSERT(mp->m_sb_bp != NULL);
1750 bp = mp->m_sb_bp;
1751 if (flags & XFS_BUF_TRYLOCK) {
1752 if (!XFS_BUF_CPSEMA(bp)) {
1753 return NULL;
1754 }
1755 } else {
1756 XFS_BUF_PSEMA(bp, PRIBIO);
1757 }
1758 XFS_BUF_HOLD(bp);
1759 ASSERT(XFS_BUF_ISDONE(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001760 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761}
1762
1763/*
1764 * Used to free the superblock along various error paths.
1765 */
1766void
1767xfs_freesb(
1768 xfs_mount_t *mp)
1769{
1770 xfs_buf_t *bp;
1771
1772 /*
1773 * Use xfs_getsb() so that the buffer will be locked
1774 * when we call xfs_buf_relse().
1775 */
1776 bp = xfs_getsb(mp, 0);
1777 XFS_BUF_UNMANAGE(bp);
1778 xfs_buf_relse(bp);
1779 mp->m_sb_bp = NULL;
1780}
1781
1782/*
1783 * See if the UUID is unique among mounted XFS filesystems.
1784 * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
1785 */
1786STATIC int
1787xfs_uuid_mount(
1788 xfs_mount_t *mp)
1789{
1790 if (uuid_is_nil(&mp->m_sb.sb_uuid)) {
1791 cmn_err(CE_WARN,
1792 "XFS: Filesystem %s has nil UUID - can't mount",
1793 mp->m_fsname);
1794 return -1;
1795 }
1796 if (!uuid_table_insert(&mp->m_sb.sb_uuid)) {
1797 cmn_err(CE_WARN,
1798 "XFS: Filesystem %s has duplicate UUID - can't mount",
1799 mp->m_fsname);
1800 return -1;
1801 }
1802 return 0;
1803}
1804
1805/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 * Used to log changes to the superblock unit and width fields which could
Eric Sandeene6957ea2008-04-10 12:19:34 +10001807 * be altered by the mount options, as well as any potential sb_features2
1808 * fixup. Only the first superblock is updated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 */
Christoph Hellwig7884bc82009-01-19 02:04:07 +01001810int
David Chinneree1c0902008-03-06 13:45:50 +11001811xfs_mount_log_sb(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 xfs_mount_t *mp,
1813 __int64_t fields)
1814{
1815 xfs_trans_t *tp;
David Chinnere5720ee2008-04-10 12:21:18 +10001816 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
David Chinneree1c0902008-03-06 13:45:50 +11001818 ASSERT(fields & (XFS_SB_UNIT | XFS_SB_WIDTH | XFS_SB_UUID |
David Chinner4b166de2008-05-20 11:30:27 +10001819 XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2 |
1820 XFS_SB_VERSIONNUM));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
1822 tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT);
David Chinnere5720ee2008-04-10 12:21:18 +10001823 error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
1824 XFS_DEFAULT_LOG_COUNT);
1825 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 xfs_trans_cancel(tp, 0);
David Chinnere5720ee2008-04-10 12:21:18 +10001827 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 }
1829 xfs_mod_sb(tp, fields);
David Chinnere5720ee2008-04-10 12:21:18 +10001830 error = xfs_trans_commit(tp, 0);
1831 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832}
David Chinner8d280b92006-03-14 13:13:09 +11001833
1834
1835#ifdef HAVE_PERCPU_SB
1836/*
1837 * Per-cpu incore superblock counters
1838 *
1839 * Simple concept, difficult implementation
1840 *
1841 * Basically, replace the incore superblock counters with a distributed per cpu
1842 * counter for contended fields (e.g. free block count).
1843 *
1844 * Difficulties arise in that the incore sb is used for ENOSPC checking, and
1845 * hence needs to be accurately read when we are running low on space. Hence
1846 * there is a method to enable and disable the per-cpu counters based on how
1847 * much "stuff" is available in them.
1848 *
1849 * Basically, a counter is enabled if there is enough free resource to justify
1850 * running a per-cpu fast-path. If the per-cpu counter runs out (i.e. a local
1851 * ENOSPC), then we disable the counters to synchronise all callers and
1852 * re-distribute the available resources.
1853 *
1854 * If, once we redistributed the available resources, we still get a failure,
1855 * we disable the per-cpu counter and go through the slow path.
1856 *
1857 * The slow path is the current xfs_mod_incore_sb() function. This means that
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001858 * when we disable a per-cpu counter, we need to drain its resources back to
David Chinner8d280b92006-03-14 13:13:09 +11001859 * the global superblock. We do this after disabling the counter to prevent
1860 * more threads from queueing up on the counter.
1861 *
1862 * Essentially, this means that we still need a lock in the fast path to enable
1863 * synchronisation between the global counters and the per-cpu counters. This
1864 * is not a problem because the lock will be local to a CPU almost all the time
1865 * and have little contention except when we get to ENOSPC conditions.
1866 *
1867 * Basically, this lock becomes a barrier that enables us to lock out the fast
1868 * path while we do things like enabling and disabling counters and
1869 * synchronising the counters.
1870 *
1871 * Locking rules:
1872 *
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001873 * 1. m_sb_lock before picking up per-cpu locks
David Chinner8d280b92006-03-14 13:13:09 +11001874 * 2. per-cpu locks always picked up via for_each_online_cpu() order
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001875 * 3. accurate counter sync requires m_sb_lock + per cpu locks
David Chinner8d280b92006-03-14 13:13:09 +11001876 * 4. modifying per-cpu counters requires holding per-cpu lock
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001877 * 5. modifying global counters requires holding m_sb_lock
1878 * 6. enabling or disabling a counter requires holding the m_sb_lock
David Chinner8d280b92006-03-14 13:13:09 +11001879 * and _none_ of the per-cpu locks.
1880 *
1881 * Disabled counters are only ever re-enabled by a balance operation
1882 * that results in more free resources per CPU than a given threshold.
1883 * To ensure counters don't remain disabled, they are rebalanced when
1884 * the global resource goes above a higher threshold (i.e. some hysteresis
1885 * is present to prevent thrashing).
David Chinner8d280b92006-03-14 13:13:09 +11001886 */
David Chinnere8234a62006-03-14 13:23:52 +11001887
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07001888#ifdef CONFIG_HOTPLUG_CPU
David Chinnere8234a62006-03-14 13:23:52 +11001889/*
1890 * hot-plug CPU notifier support.
1891 *
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07001892 * We need a notifier per filesystem as we need to be able to identify
1893 * the filesystem to balance the counters out. This is achieved by
1894 * having a notifier block embedded in the xfs_mount_t and doing pointer
1895 * magic to get the mount pointer from the notifier block address.
David Chinnere8234a62006-03-14 13:23:52 +11001896 */
1897STATIC int
1898xfs_icsb_cpu_notify(
1899 struct notifier_block *nfb,
1900 unsigned long action,
1901 void *hcpu)
1902{
1903 xfs_icsb_cnts_t *cntp;
1904 xfs_mount_t *mp;
David Chinnere8234a62006-03-14 13:23:52 +11001905
1906 mp = (xfs_mount_t *)container_of(nfb, xfs_mount_t, m_icsb_notifier);
1907 cntp = (xfs_icsb_cnts_t *)
1908 per_cpu_ptr(mp->m_sb_cnts, (unsigned long)hcpu);
1909 switch (action) {
1910 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001911 case CPU_UP_PREPARE_FROZEN:
David Chinnere8234a62006-03-14 13:23:52 +11001912 /* Easy Case - initialize the area and locks, and
1913 * then rebalance when online does everything else for us. */
David Chinner01e1b692006-03-14 13:29:16 +11001914 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
David Chinnere8234a62006-03-14 13:23:52 +11001915 break;
1916 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001917 case CPU_ONLINE_FROZEN:
David Chinner03135cf2007-02-10 18:35:15 +11001918 xfs_icsb_lock(mp);
Christoph Hellwig45af6c62008-04-22 17:34:44 +10001919 xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
1920 xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
1921 xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
David Chinner03135cf2007-02-10 18:35:15 +11001922 xfs_icsb_unlock(mp);
David Chinnere8234a62006-03-14 13:23:52 +11001923 break;
1924 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001925 case CPU_DEAD_FROZEN:
David Chinnere8234a62006-03-14 13:23:52 +11001926 /* Disable all the counters, then fold the dead cpu's
1927 * count into the total on the global superblock and
1928 * re-enable the counters. */
David Chinner03135cf2007-02-10 18:35:15 +11001929 xfs_icsb_lock(mp);
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001930 spin_lock(&mp->m_sb_lock);
David Chinnere8234a62006-03-14 13:23:52 +11001931 xfs_icsb_disable_counter(mp, XFS_SBS_ICOUNT);
1932 xfs_icsb_disable_counter(mp, XFS_SBS_IFREE);
1933 xfs_icsb_disable_counter(mp, XFS_SBS_FDBLOCKS);
1934
1935 mp->m_sb.sb_icount += cntp->icsb_icount;
1936 mp->m_sb.sb_ifree += cntp->icsb_ifree;
1937 mp->m_sb.sb_fdblocks += cntp->icsb_fdblocks;
1938
David Chinner01e1b692006-03-14 13:29:16 +11001939 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
David Chinnere8234a62006-03-14 13:23:52 +11001940
Christoph Hellwig45af6c62008-04-22 17:34:44 +10001941 xfs_icsb_balance_counter_locked(mp, XFS_SBS_ICOUNT, 0);
1942 xfs_icsb_balance_counter_locked(mp, XFS_SBS_IFREE, 0);
1943 xfs_icsb_balance_counter_locked(mp, XFS_SBS_FDBLOCKS, 0);
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001944 spin_unlock(&mp->m_sb_lock);
David Chinner03135cf2007-02-10 18:35:15 +11001945 xfs_icsb_unlock(mp);
David Chinnere8234a62006-03-14 13:23:52 +11001946 break;
1947 }
1948
1949 return NOTIFY_OK;
1950}
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07001951#endif /* CONFIG_HOTPLUG_CPU */
David Chinnere8234a62006-03-14 13:23:52 +11001952
David Chinner8d280b92006-03-14 13:13:09 +11001953int
1954xfs_icsb_init_counters(
1955 xfs_mount_t *mp)
1956{
1957 xfs_icsb_cnts_t *cntp;
1958 int i;
1959
1960 mp->m_sb_cnts = alloc_percpu(xfs_icsb_cnts_t);
1961 if (mp->m_sb_cnts == NULL)
1962 return -ENOMEM;
1963
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07001964#ifdef CONFIG_HOTPLUG_CPU
David Chinnere8234a62006-03-14 13:23:52 +11001965 mp->m_icsb_notifier.notifier_call = xfs_icsb_cpu_notify;
1966 mp->m_icsb_notifier.priority = 0;
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07001967 register_hotcpu_notifier(&mp->m_icsb_notifier);
1968#endif /* CONFIG_HOTPLUG_CPU */
David Chinnere8234a62006-03-14 13:23:52 +11001969
David Chinner8d280b92006-03-14 13:13:09 +11001970 for_each_online_cpu(i) {
1971 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
David Chinner01e1b692006-03-14 13:29:16 +11001972 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
David Chinner8d280b92006-03-14 13:13:09 +11001973 }
David Chinner20b64282007-02-10 18:35:09 +11001974
1975 mutex_init(&mp->m_icsb_mutex);
1976
David Chinner8d280b92006-03-14 13:13:09 +11001977 /*
1978 * start with all counters disabled so that the
1979 * initial balance kicks us off correctly
1980 */
1981 mp->m_icsb_counters = -1;
1982 return 0;
1983}
1984
Lachlan McIlroy5478eea2007-02-10 18:36:29 +11001985void
1986xfs_icsb_reinit_counters(
1987 xfs_mount_t *mp)
1988{
1989 xfs_icsb_lock(mp);
1990 /*
1991 * start with all counters disabled so that the
1992 * initial balance kicks us off correctly
1993 */
1994 mp->m_icsb_counters = -1;
Christoph Hellwig45af6c62008-04-22 17:34:44 +10001995 xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
1996 xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
1997 xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
Lachlan McIlroy5478eea2007-02-10 18:36:29 +11001998 xfs_icsb_unlock(mp);
1999}
2000
Christoph Hellwigc962fb72008-05-20 15:10:52 +10002001void
David Chinner8d280b92006-03-14 13:13:09 +11002002xfs_icsb_destroy_counters(
2003 xfs_mount_t *mp)
2004{
David Chinnere8234a62006-03-14 13:23:52 +11002005 if (mp->m_sb_cnts) {
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07002006 unregister_hotcpu_notifier(&mp->m_icsb_notifier);
David Chinner8d280b92006-03-14 13:13:09 +11002007 free_percpu(mp->m_sb_cnts);
David Chinnere8234a62006-03-14 13:23:52 +11002008 }
David Chinner03135cf2007-02-10 18:35:15 +11002009 mutex_destroy(&mp->m_icsb_mutex);
David Chinner8d280b92006-03-14 13:13:09 +11002010}
2011
David Chinner7989cb82007-02-10 18:34:56 +11002012STATIC_INLINE void
David Chinner01e1b692006-03-14 13:29:16 +11002013xfs_icsb_lock_cntr(
2014 xfs_icsb_cnts_t *icsbp)
2015{
2016 while (test_and_set_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags)) {
2017 ndelay(1000);
2018 }
2019}
2020
David Chinner7989cb82007-02-10 18:34:56 +11002021STATIC_INLINE void
David Chinner01e1b692006-03-14 13:29:16 +11002022xfs_icsb_unlock_cntr(
2023 xfs_icsb_cnts_t *icsbp)
2024{
2025 clear_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags);
2026}
2027
David Chinner8d280b92006-03-14 13:13:09 +11002028
David Chinner7989cb82007-02-10 18:34:56 +11002029STATIC_INLINE void
David Chinner8d280b92006-03-14 13:13:09 +11002030xfs_icsb_lock_all_counters(
2031 xfs_mount_t *mp)
2032{
2033 xfs_icsb_cnts_t *cntp;
2034 int i;
2035
2036 for_each_online_cpu(i) {
2037 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
David Chinner01e1b692006-03-14 13:29:16 +11002038 xfs_icsb_lock_cntr(cntp);
David Chinner8d280b92006-03-14 13:13:09 +11002039 }
2040}
2041
David Chinner7989cb82007-02-10 18:34:56 +11002042STATIC_INLINE void
David Chinner8d280b92006-03-14 13:13:09 +11002043xfs_icsb_unlock_all_counters(
2044 xfs_mount_t *mp)
2045{
2046 xfs_icsb_cnts_t *cntp;
2047 int i;
2048
2049 for_each_online_cpu(i) {
2050 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
David Chinner01e1b692006-03-14 13:29:16 +11002051 xfs_icsb_unlock_cntr(cntp);
David Chinner8d280b92006-03-14 13:13:09 +11002052 }
2053}
2054
2055STATIC void
2056xfs_icsb_count(
2057 xfs_mount_t *mp,
2058 xfs_icsb_cnts_t *cnt,
2059 int flags)
2060{
2061 xfs_icsb_cnts_t *cntp;
2062 int i;
2063
2064 memset(cnt, 0, sizeof(xfs_icsb_cnts_t));
2065
2066 if (!(flags & XFS_ICSB_LAZY_COUNT))
2067 xfs_icsb_lock_all_counters(mp);
2068
2069 for_each_online_cpu(i) {
2070 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
2071 cnt->icsb_icount += cntp->icsb_icount;
2072 cnt->icsb_ifree += cntp->icsb_ifree;
2073 cnt->icsb_fdblocks += cntp->icsb_fdblocks;
2074 }
2075
2076 if (!(flags & XFS_ICSB_LAZY_COUNT))
2077 xfs_icsb_unlock_all_counters(mp);
2078}
2079
2080STATIC int
2081xfs_icsb_counter_disabled(
2082 xfs_mount_t *mp,
2083 xfs_sb_field_t field)
2084{
2085 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
2086 return test_bit(field, &mp->m_icsb_counters);
2087}
2088
David Chinner36fbe6e2008-04-10 12:19:56 +10002089STATIC void
David Chinner8d280b92006-03-14 13:13:09 +11002090xfs_icsb_disable_counter(
2091 xfs_mount_t *mp,
2092 xfs_sb_field_t field)
2093{
2094 xfs_icsb_cnts_t cnt;
2095
2096 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
2097
David Chinner20b64282007-02-10 18:35:09 +11002098 /*
2099 * If we are already disabled, then there is nothing to do
2100 * here. We check before locking all the counters to avoid
2101 * the expensive lock operation when being called in the
2102 * slow path and the counter is already disabled. This is
2103 * safe because the only time we set or clear this state is under
2104 * the m_icsb_mutex.
2105 */
2106 if (xfs_icsb_counter_disabled(mp, field))
David Chinner36fbe6e2008-04-10 12:19:56 +10002107 return;
David Chinner20b64282007-02-10 18:35:09 +11002108
David Chinner8d280b92006-03-14 13:13:09 +11002109 xfs_icsb_lock_all_counters(mp);
2110 if (!test_and_set_bit(field, &mp->m_icsb_counters)) {
2111 /* drain back to superblock */
2112
Christoph Hellwigce461932008-04-22 17:34:50 +10002113 xfs_icsb_count(mp, &cnt, XFS_ICSB_LAZY_COUNT);
David Chinner8d280b92006-03-14 13:13:09 +11002114 switch(field) {
2115 case XFS_SBS_ICOUNT:
2116 mp->m_sb.sb_icount = cnt.icsb_icount;
2117 break;
2118 case XFS_SBS_IFREE:
2119 mp->m_sb.sb_ifree = cnt.icsb_ifree;
2120 break;
2121 case XFS_SBS_FDBLOCKS:
2122 mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
2123 break;
2124 default:
2125 BUG();
2126 }
2127 }
2128
2129 xfs_icsb_unlock_all_counters(mp);
David Chinner8d280b92006-03-14 13:13:09 +11002130}
2131
2132STATIC void
2133xfs_icsb_enable_counter(
2134 xfs_mount_t *mp,
2135 xfs_sb_field_t field,
2136 uint64_t count,
2137 uint64_t resid)
2138{
2139 xfs_icsb_cnts_t *cntp;
2140 int i;
2141
2142 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
2143
2144 xfs_icsb_lock_all_counters(mp);
2145 for_each_online_cpu(i) {
2146 cntp = per_cpu_ptr(mp->m_sb_cnts, i);
2147 switch (field) {
2148 case XFS_SBS_ICOUNT:
2149 cntp->icsb_icount = count + resid;
2150 break;
2151 case XFS_SBS_IFREE:
2152 cntp->icsb_ifree = count + resid;
2153 break;
2154 case XFS_SBS_FDBLOCKS:
2155 cntp->icsb_fdblocks = count + resid;
2156 break;
2157 default:
2158 BUG();
2159 break;
2160 }
2161 resid = 0;
2162 }
2163 clear_bit(field, &mp->m_icsb_counters);
2164 xfs_icsb_unlock_all_counters(mp);
2165}
2166
David Chinnerdbcabad2007-02-10 18:36:17 +11002167void
Christoph Hellwigd4d90b52008-04-22 17:34:37 +10002168xfs_icsb_sync_counters_locked(
David Chinner8d280b92006-03-14 13:13:09 +11002169 xfs_mount_t *mp,
2170 int flags)
2171{
2172 xfs_icsb_cnts_t cnt;
David Chinner8d280b92006-03-14 13:13:09 +11002173
David Chinner8d280b92006-03-14 13:13:09 +11002174 xfs_icsb_count(mp, &cnt, flags);
2175
David Chinner8d280b92006-03-14 13:13:09 +11002176 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_ICOUNT))
2177 mp->m_sb.sb_icount = cnt.icsb_icount;
2178 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_IFREE))
2179 mp->m_sb.sb_ifree = cnt.icsb_ifree;
2180 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_FDBLOCKS))
2181 mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
David Chinner8d280b92006-03-14 13:13:09 +11002182}
2183
2184/*
2185 * Accurate update of per-cpu counters to incore superblock
2186 */
Christoph Hellwigd4d90b52008-04-22 17:34:37 +10002187void
David Chinner8d280b92006-03-14 13:13:09 +11002188xfs_icsb_sync_counters(
Christoph Hellwigd4d90b52008-04-22 17:34:37 +10002189 xfs_mount_t *mp,
2190 int flags)
David Chinner8d280b92006-03-14 13:13:09 +11002191{
Christoph Hellwigd4d90b52008-04-22 17:34:37 +10002192 spin_lock(&mp->m_sb_lock);
2193 xfs_icsb_sync_counters_locked(mp, flags);
2194 spin_unlock(&mp->m_sb_lock);
David Chinner8d280b92006-03-14 13:13:09 +11002195}
2196
2197/*
2198 * Balance and enable/disable counters as necessary.
2199 *
David Chinner20b64282007-02-10 18:35:09 +11002200 * Thresholds for re-enabling counters are somewhat magic. inode counts are
2201 * chosen to be the same number as single on disk allocation chunk per CPU, and
2202 * free blocks is something far enough zero that we aren't going thrash when we
2203 * get near ENOSPC. We also need to supply a minimum we require per cpu to
2204 * prevent looping endlessly when xfs_alloc_space asks for more than will
2205 * be distributed to a single CPU but each CPU has enough blocks to be
2206 * reenabled.
2207 *
2208 * Note that we can be called when counters are already disabled.
2209 * xfs_icsb_disable_counter() optimises the counter locking in this case to
2210 * prevent locking every per-cpu counter needlessly.
David Chinner8d280b92006-03-14 13:13:09 +11002211 */
David Chinner20b64282007-02-10 18:35:09 +11002212
2213#define XFS_ICSB_INO_CNTR_REENABLE (uint64_t)64
David Chinner4be536d2006-09-07 14:26:50 +10002214#define XFS_ICSB_FDBLK_CNTR_REENABLE(mp) \
David Chinner20b64282007-02-10 18:35:09 +11002215 (uint64_t)(512 + XFS_ALLOC_SET_ASIDE(mp))
David Chinner8d280b92006-03-14 13:13:09 +11002216STATIC void
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002217xfs_icsb_balance_counter_locked(
David Chinner8d280b92006-03-14 13:13:09 +11002218 xfs_mount_t *mp,
2219 xfs_sb_field_t field,
David Chinner20b64282007-02-10 18:35:09 +11002220 int min_per_cpu)
David Chinner8d280b92006-03-14 13:13:09 +11002221{
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10002222 uint64_t count, resid;
David Chinner8d280b92006-03-14 13:13:09 +11002223 int weight = num_online_cpus();
David Chinner20b64282007-02-10 18:35:09 +11002224 uint64_t min = (uint64_t)min_per_cpu;
David Chinner8d280b92006-03-14 13:13:09 +11002225
David Chinner8d280b92006-03-14 13:13:09 +11002226 /* disable counter and sync counter */
2227 xfs_icsb_disable_counter(mp, field);
2228
2229 /* update counters - first CPU gets residual*/
2230 switch (field) {
2231 case XFS_SBS_ICOUNT:
2232 count = mp->m_sb.sb_icount;
2233 resid = do_div(count, weight);
David Chinner20b64282007-02-10 18:35:09 +11002234 if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002235 return;
David Chinner8d280b92006-03-14 13:13:09 +11002236 break;
2237 case XFS_SBS_IFREE:
2238 count = mp->m_sb.sb_ifree;
2239 resid = do_div(count, weight);
David Chinner20b64282007-02-10 18:35:09 +11002240 if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002241 return;
David Chinner8d280b92006-03-14 13:13:09 +11002242 break;
2243 case XFS_SBS_FDBLOCKS:
2244 count = mp->m_sb.sb_fdblocks;
2245 resid = do_div(count, weight);
David Chinner20b64282007-02-10 18:35:09 +11002246 if (count < max(min, XFS_ICSB_FDBLK_CNTR_REENABLE(mp)))
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002247 return;
David Chinner8d280b92006-03-14 13:13:09 +11002248 break;
2249 default:
2250 BUG();
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10002251 count = resid = 0; /* quiet, gcc */
David Chinner8d280b92006-03-14 13:13:09 +11002252 break;
2253 }
2254
2255 xfs_icsb_enable_counter(mp, field, count, resid);
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002256}
2257
2258STATIC void
2259xfs_icsb_balance_counter(
2260 xfs_mount_t *mp,
2261 xfs_sb_field_t fields,
2262 int min_per_cpu)
2263{
2264 spin_lock(&mp->m_sb_lock);
2265 xfs_icsb_balance_counter_locked(mp, fields, min_per_cpu);
2266 spin_unlock(&mp->m_sb_lock);
David Chinner8d280b92006-03-14 13:13:09 +11002267}
2268
David Chinnera8272ce2007-11-23 16:28:09 +11002269STATIC int
David Chinner20b64282007-02-10 18:35:09 +11002270xfs_icsb_modify_counters(
David Chinner8d280b92006-03-14 13:13:09 +11002271 xfs_mount_t *mp,
2272 xfs_sb_field_t field,
David Chinner20f4ebf2007-02-10 18:36:10 +11002273 int64_t delta,
David Chinner20b64282007-02-10 18:35:09 +11002274 int rsvd)
David Chinner8d280b92006-03-14 13:13:09 +11002275{
2276 xfs_icsb_cnts_t *icsbp;
2277 long long lcounter; /* long counter for 64 bit fields */
Eric Sandeen007c61c2007-10-11 17:43:56 +10002278 int cpu, ret = 0;
David Chinner8d280b92006-03-14 13:13:09 +11002279
David Chinner20b64282007-02-10 18:35:09 +11002280 might_sleep();
David Chinner8d280b92006-03-14 13:13:09 +11002281again:
2282 cpu = get_cpu();
David Chinner20b64282007-02-10 18:35:09 +11002283 icsbp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, cpu);
2284
2285 /*
2286 * if the counter is disabled, go to slow path
2287 */
David Chinner8d280b92006-03-14 13:13:09 +11002288 if (unlikely(xfs_icsb_counter_disabled(mp, field)))
2289 goto slow_path;
David Chinner20b64282007-02-10 18:35:09 +11002290 xfs_icsb_lock_cntr(icsbp);
2291 if (unlikely(xfs_icsb_counter_disabled(mp, field))) {
2292 xfs_icsb_unlock_cntr(icsbp);
2293 goto slow_path;
2294 }
David Chinner8d280b92006-03-14 13:13:09 +11002295
2296 switch (field) {
2297 case XFS_SBS_ICOUNT:
2298 lcounter = icsbp->icsb_icount;
2299 lcounter += delta;
2300 if (unlikely(lcounter < 0))
David Chinner20b64282007-02-10 18:35:09 +11002301 goto balance_counter;
David Chinner8d280b92006-03-14 13:13:09 +11002302 icsbp->icsb_icount = lcounter;
2303 break;
2304
2305 case XFS_SBS_IFREE:
2306 lcounter = icsbp->icsb_ifree;
2307 lcounter += delta;
2308 if (unlikely(lcounter < 0))
David Chinner20b64282007-02-10 18:35:09 +11002309 goto balance_counter;
David Chinner8d280b92006-03-14 13:13:09 +11002310 icsbp->icsb_ifree = lcounter;
2311 break;
2312
2313 case XFS_SBS_FDBLOCKS:
2314 BUG_ON((mp->m_resblks - mp->m_resblks_avail) != 0);
2315
David Chinner4be536d2006-09-07 14:26:50 +10002316 lcounter = icsbp->icsb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
David Chinner8d280b92006-03-14 13:13:09 +11002317 lcounter += delta;
2318 if (unlikely(lcounter < 0))
David Chinner20b64282007-02-10 18:35:09 +11002319 goto balance_counter;
David Chinner4be536d2006-09-07 14:26:50 +10002320 icsbp->icsb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
David Chinner8d280b92006-03-14 13:13:09 +11002321 break;
2322 default:
2323 BUG();
2324 break;
2325 }
David Chinner01e1b692006-03-14 13:29:16 +11002326 xfs_icsb_unlock_cntr(icsbp);
David Chinner8d280b92006-03-14 13:13:09 +11002327 put_cpu();
David Chinner8d280b92006-03-14 13:13:09 +11002328 return 0;
2329
David Chinner8d280b92006-03-14 13:13:09 +11002330slow_path:
David Chinner20b64282007-02-10 18:35:09 +11002331 put_cpu();
2332
2333 /*
2334 * serialise with a mutex so we don't burn lots of cpu on
2335 * the superblock lock. We still need to hold the superblock
2336 * lock, however, when we modify the global structures.
2337 */
David Chinner03135cf2007-02-10 18:35:15 +11002338 xfs_icsb_lock(mp);
David Chinner20b64282007-02-10 18:35:09 +11002339
2340 /*
2341 * Now running atomically.
2342 *
2343 * If the counter is enabled, someone has beaten us to rebalancing.
2344 * Drop the lock and try again in the fast path....
2345 */
2346 if (!(xfs_icsb_counter_disabled(mp, field))) {
David Chinner03135cf2007-02-10 18:35:15 +11002347 xfs_icsb_unlock(mp);
David Chinner20b64282007-02-10 18:35:09 +11002348 goto again;
2349 }
2350
2351 /*
2352 * The counter is currently disabled. Because we are
2353 * running atomically here, we know a rebalance cannot
2354 * be in progress. Hence we can go straight to operating
2355 * on the global superblock. We do not call xfs_mod_incore_sb()
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002356 * here even though we need to get the m_sb_lock. Doing so
David Chinner20b64282007-02-10 18:35:09 +11002357 * will cause us to re-enter this function and deadlock.
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002358 * Hence we get the m_sb_lock ourselves and then call
David Chinner20b64282007-02-10 18:35:09 +11002359 * xfs_mod_incore_sb_unlocked() as the unlocked path operates
2360 * directly on the global counters.
2361 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002362 spin_lock(&mp->m_sb_lock);
David Chinner20b64282007-02-10 18:35:09 +11002363 ret = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002364 spin_unlock(&mp->m_sb_lock);
David Chinner20b64282007-02-10 18:35:09 +11002365
2366 /*
2367 * Now that we've modified the global superblock, we
2368 * may be able to re-enable the distributed counters
2369 * (e.g. lots of space just got freed). After that
2370 * we are done.
2371 */
2372 if (ret != ENOSPC)
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002373 xfs_icsb_balance_counter(mp, field, 0);
David Chinner03135cf2007-02-10 18:35:15 +11002374 xfs_icsb_unlock(mp);
David Chinner20b64282007-02-10 18:35:09 +11002375 return ret;
2376
2377balance_counter:
David Chinner01e1b692006-03-14 13:29:16 +11002378 xfs_icsb_unlock_cntr(icsbp);
David Chinner8d280b92006-03-14 13:13:09 +11002379 put_cpu();
2380
David Chinner20b64282007-02-10 18:35:09 +11002381 /*
2382 * We may have multiple threads here if multiple per-cpu
2383 * counters run dry at the same time. This will mean we can
2384 * do more balances than strictly necessary but it is not
2385 * the common slowpath case.
2386 */
David Chinner03135cf2007-02-10 18:35:15 +11002387 xfs_icsb_lock(mp);
David Chinner8d280b92006-03-14 13:13:09 +11002388
David Chinner20b64282007-02-10 18:35:09 +11002389 /*
2390 * running atomically.
2391 *
2392 * This will leave the counter in the correct state for future
2393 * accesses. After the rebalance, we simply try again and our retry
2394 * will either succeed through the fast path or slow path without
2395 * another balance operation being required.
2396 */
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002397 xfs_icsb_balance_counter(mp, field, delta);
David Chinner03135cf2007-02-10 18:35:15 +11002398 xfs_icsb_unlock(mp);
David Chinner20b64282007-02-10 18:35:09 +11002399 goto again;
David Chinner8d280b92006-03-14 13:13:09 +11002400}
2401
David Chinner8d280b92006-03-14 13:13:09 +11002402#endif