blob: 00c7a876807df0f3c384fe1316f914572f9ee937 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_dinode.h"
33#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_btree.h"
35#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_alloc.h"
37#include "xfs_rtalloc.h"
38#include "xfs_bmap.h"
39#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_rw.h"
41#include "xfs_quota.h"
42#include "xfs_fsops.h"
Christoph Hellwig43355092008-03-27 18:01:08 +110043#include "xfs_utils.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000044#include "xfs_trace.h"
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100047STATIC void xfs_unmountfs_wait(xfs_mount_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
David Chinner8d280b92006-03-14 13:13:09 +110049
50#ifdef HAVE_PERCPU_SB
David Chinner20f4ebf2007-02-10 18:36:10 +110051STATIC void xfs_icsb_balance_counter(xfs_mount_t *, xfs_sb_field_t,
Christoph Hellwig45af6c62008-04-22 17:34:44 +100052 int);
53STATIC void xfs_icsb_balance_counter_locked(xfs_mount_t *, xfs_sb_field_t,
54 int);
David Chinner8d280b92006-03-14 13:13:09 +110055STATIC int xfs_icsb_modify_counters(xfs_mount_t *, xfs_sb_field_t,
David Chinner20f4ebf2007-02-10 18:36:10 +110056 int64_t, int);
David Chinner36fbe6e2008-04-10 12:19:56 +100057STATIC void xfs_icsb_disable_counter(xfs_mount_t *, xfs_sb_field_t);
David Chinner8d280b92006-03-14 13:13:09 +110058
59#else
60
Christoph Hellwig45af6c62008-04-22 17:34:44 +100061#define xfs_icsb_balance_counter(mp, a, b) do { } while (0)
62#define xfs_icsb_balance_counter_locked(mp, a, b) do { } while (0)
David Chinner8d280b92006-03-14 13:13:09 +110063#define xfs_icsb_modify_counters(mp, a, b, c) do { } while (0)
David Chinner8d280b92006-03-14 13:13:09 +110064
65#endif
66
Christoph Hellwig1df84c92006-01-11 15:29:52 +110067static const struct {
David Chinner8d280b92006-03-14 13:13:09 +110068 short offset;
69 short type; /* 0 = integer
70 * 1 = binary / string (no translation)
71 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072} xfs_sb_info[] = {
73 { offsetof(xfs_sb_t, sb_magicnum), 0 },
74 { offsetof(xfs_sb_t, sb_blocksize), 0 },
75 { offsetof(xfs_sb_t, sb_dblocks), 0 },
76 { offsetof(xfs_sb_t, sb_rblocks), 0 },
77 { offsetof(xfs_sb_t, sb_rextents), 0 },
78 { offsetof(xfs_sb_t, sb_uuid), 1 },
79 { offsetof(xfs_sb_t, sb_logstart), 0 },
80 { offsetof(xfs_sb_t, sb_rootino), 0 },
81 { offsetof(xfs_sb_t, sb_rbmino), 0 },
82 { offsetof(xfs_sb_t, sb_rsumino), 0 },
83 { offsetof(xfs_sb_t, sb_rextsize), 0 },
84 { offsetof(xfs_sb_t, sb_agblocks), 0 },
85 { offsetof(xfs_sb_t, sb_agcount), 0 },
86 { offsetof(xfs_sb_t, sb_rbmblocks), 0 },
87 { offsetof(xfs_sb_t, sb_logblocks), 0 },
88 { offsetof(xfs_sb_t, sb_versionnum), 0 },
89 { offsetof(xfs_sb_t, sb_sectsize), 0 },
90 { offsetof(xfs_sb_t, sb_inodesize), 0 },
91 { offsetof(xfs_sb_t, sb_inopblock), 0 },
92 { offsetof(xfs_sb_t, sb_fname[0]), 1 },
93 { offsetof(xfs_sb_t, sb_blocklog), 0 },
94 { offsetof(xfs_sb_t, sb_sectlog), 0 },
95 { offsetof(xfs_sb_t, sb_inodelog), 0 },
96 { offsetof(xfs_sb_t, sb_inopblog), 0 },
97 { offsetof(xfs_sb_t, sb_agblklog), 0 },
98 { offsetof(xfs_sb_t, sb_rextslog), 0 },
99 { offsetof(xfs_sb_t, sb_inprogress), 0 },
100 { offsetof(xfs_sb_t, sb_imax_pct), 0 },
101 { offsetof(xfs_sb_t, sb_icount), 0 },
102 { offsetof(xfs_sb_t, sb_ifree), 0 },
103 { offsetof(xfs_sb_t, sb_fdblocks), 0 },
104 { offsetof(xfs_sb_t, sb_frextents), 0 },
105 { offsetof(xfs_sb_t, sb_uquotino), 0 },
106 { offsetof(xfs_sb_t, sb_gquotino), 0 },
107 { offsetof(xfs_sb_t, sb_qflags), 0 },
108 { offsetof(xfs_sb_t, sb_flags), 0 },
109 { offsetof(xfs_sb_t, sb_shared_vn), 0 },
110 { offsetof(xfs_sb_t, sb_inoalignmt), 0 },
111 { offsetof(xfs_sb_t, sb_unit), 0 },
112 { offsetof(xfs_sb_t, sb_width), 0 },
113 { offsetof(xfs_sb_t, sb_dirblklog), 0 },
114 { offsetof(xfs_sb_t, sb_logsectlog), 0 },
115 { offsetof(xfs_sb_t, sb_logsectsize),0 },
116 { offsetof(xfs_sb_t, sb_logsunit), 0 },
117 { offsetof(xfs_sb_t, sb_features2), 0 },
David Chinneree1c0902008-03-06 13:45:50 +1100118 { offsetof(xfs_sb_t, sb_bad_features2), 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 { sizeof(xfs_sb_t), 0 }
120};
121
Christoph Hellwig27174202009-03-30 10:21:31 +0200122static DEFINE_MUTEX(xfs_uuid_table_mutex);
123static int xfs_uuid_table_size;
124static uuid_t *xfs_uuid_table;
125
126/*
127 * See if the UUID is unique among mounted XFS filesystems.
128 * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
129 */
130STATIC int
131xfs_uuid_mount(
132 struct xfs_mount *mp)
133{
134 uuid_t *uuid = &mp->m_sb.sb_uuid;
135 int hole, i;
136
137 if (mp->m_flags & XFS_MOUNT_NOUUID)
138 return 0;
139
140 if (uuid_is_nil(uuid)) {
141 cmn_err(CE_WARN,
142 "XFS: Filesystem %s has nil UUID - can't mount",
143 mp->m_fsname);
144 return XFS_ERROR(EINVAL);
145 }
146
147 mutex_lock(&xfs_uuid_table_mutex);
148 for (i = 0, hole = -1; i < xfs_uuid_table_size; i++) {
149 if (uuid_is_nil(&xfs_uuid_table[i])) {
150 hole = i;
151 continue;
152 }
153 if (uuid_equal(uuid, &xfs_uuid_table[i]))
154 goto out_duplicate;
155 }
156
157 if (hole < 0) {
158 xfs_uuid_table = kmem_realloc(xfs_uuid_table,
159 (xfs_uuid_table_size + 1) * sizeof(*xfs_uuid_table),
160 xfs_uuid_table_size * sizeof(*xfs_uuid_table),
161 KM_SLEEP);
162 hole = xfs_uuid_table_size++;
163 }
164 xfs_uuid_table[hole] = *uuid;
165 mutex_unlock(&xfs_uuid_table_mutex);
166
167 return 0;
168
169 out_duplicate:
170 mutex_unlock(&xfs_uuid_table_mutex);
171 cmn_err(CE_WARN, "XFS: Filesystem %s has duplicate UUID - can't mount",
172 mp->m_fsname);
173 return XFS_ERROR(EINVAL);
174}
175
176STATIC void
177xfs_uuid_unmount(
178 struct xfs_mount *mp)
179{
180 uuid_t *uuid = &mp->m_sb.sb_uuid;
181 int i;
182
183 if (mp->m_flags & XFS_MOUNT_NOUUID)
184 return;
185
186 mutex_lock(&xfs_uuid_table_mutex);
187 for (i = 0; i < xfs_uuid_table_size; i++) {
188 if (uuid_is_nil(&xfs_uuid_table[i]))
189 continue;
190 if (!uuid_equal(uuid, &xfs_uuid_table[i]))
191 continue;
192 memset(&xfs_uuid_table[i], 0, sizeof(uuid_t));
193 break;
194 }
195 ASSERT(i < xfs_uuid_table_size);
196 mutex_unlock(&xfs_uuid_table_mutex);
197}
198
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/*
Dave Chinner0fa800f2010-01-11 11:47:46 +0000201 * Reference counting access wrappers to the perag structures.
202 */
203struct xfs_perag *
204xfs_perag_get(struct xfs_mount *mp, xfs_agnumber_t agno)
205{
206 struct xfs_perag *pag;
207 int ref = 0;
208
209 spin_lock(&mp->m_perag_lock);
210 pag = radix_tree_lookup(&mp->m_perag_tree, agno);
211 if (pag) {
212 ASSERT(atomic_read(&pag->pag_ref) >= 0);
Dave Chinner0fa800f2010-01-11 11:47:46 +0000213 ref = atomic_inc_return(&pag->pag_ref);
214 }
215 spin_unlock(&mp->m_perag_lock);
216 trace_xfs_perag_get(mp, agno, ref, _RET_IP_);
217 return pag;
218}
219
220void
221xfs_perag_put(struct xfs_perag *pag)
222{
223 int ref;
224
225 ASSERT(atomic_read(&pag->pag_ref) > 0);
226 ref = atomic_dec_return(&pag->pag_ref);
227 trace_xfs_perag_put(pag->pag_mount, pag->pag_agno, ref, _RET_IP_);
228}
229
230/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 * Free up the resources associated with a mount structure. Assume that
232 * the structure was initially zeroed, so we can tell which fields got
233 * initialized.
234 */
Christoph Hellwigc962fb72008-05-20 15:10:52 +1000235STATIC void
Christoph Hellwigff4f0382008-08-13 16:50:47 +1000236xfs_free_perag(
Christoph Hellwig745f6912007-08-30 17:20:39 +1000237 xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000239 xfs_agnumber_t agno;
240 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000242 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
243 spin_lock(&mp->m_perag_lock);
244 pag = radix_tree_delete(&mp->m_perag_tree, agno);
Dave Chinnere57336f2010-01-11 11:47:49 +0000245 ASSERT(pag);
Dave Chinneraed3bb92010-01-11 11:47:45 +0000246 ASSERT(atomic_read(&pag->pag_ref) == 0);
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000247 spin_unlock(&mp->m_perag_lock);
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000248 kmem_free(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
Nathan Scott4cc929e2007-05-14 18:24:02 +1000252/*
253 * Check size of device based on the (data/realtime) block count.
254 * Note: this check is used by the growfs code as well as mount.
255 */
256int
257xfs_sb_validate_fsb_count(
258 xfs_sb_t *sbp,
259 __uint64_t nblocks)
260{
261 ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
262 ASSERT(sbp->sb_blocklog >= BBSHIFT);
263
264#if XFS_BIG_BLKNOS /* Limited by ULONG_MAX of page cache index */
265 if (nblocks >> (PAGE_CACHE_SHIFT - sbp->sb_blocklog) > ULONG_MAX)
Eric Sandeen657a4cf2010-04-30 03:42:49 +0000266 return EFBIG;
Nathan Scott4cc929e2007-05-14 18:24:02 +1000267#else /* Limited by UINT_MAX of sectors */
268 if (nblocks << (sbp->sb_blocklog - BBSHIFT) > UINT_MAX)
Eric Sandeen657a4cf2010-04-30 03:42:49 +0000269 return EFBIG;
Nathan Scott4cc929e2007-05-14 18:24:02 +1000270#endif
271 return 0;
272}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274/*
275 * Check the validity of the SB found.
276 */
277STATIC int
278xfs_mount_validate_sb(
279 xfs_mount_t *mp,
Nathan Scott764d1f82006-03-31 13:04:17 +1000280 xfs_sb_t *sbp,
281 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 /*
284 * If the log device and data device have the
285 * same device number, the log is internal.
286 * Consequently, the sb_logstart should be non-zero. If
287 * we have a zero sb_logstart in this case, we may be trying to mount
288 * a volume filesystem in a non-volume manner.
289 */
290 if (sbp->sb_magicnum != XFS_SB_MAGIC) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000291 xfs_fs_mount_cmn_err(flags, "bad magic number");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return XFS_ERROR(EWRONGFS);
293 }
294
Eric Sandeen62118702008-03-06 13:44:28 +1100295 if (!xfs_sb_good_version(sbp)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000296 xfs_fs_mount_cmn_err(flags, "bad version");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return XFS_ERROR(EWRONGFS);
298 }
299
300 if (unlikely(
301 sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000302 xfs_fs_mount_cmn_err(flags,
303 "filesystem is marked as having an external log; "
304 "specify logdev on the\nmount command line.");
305 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
307
308 if (unlikely(
309 sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000310 xfs_fs_mount_cmn_err(flags,
311 "filesystem is marked as having an internal log; "
312 "do not specify logdev on\nthe mount command line.");
313 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
315
316 /*
317 * More sanity checking. These were stolen directly from
318 * xfs_repair.
319 */
320 if (unlikely(
321 sbp->sb_agcount <= 0 ||
322 sbp->sb_sectsize < XFS_MIN_SECTORSIZE ||
323 sbp->sb_sectsize > XFS_MAX_SECTORSIZE ||
324 sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG ||
325 sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG ||
Olaf Weber2ac00af2009-04-17 16:12:45 -0500326 sbp->sb_sectsize != (1 << sbp->sb_sectlog) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 sbp->sb_blocksize < XFS_MIN_BLOCKSIZE ||
328 sbp->sb_blocksize > XFS_MAX_BLOCKSIZE ||
329 sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG ||
330 sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
Olaf Weber2ac00af2009-04-17 16:12:45 -0500331 sbp->sb_blocksize != (1 << sbp->sb_blocklog) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 sbp->sb_inodesize < XFS_DINODE_MIN_SIZE ||
333 sbp->sb_inodesize > XFS_DINODE_MAX_SIZE ||
Nathan Scott9f989c92006-03-14 13:29:32 +1100334 sbp->sb_inodelog < XFS_DINODE_MIN_LOG ||
335 sbp->sb_inodelog > XFS_DINODE_MAX_LOG ||
Olaf Weber2ac00af2009-04-17 16:12:45 -0500336 sbp->sb_inodesize != (1 << sbp->sb_inodelog) ||
Nathan Scott9f989c92006-03-14 13:29:32 +1100337 (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
339 (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
Nathan Scotte50bd162006-04-11 15:10:45 +1000340 (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */))) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000341 xfs_fs_mount_cmn_err(flags, "SB sanity check 1 failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return XFS_ERROR(EFSCORRUPTED);
343 }
344
345 /*
346 * Sanity check AG count, size fields against data size field
347 */
348 if (unlikely(
349 sbp->sb_dblocks == 0 ||
350 sbp->sb_dblocks >
351 (xfs_drfsbno_t)sbp->sb_agcount * sbp->sb_agblocks ||
352 sbp->sb_dblocks < (xfs_drfsbno_t)(sbp->sb_agcount - 1) *
353 sbp->sb_agblocks + XFS_MIN_AG_BLOCKS)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000354 xfs_fs_mount_cmn_err(flags, "SB sanity check 2 failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return XFS_ERROR(EFSCORRUPTED);
356 }
357
Lachlan McIlroy2edbddd2008-06-27 13:34:34 +1000358 /*
359 * Until this is fixed only page-sized or smaller data blocks work.
360 */
361 if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
362 xfs_fs_mount_cmn_err(flags,
363 "file system with blocksize %d bytes",
364 sbp->sb_blocksize);
365 xfs_fs_mount_cmn_err(flags,
366 "only pagesize (%ld) or less will currently work.",
367 PAGE_SIZE);
368 return XFS_ERROR(ENOSYS);
369 }
370
Christoph Hellwig1a5902c2009-03-29 19:26:46 +0200371 /*
372 * Currently only very few inode sizes are supported.
373 */
374 switch (sbp->sb_inodesize) {
375 case 256:
376 case 512:
377 case 1024:
378 case 2048:
379 break;
380 default:
381 xfs_fs_mount_cmn_err(flags,
382 "inode size of %d bytes not supported",
383 sbp->sb_inodesize);
384 return XFS_ERROR(ENOSYS);
385 }
386
Nathan Scott4cc929e2007-05-14 18:24:02 +1000387 if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) ||
388 xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000389 xfs_fs_mount_cmn_err(flags,
390 "file system too large to be mounted on this system.");
Eric Sandeen657a4cf2010-04-30 03:42:49 +0000391 return XFS_ERROR(EFBIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393
394 if (unlikely(sbp->sb_inprogress)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000395 xfs_fs_mount_cmn_err(flags, "file system busy");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return XFS_ERROR(EFSCORRUPTED);
397 }
398
399 /*
Nathan Scottde206142005-05-05 13:24:13 -0700400 * Version 1 directory format has never worked on Linux.
401 */
Eric Sandeen62118702008-03-06 13:44:28 +1100402 if (unlikely(!xfs_sb_version_hasdirv2(sbp))) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000403 xfs_fs_mount_cmn_err(flags,
404 "file system using version 1 directory format");
Nathan Scottde206142005-05-05 13:24:13 -0700405 return XFS_ERROR(ENOSYS);
406 }
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return 0;
409}
410
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000411int
Nathan Scottc11e2c32005-11-02 15:11:45 +1100412xfs_initialize_perag(
Nathan Scottc11e2c32005-11-02 15:11:45 +1100413 xfs_mount_t *mp,
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000414 xfs_agnumber_t agcount,
415 xfs_agnumber_t *maxagi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 xfs_agnumber_t index, max_metadata;
Dave Chinner8b26c582010-01-11 11:47:48 +0000418 xfs_agnumber_t first_initialised = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 xfs_perag_t *pag;
420 xfs_agino_t agino;
421 xfs_ino_t ino;
422 xfs_sb_t *sbp = &mp->m_sb;
Dave Chinner8b26c582010-01-11 11:47:48 +0000423 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000425 /*
426 * Walk the current per-ag tree so we don't try to initialise AGs
427 * that already exist (growfs case). Allocate and insert all the
428 * AGs we don't find ready for initialisation.
429 */
430 for (index = 0; index < agcount; index++) {
431 pag = xfs_perag_get(mp, index);
432 if (pag) {
433 xfs_perag_put(pag);
434 continue;
435 }
Dave Chinner8b26c582010-01-11 11:47:48 +0000436 if (!first_initialised)
437 first_initialised = index;
Christoph Hellwigfb3b5042010-05-28 19:03:10 +0000438
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000439 pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL);
440 if (!pag)
Dave Chinner8b26c582010-01-11 11:47:48 +0000441 goto out_unwind;
Christoph Hellwigfb3b5042010-05-28 19:03:10 +0000442 pag->pag_agno = index;
443 pag->pag_mount = mp;
444 rwlock_init(&pag->pag_ici_lock);
445 INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC);
446
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000447 if (radix_tree_preload(GFP_NOFS))
Dave Chinner8b26c582010-01-11 11:47:48 +0000448 goto out_unwind;
Christoph Hellwigfb3b5042010-05-28 19:03:10 +0000449
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000450 spin_lock(&mp->m_perag_lock);
451 if (radix_tree_insert(&mp->m_perag_tree, index, pag)) {
452 BUG();
453 spin_unlock(&mp->m_perag_lock);
Dave Chinner8b26c582010-01-11 11:47:48 +0000454 radix_tree_preload_end();
455 error = -EEXIST;
456 goto out_unwind;
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000457 }
458 spin_unlock(&mp->m_perag_lock);
459 radix_tree_preload_end();
460 }
461
Christoph Hellwigfb3b5042010-05-28 19:03:10 +0000462 /*
463 * If we mount with the inode64 option, or no inode overflows
464 * the legacy 32-bit address space clear the inode32 option.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 */
Christoph Hellwigfb3b5042010-05-28 19:03:10 +0000466 agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
467 ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Christoph Hellwigfb3b5042010-05-28 19:03:10 +0000469 if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) && ino > XFS_MAXINUMBER_32)
470 mp->m_flags |= XFS_MOUNT_32BITINODES;
471 else
472 mp->m_flags &= ~XFS_MOUNT_32BITINODES;
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (mp->m_flags & XFS_MOUNT_32BITINODES) {
Christoph Hellwigfb3b5042010-05-28 19:03:10 +0000475 /*
476 * Calculate how much should be reserved for inodes to meet
477 * the max inode percentage.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 */
479 if (mp->m_maxicount) {
480 __uint64_t icount;
481
482 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
483 do_div(icount, 100);
484 icount += sbp->sb_agblocks - 1;
Eric Sandeena749ee82005-11-02 15:13:42 +1100485 do_div(icount, sbp->sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 max_metadata = icount;
487 } else {
488 max_metadata = agcount;
489 }
Christoph Hellwigfb3b5042010-05-28 19:03:10 +0000490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 for (index = 0; index < agcount; index++) {
492 ino = XFS_AGINO_TO_INO(mp, index, agino);
Christoph Hellwigfb3b5042010-05-28 19:03:10 +0000493 if (ino > XFS_MAXINUMBER_32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 index++;
495 break;
496 }
497
Dave Chinner44b56e02010-01-11 11:47:43 +0000498 pag = xfs_perag_get(mp, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 pag->pagi_inodeok = 1;
500 if (index < max_metadata)
501 pag->pagf_metadata = 1;
Dave Chinner44b56e02010-01-11 11:47:43 +0000502 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 for (index = 0; index < agcount; index++) {
Dave Chinner44b56e02010-01-11 11:47:43 +0000506 pag = xfs_perag_get(mp, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 pag->pagi_inodeok = 1;
Dave Chinner44b56e02010-01-11 11:47:43 +0000508 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510 }
Christoph Hellwigfb3b5042010-05-28 19:03:10 +0000511
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000512 if (maxagi)
513 *maxagi = index;
514 return 0;
Dave Chinner8b26c582010-01-11 11:47:48 +0000515
516out_unwind:
517 kmem_free(pag);
518 for (; index > first_initialised; index--) {
519 pag = radix_tree_delete(&mp->m_perag_tree, index);
520 kmem_free(pag);
521 }
522 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000525void
526xfs_sb_from_disk(
527 xfs_sb_t *to,
528 xfs_dsb_t *from)
529{
530 to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
531 to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
532 to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
533 to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
534 to->sb_rextents = be64_to_cpu(from->sb_rextents);
535 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
536 to->sb_logstart = be64_to_cpu(from->sb_logstart);
537 to->sb_rootino = be64_to_cpu(from->sb_rootino);
538 to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
539 to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
540 to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
541 to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
542 to->sb_agcount = be32_to_cpu(from->sb_agcount);
543 to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
544 to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
545 to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
546 to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
547 to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
548 to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
549 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
550 to->sb_blocklog = from->sb_blocklog;
551 to->sb_sectlog = from->sb_sectlog;
552 to->sb_inodelog = from->sb_inodelog;
553 to->sb_inopblog = from->sb_inopblog;
554 to->sb_agblklog = from->sb_agblklog;
555 to->sb_rextslog = from->sb_rextslog;
556 to->sb_inprogress = from->sb_inprogress;
557 to->sb_imax_pct = from->sb_imax_pct;
558 to->sb_icount = be64_to_cpu(from->sb_icount);
559 to->sb_ifree = be64_to_cpu(from->sb_ifree);
560 to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
561 to->sb_frextents = be64_to_cpu(from->sb_frextents);
562 to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
563 to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
564 to->sb_qflags = be16_to_cpu(from->sb_qflags);
565 to->sb_flags = from->sb_flags;
566 to->sb_shared_vn = from->sb_shared_vn;
567 to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
568 to->sb_unit = be32_to_cpu(from->sb_unit);
569 to->sb_width = be32_to_cpu(from->sb_width);
570 to->sb_dirblklog = from->sb_dirblklog;
571 to->sb_logsectlog = from->sb_logsectlog;
572 to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
573 to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
574 to->sb_features2 = be32_to_cpu(from->sb_features2);
David Chinneree1c0902008-03-06 13:45:50 +1100575 to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000576}
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578/*
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000579 * Copy in core superblock to ondisk one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 *
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000581 * The fields argument is mask of superblock fields to copy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 */
583void
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000584xfs_sb_to_disk(
585 xfs_dsb_t *to,
586 xfs_sb_t *from,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 __int64_t fields)
588{
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000589 xfs_caddr_t to_ptr = (xfs_caddr_t)to;
590 xfs_caddr_t from_ptr = (xfs_caddr_t)from;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 xfs_sb_field_t f;
592 int first;
593 int size;
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 ASSERT(fields);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (!fields)
597 return;
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 while (fields) {
600 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
601 first = xfs_sb_info[f].offset;
602 size = xfs_sb_info[f + 1].offset - first;
603
604 ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1);
605
606 if (size == 1 || xfs_sb_info[f].type == 1) {
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000607 memcpy(to_ptr + first, from_ptr + first, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 } else {
609 switch (size) {
610 case 2:
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000611 *(__be16 *)(to_ptr + first) =
612 cpu_to_be16(*(__u16 *)(from_ptr + first));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 break;
614 case 4:
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000615 *(__be32 *)(to_ptr + first) =
616 cpu_to_be32(*(__u32 *)(from_ptr + first));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 break;
618 case 8:
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000619 *(__be64 *)(to_ptr + first) =
620 cpu_to_be64(*(__u64 *)(from_ptr + first));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 break;
622 default:
623 ASSERT(0);
624 }
625 }
626
627 fields &= ~(1LL << f);
628 }
629}
630
631/*
632 * xfs_readsb
633 *
634 * Does the initial read of the superblock.
635 */
636int
Nathan Scott764d1f82006-03-31 13:04:17 +1000637xfs_readsb(xfs_mount_t *mp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 unsigned int sector_size;
640 unsigned int extra_flags;
641 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 int error;
643
644 ASSERT(mp->m_sb_bp == NULL);
645 ASSERT(mp->m_ddev_targp != NULL);
646
647 /*
648 * Allocate a (locked) buffer to hold the superblock.
649 * This will be kept around at all times to optimize
650 * access to the superblock.
651 */
652 sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000653 extra_flags = XBF_LOCK | XBF_FS_MANAGED | XBF_MAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000655 bp = xfs_buf_read(mp->m_ddev_targp, XFS_SB_DADDR, BTOBB(sector_size),
656 extra_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (!bp || XFS_BUF_ISERROR(bp)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000658 xfs_fs_mount_cmn_err(flags, "SB read failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
660 goto fail;
661 }
662 ASSERT(XFS_BUF_ISBUSY(bp));
663 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
664
665 /*
666 * Initialize the mount structure from the superblock.
667 * But first do some basic consistency checking.
668 */
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000669 xfs_sb_from_disk(&mp->m_sb, XFS_BUF_TO_SBP(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Nathan Scott764d1f82006-03-31 13:04:17 +1000671 error = xfs_mount_validate_sb(mp, &(mp->m_sb), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (error) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000673 xfs_fs_mount_cmn_err(flags, "SB validate failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 goto fail;
675 }
676
677 /*
678 * We must be able to do sector-sized and sector-aligned IO.
679 */
680 if (sector_size > mp->m_sb.sb_sectsize) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000681 xfs_fs_mount_cmn_err(flags,
682 "device supports only %u byte sectors (not %u)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 sector_size, mp->m_sb.sb_sectsize);
684 error = ENOSYS;
685 goto fail;
686 }
687
688 /*
689 * If device sector size is smaller than the superblock size,
690 * re-read the superblock so the buffer is correctly sized.
691 */
692 if (sector_size < mp->m_sb.sb_sectsize) {
693 XFS_BUF_UNMANAGE(bp);
694 xfs_buf_relse(bp);
695 sector_size = mp->m_sb.sb_sectsize;
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000696 bp = xfs_buf_read(mp->m_ddev_targp, XFS_SB_DADDR,
697 BTOBB(sector_size), extra_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (!bp || XFS_BUF_ISERROR(bp)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000699 xfs_fs_mount_cmn_err(flags, "SB re-read failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
701 goto fail;
702 }
703 ASSERT(XFS_BUF_ISBUSY(bp));
704 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
705 }
706
Lachlan McIlroy5478eea2007-02-10 18:36:29 +1100707 /* Initialize per-cpu counters */
708 xfs_icsb_reinit_counters(mp);
David Chinner8d280b92006-03-14 13:13:09 +1100709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 mp->m_sb_bp = bp;
711 xfs_buf_relse(bp);
712 ASSERT(XFS_BUF_VALUSEMA(bp) > 0);
713 return 0;
714
715 fail:
716 if (bp) {
717 XFS_BUF_UNMANAGE(bp);
718 xfs_buf_relse(bp);
719 }
720 return error;
721}
722
723
724/*
725 * xfs_mount_common
726 *
727 * Mount initialization code establishing various mount
728 * fields from the superblock associated with the given
729 * mount structure
730 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000731STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732xfs_mount_common(xfs_mount_t *mp, xfs_sb_t *sbp)
733{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 mp->m_agfrotor = mp->m_agirotor = 0;
Eric Sandeen007c61c2007-10-11 17:43:56 +1000735 spin_lock_init(&mp->m_agirotor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 mp->m_maxagi = mp->m_sb.sb_agcount;
737 mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
738 mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
739 mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
740 mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
741 mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 mp->m_blockmask = sbp->sb_blocksize - 1;
743 mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
744 mp->m_blockwmask = mp->m_blockwsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Christoph Hellwig60197e82008-10-30 17:11:19 +1100746 mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
747 mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
748 mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
749 mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
750
751 mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
752 mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
753 mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2;
754 mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2;
755
756 mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
757 mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
758 mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
759 mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
762 mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
763 sbp->sb_inopblock);
764 mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
765}
David Chinner92821e22007-05-24 15:26:31 +1000766
767/*
768 * xfs_initialize_perag_data
769 *
770 * Read in each per-ag structure so we can count up the number of
771 * allocated inodes, free inodes and used filesystem blocks as this
772 * information is no longer persistent in the superblock. Once we have
773 * this information, write it into the in-core superblock structure.
774 */
775STATIC int
776xfs_initialize_perag_data(xfs_mount_t *mp, xfs_agnumber_t agcount)
777{
778 xfs_agnumber_t index;
779 xfs_perag_t *pag;
780 xfs_sb_t *sbp = &mp->m_sb;
781 uint64_t ifree = 0;
782 uint64_t ialloc = 0;
783 uint64_t bfree = 0;
784 uint64_t bfreelst = 0;
785 uint64_t btree = 0;
786 int error;
David Chinner92821e22007-05-24 15:26:31 +1000787
788 for (index = 0; index < agcount; index++) {
789 /*
790 * read the agf, then the agi. This gets us
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200791 * all the information we need and populates the
David Chinner92821e22007-05-24 15:26:31 +1000792 * per-ag structures for us.
793 */
794 error = xfs_alloc_pagf_init(mp, NULL, index, 0);
795 if (error)
796 return error;
797
798 error = xfs_ialloc_pagi_init(mp, NULL, index);
799 if (error)
800 return error;
Dave Chinner44b56e02010-01-11 11:47:43 +0000801 pag = xfs_perag_get(mp, index);
David Chinner92821e22007-05-24 15:26:31 +1000802 ifree += pag->pagi_freecount;
803 ialloc += pag->pagi_count;
804 bfree += pag->pagf_freeblks;
805 bfreelst += pag->pagf_flcount;
806 btree += pag->pagf_btreeblks;
Dave Chinner44b56e02010-01-11 11:47:43 +0000807 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +1000808 }
809 /*
810 * Overwrite incore superblock counters with just-read data
811 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000812 spin_lock(&mp->m_sb_lock);
David Chinner92821e22007-05-24 15:26:31 +1000813 sbp->sb_ifree = ifree;
814 sbp->sb_icount = ialloc;
815 sbp->sb_fdblocks = bfree + bfreelst + btree;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000816 spin_unlock(&mp->m_sb_lock);
David Chinner92821e22007-05-24 15:26:31 +1000817
818 /* Fixup the per-cpu counters as well. */
819 xfs_icsb_reinit_counters(mp);
820
821 return 0;
822}
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824/*
Eric Sandeen0771fb42007-10-12 11:03:40 +1000825 * Update alignment values based on mount options and sb values
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 */
Eric Sandeen0771fb42007-10-12 11:03:40 +1000827STATIC int
Christoph Hellwig7884bc82009-01-19 02:04:07 +0100828xfs_update_alignment(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 xfs_sb_t *sbp = &(mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Christoph Hellwig42490232008-08-13 16:49:32 +1000832 if (mp->m_dalign) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 /*
834 * If stripe unit and stripe width are not multiples
835 * of the fs blocksize turn off alignment.
836 */
837 if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
838 (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
839 if (mp->m_flags & XFS_MOUNT_RETERR) {
840 cmn_err(CE_WARN,
841 "XFS: alignment check 1 failed");
Eric Sandeen0771fb42007-10-12 11:03:40 +1000842 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
844 mp->m_dalign = mp->m_swidth = 0;
845 } else {
846 /*
847 * Convert the stripe unit and width to FSBs.
848 */
849 mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
850 if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
851 if (mp->m_flags & XFS_MOUNT_RETERR) {
Eric Sandeen0771fb42007-10-12 11:03:40 +1000852 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
854 xfs_fs_cmn_err(CE_WARN, mp,
855"stripe alignment turned off: sunit(%d)/swidth(%d) incompatible with agsize(%d)",
856 mp->m_dalign, mp->m_swidth,
857 sbp->sb_agblocks);
858
859 mp->m_dalign = 0;
860 mp->m_swidth = 0;
861 } else if (mp->m_dalign) {
862 mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
863 } else {
864 if (mp->m_flags & XFS_MOUNT_RETERR) {
865 xfs_fs_cmn_err(CE_WARN, mp,
866"stripe alignment turned off: sunit(%d) less than bsize(%d)",
867 mp->m_dalign,
868 mp->m_blockmask +1);
Eric Sandeen0771fb42007-10-12 11:03:40 +1000869 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
871 mp->m_swidth = 0;
872 }
873 }
874
875 /*
876 * Update superblock with new values
877 * and log changes
878 */
Eric Sandeen62118702008-03-06 13:44:28 +1100879 if (xfs_sb_version_hasdalign(sbp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (sbp->sb_unit != mp->m_dalign) {
881 sbp->sb_unit = mp->m_dalign;
Christoph Hellwig7884bc82009-01-19 02:04:07 +0100882 mp->m_update_flags |= XFS_SB_UNIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
884 if (sbp->sb_width != mp->m_swidth) {
885 sbp->sb_width = mp->m_swidth;
Christoph Hellwig7884bc82009-01-19 02:04:07 +0100886 mp->m_update_flags |= XFS_SB_WIDTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888 }
889 } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
Eric Sandeen62118702008-03-06 13:44:28 +1100890 xfs_sb_version_hasdalign(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 mp->m_dalign = sbp->sb_unit;
892 mp->m_swidth = sbp->sb_width;
893 }
894
Eric Sandeen0771fb42007-10-12 11:03:40 +1000895 return 0;
896}
897
898/*
899 * Set the maximum inode count for this filesystem
900 */
901STATIC void
902xfs_set_maxicount(xfs_mount_t *mp)
903{
904 xfs_sb_t *sbp = &(mp->m_sb);
905 __uint64_t icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 if (sbp->sb_imax_pct) {
Eric Sandeen0771fb42007-10-12 11:03:40 +1000908 /*
909 * Make sure the maximum inode count is a multiple
910 * of the units we allocate inodes in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
913 do_div(icount, 100);
914 do_div(icount, mp->m_ialloc_blks);
915 mp->m_maxicount = (icount * mp->m_ialloc_blks) <<
916 sbp->sb_inopblog;
Eric Sandeen0771fb42007-10-12 11:03:40 +1000917 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 mp->m_maxicount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
Eric Sandeen0771fb42007-10-12 11:03:40 +1000920}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Eric Sandeen0771fb42007-10-12 11:03:40 +1000922/*
923 * Set the default minimum read and write sizes unless
924 * already specified in a mount option.
925 * We use smaller I/O sizes when the file system
926 * is being used for NFS service (wsync mount option).
927 */
928STATIC void
929xfs_set_rw_sizes(xfs_mount_t *mp)
930{
931 xfs_sb_t *sbp = &(mp->m_sb);
932 int readio_log, writeio_log;
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
935 if (mp->m_flags & XFS_MOUNT_WSYNC) {
936 readio_log = XFS_WSYNC_READIO_LOG;
937 writeio_log = XFS_WSYNC_WRITEIO_LOG;
938 } else {
939 readio_log = XFS_READIO_LOG_LARGE;
940 writeio_log = XFS_WRITEIO_LOG_LARGE;
941 }
942 } else {
943 readio_log = mp->m_readio_log;
944 writeio_log = mp->m_writeio_log;
945 }
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (sbp->sb_blocklog > readio_log) {
948 mp->m_readio_log = sbp->sb_blocklog;
949 } else {
950 mp->m_readio_log = readio_log;
951 }
952 mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
953 if (sbp->sb_blocklog > writeio_log) {
954 mp->m_writeio_log = sbp->sb_blocklog;
955 } else {
956 mp->m_writeio_log = writeio_log;
957 }
958 mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
Eric Sandeen0771fb42007-10-12 11:03:40 +1000959}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Eric Sandeen0771fb42007-10-12 11:03:40 +1000961/*
962 * Set whether we're using inode alignment.
963 */
964STATIC void
965xfs_set_inoalignment(xfs_mount_t *mp)
966{
Eric Sandeen62118702008-03-06 13:44:28 +1100967 if (xfs_sb_version_hasalign(&mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 mp->m_sb.sb_inoalignmt >=
969 XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
970 mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
971 else
972 mp->m_inoalign_mask = 0;
973 /*
974 * If we are using stripe alignment, check whether
975 * the stripe unit is a multiple of the inode alignment
976 */
977 if (mp->m_dalign && mp->m_inoalign_mask &&
978 !(mp->m_dalign & mp->m_inoalign_mask))
979 mp->m_sinoalign = mp->m_dalign;
980 else
981 mp->m_sinoalign = 0;
Eric Sandeen0771fb42007-10-12 11:03:40 +1000982}
983
984/*
985 * Check that the data (and log if separate) are an ok size.
986 */
987STATIC int
Christoph Hellwig42490232008-08-13 16:49:32 +1000988xfs_check_sizes(xfs_mount_t *mp)
Eric Sandeen0771fb42007-10-12 11:03:40 +1000989{
990 xfs_buf_t *bp;
991 xfs_daddr_t d;
992 int error;
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
995 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
996 cmn_err(CE_WARN, "XFS: size check 1 failed");
Eric Sandeen657a4cf2010-04-30 03:42:49 +0000997 return XFS_ERROR(EFBIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
999 error = xfs_read_buf(mp, mp->m_ddev_targp,
1000 d - XFS_FSS_TO_BB(mp, 1),
1001 XFS_FSS_TO_BB(mp, 1), 0, &bp);
1002 if (!error) {
1003 xfs_buf_relse(bp);
1004 } else {
1005 cmn_err(CE_WARN, "XFS: size check 2 failed");
Eric Sandeen0771fb42007-10-12 11:03:40 +10001006 if (error == ENOSPC)
Eric Sandeen657a4cf2010-04-30 03:42:49 +00001007 error = XFS_ERROR(EFBIG);
Eric Sandeen0771fb42007-10-12 11:03:40 +10001008 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010
Christoph Hellwig42490232008-08-13 16:49:32 +10001011 if (mp->m_logdev_targp != mp->m_ddev_targp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
1013 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
1014 cmn_err(CE_WARN, "XFS: size check 3 failed");
Eric Sandeen657a4cf2010-04-30 03:42:49 +00001015 return XFS_ERROR(EFBIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017 error = xfs_read_buf(mp, mp->m_logdev_targp,
1018 d - XFS_FSB_TO_BB(mp, 1),
1019 XFS_FSB_TO_BB(mp, 1), 0, &bp);
1020 if (!error) {
1021 xfs_buf_relse(bp);
1022 } else {
1023 cmn_err(CE_WARN, "XFS: size check 3 failed");
Eric Sandeen0771fb42007-10-12 11:03:40 +10001024 if (error == ENOSPC)
Eric Sandeen657a4cf2010-04-30 03:42:49 +00001025 error = XFS_ERROR(EFBIG);
Eric Sandeen0771fb42007-10-12 11:03:40 +10001026 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
1028 }
Eric Sandeen0771fb42007-10-12 11:03:40 +10001029 return 0;
1030}
1031
1032/*
Christoph Hellwig7d095252009-06-08 15:33:32 +02001033 * Clear the quotaflags in memory and in the superblock.
1034 */
1035int
1036xfs_mount_reset_sbqflags(
1037 struct xfs_mount *mp)
1038{
1039 int error;
1040 struct xfs_trans *tp;
1041
1042 mp->m_qflags = 0;
1043
1044 /*
1045 * It is OK to look at sb_qflags here in mount path,
1046 * without m_sb_lock.
1047 */
1048 if (mp->m_sb.sb_qflags == 0)
1049 return 0;
1050 spin_lock(&mp->m_sb_lock);
1051 mp->m_sb.sb_qflags = 0;
1052 spin_unlock(&mp->m_sb_lock);
1053
1054 /*
1055 * If the fs is readonly, let the incore superblock run
1056 * with quotas off but don't flush the update out to disk
1057 */
1058 if (mp->m_flags & XFS_MOUNT_RDONLY)
1059 return 0;
1060
1061#ifdef QUOTADEBUG
1062 xfs_fs_cmn_err(CE_NOTE, mp, "Writing superblock quota changes");
1063#endif
1064
1065 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SBCHANGE);
1066 error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
1067 XFS_DEFAULT_LOG_COUNT);
1068 if (error) {
1069 xfs_trans_cancel(tp, 0);
1070 xfs_fs_cmn_err(CE_ALERT, mp,
1071 "xfs_mount_reset_sbqflags: Superblock update failed!");
1072 return error;
1073 }
1074
1075 xfs_mod_sb(tp, XFS_SB_QFLAGS);
1076 return xfs_trans_commit(tp, 0);
1077}
1078
Eric Sandeend5db0f92010-02-05 22:59:53 +00001079__uint64_t
1080xfs_default_resblks(xfs_mount_t *mp)
1081{
1082 __uint64_t resblks;
1083
1084 /*
Dave Chinner8babd8a2010-03-04 01:46:25 +00001085 * We default to 5% or 8192 fsbs of space reserved, whichever is
1086 * smaller. This is intended to cover concurrent allocation
1087 * transactions when we initially hit enospc. These each require a 4
1088 * block reservation. Hence by default we cover roughly 2000 concurrent
1089 * allocation reservations.
Eric Sandeend5db0f92010-02-05 22:59:53 +00001090 */
1091 resblks = mp->m_sb.sb_dblocks;
1092 do_div(resblks, 20);
Dave Chinner8babd8a2010-03-04 01:46:25 +00001093 resblks = min_t(__uint64_t, resblks, 8192);
Eric Sandeend5db0f92010-02-05 22:59:53 +00001094 return resblks;
1095}
1096
Christoph Hellwig7d095252009-06-08 15:33:32 +02001097/*
Eric Sandeen0771fb42007-10-12 11:03:40 +10001098 * This function does the following on an initial mount of a file system:
1099 * - reads the superblock from disk and init the mount struct
1100 * - if we're a 32-bit kernel, do a size check on the superblock
1101 * so we don't mount terabyte filesystems
1102 * - init mount struct realtime fields
1103 * - allocate inode hash table for fs
1104 * - init directory manager
1105 * - perform recovery and init the log manager
1106 */
1107int
1108xfs_mountfs(
Christoph Hellwig42490232008-08-13 16:49:32 +10001109 xfs_mount_t *mp)
Eric Sandeen0771fb42007-10-12 11:03:40 +10001110{
1111 xfs_sb_t *sbp = &(mp->m_sb);
1112 xfs_inode_t *rip;
Eric Sandeen0771fb42007-10-12 11:03:40 +10001113 __uint64_t resblks;
Christoph Hellwig7d095252009-06-08 15:33:32 +02001114 uint quotamount = 0;
1115 uint quotaflags = 0;
Eric Sandeen0771fb42007-10-12 11:03:40 +10001116 int error = 0;
1117
Eric Sandeen0771fb42007-10-12 11:03:40 +10001118 xfs_mount_common(mp, sbp);
1119
1120 /*
Eric Sandeene6957ea2008-04-10 12:19:34 +10001121 * Check for a mismatched features2 values. Older kernels
1122 * read & wrote into the wrong sb offset for sb_features2
1123 * on some platforms due to xfs_sb_t not being 64bit size aligned
1124 * when sb_features2 was added, which made older superblock
1125 * reading/writing routines swap it as a 64-bit value.
David Chinneree1c0902008-03-06 13:45:50 +11001126 *
Eric Sandeene6957ea2008-04-10 12:19:34 +10001127 * For backwards compatibility, we make both slots equal.
1128 *
1129 * If we detect a mismatched field, we OR the set bits into the
1130 * existing features2 field in case it has already been modified; we
1131 * don't want to lose any features. We then update the bad location
1132 * with the ORed value so that older kernels will see any features2
1133 * flags, and mark the two fields as needing updates once the
1134 * transaction subsystem is online.
David Chinneree1c0902008-03-06 13:45:50 +11001135 */
Eric Sandeene6957ea2008-04-10 12:19:34 +10001136 if (xfs_sb_has_mismatched_features2(sbp)) {
David Chinneree1c0902008-03-06 13:45:50 +11001137 cmn_err(CE_WARN,
1138 "XFS: correcting sb_features alignment problem");
1139 sbp->sb_features2 |= sbp->sb_bad_features2;
Eric Sandeene6957ea2008-04-10 12:19:34 +10001140 sbp->sb_bad_features2 = sbp->sb_features2;
Christoph Hellwig7884bc82009-01-19 02:04:07 +01001141 mp->m_update_flags |= XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2;
Eric Sandeene6957ea2008-04-10 12:19:34 +10001142
1143 /*
1144 * Re-check for ATTR2 in case it was found in bad_features2
1145 * slot.
1146 */
Tim Shimmin7c12f292008-04-30 18:15:28 +10001147 if (xfs_sb_version_hasattr2(&mp->m_sb) &&
1148 !(mp->m_flags & XFS_MOUNT_NOATTR2))
Eric Sandeene6957ea2008-04-10 12:19:34 +10001149 mp->m_flags |= XFS_MOUNT_ATTR2;
Tim Shimmin7c12f292008-04-30 18:15:28 +10001150 }
Eric Sandeene6957ea2008-04-10 12:19:34 +10001151
Tim Shimmin7c12f292008-04-30 18:15:28 +10001152 if (xfs_sb_version_hasattr2(&mp->m_sb) &&
1153 (mp->m_flags & XFS_MOUNT_NOATTR2)) {
1154 xfs_sb_version_removeattr2(&mp->m_sb);
Christoph Hellwig7884bc82009-01-19 02:04:07 +01001155 mp->m_update_flags |= XFS_SB_FEATURES2;
Tim Shimmin7c12f292008-04-30 18:15:28 +10001156
1157 /* update sb_versionnum for the clearing of the morebits */
1158 if (!sbp->sb_features2)
Christoph Hellwig7884bc82009-01-19 02:04:07 +01001159 mp->m_update_flags |= XFS_SB_VERSIONNUM;
David Chinneree1c0902008-03-06 13:45:50 +11001160 }
1161
1162 /*
Eric Sandeen0771fb42007-10-12 11:03:40 +10001163 * Check if sb_agblocks is aligned at stripe boundary
1164 * If sb_agblocks is NOT aligned turn off m_dalign since
1165 * allocator alignment is within an ag, therefore ag has
1166 * to be aligned at stripe boundary.
1167 */
Christoph Hellwig7884bc82009-01-19 02:04:07 +01001168 error = xfs_update_alignment(mp);
Eric Sandeen0771fb42007-10-12 11:03:40 +10001169 if (error)
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001170 goto out;
Eric Sandeen0771fb42007-10-12 11:03:40 +10001171
1172 xfs_alloc_compute_maxlevels(mp);
1173 xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
1174 xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
1175 xfs_ialloc_compute_maxlevels(mp);
1176
1177 xfs_set_maxicount(mp);
1178
1179 mp->m_maxioffset = xfs_max_file_offset(sbp->sb_blocklog);
1180
Christoph Hellwig27174202009-03-30 10:21:31 +02001181 error = xfs_uuid_mount(mp);
1182 if (error)
1183 goto out;
Eric Sandeen0771fb42007-10-12 11:03:40 +10001184
1185 /*
1186 * Set the minimum read and write sizes
1187 */
1188 xfs_set_rw_sizes(mp);
1189
1190 /*
1191 * Set the inode cluster size.
1192 * This may still be overridden by the file system
1193 * block size if it is larger than the chosen cluster size.
1194 */
1195 mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
1196
1197 /*
1198 * Set inode alignment fields
1199 */
1200 xfs_set_inoalignment(mp);
1201
1202 /*
1203 * Check that the data (and log if separate) are an ok size.
1204 */
Christoph Hellwig42490232008-08-13 16:49:32 +10001205 error = xfs_check_sizes(mp);
Eric Sandeen0771fb42007-10-12 11:03:40 +10001206 if (error)
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001207 goto out_remove_uuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 /*
1210 * Initialize realtime fields in the mount structure
1211 */
Eric Sandeen0771fb42007-10-12 11:03:40 +10001212 error = xfs_rtmount_init(mp);
1213 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 cmn_err(CE_WARN, "XFS: RT mount failed");
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001215 goto out_remove_uuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217
1218 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 * Copies the low order bits of the timestamp and the randomly
1220 * set "sequence" number out of a UUID.
1221 */
1222 uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid);
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 mp->m_dmevmask = 0; /* not persistent; set after each mount */
1225
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001226 xfs_dir_mount(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 /*
1229 * Initialize the attribute manager's entries.
1230 */
1231 mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100;
1232
1233 /*
1234 * Initialize the precomputed transaction reservations values.
1235 */
1236 xfs_trans_init(mp);
1237
1238 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 * Allocate and initialize the per-ag data.
1240 */
Dave Chinner1c1c6eb2010-01-11 11:47:44 +00001241 spin_lock_init(&mp->m_perag_lock);
Dave Chinner9b98b6f2010-05-27 01:58:13 +00001242 INIT_RADIX_TREE(&mp->m_perag_tree, GFP_ATOMIC);
Dave Chinner1c1c6eb2010-01-11 11:47:44 +00001243 error = xfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi);
1244 if (error) {
1245 cmn_err(CE_WARN, "XFS: Failed per-ag init: %d", error);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001246 goto out_remove_uuid;
Dave Chinner1c1c6eb2010-01-11 11:47:44 +00001247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001249 if (!sbp->sb_logblocks) {
1250 cmn_err(CE_WARN, "XFS: no log defined");
1251 XFS_ERROR_REPORT("xfs_mountfs", XFS_ERRLEVEL_LOW, mp);
1252 error = XFS_ERROR(EFSCORRUPTED);
1253 goto out_free_perag;
1254 }
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 /*
1257 * log's mount-time initialization. Perform 1st part recovery if needed
1258 */
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001259 error = xfs_log_mount(mp, mp->m_logdev_targp,
1260 XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
1261 XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
1262 if (error) {
1263 cmn_err(CE_WARN, "XFS: log mount failed");
1264 goto out_free_perag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266
1267 /*
David Chinner92821e22007-05-24 15:26:31 +10001268 * Now the log is mounted, we know if it was an unclean shutdown or
1269 * not. If it was, with the first phase of recovery has completed, we
1270 * have consistent AG blocks on disk. We have not recovered EFIs yet,
1271 * but they are recovered transactionally in the second recovery phase
1272 * later.
1273 *
1274 * Hence we can safely re-initialise incore superblock counters from
1275 * the per-ag data. These may not be correct if the filesystem was not
1276 * cleanly unmounted, so we need to wait for recovery to finish before
1277 * doing this.
1278 *
1279 * If the filesystem was cleanly unmounted, then we can trust the
1280 * values in the superblock to be correct and we don't need to do
1281 * anything here.
1282 *
1283 * If we are currently making the filesystem, the initialisation will
1284 * fail as the perag data is in an undefined state.
1285 */
David Chinner92821e22007-05-24 15:26:31 +10001286 if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
1287 !XFS_LAST_UNMOUNT_WAS_CLEAN(mp) &&
1288 !mp->m_sb.sb_inprogress) {
1289 error = xfs_initialize_perag_data(mp, sbp->sb_agcount);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001290 if (error)
1291 goto out_free_perag;
David Chinner92821e22007-05-24 15:26:31 +10001292 }
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001293
David Chinner92821e22007-05-24 15:26:31 +10001294 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 * Get and sanity-check the root inode.
1296 * Save the pointer to it in the mount structure.
1297 */
Dave Chinner7b6259e2010-06-24 11:35:17 +10001298 error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (error) {
1300 cmn_err(CE_WARN, "XFS: failed to read root inode");
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001301 goto out_log_dealloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
1303
1304 ASSERT(rip != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 if (unlikely((rip->i_d.di_mode & S_IFMT) != S_IFDIR)) {
1307 cmn_err(CE_WARN, "XFS: corrupted root inode");
Nathan Scottb6574522006-06-09 15:29:40 +10001308 cmn_err(CE_WARN, "Device %s - root %llu is not a directory",
1309 XFS_BUFTARG_NAME(mp->m_ddev_targp),
1310 (unsigned long long)rip->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 xfs_iunlock(rip, XFS_ILOCK_EXCL);
1312 XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
1313 mp);
1314 error = XFS_ERROR(EFSCORRUPTED);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001315 goto out_rele_rip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
1317 mp->m_rootip = rip; /* save it */
1318
1319 xfs_iunlock(rip, XFS_ILOCK_EXCL);
1320
1321 /*
1322 * Initialize realtime inode pointers in the mount structure
1323 */
Eric Sandeen0771fb42007-10-12 11:03:40 +10001324 error = xfs_rtmount_inodes(mp);
1325 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 /*
1327 * Free up the root inode.
1328 */
1329 cmn_err(CE_WARN, "XFS: failed to read RT inodes");
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001330 goto out_rele_rip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
1332
1333 /*
Christoph Hellwig7884bc82009-01-19 02:04:07 +01001334 * If this is a read-only mount defer the superblock updates until
1335 * the next remount into writeable mode. Otherwise we would never
1336 * perform the update e.g. for the root filesystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 */
Christoph Hellwig7884bc82009-01-19 02:04:07 +01001338 if (mp->m_update_flags && !(mp->m_flags & XFS_MOUNT_RDONLY)) {
1339 error = xfs_mount_log_sb(mp, mp->m_update_flags);
David Chinnere5720ee2008-04-10 12:21:18 +10001340 if (error) {
1341 cmn_err(CE_WARN, "XFS: failed to write sb changes");
Christoph Hellwigb93b6e42009-02-04 09:33:58 +01001342 goto out_rtunmount;
David Chinnere5720ee2008-04-10 12:21:18 +10001343 }
1344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 /*
1347 * Initialise the XFS quota management subsystem for this mount
1348 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001349 if (XFS_IS_QUOTA_RUNNING(mp)) {
1350 error = xfs_qm_newmount(mp, &quotamount, &quotaflags);
1351 if (error)
1352 goto out_rtunmount;
1353 } else {
1354 ASSERT(!XFS_IS_QUOTA_ON(mp));
1355
1356 /*
1357 * If a file system had quotas running earlier, but decided to
1358 * mount without -o uquota/pquota/gquota options, revoke the
1359 * quotachecked license.
1360 */
1361 if (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT) {
1362 cmn_err(CE_NOTE,
1363 "XFS: resetting qflags for filesystem %s",
1364 mp->m_fsname);
1365
1366 error = xfs_mount_reset_sbqflags(mp);
1367 if (error)
1368 return error;
1369 }
1370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 /*
1373 * Finish recovering the file system. This part needed to be
1374 * delayed until after the root and real-time bitmap inodes
1375 * were consistently read in.
1376 */
Christoph Hellwig42490232008-08-13 16:49:32 +10001377 error = xfs_log_mount_finish(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (error) {
1379 cmn_err(CE_WARN, "XFS: log mount finish failed");
Christoph Hellwigb93b6e42009-02-04 09:33:58 +01001380 goto out_rtunmount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
1382
1383 /*
1384 * Complete the quota initialisation, post-log-replay component.
1385 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001386 if (quotamount) {
1387 ASSERT(mp->m_qflags == 0);
1388 mp->m_qflags = quotaflags;
1389
1390 xfs_qm_mount_quotas(mp);
1391 }
1392
David Chinner84e1e992007-06-18 16:50:27 +10001393 /*
1394 * Now we are mounted, reserve a small amount of unused space for
1395 * privileged transactions. This is needed so that transaction
1396 * space required for critical operations can dip into this pool
1397 * when at ENOSPC. This is needed for operations like create with
1398 * attr, unwritten extent conversion at ENOSPC, etc. Data allocations
1399 * are not allowed to use this reserved space.
Dave Chinner8babd8a2010-03-04 01:46:25 +00001400 *
1401 * This may drive us straight to ENOSPC on mount, but that implies
1402 * we were already there on the last unmount. Warn if this occurs.
David Chinner84e1e992007-06-18 16:50:27 +10001403 */
Eric Sandeend5db0f92010-02-05 22:59:53 +00001404 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
1405 resblks = xfs_default_resblks(mp);
1406 error = xfs_reserve_blocks(mp, &resblks, NULL);
1407 if (error)
1408 cmn_err(CE_WARN, "XFS: Unable to allocate reserve "
1409 "blocks. Continuing without a reserve pool.");
1410 }
David Chinner84e1e992007-06-18 16:50:27 +10001411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return 0;
1413
Christoph Hellwigb93b6e42009-02-04 09:33:58 +01001414 out_rtunmount:
1415 xfs_rtunmount_inodes(mp);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001416 out_rele_rip:
Christoph Hellwig43355092008-03-27 18:01:08 +11001417 IRELE(rip);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001418 out_log_dealloc:
Christoph Hellwig21b699c2009-03-16 08:19:29 +01001419 xfs_log_unmount(mp);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001420 out_free_perag:
Christoph Hellwigff4f0382008-08-13 16:50:47 +10001421 xfs_free_perag(mp);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001422 out_remove_uuid:
Christoph Hellwig27174202009-03-30 10:21:31 +02001423 xfs_uuid_unmount(mp);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001424 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return error;
1426}
1427
1428/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 * This flushes out the inodes,dquots and the superblock, unmounts the
1430 * log and makes sure that incore structures are freed.
1431 */
Christoph Hellwig41b5c2e2008-08-13 16:49:57 +10001432void
1433xfs_unmountfs(
1434 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
Christoph Hellwig41b5c2e2008-08-13 16:49:57 +10001436 __uint64_t resblks;
1437 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Christoph Hellwig7d095252009-06-08 15:33:32 +02001439 xfs_qm_unmount_quotas(mp);
Christoph Hellwigb93b6e42009-02-04 09:33:58 +01001440 xfs_rtunmount_inodes(mp);
Christoph Hellwig77508ec2008-08-13 16:49:04 +10001441 IRELE(mp->m_rootip);
1442
David Chinner641c56f2007-06-18 16:50:17 +10001443 /*
1444 * We can potentially deadlock here if we have an inode cluster
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001445 * that has been freed has its buffer still pinned in memory because
David Chinner641c56f2007-06-18 16:50:17 +10001446 * the transaction is still sitting in a iclog. The stale inodes
1447 * on that buffer will have their flush locks held until the
1448 * transaction hits the disk and the callbacks run. the inode
1449 * flush takes the flush lock unconditionally and with nothing to
1450 * push out the iclog we will never get that unlocked. hence we
1451 * need to force the log first.
1452 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00001453 xfs_log_force(mp, XFS_LOG_SYNC);
Dave Chinnerc8543632010-02-06 12:39:36 +11001454
1455 /*
1456 * Do a delwri reclaim pass first so that as many dirty inodes are
1457 * queued up for IO as possible. Then flush the buffers before making
1458 * a synchronous path to catch all the remaining inodes are reclaimed.
1459 * This makes the reclaim process as quick as possible by avoiding
1460 * synchronous writeout and blocking on inodes already in the delwri
1461 * state as much as possible.
1462 */
1463 xfs_reclaim_inodes(mp, 0);
1464 XFS_bflush(mp->m_ddev_targp);
1465 xfs_reclaim_inodes(mp, SYNC_WAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Christoph Hellwig7d095252009-06-08 15:33:32 +02001467 xfs_qm_unmount(mp);
Lachlan McIlroya357a122008-10-30 16:53:25 +11001468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 /*
1470 * Flush out the log synchronously so that we know for sure
1471 * that nothing is pinned. This is important because bflush()
1472 * will skip pinned buffers.
1473 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00001474 xfs_log_force(mp, XFS_LOG_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476 xfs_binval(mp->m_ddev_targp);
1477 if (mp->m_rtdev_targp) {
1478 xfs_binval(mp->m_rtdev_targp);
1479 }
1480
David Chinner84e1e992007-06-18 16:50:27 +10001481 /*
1482 * Unreserve any blocks we have so that when we unmount we don't account
1483 * the reserved free space as used. This is really only necessary for
1484 * lazy superblock counting because it trusts the incore superblock
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001485 * counters to be absolutely correct on clean unmount.
David Chinner84e1e992007-06-18 16:50:27 +10001486 *
1487 * We don't bother correcting this elsewhere for lazy superblock
1488 * counting because on mount of an unclean filesystem we reconstruct the
1489 * correct counter value and this is irrelevant.
1490 *
1491 * For non-lazy counter filesystems, this doesn't matter at all because
1492 * we only every apply deltas to the superblock and hence the incore
1493 * value does not matter....
1494 */
1495 resblks = 0;
David Chinner714082b2008-04-10 12:20:03 +10001496 error = xfs_reserve_blocks(mp, &resblks, NULL);
1497 if (error)
1498 cmn_err(CE_WARN, "XFS: Unable to free reserved block pool. "
1499 "Freespace may not be correct on next mount.");
1500
David Chinnere5720ee2008-04-10 12:21:18 +10001501 error = xfs_log_sbcount(mp, 1);
1502 if (error)
1503 cmn_err(CE_WARN, "XFS: Unable to update superblock counters. "
1504 "Freespace may not be correct on next mount.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 xfs_unmountfs_writesb(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 xfs_unmountfs_wait(mp); /* wait for async bufs */
Christoph Hellwig21b699c2009-03-16 08:19:29 +01001507 xfs_log_unmount_write(mp);
1508 xfs_log_unmount(mp);
Christoph Hellwig27174202009-03-30 10:21:31 +02001509 xfs_uuid_unmount(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Christoph Hellwig1550d0b2008-08-13 16:17:37 +10001511#if defined(DEBUG)
Christoph Hellwig0ce4cfd2007-08-30 17:20:53 +10001512 xfs_errortag_clearall(mp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513#endif
Christoph Hellwigff4f0382008-08-13 16:50:47 +10001514 xfs_free_perag(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515}
1516
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001517STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518xfs_unmountfs_wait(xfs_mount_t *mp)
1519{
1520 if (mp->m_logdev_targp != mp->m_ddev_targp)
1521 xfs_wait_buftarg(mp->m_logdev_targp);
1522 if (mp->m_rtdev_targp)
1523 xfs_wait_buftarg(mp->m_rtdev_targp);
1524 xfs_wait_buftarg(mp->m_ddev_targp);
1525}
1526
1527int
David Chinner92821e22007-05-24 15:26:31 +10001528xfs_fs_writable(xfs_mount_t *mp)
1529{
Christoph Hellwigb267ce92007-08-30 17:21:30 +10001530 return !(xfs_test_for_freeze(mp) || XFS_FORCED_SHUTDOWN(mp) ||
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001531 (mp->m_flags & XFS_MOUNT_RDONLY));
David Chinner92821e22007-05-24 15:26:31 +10001532}
1533
1534/*
1535 * xfs_log_sbcount
1536 *
1537 * Called either periodically to keep the on disk superblock values
1538 * roughly up to date or from unmount to make sure the values are
1539 * correct on a clean unmount.
1540 *
1541 * Note this code can be called during the process of freezing, so
1542 * we may need to use the transaction allocator which does not not
1543 * block when the transaction subsystem is in its frozen state.
1544 */
1545int
1546xfs_log_sbcount(
1547 xfs_mount_t *mp,
1548 uint sync)
1549{
1550 xfs_trans_t *tp;
1551 int error;
1552
1553 if (!xfs_fs_writable(mp))
1554 return 0;
1555
Christoph Hellwigd4d90b52008-04-22 17:34:37 +10001556 xfs_icsb_sync_counters(mp, 0);
David Chinner92821e22007-05-24 15:26:31 +10001557
1558 /*
1559 * we don't need to do this if we are updating the superblock
1560 * counters on every modification.
1561 */
1562 if (!xfs_sb_version_haslazysbcount(&mp->m_sb))
1563 return 0;
1564
Christoph Hellwig80641dc2009-10-19 04:00:03 +00001565 tp = _xfs_trans_alloc(mp, XFS_TRANS_SB_COUNT, KM_SLEEP);
David Chinner92821e22007-05-24 15:26:31 +10001566 error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
1567 XFS_DEFAULT_LOG_COUNT);
1568 if (error) {
1569 xfs_trans_cancel(tp, 0);
1570 return error;
1571 }
1572
1573 xfs_mod_sb(tp, XFS_SB_IFREE | XFS_SB_ICOUNT | XFS_SB_FDBLOCKS);
1574 if (sync)
1575 xfs_trans_set_sync(tp);
David Chinnere5720ee2008-04-10 12:21:18 +10001576 error = xfs_trans_commit(tp, 0);
1577 return error;
David Chinner92821e22007-05-24 15:26:31 +10001578}
1579
1580int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581xfs_unmountfs_writesb(xfs_mount_t *mp)
1582{
1583 xfs_buf_t *sbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 int error = 0;
1585
1586 /*
1587 * skip superblock write if fs is read-only, or
1588 * if we are doing a forced umount.
1589 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001590 if (!((mp->m_flags & XFS_MOUNT_RDONLY) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 XFS_FORCED_SHUTDOWN(mp))) {
David Chinner8d280b92006-03-14 13:13:09 +11001592
David Chinner92821e22007-05-24 15:26:31 +10001593 sbp = xfs_getsb(mp, 0);
David Chinner8d280b92006-03-14 13:13:09 +11001594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 XFS_BUF_UNDONE(sbp);
1596 XFS_BUF_UNREAD(sbp);
1597 XFS_BUF_UNDELAYWRITE(sbp);
1598 XFS_BUF_WRITE(sbp);
1599 XFS_BUF_UNASYNC(sbp);
1600 ASSERT(XFS_BUF_TARGET(sbp) == mp->m_ddev_targp);
1601 xfsbdstrat(mp, sbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 error = xfs_iowait(sbp);
1603 if (error)
1604 xfs_ioerror_alert("xfs_unmountfs_writesb",
1605 mp, sbp, XFS_BUF_ADDR(sbp));
David Chinner92821e22007-05-24 15:26:31 +10001606 xfs_buf_relse(sbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001608 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609}
1610
1611/*
1612 * xfs_mod_sb() can be used to copy arbitrary changes to the
1613 * in-core superblock into the superblock buffer to be logged.
1614 * It does not provide the higher level of locking that is
1615 * needed to protect the in-core superblock from concurrent
1616 * access.
1617 */
1618void
1619xfs_mod_sb(xfs_trans_t *tp, __int64_t fields)
1620{
1621 xfs_buf_t *bp;
1622 int first;
1623 int last;
1624 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 xfs_sb_field_t f;
1626
1627 ASSERT(fields);
1628 if (!fields)
1629 return;
1630 mp = tp->t_mountp;
1631 bp = xfs_trans_getsb(tp, mp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 first = sizeof(xfs_sb_t);
1633 last = 0;
1634
1635 /* translate/copy */
1636
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +10001637 xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb, fields);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639 /* find modified range */
Dave Chinner587aa0f2010-01-20 12:04:53 +11001640 f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields);
1641 ASSERT((1LL << f) & XFS_SB_MOD_BITS);
1642 last = xfs_sb_info[f + 1].offset - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
1645 ASSERT((1LL << f) & XFS_SB_MOD_BITS);
1646 first = xfs_sb_info[f].offset;
1647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 xfs_trans_log_buf(tp, bp, first, last);
1649}
Yingping Lud210a282006-06-09 14:55:18 +10001650
Yingping Lud210a282006-06-09 14:55:18 +10001651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652/*
1653 * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply
1654 * a delta to a specified field in the in-core superblock. Simply
1655 * switch on the field indicated and apply the delta to that field.
1656 * Fields are not allowed to dip below zero, so if the delta would
1657 * do this do not apply it and return EINVAL.
1658 *
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001659 * The m_sb_lock must be held when this routine is called.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05001661STATIC int
David Chinner20f4ebf2007-02-10 18:36:10 +11001662xfs_mod_incore_sb_unlocked(
1663 xfs_mount_t *mp,
1664 xfs_sb_field_t field,
1665 int64_t delta,
1666 int rsvd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
1668 int scounter; /* short counter for 32 bit fields */
1669 long long lcounter; /* long counter for 64 bit fields */
1670 long long res_used, rem;
1671
1672 /*
1673 * With the in-core superblock spin lock held, switch
1674 * on the indicated field. Apply the delta to the
1675 * proper field. If the fields value would dip below
1676 * 0, then do not apply the delta and return EINVAL.
1677 */
1678 switch (field) {
1679 case XFS_SBS_ICOUNT:
1680 lcounter = (long long)mp->m_sb.sb_icount;
1681 lcounter += delta;
1682 if (lcounter < 0) {
1683 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001684 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
1686 mp->m_sb.sb_icount = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001687 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 case XFS_SBS_IFREE:
1689 lcounter = (long long)mp->m_sb.sb_ifree;
1690 lcounter += delta;
1691 if (lcounter < 0) {
1692 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001693 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 }
1695 mp->m_sb.sb_ifree = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001696 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 case XFS_SBS_FDBLOCKS:
David Chinner4be536d2006-09-07 14:26:50 +10001698 lcounter = (long long)
1699 mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
1701
1702 if (delta > 0) { /* Putting blocks back */
1703 if (res_used > delta) {
1704 mp->m_resblks_avail += delta;
1705 } else {
1706 rem = delta - res_used;
1707 mp->m_resblks_avail = mp->m_resblks;
1708 lcounter += rem;
1709 }
1710 } else { /* Taking blocks away */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 lcounter += delta;
Dave Chinner8babd8a2010-03-04 01:46:25 +00001712 if (lcounter >= 0) {
1713 mp->m_sb.sb_fdblocks = lcounter +
1714 XFS_ALLOC_SET_ASIDE(mp);
1715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 }
Dave Chinner8babd8a2010-03-04 01:46:25 +00001717
1718 /*
1719 * We are out of blocks, use any available reserved
1720 * blocks if were allowed to.
1721 */
1722 if (!rsvd)
1723 return XFS_ERROR(ENOSPC);
1724
1725 lcounter = (long long)mp->m_resblks_avail + delta;
1726 if (lcounter >= 0) {
1727 mp->m_resblks_avail = lcounter;
1728 return 0;
1729 }
1730 printk_once(KERN_WARNING
1731 "Filesystem \"%s\": reserve blocks depleted! "
1732 "Consider increasing reserve pool size.",
1733 mp->m_fsname);
1734 return XFS_ERROR(ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 }
1736
David Chinner4be536d2006-09-07 14:26:50 +10001737 mp->m_sb.sb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
Jesper Juhl014c2542006-01-15 02:37:08 +01001738 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 case XFS_SBS_FREXTENTS:
1740 lcounter = (long long)mp->m_sb.sb_frextents;
1741 lcounter += delta;
1742 if (lcounter < 0) {
Jesper Juhl014c2542006-01-15 02:37:08 +01001743 return XFS_ERROR(ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 }
1745 mp->m_sb.sb_frextents = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001746 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 case XFS_SBS_DBLOCKS:
1748 lcounter = (long long)mp->m_sb.sb_dblocks;
1749 lcounter += delta;
1750 if (lcounter < 0) {
1751 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001752 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 }
1754 mp->m_sb.sb_dblocks = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001755 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 case XFS_SBS_AGCOUNT:
1757 scounter = mp->m_sb.sb_agcount;
1758 scounter += delta;
1759 if (scounter < 0) {
1760 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001761 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
1763 mp->m_sb.sb_agcount = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001764 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 case XFS_SBS_IMAX_PCT:
1766 scounter = mp->m_sb.sb_imax_pct;
1767 scounter += delta;
1768 if (scounter < 0) {
1769 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001770 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 }
1772 mp->m_sb.sb_imax_pct = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001773 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 case XFS_SBS_REXTSIZE:
1775 scounter = mp->m_sb.sb_rextsize;
1776 scounter += delta;
1777 if (scounter < 0) {
1778 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001779 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
1781 mp->m_sb.sb_rextsize = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001782 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 case XFS_SBS_RBMBLOCKS:
1784 scounter = mp->m_sb.sb_rbmblocks;
1785 scounter += delta;
1786 if (scounter < 0) {
1787 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001788 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 }
1790 mp->m_sb.sb_rbmblocks = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001791 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 case XFS_SBS_RBLOCKS:
1793 lcounter = (long long)mp->m_sb.sb_rblocks;
1794 lcounter += delta;
1795 if (lcounter < 0) {
1796 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001797 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 }
1799 mp->m_sb.sb_rblocks = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001800 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 case XFS_SBS_REXTENTS:
1802 lcounter = (long long)mp->m_sb.sb_rextents;
1803 lcounter += delta;
1804 if (lcounter < 0) {
1805 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001806 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 }
1808 mp->m_sb.sb_rextents = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001809 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 case XFS_SBS_REXTSLOG:
1811 scounter = mp->m_sb.sb_rextslog;
1812 scounter += delta;
1813 if (scounter < 0) {
1814 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001815 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
1817 mp->m_sb.sb_rextslog = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001818 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 default:
1820 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001821 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 }
1823}
1824
1825/*
1826 * xfs_mod_incore_sb() is used to change a field in the in-core
1827 * superblock structure by the specified delta. This modification
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001828 * is protected by the m_sb_lock. Just use the xfs_mod_incore_sb_unlocked()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 * routine to do the work.
1830 */
1831int
David Chinner20f4ebf2007-02-10 18:36:10 +11001832xfs_mod_incore_sb(
1833 xfs_mount_t *mp,
1834 xfs_sb_field_t field,
1835 int64_t delta,
1836 int rsvd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 int status;
1839
David Chinner8d280b92006-03-14 13:13:09 +11001840 /* check for per-cpu counters */
1841 switch (field) {
1842#ifdef HAVE_PERCPU_SB
1843 case XFS_SBS_ICOUNT:
1844 case XFS_SBS_IFREE:
1845 case XFS_SBS_FDBLOCKS:
1846 if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
1847 status = xfs_icsb_modify_counters(mp, field,
1848 delta, rsvd);
1849 break;
1850 }
1851 /* FALLTHROUGH */
1852#endif
1853 default:
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001854 spin_lock(&mp->m_sb_lock);
David Chinner8d280b92006-03-14 13:13:09 +11001855 status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001856 spin_unlock(&mp->m_sb_lock);
David Chinner8d280b92006-03-14 13:13:09 +11001857 break;
1858 }
1859
Jesper Juhl014c2542006-01-15 02:37:08 +01001860 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861}
1862
1863/*
1864 * xfs_mod_incore_sb_batch() is used to change more than one field
1865 * in the in-core superblock structure at a time. This modification
1866 * is protected by a lock internal to this module. The fields and
1867 * changes to those fields are specified in the array of xfs_mod_sb
1868 * structures passed in.
1869 *
1870 * Either all of the specified deltas will be applied or none of
1871 * them will. If any modified field dips below 0, then all modifications
1872 * will be backed out and EINVAL will be returned.
1873 */
1874int
1875xfs_mod_incore_sb_batch(xfs_mount_t *mp, xfs_mod_sb_t *msb, uint nmsb, int rsvd)
1876{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 int status=0;
1878 xfs_mod_sb_t *msbp;
1879
1880 /*
1881 * Loop through the array of mod structures and apply each
1882 * individually. If any fail, then back out all those
1883 * which have already been applied. Do all of this within
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001884 * the scope of the m_sb_lock so that all of the changes will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 * be atomic.
1886 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001887 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 msbp = &msb[0];
1889 for (msbp = &msbp[0]; msbp < (msb + nmsb); msbp++) {
1890 /*
1891 * Apply the delta at index n. If it fails, break
1892 * from the loop so we'll fall into the undo loop
1893 * below.
1894 */
David Chinner8d280b92006-03-14 13:13:09 +11001895 switch (msbp->msb_field) {
1896#ifdef HAVE_PERCPU_SB
1897 case XFS_SBS_ICOUNT:
1898 case XFS_SBS_IFREE:
1899 case XFS_SBS_FDBLOCKS:
1900 if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001901 spin_unlock(&mp->m_sb_lock);
David Chinner20b64282007-02-10 18:35:09 +11001902 status = xfs_icsb_modify_counters(mp,
David Chinner8d280b92006-03-14 13:13:09 +11001903 msbp->msb_field,
1904 msbp->msb_delta, rsvd);
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001905 spin_lock(&mp->m_sb_lock);
David Chinner8d280b92006-03-14 13:13:09 +11001906 break;
1907 }
1908 /* FALLTHROUGH */
1909#endif
1910 default:
1911 status = xfs_mod_incore_sb_unlocked(mp,
1912 msbp->msb_field,
1913 msbp->msb_delta, rsvd);
1914 break;
1915 }
1916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 if (status != 0) {
1918 break;
1919 }
1920 }
1921
1922 /*
1923 * If we didn't complete the loop above, then back out
1924 * any changes made to the superblock. If you add code
1925 * between the loop above and here, make sure that you
1926 * preserve the value of status. Loop back until
1927 * we step below the beginning of the array. Make sure
1928 * we don't touch anything back there.
1929 */
1930 if (status != 0) {
1931 msbp--;
1932 while (msbp >= msb) {
David Chinner8d280b92006-03-14 13:13:09 +11001933 switch (msbp->msb_field) {
1934#ifdef HAVE_PERCPU_SB
1935 case XFS_SBS_ICOUNT:
1936 case XFS_SBS_IFREE:
1937 case XFS_SBS_FDBLOCKS:
1938 if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001939 spin_unlock(&mp->m_sb_lock);
David Chinner20b64282007-02-10 18:35:09 +11001940 status = xfs_icsb_modify_counters(mp,
David Chinner8d280b92006-03-14 13:13:09 +11001941 msbp->msb_field,
1942 -(msbp->msb_delta),
1943 rsvd);
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001944 spin_lock(&mp->m_sb_lock);
David Chinner8d280b92006-03-14 13:13:09 +11001945 break;
1946 }
1947 /* FALLTHROUGH */
1948#endif
1949 default:
1950 status = xfs_mod_incore_sb_unlocked(mp,
1951 msbp->msb_field,
1952 -(msbp->msb_delta),
1953 rsvd);
1954 break;
1955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 ASSERT(status == 0);
1957 msbp--;
1958 }
1959 }
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001960 spin_unlock(&mp->m_sb_lock);
Jesper Juhl014c2542006-01-15 02:37:08 +01001961 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962}
1963
1964/*
1965 * xfs_getsb() is called to obtain the buffer for the superblock.
1966 * The buffer is returned locked and read in from disk.
1967 * The buffer should be released with a call to xfs_brelse().
1968 *
1969 * If the flags parameter is BUF_TRYLOCK, then we'll only return
1970 * the superblock buffer if it can be locked without sleeping.
1971 * If it can't then we'll return NULL.
1972 */
1973xfs_buf_t *
1974xfs_getsb(
1975 xfs_mount_t *mp,
1976 int flags)
1977{
1978 xfs_buf_t *bp;
1979
1980 ASSERT(mp->m_sb_bp != NULL);
1981 bp = mp->m_sb_bp;
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001982 if (flags & XBF_TRYLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 if (!XFS_BUF_CPSEMA(bp)) {
1984 return NULL;
1985 }
1986 } else {
1987 XFS_BUF_PSEMA(bp, PRIBIO);
1988 }
1989 XFS_BUF_HOLD(bp);
1990 ASSERT(XFS_BUF_ISDONE(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001991 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992}
1993
1994/*
1995 * Used to free the superblock along various error paths.
1996 */
1997void
1998xfs_freesb(
1999 xfs_mount_t *mp)
2000{
2001 xfs_buf_t *bp;
2002
2003 /*
2004 * Use xfs_getsb() so that the buffer will be locked
2005 * when we call xfs_buf_relse().
2006 */
2007 bp = xfs_getsb(mp, 0);
2008 XFS_BUF_UNMANAGE(bp);
2009 xfs_buf_relse(bp);
2010 mp->m_sb_bp = NULL;
2011}
2012
2013/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 * Used to log changes to the superblock unit and width fields which could
Eric Sandeene6957ea2008-04-10 12:19:34 +10002015 * be altered by the mount options, as well as any potential sb_features2
2016 * fixup. Only the first superblock is updated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 */
Christoph Hellwig7884bc82009-01-19 02:04:07 +01002018int
David Chinneree1c0902008-03-06 13:45:50 +11002019xfs_mount_log_sb(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 xfs_mount_t *mp,
2021 __int64_t fields)
2022{
2023 xfs_trans_t *tp;
David Chinnere5720ee2008-04-10 12:21:18 +10002024 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
David Chinneree1c0902008-03-06 13:45:50 +11002026 ASSERT(fields & (XFS_SB_UNIT | XFS_SB_WIDTH | XFS_SB_UUID |
David Chinner4b166de2008-05-20 11:30:27 +10002027 XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2 |
2028 XFS_SB_VERSIONNUM));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
2030 tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT);
David Chinnere5720ee2008-04-10 12:21:18 +10002031 error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
2032 XFS_DEFAULT_LOG_COUNT);
2033 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 xfs_trans_cancel(tp, 0);
David Chinnere5720ee2008-04-10 12:21:18 +10002035 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 }
2037 xfs_mod_sb(tp, fields);
David Chinnere5720ee2008-04-10 12:21:18 +10002038 error = xfs_trans_commit(tp, 0);
2039 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040}
David Chinner8d280b92006-03-14 13:13:09 +11002041
Christoph Hellwigdda35b82010-02-15 09:44:46 +00002042/*
2043 * If the underlying (data/log/rt) device is readonly, there are some
2044 * operations that cannot proceed.
2045 */
2046int
2047xfs_dev_is_read_only(
2048 struct xfs_mount *mp,
2049 char *message)
2050{
2051 if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
2052 xfs_readonly_buftarg(mp->m_logdev_targp) ||
2053 (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
2054 cmn_err(CE_NOTE,
2055 "XFS: %s required on read-only device.", message);
2056 cmn_err(CE_NOTE,
2057 "XFS: write access unavailable, cannot proceed.");
2058 return EROFS;
2059 }
2060 return 0;
2061}
David Chinner8d280b92006-03-14 13:13:09 +11002062
2063#ifdef HAVE_PERCPU_SB
2064/*
2065 * Per-cpu incore superblock counters
2066 *
2067 * Simple concept, difficult implementation
2068 *
2069 * Basically, replace the incore superblock counters with a distributed per cpu
2070 * counter for contended fields (e.g. free block count).
2071 *
2072 * Difficulties arise in that the incore sb is used for ENOSPC checking, and
2073 * hence needs to be accurately read when we are running low on space. Hence
2074 * there is a method to enable and disable the per-cpu counters based on how
2075 * much "stuff" is available in them.
2076 *
2077 * Basically, a counter is enabled if there is enough free resource to justify
2078 * running a per-cpu fast-path. If the per-cpu counter runs out (i.e. a local
2079 * ENOSPC), then we disable the counters to synchronise all callers and
2080 * re-distribute the available resources.
2081 *
2082 * If, once we redistributed the available resources, we still get a failure,
2083 * we disable the per-cpu counter and go through the slow path.
2084 *
2085 * The slow path is the current xfs_mod_incore_sb() function. This means that
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002086 * when we disable a per-cpu counter, we need to drain its resources back to
David Chinner8d280b92006-03-14 13:13:09 +11002087 * the global superblock. We do this after disabling the counter to prevent
2088 * more threads from queueing up on the counter.
2089 *
2090 * Essentially, this means that we still need a lock in the fast path to enable
2091 * synchronisation between the global counters and the per-cpu counters. This
2092 * is not a problem because the lock will be local to a CPU almost all the time
2093 * and have little contention except when we get to ENOSPC conditions.
2094 *
2095 * Basically, this lock becomes a barrier that enables us to lock out the fast
2096 * path while we do things like enabling and disabling counters and
2097 * synchronising the counters.
2098 *
2099 * Locking rules:
2100 *
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002101 * 1. m_sb_lock before picking up per-cpu locks
David Chinner8d280b92006-03-14 13:13:09 +11002102 * 2. per-cpu locks always picked up via for_each_online_cpu() order
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002103 * 3. accurate counter sync requires m_sb_lock + per cpu locks
David Chinner8d280b92006-03-14 13:13:09 +11002104 * 4. modifying per-cpu counters requires holding per-cpu lock
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002105 * 5. modifying global counters requires holding m_sb_lock
2106 * 6. enabling or disabling a counter requires holding the m_sb_lock
David Chinner8d280b92006-03-14 13:13:09 +11002107 * and _none_ of the per-cpu locks.
2108 *
2109 * Disabled counters are only ever re-enabled by a balance operation
2110 * that results in more free resources per CPU than a given threshold.
2111 * To ensure counters don't remain disabled, they are rebalanced when
2112 * the global resource goes above a higher threshold (i.e. some hysteresis
2113 * is present to prevent thrashing).
David Chinner8d280b92006-03-14 13:13:09 +11002114 */
David Chinnere8234a62006-03-14 13:23:52 +11002115
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07002116#ifdef CONFIG_HOTPLUG_CPU
David Chinnere8234a62006-03-14 13:23:52 +11002117/*
2118 * hot-plug CPU notifier support.
2119 *
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07002120 * We need a notifier per filesystem as we need to be able to identify
2121 * the filesystem to balance the counters out. This is achieved by
2122 * having a notifier block embedded in the xfs_mount_t and doing pointer
2123 * magic to get the mount pointer from the notifier block address.
David Chinnere8234a62006-03-14 13:23:52 +11002124 */
2125STATIC int
2126xfs_icsb_cpu_notify(
2127 struct notifier_block *nfb,
2128 unsigned long action,
2129 void *hcpu)
2130{
2131 xfs_icsb_cnts_t *cntp;
2132 xfs_mount_t *mp;
David Chinnere8234a62006-03-14 13:23:52 +11002133
2134 mp = (xfs_mount_t *)container_of(nfb, xfs_mount_t, m_icsb_notifier);
2135 cntp = (xfs_icsb_cnts_t *)
2136 per_cpu_ptr(mp->m_sb_cnts, (unsigned long)hcpu);
2137 switch (action) {
2138 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002139 case CPU_UP_PREPARE_FROZEN:
David Chinnere8234a62006-03-14 13:23:52 +11002140 /* Easy Case - initialize the area and locks, and
2141 * then rebalance when online does everything else for us. */
David Chinner01e1b692006-03-14 13:29:16 +11002142 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
David Chinnere8234a62006-03-14 13:23:52 +11002143 break;
2144 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002145 case CPU_ONLINE_FROZEN:
David Chinner03135cf2007-02-10 18:35:15 +11002146 xfs_icsb_lock(mp);
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002147 xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
2148 xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
2149 xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
David Chinner03135cf2007-02-10 18:35:15 +11002150 xfs_icsb_unlock(mp);
David Chinnere8234a62006-03-14 13:23:52 +11002151 break;
2152 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002153 case CPU_DEAD_FROZEN:
David Chinnere8234a62006-03-14 13:23:52 +11002154 /* Disable all the counters, then fold the dead cpu's
2155 * count into the total on the global superblock and
2156 * re-enable the counters. */
David Chinner03135cf2007-02-10 18:35:15 +11002157 xfs_icsb_lock(mp);
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002158 spin_lock(&mp->m_sb_lock);
David Chinnere8234a62006-03-14 13:23:52 +11002159 xfs_icsb_disable_counter(mp, XFS_SBS_ICOUNT);
2160 xfs_icsb_disable_counter(mp, XFS_SBS_IFREE);
2161 xfs_icsb_disable_counter(mp, XFS_SBS_FDBLOCKS);
2162
2163 mp->m_sb.sb_icount += cntp->icsb_icount;
2164 mp->m_sb.sb_ifree += cntp->icsb_ifree;
2165 mp->m_sb.sb_fdblocks += cntp->icsb_fdblocks;
2166
David Chinner01e1b692006-03-14 13:29:16 +11002167 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
David Chinnere8234a62006-03-14 13:23:52 +11002168
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002169 xfs_icsb_balance_counter_locked(mp, XFS_SBS_ICOUNT, 0);
2170 xfs_icsb_balance_counter_locked(mp, XFS_SBS_IFREE, 0);
2171 xfs_icsb_balance_counter_locked(mp, XFS_SBS_FDBLOCKS, 0);
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002172 spin_unlock(&mp->m_sb_lock);
David Chinner03135cf2007-02-10 18:35:15 +11002173 xfs_icsb_unlock(mp);
David Chinnere8234a62006-03-14 13:23:52 +11002174 break;
2175 }
2176
2177 return NOTIFY_OK;
2178}
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07002179#endif /* CONFIG_HOTPLUG_CPU */
David Chinnere8234a62006-03-14 13:23:52 +11002180
David Chinner8d280b92006-03-14 13:13:09 +11002181int
2182xfs_icsb_init_counters(
2183 xfs_mount_t *mp)
2184{
2185 xfs_icsb_cnts_t *cntp;
2186 int i;
2187
2188 mp->m_sb_cnts = alloc_percpu(xfs_icsb_cnts_t);
2189 if (mp->m_sb_cnts == NULL)
2190 return -ENOMEM;
2191
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07002192#ifdef CONFIG_HOTPLUG_CPU
David Chinnere8234a62006-03-14 13:23:52 +11002193 mp->m_icsb_notifier.notifier_call = xfs_icsb_cpu_notify;
2194 mp->m_icsb_notifier.priority = 0;
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07002195 register_hotcpu_notifier(&mp->m_icsb_notifier);
2196#endif /* CONFIG_HOTPLUG_CPU */
David Chinnere8234a62006-03-14 13:23:52 +11002197
David Chinner8d280b92006-03-14 13:13:09 +11002198 for_each_online_cpu(i) {
2199 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
David Chinner01e1b692006-03-14 13:29:16 +11002200 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
David Chinner8d280b92006-03-14 13:13:09 +11002201 }
David Chinner20b64282007-02-10 18:35:09 +11002202
2203 mutex_init(&mp->m_icsb_mutex);
2204
David Chinner8d280b92006-03-14 13:13:09 +11002205 /*
2206 * start with all counters disabled so that the
2207 * initial balance kicks us off correctly
2208 */
2209 mp->m_icsb_counters = -1;
2210 return 0;
2211}
2212
Lachlan McIlroy5478eea2007-02-10 18:36:29 +11002213void
2214xfs_icsb_reinit_counters(
2215 xfs_mount_t *mp)
2216{
2217 xfs_icsb_lock(mp);
2218 /*
2219 * start with all counters disabled so that the
2220 * initial balance kicks us off correctly
2221 */
2222 mp->m_icsb_counters = -1;
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002223 xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
2224 xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
2225 xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
Lachlan McIlroy5478eea2007-02-10 18:36:29 +11002226 xfs_icsb_unlock(mp);
2227}
2228
Christoph Hellwigc962fb72008-05-20 15:10:52 +10002229void
David Chinner8d280b92006-03-14 13:13:09 +11002230xfs_icsb_destroy_counters(
2231 xfs_mount_t *mp)
2232{
David Chinnere8234a62006-03-14 13:23:52 +11002233 if (mp->m_sb_cnts) {
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07002234 unregister_hotcpu_notifier(&mp->m_icsb_notifier);
David Chinner8d280b92006-03-14 13:13:09 +11002235 free_percpu(mp->m_sb_cnts);
David Chinnere8234a62006-03-14 13:23:52 +11002236 }
David Chinner03135cf2007-02-10 18:35:15 +11002237 mutex_destroy(&mp->m_icsb_mutex);
David Chinner8d280b92006-03-14 13:13:09 +11002238}
2239
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00002240STATIC void
David Chinner01e1b692006-03-14 13:29:16 +11002241xfs_icsb_lock_cntr(
2242 xfs_icsb_cnts_t *icsbp)
2243{
2244 while (test_and_set_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags)) {
2245 ndelay(1000);
2246 }
2247}
2248
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00002249STATIC void
David Chinner01e1b692006-03-14 13:29:16 +11002250xfs_icsb_unlock_cntr(
2251 xfs_icsb_cnts_t *icsbp)
2252{
2253 clear_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags);
2254}
2255
David Chinner8d280b92006-03-14 13:13:09 +11002256
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00002257STATIC void
David Chinner8d280b92006-03-14 13:13:09 +11002258xfs_icsb_lock_all_counters(
2259 xfs_mount_t *mp)
2260{
2261 xfs_icsb_cnts_t *cntp;
2262 int i;
2263
2264 for_each_online_cpu(i) {
2265 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
David Chinner01e1b692006-03-14 13:29:16 +11002266 xfs_icsb_lock_cntr(cntp);
David Chinner8d280b92006-03-14 13:13:09 +11002267 }
2268}
2269
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00002270STATIC void
David Chinner8d280b92006-03-14 13:13:09 +11002271xfs_icsb_unlock_all_counters(
2272 xfs_mount_t *mp)
2273{
2274 xfs_icsb_cnts_t *cntp;
2275 int i;
2276
2277 for_each_online_cpu(i) {
2278 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
David Chinner01e1b692006-03-14 13:29:16 +11002279 xfs_icsb_unlock_cntr(cntp);
David Chinner8d280b92006-03-14 13:13:09 +11002280 }
2281}
2282
2283STATIC void
2284xfs_icsb_count(
2285 xfs_mount_t *mp,
2286 xfs_icsb_cnts_t *cnt,
2287 int flags)
2288{
2289 xfs_icsb_cnts_t *cntp;
2290 int i;
2291
2292 memset(cnt, 0, sizeof(xfs_icsb_cnts_t));
2293
2294 if (!(flags & XFS_ICSB_LAZY_COUNT))
2295 xfs_icsb_lock_all_counters(mp);
2296
2297 for_each_online_cpu(i) {
2298 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
2299 cnt->icsb_icount += cntp->icsb_icount;
2300 cnt->icsb_ifree += cntp->icsb_ifree;
2301 cnt->icsb_fdblocks += cntp->icsb_fdblocks;
2302 }
2303
2304 if (!(flags & XFS_ICSB_LAZY_COUNT))
2305 xfs_icsb_unlock_all_counters(mp);
2306}
2307
2308STATIC int
2309xfs_icsb_counter_disabled(
2310 xfs_mount_t *mp,
2311 xfs_sb_field_t field)
2312{
2313 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
2314 return test_bit(field, &mp->m_icsb_counters);
2315}
2316
David Chinner36fbe6e2008-04-10 12:19:56 +10002317STATIC void
David Chinner8d280b92006-03-14 13:13:09 +11002318xfs_icsb_disable_counter(
2319 xfs_mount_t *mp,
2320 xfs_sb_field_t field)
2321{
2322 xfs_icsb_cnts_t cnt;
2323
2324 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
2325
David Chinner20b64282007-02-10 18:35:09 +11002326 /*
2327 * If we are already disabled, then there is nothing to do
2328 * here. We check before locking all the counters to avoid
2329 * the expensive lock operation when being called in the
2330 * slow path and the counter is already disabled. This is
2331 * safe because the only time we set or clear this state is under
2332 * the m_icsb_mutex.
2333 */
2334 if (xfs_icsb_counter_disabled(mp, field))
David Chinner36fbe6e2008-04-10 12:19:56 +10002335 return;
David Chinner20b64282007-02-10 18:35:09 +11002336
David Chinner8d280b92006-03-14 13:13:09 +11002337 xfs_icsb_lock_all_counters(mp);
2338 if (!test_and_set_bit(field, &mp->m_icsb_counters)) {
2339 /* drain back to superblock */
2340
Christoph Hellwigce461932008-04-22 17:34:50 +10002341 xfs_icsb_count(mp, &cnt, XFS_ICSB_LAZY_COUNT);
David Chinner8d280b92006-03-14 13:13:09 +11002342 switch(field) {
2343 case XFS_SBS_ICOUNT:
2344 mp->m_sb.sb_icount = cnt.icsb_icount;
2345 break;
2346 case XFS_SBS_IFREE:
2347 mp->m_sb.sb_ifree = cnt.icsb_ifree;
2348 break;
2349 case XFS_SBS_FDBLOCKS:
2350 mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
2351 break;
2352 default:
2353 BUG();
2354 }
2355 }
2356
2357 xfs_icsb_unlock_all_counters(mp);
David Chinner8d280b92006-03-14 13:13:09 +11002358}
2359
2360STATIC void
2361xfs_icsb_enable_counter(
2362 xfs_mount_t *mp,
2363 xfs_sb_field_t field,
2364 uint64_t count,
2365 uint64_t resid)
2366{
2367 xfs_icsb_cnts_t *cntp;
2368 int i;
2369
2370 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
2371
2372 xfs_icsb_lock_all_counters(mp);
2373 for_each_online_cpu(i) {
2374 cntp = per_cpu_ptr(mp->m_sb_cnts, i);
2375 switch (field) {
2376 case XFS_SBS_ICOUNT:
2377 cntp->icsb_icount = count + resid;
2378 break;
2379 case XFS_SBS_IFREE:
2380 cntp->icsb_ifree = count + resid;
2381 break;
2382 case XFS_SBS_FDBLOCKS:
2383 cntp->icsb_fdblocks = count + resid;
2384 break;
2385 default:
2386 BUG();
2387 break;
2388 }
2389 resid = 0;
2390 }
2391 clear_bit(field, &mp->m_icsb_counters);
2392 xfs_icsb_unlock_all_counters(mp);
2393}
2394
David Chinnerdbcabad2007-02-10 18:36:17 +11002395void
Christoph Hellwigd4d90b52008-04-22 17:34:37 +10002396xfs_icsb_sync_counters_locked(
David Chinner8d280b92006-03-14 13:13:09 +11002397 xfs_mount_t *mp,
2398 int flags)
2399{
2400 xfs_icsb_cnts_t cnt;
David Chinner8d280b92006-03-14 13:13:09 +11002401
David Chinner8d280b92006-03-14 13:13:09 +11002402 xfs_icsb_count(mp, &cnt, flags);
2403
David Chinner8d280b92006-03-14 13:13:09 +11002404 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_ICOUNT))
2405 mp->m_sb.sb_icount = cnt.icsb_icount;
2406 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_IFREE))
2407 mp->m_sb.sb_ifree = cnt.icsb_ifree;
2408 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_FDBLOCKS))
2409 mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
David Chinner8d280b92006-03-14 13:13:09 +11002410}
2411
2412/*
2413 * Accurate update of per-cpu counters to incore superblock
2414 */
Christoph Hellwigd4d90b52008-04-22 17:34:37 +10002415void
David Chinner8d280b92006-03-14 13:13:09 +11002416xfs_icsb_sync_counters(
Christoph Hellwigd4d90b52008-04-22 17:34:37 +10002417 xfs_mount_t *mp,
2418 int flags)
David Chinner8d280b92006-03-14 13:13:09 +11002419{
Christoph Hellwigd4d90b52008-04-22 17:34:37 +10002420 spin_lock(&mp->m_sb_lock);
2421 xfs_icsb_sync_counters_locked(mp, flags);
2422 spin_unlock(&mp->m_sb_lock);
David Chinner8d280b92006-03-14 13:13:09 +11002423}
2424
2425/*
2426 * Balance and enable/disable counters as necessary.
2427 *
David Chinner20b64282007-02-10 18:35:09 +11002428 * Thresholds for re-enabling counters are somewhat magic. inode counts are
2429 * chosen to be the same number as single on disk allocation chunk per CPU, and
2430 * free blocks is something far enough zero that we aren't going thrash when we
2431 * get near ENOSPC. We also need to supply a minimum we require per cpu to
2432 * prevent looping endlessly when xfs_alloc_space asks for more than will
2433 * be distributed to a single CPU but each CPU has enough blocks to be
2434 * reenabled.
2435 *
2436 * Note that we can be called when counters are already disabled.
2437 * xfs_icsb_disable_counter() optimises the counter locking in this case to
2438 * prevent locking every per-cpu counter needlessly.
David Chinner8d280b92006-03-14 13:13:09 +11002439 */
David Chinner20b64282007-02-10 18:35:09 +11002440
2441#define XFS_ICSB_INO_CNTR_REENABLE (uint64_t)64
David Chinner4be536d2006-09-07 14:26:50 +10002442#define XFS_ICSB_FDBLK_CNTR_REENABLE(mp) \
David Chinner20b64282007-02-10 18:35:09 +11002443 (uint64_t)(512 + XFS_ALLOC_SET_ASIDE(mp))
David Chinner8d280b92006-03-14 13:13:09 +11002444STATIC void
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002445xfs_icsb_balance_counter_locked(
David Chinner8d280b92006-03-14 13:13:09 +11002446 xfs_mount_t *mp,
2447 xfs_sb_field_t field,
David Chinner20b64282007-02-10 18:35:09 +11002448 int min_per_cpu)
David Chinner8d280b92006-03-14 13:13:09 +11002449{
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10002450 uint64_t count, resid;
David Chinner8d280b92006-03-14 13:13:09 +11002451 int weight = num_online_cpus();
David Chinner20b64282007-02-10 18:35:09 +11002452 uint64_t min = (uint64_t)min_per_cpu;
David Chinner8d280b92006-03-14 13:13:09 +11002453
David Chinner8d280b92006-03-14 13:13:09 +11002454 /* disable counter and sync counter */
2455 xfs_icsb_disable_counter(mp, field);
2456
2457 /* update counters - first CPU gets residual*/
2458 switch (field) {
2459 case XFS_SBS_ICOUNT:
2460 count = mp->m_sb.sb_icount;
2461 resid = do_div(count, weight);
David Chinner20b64282007-02-10 18:35:09 +11002462 if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002463 return;
David Chinner8d280b92006-03-14 13:13:09 +11002464 break;
2465 case XFS_SBS_IFREE:
2466 count = mp->m_sb.sb_ifree;
2467 resid = do_div(count, weight);
David Chinner20b64282007-02-10 18:35:09 +11002468 if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002469 return;
David Chinner8d280b92006-03-14 13:13:09 +11002470 break;
2471 case XFS_SBS_FDBLOCKS:
2472 count = mp->m_sb.sb_fdblocks;
2473 resid = do_div(count, weight);
David Chinner20b64282007-02-10 18:35:09 +11002474 if (count < max(min, XFS_ICSB_FDBLK_CNTR_REENABLE(mp)))
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002475 return;
David Chinner8d280b92006-03-14 13:13:09 +11002476 break;
2477 default:
2478 BUG();
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10002479 count = resid = 0; /* quiet, gcc */
David Chinner8d280b92006-03-14 13:13:09 +11002480 break;
2481 }
2482
2483 xfs_icsb_enable_counter(mp, field, count, resid);
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002484}
2485
2486STATIC void
2487xfs_icsb_balance_counter(
2488 xfs_mount_t *mp,
2489 xfs_sb_field_t fields,
2490 int min_per_cpu)
2491{
2492 spin_lock(&mp->m_sb_lock);
2493 xfs_icsb_balance_counter_locked(mp, fields, min_per_cpu);
2494 spin_unlock(&mp->m_sb_lock);
David Chinner8d280b92006-03-14 13:13:09 +11002495}
2496
David Chinnera8272ce2007-11-23 16:28:09 +11002497STATIC int
David Chinner20b64282007-02-10 18:35:09 +11002498xfs_icsb_modify_counters(
David Chinner8d280b92006-03-14 13:13:09 +11002499 xfs_mount_t *mp,
2500 xfs_sb_field_t field,
David Chinner20f4ebf2007-02-10 18:36:10 +11002501 int64_t delta,
David Chinner20b64282007-02-10 18:35:09 +11002502 int rsvd)
David Chinner8d280b92006-03-14 13:13:09 +11002503{
2504 xfs_icsb_cnts_t *icsbp;
2505 long long lcounter; /* long counter for 64 bit fields */
Christoph Lameter7a9e02d2009-10-03 19:48:23 +09002506 int ret = 0;
David Chinner8d280b92006-03-14 13:13:09 +11002507
David Chinner20b64282007-02-10 18:35:09 +11002508 might_sleep();
David Chinner8d280b92006-03-14 13:13:09 +11002509again:
Christoph Lameter7a9e02d2009-10-03 19:48:23 +09002510 preempt_disable();
2511 icsbp = this_cpu_ptr(mp->m_sb_cnts);
David Chinner20b64282007-02-10 18:35:09 +11002512
2513 /*
2514 * if the counter is disabled, go to slow path
2515 */
David Chinner8d280b92006-03-14 13:13:09 +11002516 if (unlikely(xfs_icsb_counter_disabled(mp, field)))
2517 goto slow_path;
David Chinner20b64282007-02-10 18:35:09 +11002518 xfs_icsb_lock_cntr(icsbp);
2519 if (unlikely(xfs_icsb_counter_disabled(mp, field))) {
2520 xfs_icsb_unlock_cntr(icsbp);
2521 goto slow_path;
2522 }
David Chinner8d280b92006-03-14 13:13:09 +11002523
2524 switch (field) {
2525 case XFS_SBS_ICOUNT:
2526 lcounter = icsbp->icsb_icount;
2527 lcounter += delta;
2528 if (unlikely(lcounter < 0))
David Chinner20b64282007-02-10 18:35:09 +11002529 goto balance_counter;
David Chinner8d280b92006-03-14 13:13:09 +11002530 icsbp->icsb_icount = lcounter;
2531 break;
2532
2533 case XFS_SBS_IFREE:
2534 lcounter = icsbp->icsb_ifree;
2535 lcounter += delta;
2536 if (unlikely(lcounter < 0))
David Chinner20b64282007-02-10 18:35:09 +11002537 goto balance_counter;
David Chinner8d280b92006-03-14 13:13:09 +11002538 icsbp->icsb_ifree = lcounter;
2539 break;
2540
2541 case XFS_SBS_FDBLOCKS:
2542 BUG_ON((mp->m_resblks - mp->m_resblks_avail) != 0);
2543
David Chinner4be536d2006-09-07 14:26:50 +10002544 lcounter = icsbp->icsb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
David Chinner8d280b92006-03-14 13:13:09 +11002545 lcounter += delta;
2546 if (unlikely(lcounter < 0))
David Chinner20b64282007-02-10 18:35:09 +11002547 goto balance_counter;
David Chinner4be536d2006-09-07 14:26:50 +10002548 icsbp->icsb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
David Chinner8d280b92006-03-14 13:13:09 +11002549 break;
2550 default:
2551 BUG();
2552 break;
2553 }
David Chinner01e1b692006-03-14 13:29:16 +11002554 xfs_icsb_unlock_cntr(icsbp);
Christoph Lameter7a9e02d2009-10-03 19:48:23 +09002555 preempt_enable();
David Chinner8d280b92006-03-14 13:13:09 +11002556 return 0;
2557
David Chinner8d280b92006-03-14 13:13:09 +11002558slow_path:
Christoph Lameter7a9e02d2009-10-03 19:48:23 +09002559 preempt_enable();
David Chinner20b64282007-02-10 18:35:09 +11002560
2561 /*
2562 * serialise with a mutex so we don't burn lots of cpu on
2563 * the superblock lock. We still need to hold the superblock
2564 * lock, however, when we modify the global structures.
2565 */
David Chinner03135cf2007-02-10 18:35:15 +11002566 xfs_icsb_lock(mp);
David Chinner20b64282007-02-10 18:35:09 +11002567
2568 /*
2569 * Now running atomically.
2570 *
2571 * If the counter is enabled, someone has beaten us to rebalancing.
2572 * Drop the lock and try again in the fast path....
2573 */
2574 if (!(xfs_icsb_counter_disabled(mp, field))) {
David Chinner03135cf2007-02-10 18:35:15 +11002575 xfs_icsb_unlock(mp);
David Chinner20b64282007-02-10 18:35:09 +11002576 goto again;
2577 }
2578
2579 /*
2580 * The counter is currently disabled. Because we are
2581 * running atomically here, we know a rebalance cannot
2582 * be in progress. Hence we can go straight to operating
2583 * on the global superblock. We do not call xfs_mod_incore_sb()
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002584 * here even though we need to get the m_sb_lock. Doing so
David Chinner20b64282007-02-10 18:35:09 +11002585 * will cause us to re-enter this function and deadlock.
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002586 * Hence we get the m_sb_lock ourselves and then call
David Chinner20b64282007-02-10 18:35:09 +11002587 * xfs_mod_incore_sb_unlocked() as the unlocked path operates
2588 * directly on the global counters.
2589 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002590 spin_lock(&mp->m_sb_lock);
David Chinner20b64282007-02-10 18:35:09 +11002591 ret = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002592 spin_unlock(&mp->m_sb_lock);
David Chinner20b64282007-02-10 18:35:09 +11002593
2594 /*
2595 * Now that we've modified the global superblock, we
2596 * may be able to re-enable the distributed counters
2597 * (e.g. lots of space just got freed). After that
2598 * we are done.
2599 */
2600 if (ret != ENOSPC)
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002601 xfs_icsb_balance_counter(mp, field, 0);
David Chinner03135cf2007-02-10 18:35:15 +11002602 xfs_icsb_unlock(mp);
David Chinner20b64282007-02-10 18:35:09 +11002603 return ret;
2604
2605balance_counter:
David Chinner01e1b692006-03-14 13:29:16 +11002606 xfs_icsb_unlock_cntr(icsbp);
Christoph Lameter7a9e02d2009-10-03 19:48:23 +09002607 preempt_enable();
David Chinner8d280b92006-03-14 13:13:09 +11002608
David Chinner20b64282007-02-10 18:35:09 +11002609 /*
2610 * We may have multiple threads here if multiple per-cpu
2611 * counters run dry at the same time. This will mean we can
2612 * do more balances than strictly necessary but it is not
2613 * the common slowpath case.
2614 */
David Chinner03135cf2007-02-10 18:35:15 +11002615 xfs_icsb_lock(mp);
David Chinner8d280b92006-03-14 13:13:09 +11002616
David Chinner20b64282007-02-10 18:35:09 +11002617 /*
2618 * running atomically.
2619 *
2620 * This will leave the counter in the correct state for future
2621 * accesses. After the rebalance, we simply try again and our retry
2622 * will either succeed through the fast path or slow path without
2623 * another balance operation being required.
2624 */
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002625 xfs_icsb_balance_counter(mp, field, delta);
David Chinner03135cf2007-02-10 18:35:15 +11002626 xfs_icsb_unlock(mp);
David Chinner20b64282007-02-10 18:35:09 +11002627 goto again;
David Chinner8d280b92006-03-14 13:13:09 +11002628}
2629
David Chinner8d280b92006-03-14 13:13:09 +11002630#endif