blob: 9055b60730d06e703c19cd60bdfed5fe1a50e88d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_dinode.h"
36#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_btree.h"
38#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_alloc.h"
40#include "xfs_rtalloc.h"
41#include "xfs_bmap.h"
42#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_rw.h"
44#include "xfs_quota.h"
45#include "xfs_fsops.h"
Christoph Hellwig43355092008-03-27 18:01:08 +110046#include "xfs_utils.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000047#include "xfs_trace.h"
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100050STATIC void xfs_unmountfs_wait(xfs_mount_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
David Chinner8d280b92006-03-14 13:13:09 +110052
53#ifdef HAVE_PERCPU_SB
David Chinner20f4ebf2007-02-10 18:36:10 +110054STATIC void xfs_icsb_balance_counter(xfs_mount_t *, xfs_sb_field_t,
Christoph Hellwig45af6c62008-04-22 17:34:44 +100055 int);
56STATIC void xfs_icsb_balance_counter_locked(xfs_mount_t *, xfs_sb_field_t,
57 int);
David Chinner8d280b92006-03-14 13:13:09 +110058STATIC int xfs_icsb_modify_counters(xfs_mount_t *, xfs_sb_field_t,
David Chinner20f4ebf2007-02-10 18:36:10 +110059 int64_t, int);
David Chinner36fbe6e2008-04-10 12:19:56 +100060STATIC void xfs_icsb_disable_counter(xfs_mount_t *, xfs_sb_field_t);
David Chinner8d280b92006-03-14 13:13:09 +110061
62#else
63
Christoph Hellwig45af6c62008-04-22 17:34:44 +100064#define xfs_icsb_balance_counter(mp, a, b) do { } while (0)
65#define xfs_icsb_balance_counter_locked(mp, a, b) do { } while (0)
David Chinner8d280b92006-03-14 13:13:09 +110066#define xfs_icsb_modify_counters(mp, a, b, c) do { } while (0)
David Chinner8d280b92006-03-14 13:13:09 +110067
68#endif
69
Christoph Hellwig1df84c92006-01-11 15:29:52 +110070static const struct {
David Chinner8d280b92006-03-14 13:13:09 +110071 short offset;
72 short type; /* 0 = integer
73 * 1 = binary / string (no translation)
74 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070075} xfs_sb_info[] = {
76 { offsetof(xfs_sb_t, sb_magicnum), 0 },
77 { offsetof(xfs_sb_t, sb_blocksize), 0 },
78 { offsetof(xfs_sb_t, sb_dblocks), 0 },
79 { offsetof(xfs_sb_t, sb_rblocks), 0 },
80 { offsetof(xfs_sb_t, sb_rextents), 0 },
81 { offsetof(xfs_sb_t, sb_uuid), 1 },
82 { offsetof(xfs_sb_t, sb_logstart), 0 },
83 { offsetof(xfs_sb_t, sb_rootino), 0 },
84 { offsetof(xfs_sb_t, sb_rbmino), 0 },
85 { offsetof(xfs_sb_t, sb_rsumino), 0 },
86 { offsetof(xfs_sb_t, sb_rextsize), 0 },
87 { offsetof(xfs_sb_t, sb_agblocks), 0 },
88 { offsetof(xfs_sb_t, sb_agcount), 0 },
89 { offsetof(xfs_sb_t, sb_rbmblocks), 0 },
90 { offsetof(xfs_sb_t, sb_logblocks), 0 },
91 { offsetof(xfs_sb_t, sb_versionnum), 0 },
92 { offsetof(xfs_sb_t, sb_sectsize), 0 },
93 { offsetof(xfs_sb_t, sb_inodesize), 0 },
94 { offsetof(xfs_sb_t, sb_inopblock), 0 },
95 { offsetof(xfs_sb_t, sb_fname[0]), 1 },
96 { offsetof(xfs_sb_t, sb_blocklog), 0 },
97 { offsetof(xfs_sb_t, sb_sectlog), 0 },
98 { offsetof(xfs_sb_t, sb_inodelog), 0 },
99 { offsetof(xfs_sb_t, sb_inopblog), 0 },
100 { offsetof(xfs_sb_t, sb_agblklog), 0 },
101 { offsetof(xfs_sb_t, sb_rextslog), 0 },
102 { offsetof(xfs_sb_t, sb_inprogress), 0 },
103 { offsetof(xfs_sb_t, sb_imax_pct), 0 },
104 { offsetof(xfs_sb_t, sb_icount), 0 },
105 { offsetof(xfs_sb_t, sb_ifree), 0 },
106 { offsetof(xfs_sb_t, sb_fdblocks), 0 },
107 { offsetof(xfs_sb_t, sb_frextents), 0 },
108 { offsetof(xfs_sb_t, sb_uquotino), 0 },
109 { offsetof(xfs_sb_t, sb_gquotino), 0 },
110 { offsetof(xfs_sb_t, sb_qflags), 0 },
111 { offsetof(xfs_sb_t, sb_flags), 0 },
112 { offsetof(xfs_sb_t, sb_shared_vn), 0 },
113 { offsetof(xfs_sb_t, sb_inoalignmt), 0 },
114 { offsetof(xfs_sb_t, sb_unit), 0 },
115 { offsetof(xfs_sb_t, sb_width), 0 },
116 { offsetof(xfs_sb_t, sb_dirblklog), 0 },
117 { offsetof(xfs_sb_t, sb_logsectlog), 0 },
118 { offsetof(xfs_sb_t, sb_logsectsize),0 },
119 { offsetof(xfs_sb_t, sb_logsunit), 0 },
120 { offsetof(xfs_sb_t, sb_features2), 0 },
David Chinneree1c0902008-03-06 13:45:50 +1100121 { offsetof(xfs_sb_t, sb_bad_features2), 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 { sizeof(xfs_sb_t), 0 }
123};
124
Christoph Hellwig27174202009-03-30 10:21:31 +0200125static DEFINE_MUTEX(xfs_uuid_table_mutex);
126static int xfs_uuid_table_size;
127static uuid_t *xfs_uuid_table;
128
129/*
130 * See if the UUID is unique among mounted XFS filesystems.
131 * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
132 */
133STATIC int
134xfs_uuid_mount(
135 struct xfs_mount *mp)
136{
137 uuid_t *uuid = &mp->m_sb.sb_uuid;
138 int hole, i;
139
140 if (mp->m_flags & XFS_MOUNT_NOUUID)
141 return 0;
142
143 if (uuid_is_nil(uuid)) {
144 cmn_err(CE_WARN,
145 "XFS: Filesystem %s has nil UUID - can't mount",
146 mp->m_fsname);
147 return XFS_ERROR(EINVAL);
148 }
149
150 mutex_lock(&xfs_uuid_table_mutex);
151 for (i = 0, hole = -1; i < xfs_uuid_table_size; i++) {
152 if (uuid_is_nil(&xfs_uuid_table[i])) {
153 hole = i;
154 continue;
155 }
156 if (uuid_equal(uuid, &xfs_uuid_table[i]))
157 goto out_duplicate;
158 }
159
160 if (hole < 0) {
161 xfs_uuid_table = kmem_realloc(xfs_uuid_table,
162 (xfs_uuid_table_size + 1) * sizeof(*xfs_uuid_table),
163 xfs_uuid_table_size * sizeof(*xfs_uuid_table),
164 KM_SLEEP);
165 hole = xfs_uuid_table_size++;
166 }
167 xfs_uuid_table[hole] = *uuid;
168 mutex_unlock(&xfs_uuid_table_mutex);
169
170 return 0;
171
172 out_duplicate:
173 mutex_unlock(&xfs_uuid_table_mutex);
174 cmn_err(CE_WARN, "XFS: Filesystem %s has duplicate UUID - can't mount",
175 mp->m_fsname);
176 return XFS_ERROR(EINVAL);
177}
178
179STATIC void
180xfs_uuid_unmount(
181 struct xfs_mount *mp)
182{
183 uuid_t *uuid = &mp->m_sb.sb_uuid;
184 int i;
185
186 if (mp->m_flags & XFS_MOUNT_NOUUID)
187 return;
188
189 mutex_lock(&xfs_uuid_table_mutex);
190 for (i = 0; i < xfs_uuid_table_size; i++) {
191 if (uuid_is_nil(&xfs_uuid_table[i]))
192 continue;
193 if (!uuid_equal(uuid, &xfs_uuid_table[i]))
194 continue;
195 memset(&xfs_uuid_table[i], 0, sizeof(uuid_t));
196 break;
197 }
198 ASSERT(i < xfs_uuid_table_size);
199 mutex_unlock(&xfs_uuid_table_mutex);
200}
201
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 * Free up the resources associated with a mount structure. Assume that
205 * the structure was initially zeroed, so we can tell which fields got
206 * initialized.
207 */
Christoph Hellwigc962fb72008-05-20 15:10:52 +1000208STATIC void
Christoph Hellwigff4f0382008-08-13 16:50:47 +1000209xfs_free_perag(
Christoph Hellwig745f6912007-08-30 17:20:39 +1000210 xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (mp->m_perag) {
213 int agno;
214
215 for (agno = 0; agno < mp->m_maxagi; agno++)
216 if (mp->m_perag[agno].pagb_list)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000217 kmem_free(mp->m_perag[agno].pagb_list);
218 kmem_free(mp->m_perag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Nathan Scott4cc929e2007-05-14 18:24:02 +1000222/*
223 * Check size of device based on the (data/realtime) block count.
224 * Note: this check is used by the growfs code as well as mount.
225 */
226int
227xfs_sb_validate_fsb_count(
228 xfs_sb_t *sbp,
229 __uint64_t nblocks)
230{
231 ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
232 ASSERT(sbp->sb_blocklog >= BBSHIFT);
233
234#if XFS_BIG_BLKNOS /* Limited by ULONG_MAX of page cache index */
235 if (nblocks >> (PAGE_CACHE_SHIFT - sbp->sb_blocklog) > ULONG_MAX)
236 return E2BIG;
237#else /* Limited by UINT_MAX of sectors */
238 if (nblocks << (sbp->sb_blocklog - BBSHIFT) > UINT_MAX)
239 return E2BIG;
240#endif
241 return 0;
242}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244/*
245 * Check the validity of the SB found.
246 */
247STATIC int
248xfs_mount_validate_sb(
249 xfs_mount_t *mp,
Nathan Scott764d1f82006-03-31 13:04:17 +1000250 xfs_sb_t *sbp,
251 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 /*
254 * If the log device and data device have the
255 * same device number, the log is internal.
256 * Consequently, the sb_logstart should be non-zero. If
257 * we have a zero sb_logstart in this case, we may be trying to mount
258 * a volume filesystem in a non-volume manner.
259 */
260 if (sbp->sb_magicnum != XFS_SB_MAGIC) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000261 xfs_fs_mount_cmn_err(flags, "bad magic number");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return XFS_ERROR(EWRONGFS);
263 }
264
Eric Sandeen62118702008-03-06 13:44:28 +1100265 if (!xfs_sb_good_version(sbp)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000266 xfs_fs_mount_cmn_err(flags, "bad version");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return XFS_ERROR(EWRONGFS);
268 }
269
270 if (unlikely(
271 sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000272 xfs_fs_mount_cmn_err(flags,
273 "filesystem is marked as having an external log; "
274 "specify logdev on the\nmount command line.");
275 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
277
278 if (unlikely(
279 sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000280 xfs_fs_mount_cmn_err(flags,
281 "filesystem is marked as having an internal log; "
282 "do not specify logdev on\nthe mount command line.");
283 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285
286 /*
287 * More sanity checking. These were stolen directly from
288 * xfs_repair.
289 */
290 if (unlikely(
291 sbp->sb_agcount <= 0 ||
292 sbp->sb_sectsize < XFS_MIN_SECTORSIZE ||
293 sbp->sb_sectsize > XFS_MAX_SECTORSIZE ||
294 sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG ||
295 sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG ||
Olaf Weber2ac00af2009-04-17 16:12:45 -0500296 sbp->sb_sectsize != (1 << sbp->sb_sectlog) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 sbp->sb_blocksize < XFS_MIN_BLOCKSIZE ||
298 sbp->sb_blocksize > XFS_MAX_BLOCKSIZE ||
299 sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG ||
300 sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
Olaf Weber2ac00af2009-04-17 16:12:45 -0500301 sbp->sb_blocksize != (1 << sbp->sb_blocklog) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 sbp->sb_inodesize < XFS_DINODE_MIN_SIZE ||
303 sbp->sb_inodesize > XFS_DINODE_MAX_SIZE ||
Nathan Scott9f989c92006-03-14 13:29:32 +1100304 sbp->sb_inodelog < XFS_DINODE_MIN_LOG ||
305 sbp->sb_inodelog > XFS_DINODE_MAX_LOG ||
Olaf Weber2ac00af2009-04-17 16:12:45 -0500306 sbp->sb_inodesize != (1 << sbp->sb_inodelog) ||
Nathan Scott9f989c92006-03-14 13:29:32 +1100307 (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
309 (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
Nathan Scotte50bd162006-04-11 15:10:45 +1000310 (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */))) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000311 xfs_fs_mount_cmn_err(flags, "SB sanity check 1 failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return XFS_ERROR(EFSCORRUPTED);
313 }
314
315 /*
316 * Sanity check AG count, size fields against data size field
317 */
318 if (unlikely(
319 sbp->sb_dblocks == 0 ||
320 sbp->sb_dblocks >
321 (xfs_drfsbno_t)sbp->sb_agcount * sbp->sb_agblocks ||
322 sbp->sb_dblocks < (xfs_drfsbno_t)(sbp->sb_agcount - 1) *
323 sbp->sb_agblocks + XFS_MIN_AG_BLOCKS)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000324 xfs_fs_mount_cmn_err(flags, "SB sanity check 2 failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return XFS_ERROR(EFSCORRUPTED);
326 }
327
Lachlan McIlroy2edbddd2008-06-27 13:34:34 +1000328 /*
329 * Until this is fixed only page-sized or smaller data blocks work.
330 */
331 if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
332 xfs_fs_mount_cmn_err(flags,
333 "file system with blocksize %d bytes",
334 sbp->sb_blocksize);
335 xfs_fs_mount_cmn_err(flags,
336 "only pagesize (%ld) or less will currently work.",
337 PAGE_SIZE);
338 return XFS_ERROR(ENOSYS);
339 }
340
Christoph Hellwig1a5902c2009-03-29 19:26:46 +0200341 /*
342 * Currently only very few inode sizes are supported.
343 */
344 switch (sbp->sb_inodesize) {
345 case 256:
346 case 512:
347 case 1024:
348 case 2048:
349 break;
350 default:
351 xfs_fs_mount_cmn_err(flags,
352 "inode size of %d bytes not supported",
353 sbp->sb_inodesize);
354 return XFS_ERROR(ENOSYS);
355 }
356
Nathan Scott4cc929e2007-05-14 18:24:02 +1000357 if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) ||
358 xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000359 xfs_fs_mount_cmn_err(flags,
360 "file system too large to be mounted on this system.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return XFS_ERROR(E2BIG);
362 }
363
364 if (unlikely(sbp->sb_inprogress)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000365 xfs_fs_mount_cmn_err(flags, "file system busy");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return XFS_ERROR(EFSCORRUPTED);
367 }
368
369 /*
Nathan Scottde206142005-05-05 13:24:13 -0700370 * Version 1 directory format has never worked on Linux.
371 */
Eric Sandeen62118702008-03-06 13:44:28 +1100372 if (unlikely(!xfs_sb_version_hasdirv2(sbp))) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000373 xfs_fs_mount_cmn_err(flags,
374 "file system using version 1 directory format");
Nathan Scottde206142005-05-05 13:24:13 -0700375 return XFS_ERROR(ENOSYS);
376 }
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return 0;
379}
380
David Chinnerda353b02007-08-28 14:00:13 +1000381STATIC void
382xfs_initialize_perag_icache(
383 xfs_perag_t *pag)
384{
385 if (!pag->pag_ici_init) {
386 rwlock_init(&pag->pag_ici_lock);
387 INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC);
388 pag->pag_ici_init = 1;
389 }
390}
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392xfs_agnumber_t
Nathan Scottc11e2c32005-11-02 15:11:45 +1100393xfs_initialize_perag(
Nathan Scottc11e2c32005-11-02 15:11:45 +1100394 xfs_mount_t *mp,
395 xfs_agnumber_t agcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 xfs_agnumber_t index, max_metadata;
398 xfs_perag_t *pag;
399 xfs_agino_t agino;
400 xfs_ino_t ino;
401 xfs_sb_t *sbp = &mp->m_sb;
402 xfs_ino_t max_inum = XFS_MAXINUMBER_32;
403
404 /* Check to see if the filesystem can overflow 32 bit inodes */
405 agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
406 ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
407
408 /* Clear the mount flag if no inode can overflow 32 bits
409 * on this filesystem, or if specifically requested..
410 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000411 if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) && ino > max_inum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 mp->m_flags |= XFS_MOUNT_32BITINODES;
413 } else {
414 mp->m_flags &= ~XFS_MOUNT_32BITINODES;
415 }
416
417 /* If we can overflow then setup the ag headers accordingly */
418 if (mp->m_flags & XFS_MOUNT_32BITINODES) {
419 /* Calculate how much should be reserved for inodes to
420 * meet the max inode percentage.
421 */
422 if (mp->m_maxicount) {
423 __uint64_t icount;
424
425 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
426 do_div(icount, 100);
427 icount += sbp->sb_agblocks - 1;
Eric Sandeena749ee82005-11-02 15:13:42 +1100428 do_div(icount, sbp->sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 max_metadata = icount;
430 } else {
431 max_metadata = agcount;
432 }
433 for (index = 0; index < agcount; index++) {
434 ino = XFS_AGINO_TO_INO(mp, index, agino);
435 if (ino > max_inum) {
436 index++;
437 break;
438 }
439
Nathan Scottc41564b2006-03-29 08:55:14 +1000440 /* This ag is preferred for inodes */
Dave Chinner44b56e02010-01-11 11:47:43 +0000441 pag = xfs_perag_get(mp, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 pag->pagi_inodeok = 1;
443 if (index < max_metadata)
444 pag->pagf_metadata = 1;
David Chinnerda353b02007-08-28 14:00:13 +1000445 xfs_initialize_perag_icache(pag);
Dave Chinner44b56e02010-01-11 11:47:43 +0000446 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448 } else {
449 /* Setup default behavior for smaller filesystems */
450 for (index = 0; index < agcount; index++) {
Dave Chinner44b56e02010-01-11 11:47:43 +0000451 pag = xfs_perag_get(mp, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 pag->pagi_inodeok = 1;
David Chinnerda353b02007-08-28 14:00:13 +1000453 xfs_initialize_perag_icache(pag);
Dave Chinner44b56e02010-01-11 11:47:43 +0000454 xfs_perag_put(pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
456 }
457 return index;
458}
459
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000460void
461xfs_sb_from_disk(
462 xfs_sb_t *to,
463 xfs_dsb_t *from)
464{
465 to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
466 to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
467 to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
468 to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
469 to->sb_rextents = be64_to_cpu(from->sb_rextents);
470 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
471 to->sb_logstart = be64_to_cpu(from->sb_logstart);
472 to->sb_rootino = be64_to_cpu(from->sb_rootino);
473 to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
474 to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
475 to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
476 to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
477 to->sb_agcount = be32_to_cpu(from->sb_agcount);
478 to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
479 to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
480 to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
481 to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
482 to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
483 to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
484 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
485 to->sb_blocklog = from->sb_blocklog;
486 to->sb_sectlog = from->sb_sectlog;
487 to->sb_inodelog = from->sb_inodelog;
488 to->sb_inopblog = from->sb_inopblog;
489 to->sb_agblklog = from->sb_agblklog;
490 to->sb_rextslog = from->sb_rextslog;
491 to->sb_inprogress = from->sb_inprogress;
492 to->sb_imax_pct = from->sb_imax_pct;
493 to->sb_icount = be64_to_cpu(from->sb_icount);
494 to->sb_ifree = be64_to_cpu(from->sb_ifree);
495 to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
496 to->sb_frextents = be64_to_cpu(from->sb_frextents);
497 to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
498 to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
499 to->sb_qflags = be16_to_cpu(from->sb_qflags);
500 to->sb_flags = from->sb_flags;
501 to->sb_shared_vn = from->sb_shared_vn;
502 to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
503 to->sb_unit = be32_to_cpu(from->sb_unit);
504 to->sb_width = be32_to_cpu(from->sb_width);
505 to->sb_dirblklog = from->sb_dirblklog;
506 to->sb_logsectlog = from->sb_logsectlog;
507 to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
508 to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
509 to->sb_features2 = be32_to_cpu(from->sb_features2);
David Chinneree1c0902008-03-06 13:45:50 +1100510 to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000511}
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513/*
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000514 * Copy in core superblock to ondisk one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 *
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000516 * The fields argument is mask of superblock fields to copy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 */
518void
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000519xfs_sb_to_disk(
520 xfs_dsb_t *to,
521 xfs_sb_t *from,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 __int64_t fields)
523{
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000524 xfs_caddr_t to_ptr = (xfs_caddr_t)to;
525 xfs_caddr_t from_ptr = (xfs_caddr_t)from;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 xfs_sb_field_t f;
527 int first;
528 int size;
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 ASSERT(fields);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (!fields)
532 return;
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 while (fields) {
535 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
536 first = xfs_sb_info[f].offset;
537 size = xfs_sb_info[f + 1].offset - first;
538
539 ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1);
540
541 if (size == 1 || xfs_sb_info[f].type == 1) {
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000542 memcpy(to_ptr + first, from_ptr + first, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 } else {
544 switch (size) {
545 case 2:
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000546 *(__be16 *)(to_ptr + first) =
547 cpu_to_be16(*(__u16 *)(from_ptr + first));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 break;
549 case 4:
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000550 *(__be32 *)(to_ptr + first) =
551 cpu_to_be32(*(__u32 *)(from_ptr + first));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 break;
553 case 8:
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000554 *(__be64 *)(to_ptr + first) =
555 cpu_to_be64(*(__u64 *)(from_ptr + first));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 break;
557 default:
558 ASSERT(0);
559 }
560 }
561
562 fields &= ~(1LL << f);
563 }
564}
565
566/*
567 * xfs_readsb
568 *
569 * Does the initial read of the superblock.
570 */
571int
Nathan Scott764d1f82006-03-31 13:04:17 +1000572xfs_readsb(xfs_mount_t *mp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 unsigned int sector_size;
575 unsigned int extra_flags;
576 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 int error;
578
579 ASSERT(mp->m_sb_bp == NULL);
580 ASSERT(mp->m_ddev_targp != NULL);
581
582 /*
583 * Allocate a (locked) buffer to hold the superblock.
584 * This will be kept around at all times to optimize
585 * access to the superblock.
586 */
587 sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
588 extra_flags = XFS_BUF_LOCK | XFS_BUF_MANAGE | XFS_BUF_MAPPED;
589
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000590 bp = xfs_buf_read(mp->m_ddev_targp, XFS_SB_DADDR, BTOBB(sector_size),
591 extra_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (!bp || XFS_BUF_ISERROR(bp)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000593 xfs_fs_mount_cmn_err(flags, "SB read failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
595 goto fail;
596 }
597 ASSERT(XFS_BUF_ISBUSY(bp));
598 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
599
600 /*
601 * Initialize the mount structure from the superblock.
602 * But first do some basic consistency checking.
603 */
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000604 xfs_sb_from_disk(&mp->m_sb, XFS_BUF_TO_SBP(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Nathan Scott764d1f82006-03-31 13:04:17 +1000606 error = xfs_mount_validate_sb(mp, &(mp->m_sb), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (error) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000608 xfs_fs_mount_cmn_err(flags, "SB validate failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 goto fail;
610 }
611
612 /*
613 * We must be able to do sector-sized and sector-aligned IO.
614 */
615 if (sector_size > mp->m_sb.sb_sectsize) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000616 xfs_fs_mount_cmn_err(flags,
617 "device supports only %u byte sectors (not %u)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 sector_size, mp->m_sb.sb_sectsize);
619 error = ENOSYS;
620 goto fail;
621 }
622
623 /*
624 * If device sector size is smaller than the superblock size,
625 * re-read the superblock so the buffer is correctly sized.
626 */
627 if (sector_size < mp->m_sb.sb_sectsize) {
628 XFS_BUF_UNMANAGE(bp);
629 xfs_buf_relse(bp);
630 sector_size = mp->m_sb.sb_sectsize;
Christoph Hellwig6ad112b2009-11-24 18:02:23 +0000631 bp = xfs_buf_read(mp->m_ddev_targp, XFS_SB_DADDR,
632 BTOBB(sector_size), extra_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (!bp || XFS_BUF_ISERROR(bp)) {
Nathan Scott764d1f82006-03-31 13:04:17 +1000634 xfs_fs_mount_cmn_err(flags, "SB re-read failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
636 goto fail;
637 }
638 ASSERT(XFS_BUF_ISBUSY(bp));
639 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
640 }
641
Lachlan McIlroy5478eea2007-02-10 18:36:29 +1100642 /* Initialize per-cpu counters */
643 xfs_icsb_reinit_counters(mp);
David Chinner8d280b92006-03-14 13:13:09 +1100644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 mp->m_sb_bp = bp;
646 xfs_buf_relse(bp);
647 ASSERT(XFS_BUF_VALUSEMA(bp) > 0);
648 return 0;
649
650 fail:
651 if (bp) {
652 XFS_BUF_UNMANAGE(bp);
653 xfs_buf_relse(bp);
654 }
655 return error;
656}
657
658
659/*
660 * xfs_mount_common
661 *
662 * Mount initialization code establishing various mount
663 * fields from the superblock associated with the given
664 * mount structure
665 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000666STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667xfs_mount_common(xfs_mount_t *mp, xfs_sb_t *sbp)
668{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 mp->m_agfrotor = mp->m_agirotor = 0;
Eric Sandeen007c61c2007-10-11 17:43:56 +1000670 spin_lock_init(&mp->m_agirotor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 mp->m_maxagi = mp->m_sb.sb_agcount;
672 mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
673 mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
674 mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
675 mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
676 mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 mp->m_blockmask = sbp->sb_blocksize - 1;
678 mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
679 mp->m_blockwmask = mp->m_blockwsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Christoph Hellwig60197e82008-10-30 17:11:19 +1100681 mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
682 mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
683 mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
684 mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
685
686 mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
687 mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
688 mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2;
689 mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2;
690
691 mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
692 mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
693 mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
694 mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
697 mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
698 sbp->sb_inopblock);
699 mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
700}
David Chinner92821e22007-05-24 15:26:31 +1000701
702/*
703 * xfs_initialize_perag_data
704 *
705 * Read in each per-ag structure so we can count up the number of
706 * allocated inodes, free inodes and used filesystem blocks as this
707 * information is no longer persistent in the superblock. Once we have
708 * this information, write it into the in-core superblock structure.
709 */
710STATIC int
711xfs_initialize_perag_data(xfs_mount_t *mp, xfs_agnumber_t agcount)
712{
713 xfs_agnumber_t index;
714 xfs_perag_t *pag;
715 xfs_sb_t *sbp = &mp->m_sb;
716 uint64_t ifree = 0;
717 uint64_t ialloc = 0;
718 uint64_t bfree = 0;
719 uint64_t bfreelst = 0;
720 uint64_t btree = 0;
721 int error;
David Chinner92821e22007-05-24 15:26:31 +1000722
723 for (index = 0; index < agcount; index++) {
724 /*
725 * read the agf, then the agi. This gets us
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200726 * all the information we need and populates the
David Chinner92821e22007-05-24 15:26:31 +1000727 * per-ag structures for us.
728 */
729 error = xfs_alloc_pagf_init(mp, NULL, index, 0);
730 if (error)
731 return error;
732
733 error = xfs_ialloc_pagi_init(mp, NULL, index);
734 if (error)
735 return error;
Dave Chinner44b56e02010-01-11 11:47:43 +0000736 pag = xfs_perag_get(mp, index);
David Chinner92821e22007-05-24 15:26:31 +1000737 ifree += pag->pagi_freecount;
738 ialloc += pag->pagi_count;
739 bfree += pag->pagf_freeblks;
740 bfreelst += pag->pagf_flcount;
741 btree += pag->pagf_btreeblks;
Dave Chinner44b56e02010-01-11 11:47:43 +0000742 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +1000743 }
744 /*
745 * Overwrite incore superblock counters with just-read data
746 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000747 spin_lock(&mp->m_sb_lock);
David Chinner92821e22007-05-24 15:26:31 +1000748 sbp->sb_ifree = ifree;
749 sbp->sb_icount = ialloc;
750 sbp->sb_fdblocks = bfree + bfreelst + btree;
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000751 spin_unlock(&mp->m_sb_lock);
David Chinner92821e22007-05-24 15:26:31 +1000752
753 /* Fixup the per-cpu counters as well. */
754 xfs_icsb_reinit_counters(mp);
755
756 return 0;
757}
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759/*
Eric Sandeen0771fb42007-10-12 11:03:40 +1000760 * Update alignment values based on mount options and sb values
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 */
Eric Sandeen0771fb42007-10-12 11:03:40 +1000762STATIC int
Christoph Hellwig7884bc82009-01-19 02:04:07 +0100763xfs_update_alignment(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 xfs_sb_t *sbp = &(mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Christoph Hellwig42490232008-08-13 16:49:32 +1000767 if (mp->m_dalign) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 /*
769 * If stripe unit and stripe width are not multiples
770 * of the fs blocksize turn off alignment.
771 */
772 if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
773 (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
774 if (mp->m_flags & XFS_MOUNT_RETERR) {
775 cmn_err(CE_WARN,
776 "XFS: alignment check 1 failed");
Eric Sandeen0771fb42007-10-12 11:03:40 +1000777 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
779 mp->m_dalign = mp->m_swidth = 0;
780 } else {
781 /*
782 * Convert the stripe unit and width to FSBs.
783 */
784 mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
785 if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
786 if (mp->m_flags & XFS_MOUNT_RETERR) {
Eric Sandeen0771fb42007-10-12 11:03:40 +1000787 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
789 xfs_fs_cmn_err(CE_WARN, mp,
790"stripe alignment turned off: sunit(%d)/swidth(%d) incompatible with agsize(%d)",
791 mp->m_dalign, mp->m_swidth,
792 sbp->sb_agblocks);
793
794 mp->m_dalign = 0;
795 mp->m_swidth = 0;
796 } else if (mp->m_dalign) {
797 mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
798 } else {
799 if (mp->m_flags & XFS_MOUNT_RETERR) {
800 xfs_fs_cmn_err(CE_WARN, mp,
801"stripe alignment turned off: sunit(%d) less than bsize(%d)",
802 mp->m_dalign,
803 mp->m_blockmask +1);
Eric Sandeen0771fb42007-10-12 11:03:40 +1000804 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
806 mp->m_swidth = 0;
807 }
808 }
809
810 /*
811 * Update superblock with new values
812 * and log changes
813 */
Eric Sandeen62118702008-03-06 13:44:28 +1100814 if (xfs_sb_version_hasdalign(sbp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (sbp->sb_unit != mp->m_dalign) {
816 sbp->sb_unit = mp->m_dalign;
Christoph Hellwig7884bc82009-01-19 02:04:07 +0100817 mp->m_update_flags |= XFS_SB_UNIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
819 if (sbp->sb_width != mp->m_swidth) {
820 sbp->sb_width = mp->m_swidth;
Christoph Hellwig7884bc82009-01-19 02:04:07 +0100821 mp->m_update_flags |= XFS_SB_WIDTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823 }
824 } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
Eric Sandeen62118702008-03-06 13:44:28 +1100825 xfs_sb_version_hasdalign(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 mp->m_dalign = sbp->sb_unit;
827 mp->m_swidth = sbp->sb_width;
828 }
829
Eric Sandeen0771fb42007-10-12 11:03:40 +1000830 return 0;
831}
832
833/*
834 * Set the maximum inode count for this filesystem
835 */
836STATIC void
837xfs_set_maxicount(xfs_mount_t *mp)
838{
839 xfs_sb_t *sbp = &(mp->m_sb);
840 __uint64_t icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 if (sbp->sb_imax_pct) {
Eric Sandeen0771fb42007-10-12 11:03:40 +1000843 /*
844 * Make sure the maximum inode count is a multiple
845 * of the units we allocate inodes in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
848 do_div(icount, 100);
849 do_div(icount, mp->m_ialloc_blks);
850 mp->m_maxicount = (icount * mp->m_ialloc_blks) <<
851 sbp->sb_inopblog;
Eric Sandeen0771fb42007-10-12 11:03:40 +1000852 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 mp->m_maxicount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
Eric Sandeen0771fb42007-10-12 11:03:40 +1000855}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Eric Sandeen0771fb42007-10-12 11:03:40 +1000857/*
858 * Set the default minimum read and write sizes unless
859 * already specified in a mount option.
860 * We use smaller I/O sizes when the file system
861 * is being used for NFS service (wsync mount option).
862 */
863STATIC void
864xfs_set_rw_sizes(xfs_mount_t *mp)
865{
866 xfs_sb_t *sbp = &(mp->m_sb);
867 int readio_log, writeio_log;
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
870 if (mp->m_flags & XFS_MOUNT_WSYNC) {
871 readio_log = XFS_WSYNC_READIO_LOG;
872 writeio_log = XFS_WSYNC_WRITEIO_LOG;
873 } else {
874 readio_log = XFS_READIO_LOG_LARGE;
875 writeio_log = XFS_WRITEIO_LOG_LARGE;
876 }
877 } else {
878 readio_log = mp->m_readio_log;
879 writeio_log = mp->m_writeio_log;
880 }
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (sbp->sb_blocklog > readio_log) {
883 mp->m_readio_log = sbp->sb_blocklog;
884 } else {
885 mp->m_readio_log = readio_log;
886 }
887 mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
888 if (sbp->sb_blocklog > writeio_log) {
889 mp->m_writeio_log = sbp->sb_blocklog;
890 } else {
891 mp->m_writeio_log = writeio_log;
892 }
893 mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
Eric Sandeen0771fb42007-10-12 11:03:40 +1000894}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Eric Sandeen0771fb42007-10-12 11:03:40 +1000896/*
897 * Set whether we're using inode alignment.
898 */
899STATIC void
900xfs_set_inoalignment(xfs_mount_t *mp)
901{
Eric Sandeen62118702008-03-06 13:44:28 +1100902 if (xfs_sb_version_hasalign(&mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 mp->m_sb.sb_inoalignmt >=
904 XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
905 mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
906 else
907 mp->m_inoalign_mask = 0;
908 /*
909 * If we are using stripe alignment, check whether
910 * the stripe unit is a multiple of the inode alignment
911 */
912 if (mp->m_dalign && mp->m_inoalign_mask &&
913 !(mp->m_dalign & mp->m_inoalign_mask))
914 mp->m_sinoalign = mp->m_dalign;
915 else
916 mp->m_sinoalign = 0;
Eric Sandeen0771fb42007-10-12 11:03:40 +1000917}
918
919/*
920 * Check that the data (and log if separate) are an ok size.
921 */
922STATIC int
Christoph Hellwig42490232008-08-13 16:49:32 +1000923xfs_check_sizes(xfs_mount_t *mp)
Eric Sandeen0771fb42007-10-12 11:03:40 +1000924{
925 xfs_buf_t *bp;
926 xfs_daddr_t d;
927 int error;
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
930 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
931 cmn_err(CE_WARN, "XFS: size check 1 failed");
Eric Sandeen0771fb42007-10-12 11:03:40 +1000932 return XFS_ERROR(E2BIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
934 error = xfs_read_buf(mp, mp->m_ddev_targp,
935 d - XFS_FSS_TO_BB(mp, 1),
936 XFS_FSS_TO_BB(mp, 1), 0, &bp);
937 if (!error) {
938 xfs_buf_relse(bp);
939 } else {
940 cmn_err(CE_WARN, "XFS: size check 2 failed");
Eric Sandeen0771fb42007-10-12 11:03:40 +1000941 if (error == ENOSPC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 error = XFS_ERROR(E2BIG);
Eric Sandeen0771fb42007-10-12 11:03:40 +1000943 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945
Christoph Hellwig42490232008-08-13 16:49:32 +1000946 if (mp->m_logdev_targp != mp->m_ddev_targp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
948 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
949 cmn_err(CE_WARN, "XFS: size check 3 failed");
Eric Sandeen0771fb42007-10-12 11:03:40 +1000950 return XFS_ERROR(E2BIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
952 error = xfs_read_buf(mp, mp->m_logdev_targp,
953 d - XFS_FSB_TO_BB(mp, 1),
954 XFS_FSB_TO_BB(mp, 1), 0, &bp);
955 if (!error) {
956 xfs_buf_relse(bp);
957 } else {
958 cmn_err(CE_WARN, "XFS: size check 3 failed");
Eric Sandeen0771fb42007-10-12 11:03:40 +1000959 if (error == ENOSPC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 error = XFS_ERROR(E2BIG);
Eric Sandeen0771fb42007-10-12 11:03:40 +1000961 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
963 }
Eric Sandeen0771fb42007-10-12 11:03:40 +1000964 return 0;
965}
966
967/*
Christoph Hellwig7d095252009-06-08 15:33:32 +0200968 * Clear the quotaflags in memory and in the superblock.
969 */
970int
971xfs_mount_reset_sbqflags(
972 struct xfs_mount *mp)
973{
974 int error;
975 struct xfs_trans *tp;
976
977 mp->m_qflags = 0;
978
979 /*
980 * It is OK to look at sb_qflags here in mount path,
981 * without m_sb_lock.
982 */
983 if (mp->m_sb.sb_qflags == 0)
984 return 0;
985 spin_lock(&mp->m_sb_lock);
986 mp->m_sb.sb_qflags = 0;
987 spin_unlock(&mp->m_sb_lock);
988
989 /*
990 * If the fs is readonly, let the incore superblock run
991 * with quotas off but don't flush the update out to disk
992 */
993 if (mp->m_flags & XFS_MOUNT_RDONLY)
994 return 0;
995
996#ifdef QUOTADEBUG
997 xfs_fs_cmn_err(CE_NOTE, mp, "Writing superblock quota changes");
998#endif
999
1000 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SBCHANGE);
1001 error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
1002 XFS_DEFAULT_LOG_COUNT);
1003 if (error) {
1004 xfs_trans_cancel(tp, 0);
1005 xfs_fs_cmn_err(CE_ALERT, mp,
1006 "xfs_mount_reset_sbqflags: Superblock update failed!");
1007 return error;
1008 }
1009
1010 xfs_mod_sb(tp, XFS_SB_QFLAGS);
1011 return xfs_trans_commit(tp, 0);
1012}
1013
1014/*
Eric Sandeen0771fb42007-10-12 11:03:40 +10001015 * This function does the following on an initial mount of a file system:
1016 * - reads the superblock from disk and init the mount struct
1017 * - if we're a 32-bit kernel, do a size check on the superblock
1018 * so we don't mount terabyte filesystems
1019 * - init mount struct realtime fields
1020 * - allocate inode hash table for fs
1021 * - init directory manager
1022 * - perform recovery and init the log manager
1023 */
1024int
1025xfs_mountfs(
Christoph Hellwig42490232008-08-13 16:49:32 +10001026 xfs_mount_t *mp)
Eric Sandeen0771fb42007-10-12 11:03:40 +10001027{
1028 xfs_sb_t *sbp = &(mp->m_sb);
1029 xfs_inode_t *rip;
Eric Sandeen0771fb42007-10-12 11:03:40 +10001030 __uint64_t resblks;
Christoph Hellwig7d095252009-06-08 15:33:32 +02001031 uint quotamount = 0;
1032 uint quotaflags = 0;
Eric Sandeen0771fb42007-10-12 11:03:40 +10001033 int error = 0;
1034
Eric Sandeen0771fb42007-10-12 11:03:40 +10001035 xfs_mount_common(mp, sbp);
1036
1037 /*
Eric Sandeene6957ea2008-04-10 12:19:34 +10001038 * Check for a mismatched features2 values. Older kernels
1039 * read & wrote into the wrong sb offset for sb_features2
1040 * on some platforms due to xfs_sb_t not being 64bit size aligned
1041 * when sb_features2 was added, which made older superblock
1042 * reading/writing routines swap it as a 64-bit value.
David Chinneree1c0902008-03-06 13:45:50 +11001043 *
Eric Sandeene6957ea2008-04-10 12:19:34 +10001044 * For backwards compatibility, we make both slots equal.
1045 *
1046 * If we detect a mismatched field, we OR the set bits into the
1047 * existing features2 field in case it has already been modified; we
1048 * don't want to lose any features. We then update the bad location
1049 * with the ORed value so that older kernels will see any features2
1050 * flags, and mark the two fields as needing updates once the
1051 * transaction subsystem is online.
David Chinneree1c0902008-03-06 13:45:50 +11001052 */
Eric Sandeene6957ea2008-04-10 12:19:34 +10001053 if (xfs_sb_has_mismatched_features2(sbp)) {
David Chinneree1c0902008-03-06 13:45:50 +11001054 cmn_err(CE_WARN,
1055 "XFS: correcting sb_features alignment problem");
1056 sbp->sb_features2 |= sbp->sb_bad_features2;
Eric Sandeene6957ea2008-04-10 12:19:34 +10001057 sbp->sb_bad_features2 = sbp->sb_features2;
Christoph Hellwig7884bc82009-01-19 02:04:07 +01001058 mp->m_update_flags |= XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2;
Eric Sandeene6957ea2008-04-10 12:19:34 +10001059
1060 /*
1061 * Re-check for ATTR2 in case it was found in bad_features2
1062 * slot.
1063 */
Tim Shimmin7c12f292008-04-30 18:15:28 +10001064 if (xfs_sb_version_hasattr2(&mp->m_sb) &&
1065 !(mp->m_flags & XFS_MOUNT_NOATTR2))
Eric Sandeene6957ea2008-04-10 12:19:34 +10001066 mp->m_flags |= XFS_MOUNT_ATTR2;
Tim Shimmin7c12f292008-04-30 18:15:28 +10001067 }
Eric Sandeene6957ea2008-04-10 12:19:34 +10001068
Tim Shimmin7c12f292008-04-30 18:15:28 +10001069 if (xfs_sb_version_hasattr2(&mp->m_sb) &&
1070 (mp->m_flags & XFS_MOUNT_NOATTR2)) {
1071 xfs_sb_version_removeattr2(&mp->m_sb);
Christoph Hellwig7884bc82009-01-19 02:04:07 +01001072 mp->m_update_flags |= XFS_SB_FEATURES2;
Tim Shimmin7c12f292008-04-30 18:15:28 +10001073
1074 /* update sb_versionnum for the clearing of the morebits */
1075 if (!sbp->sb_features2)
Christoph Hellwig7884bc82009-01-19 02:04:07 +01001076 mp->m_update_flags |= XFS_SB_VERSIONNUM;
David Chinneree1c0902008-03-06 13:45:50 +11001077 }
1078
1079 /*
Eric Sandeen0771fb42007-10-12 11:03:40 +10001080 * Check if sb_agblocks is aligned at stripe boundary
1081 * If sb_agblocks is NOT aligned turn off m_dalign since
1082 * allocator alignment is within an ag, therefore ag has
1083 * to be aligned at stripe boundary.
1084 */
Christoph Hellwig7884bc82009-01-19 02:04:07 +01001085 error = xfs_update_alignment(mp);
Eric Sandeen0771fb42007-10-12 11:03:40 +10001086 if (error)
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001087 goto out;
Eric Sandeen0771fb42007-10-12 11:03:40 +10001088
1089 xfs_alloc_compute_maxlevels(mp);
1090 xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
1091 xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
1092 xfs_ialloc_compute_maxlevels(mp);
1093
1094 xfs_set_maxicount(mp);
1095
1096 mp->m_maxioffset = xfs_max_file_offset(sbp->sb_blocklog);
1097
Christoph Hellwig27174202009-03-30 10:21:31 +02001098 error = xfs_uuid_mount(mp);
1099 if (error)
1100 goto out;
Eric Sandeen0771fb42007-10-12 11:03:40 +10001101
1102 /*
1103 * Set the minimum read and write sizes
1104 */
1105 xfs_set_rw_sizes(mp);
1106
1107 /*
1108 * Set the inode cluster size.
1109 * This may still be overridden by the file system
1110 * block size if it is larger than the chosen cluster size.
1111 */
1112 mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
1113
1114 /*
1115 * Set inode alignment fields
1116 */
1117 xfs_set_inoalignment(mp);
1118
1119 /*
1120 * Check that the data (and log if separate) are an ok size.
1121 */
Christoph Hellwig42490232008-08-13 16:49:32 +10001122 error = xfs_check_sizes(mp);
Eric Sandeen0771fb42007-10-12 11:03:40 +10001123 if (error)
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001124 goto out_remove_uuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 /*
1127 * Initialize realtime fields in the mount structure
1128 */
Eric Sandeen0771fb42007-10-12 11:03:40 +10001129 error = xfs_rtmount_init(mp);
1130 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 cmn_err(CE_WARN, "XFS: RT mount failed");
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001132 goto out_remove_uuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
1134
1135 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 * Copies the low order bits of the timestamp and the randomly
1137 * set "sequence" number out of a UUID.
1138 */
1139 uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid);
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 mp->m_dmevmask = 0; /* not persistent; set after each mount */
1142
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001143 xfs_dir_mount(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 /*
1146 * Initialize the attribute manager's entries.
1147 */
1148 mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100;
1149
1150 /*
1151 * Initialize the precomputed transaction reservations values.
1152 */
1153 xfs_trans_init(mp);
1154
1155 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 * Allocate and initialize the per-ag data.
1157 */
1158 init_rwsem(&mp->m_peraglock);
Christoph Hellwigd62c2512008-08-13 16:51:29 +10001159 mp->m_perag = kmem_zalloc(sbp->sb_agcount * sizeof(xfs_perag_t),
1160 KM_MAYFAIL);
1161 if (!mp->m_perag)
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001162 goto out_remove_uuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Christoph Hellwigb267ce92007-08-30 17:21:30 +10001164 mp->m_maxagi = xfs_initialize_perag(mp, sbp->sb_agcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001166 if (!sbp->sb_logblocks) {
1167 cmn_err(CE_WARN, "XFS: no log defined");
1168 XFS_ERROR_REPORT("xfs_mountfs", XFS_ERRLEVEL_LOW, mp);
1169 error = XFS_ERROR(EFSCORRUPTED);
1170 goto out_free_perag;
1171 }
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 /*
1174 * log's mount-time initialization. Perform 1st part recovery if needed
1175 */
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001176 error = xfs_log_mount(mp, mp->m_logdev_targp,
1177 XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
1178 XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
1179 if (error) {
1180 cmn_err(CE_WARN, "XFS: log mount failed");
1181 goto out_free_perag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183
1184 /*
David Chinner92821e22007-05-24 15:26:31 +10001185 * Now the log is mounted, we know if it was an unclean shutdown or
1186 * not. If it was, with the first phase of recovery has completed, we
1187 * have consistent AG blocks on disk. We have not recovered EFIs yet,
1188 * but they are recovered transactionally in the second recovery phase
1189 * later.
1190 *
1191 * Hence we can safely re-initialise incore superblock counters from
1192 * the per-ag data. These may not be correct if the filesystem was not
1193 * cleanly unmounted, so we need to wait for recovery to finish before
1194 * doing this.
1195 *
1196 * If the filesystem was cleanly unmounted, then we can trust the
1197 * values in the superblock to be correct and we don't need to do
1198 * anything here.
1199 *
1200 * If we are currently making the filesystem, the initialisation will
1201 * fail as the perag data is in an undefined state.
1202 */
David Chinner92821e22007-05-24 15:26:31 +10001203 if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
1204 !XFS_LAST_UNMOUNT_WAS_CLEAN(mp) &&
1205 !mp->m_sb.sb_inprogress) {
1206 error = xfs_initialize_perag_data(mp, sbp->sb_agcount);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001207 if (error)
1208 goto out_free_perag;
David Chinner92821e22007-05-24 15:26:31 +10001209 }
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001210
David Chinner92821e22007-05-24 15:26:31 +10001211 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 * Get and sanity-check the root inode.
1213 * Save the pointer to it in the mount structure.
1214 */
1215 error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip, 0);
1216 if (error) {
1217 cmn_err(CE_WARN, "XFS: failed to read root inode");
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001218 goto out_log_dealloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
1220
1221 ASSERT(rip != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 if (unlikely((rip->i_d.di_mode & S_IFMT) != S_IFDIR)) {
1224 cmn_err(CE_WARN, "XFS: corrupted root inode");
Nathan Scottb6574522006-06-09 15:29:40 +10001225 cmn_err(CE_WARN, "Device %s - root %llu is not a directory",
1226 XFS_BUFTARG_NAME(mp->m_ddev_targp),
1227 (unsigned long long)rip->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 xfs_iunlock(rip, XFS_ILOCK_EXCL);
1229 XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
1230 mp);
1231 error = XFS_ERROR(EFSCORRUPTED);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001232 goto out_rele_rip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
1234 mp->m_rootip = rip; /* save it */
1235
1236 xfs_iunlock(rip, XFS_ILOCK_EXCL);
1237
1238 /*
1239 * Initialize realtime inode pointers in the mount structure
1240 */
Eric Sandeen0771fb42007-10-12 11:03:40 +10001241 error = xfs_rtmount_inodes(mp);
1242 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 /*
1244 * Free up the root inode.
1245 */
1246 cmn_err(CE_WARN, "XFS: failed to read RT inodes");
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001247 goto out_rele_rip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
1249
1250 /*
Christoph Hellwig7884bc82009-01-19 02:04:07 +01001251 * If this is a read-only mount defer the superblock updates until
1252 * the next remount into writeable mode. Otherwise we would never
1253 * perform the update e.g. for the root filesystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 */
Christoph Hellwig7884bc82009-01-19 02:04:07 +01001255 if (mp->m_update_flags && !(mp->m_flags & XFS_MOUNT_RDONLY)) {
1256 error = xfs_mount_log_sb(mp, mp->m_update_flags);
David Chinnere5720ee2008-04-10 12:21:18 +10001257 if (error) {
1258 cmn_err(CE_WARN, "XFS: failed to write sb changes");
Christoph Hellwigb93b6e42009-02-04 09:33:58 +01001259 goto out_rtunmount;
David Chinnere5720ee2008-04-10 12:21:18 +10001260 }
1261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 /*
1264 * Initialise the XFS quota management subsystem for this mount
1265 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001266 if (XFS_IS_QUOTA_RUNNING(mp)) {
1267 error = xfs_qm_newmount(mp, &quotamount, &quotaflags);
1268 if (error)
1269 goto out_rtunmount;
1270 } else {
1271 ASSERT(!XFS_IS_QUOTA_ON(mp));
1272
1273 /*
1274 * If a file system had quotas running earlier, but decided to
1275 * mount without -o uquota/pquota/gquota options, revoke the
1276 * quotachecked license.
1277 */
1278 if (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT) {
1279 cmn_err(CE_NOTE,
1280 "XFS: resetting qflags for filesystem %s",
1281 mp->m_fsname);
1282
1283 error = xfs_mount_reset_sbqflags(mp);
1284 if (error)
1285 return error;
1286 }
1287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 /*
1290 * Finish recovering the file system. This part needed to be
1291 * delayed until after the root and real-time bitmap inodes
1292 * were consistently read in.
1293 */
Christoph Hellwig42490232008-08-13 16:49:32 +10001294 error = xfs_log_mount_finish(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 if (error) {
1296 cmn_err(CE_WARN, "XFS: log mount finish failed");
Christoph Hellwigb93b6e42009-02-04 09:33:58 +01001297 goto out_rtunmount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
1299
1300 /*
1301 * Complete the quota initialisation, post-log-replay component.
1302 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001303 if (quotamount) {
1304 ASSERT(mp->m_qflags == 0);
1305 mp->m_qflags = quotaflags;
1306
1307 xfs_qm_mount_quotas(mp);
1308 }
1309
1310#if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
1311 if (XFS_IS_QUOTA_ON(mp))
1312 xfs_fs_cmn_err(CE_NOTE, mp, "Disk quotas turned on");
1313 else
1314 xfs_fs_cmn_err(CE_NOTE, mp, "Disk quotas not turned on");
1315#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
David Chinner84e1e992007-06-18 16:50:27 +10001317 /*
1318 * Now we are mounted, reserve a small amount of unused space for
1319 * privileged transactions. This is needed so that transaction
1320 * space required for critical operations can dip into this pool
1321 * when at ENOSPC. This is needed for operations like create with
1322 * attr, unwritten extent conversion at ENOSPC, etc. Data allocations
1323 * are not allowed to use this reserved space.
1324 *
1325 * We default to 5% or 1024 fsbs of space reserved, whichever is smaller.
1326 * This may drive us straight to ENOSPC on mount, but that implies
David Chinner714082b2008-04-10 12:20:03 +10001327 * we were already there on the last unmount. Warn if this occurs.
David Chinner84e1e992007-06-18 16:50:27 +10001328 */
Christoph Hellwig39726be2007-06-18 17:57:45 +10001329 resblks = mp->m_sb.sb_dblocks;
1330 do_div(resblks, 20);
1331 resblks = min_t(__uint64_t, resblks, 1024);
David Chinner714082b2008-04-10 12:20:03 +10001332 error = xfs_reserve_blocks(mp, &resblks, NULL);
1333 if (error)
1334 cmn_err(CE_WARN, "XFS: Unable to allocate reserve blocks. "
1335 "Continuing without a reserve pool.");
David Chinner84e1e992007-06-18 16:50:27 +10001336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 return 0;
1338
Christoph Hellwigb93b6e42009-02-04 09:33:58 +01001339 out_rtunmount:
1340 xfs_rtunmount_inodes(mp);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001341 out_rele_rip:
Christoph Hellwig43355092008-03-27 18:01:08 +11001342 IRELE(rip);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001343 out_log_dealloc:
Christoph Hellwig21b699c2009-03-16 08:19:29 +01001344 xfs_log_unmount(mp);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001345 out_free_perag:
Christoph Hellwigff4f0382008-08-13 16:50:47 +10001346 xfs_free_perag(mp);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001347 out_remove_uuid:
Christoph Hellwig27174202009-03-30 10:21:31 +02001348 xfs_uuid_unmount(mp);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001349 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return error;
1351}
1352
1353/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 * This flushes out the inodes,dquots and the superblock, unmounts the
1355 * log and makes sure that incore structures are freed.
1356 */
Christoph Hellwig41b5c2e2008-08-13 16:49:57 +10001357void
1358xfs_unmountfs(
1359 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Christoph Hellwig41b5c2e2008-08-13 16:49:57 +10001361 __uint64_t resblks;
1362 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Christoph Hellwig7d095252009-06-08 15:33:32 +02001364 xfs_qm_unmount_quotas(mp);
Christoph Hellwigb93b6e42009-02-04 09:33:58 +01001365 xfs_rtunmount_inodes(mp);
Christoph Hellwig77508ec2008-08-13 16:49:04 +10001366 IRELE(mp->m_rootip);
1367
David Chinner641c56f2007-06-18 16:50:17 +10001368 /*
1369 * We can potentially deadlock here if we have an inode cluster
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001370 * that has been freed has its buffer still pinned in memory because
David Chinner641c56f2007-06-18 16:50:17 +10001371 * the transaction is still sitting in a iclog. The stale inodes
1372 * on that buffer will have their flush locks held until the
1373 * transaction hits the disk and the callbacks run. the inode
1374 * flush takes the flush lock unconditionally and with nothing to
1375 * push out the iclog we will never get that unlocked. hence we
1376 * need to force the log first.
1377 */
1378 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
Dave Chinnerabc10642009-06-08 15:35:12 +02001379 xfs_reclaim_inodes(mp, XFS_IFLUSH_ASYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Christoph Hellwig7d095252009-06-08 15:33:32 +02001381 xfs_qm_unmount(mp);
Lachlan McIlroya357a122008-10-30 16:53:25 +11001382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 /*
1384 * Flush out the log synchronously so that we know for sure
1385 * that nothing is pinned. This is important because bflush()
1386 * will skip pinned buffers.
1387 */
1388 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1389
1390 xfs_binval(mp->m_ddev_targp);
1391 if (mp->m_rtdev_targp) {
1392 xfs_binval(mp->m_rtdev_targp);
1393 }
1394
David Chinner84e1e992007-06-18 16:50:27 +10001395 /*
1396 * Unreserve any blocks we have so that when we unmount we don't account
1397 * the reserved free space as used. This is really only necessary for
1398 * lazy superblock counting because it trusts the incore superblock
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001399 * counters to be absolutely correct on clean unmount.
David Chinner84e1e992007-06-18 16:50:27 +10001400 *
1401 * We don't bother correcting this elsewhere for lazy superblock
1402 * counting because on mount of an unclean filesystem we reconstruct the
1403 * correct counter value and this is irrelevant.
1404 *
1405 * For non-lazy counter filesystems, this doesn't matter at all because
1406 * we only every apply deltas to the superblock and hence the incore
1407 * value does not matter....
1408 */
1409 resblks = 0;
David Chinner714082b2008-04-10 12:20:03 +10001410 error = xfs_reserve_blocks(mp, &resblks, NULL);
1411 if (error)
1412 cmn_err(CE_WARN, "XFS: Unable to free reserved block pool. "
1413 "Freespace may not be correct on next mount.");
1414
David Chinnere5720ee2008-04-10 12:21:18 +10001415 error = xfs_log_sbcount(mp, 1);
1416 if (error)
1417 cmn_err(CE_WARN, "XFS: Unable to update superblock counters. "
1418 "Freespace may not be correct on next mount.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 xfs_unmountfs_writesb(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 xfs_unmountfs_wait(mp); /* wait for async bufs */
Christoph Hellwig21b699c2009-03-16 08:19:29 +01001421 xfs_log_unmount_write(mp);
1422 xfs_log_unmount(mp);
Christoph Hellwig27174202009-03-30 10:21:31 +02001423 xfs_uuid_unmount(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Christoph Hellwig1550d0b2008-08-13 16:17:37 +10001425#if defined(DEBUG)
Christoph Hellwig0ce4cfd2007-08-30 17:20:53 +10001426 xfs_errortag_clearall(mp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427#endif
Christoph Hellwigff4f0382008-08-13 16:50:47 +10001428 xfs_free_perag(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001431STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432xfs_unmountfs_wait(xfs_mount_t *mp)
1433{
1434 if (mp->m_logdev_targp != mp->m_ddev_targp)
1435 xfs_wait_buftarg(mp->m_logdev_targp);
1436 if (mp->m_rtdev_targp)
1437 xfs_wait_buftarg(mp->m_rtdev_targp);
1438 xfs_wait_buftarg(mp->m_ddev_targp);
1439}
1440
1441int
David Chinner92821e22007-05-24 15:26:31 +10001442xfs_fs_writable(xfs_mount_t *mp)
1443{
Christoph Hellwigb267ce92007-08-30 17:21:30 +10001444 return !(xfs_test_for_freeze(mp) || XFS_FORCED_SHUTDOWN(mp) ||
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001445 (mp->m_flags & XFS_MOUNT_RDONLY));
David Chinner92821e22007-05-24 15:26:31 +10001446}
1447
1448/*
1449 * xfs_log_sbcount
1450 *
1451 * Called either periodically to keep the on disk superblock values
1452 * roughly up to date or from unmount to make sure the values are
1453 * correct on a clean unmount.
1454 *
1455 * Note this code can be called during the process of freezing, so
1456 * we may need to use the transaction allocator which does not not
1457 * block when the transaction subsystem is in its frozen state.
1458 */
1459int
1460xfs_log_sbcount(
1461 xfs_mount_t *mp,
1462 uint sync)
1463{
1464 xfs_trans_t *tp;
1465 int error;
1466
1467 if (!xfs_fs_writable(mp))
1468 return 0;
1469
Christoph Hellwigd4d90b52008-04-22 17:34:37 +10001470 xfs_icsb_sync_counters(mp, 0);
David Chinner92821e22007-05-24 15:26:31 +10001471
1472 /*
1473 * we don't need to do this if we are updating the superblock
1474 * counters on every modification.
1475 */
1476 if (!xfs_sb_version_haslazysbcount(&mp->m_sb))
1477 return 0;
1478
Christoph Hellwig80641dc2009-10-19 04:00:03 +00001479 tp = _xfs_trans_alloc(mp, XFS_TRANS_SB_COUNT, KM_SLEEP);
David Chinner92821e22007-05-24 15:26:31 +10001480 error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
1481 XFS_DEFAULT_LOG_COUNT);
1482 if (error) {
1483 xfs_trans_cancel(tp, 0);
1484 return error;
1485 }
1486
1487 xfs_mod_sb(tp, XFS_SB_IFREE | XFS_SB_ICOUNT | XFS_SB_FDBLOCKS);
1488 if (sync)
1489 xfs_trans_set_sync(tp);
David Chinnere5720ee2008-04-10 12:21:18 +10001490 error = xfs_trans_commit(tp, 0);
1491 return error;
David Chinner92821e22007-05-24 15:26:31 +10001492}
1493
1494int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495xfs_unmountfs_writesb(xfs_mount_t *mp)
1496{
1497 xfs_buf_t *sbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 int error = 0;
1499
1500 /*
1501 * skip superblock write if fs is read-only, or
1502 * if we are doing a forced umount.
1503 */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001504 if (!((mp->m_flags & XFS_MOUNT_RDONLY) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 XFS_FORCED_SHUTDOWN(mp))) {
David Chinner8d280b92006-03-14 13:13:09 +11001506
David Chinner92821e22007-05-24 15:26:31 +10001507 sbp = xfs_getsb(mp, 0);
David Chinner8d280b92006-03-14 13:13:09 +11001508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 XFS_BUF_UNDONE(sbp);
1510 XFS_BUF_UNREAD(sbp);
1511 XFS_BUF_UNDELAYWRITE(sbp);
1512 XFS_BUF_WRITE(sbp);
1513 XFS_BUF_UNASYNC(sbp);
1514 ASSERT(XFS_BUF_TARGET(sbp) == mp->m_ddev_targp);
1515 xfsbdstrat(mp, sbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 error = xfs_iowait(sbp);
1517 if (error)
1518 xfs_ioerror_alert("xfs_unmountfs_writesb",
1519 mp, sbp, XFS_BUF_ADDR(sbp));
David Chinner92821e22007-05-24 15:26:31 +10001520 xfs_buf_relse(sbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001522 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
1525/*
1526 * xfs_mod_sb() can be used to copy arbitrary changes to the
1527 * in-core superblock into the superblock buffer to be logged.
1528 * It does not provide the higher level of locking that is
1529 * needed to protect the in-core superblock from concurrent
1530 * access.
1531 */
1532void
1533xfs_mod_sb(xfs_trans_t *tp, __int64_t fields)
1534{
1535 xfs_buf_t *bp;
1536 int first;
1537 int last;
1538 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 xfs_sb_field_t f;
1540
1541 ASSERT(fields);
1542 if (!fields)
1543 return;
1544 mp = tp->t_mountp;
1545 bp = xfs_trans_getsb(tp, mp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 first = sizeof(xfs_sb_t);
1547 last = 0;
1548
1549 /* translate/copy */
1550
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +10001551 xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb, fields);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553 /* find modified range */
1554
1555 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
1556 ASSERT((1LL << f) & XFS_SB_MOD_BITS);
1557 first = xfs_sb_info[f].offset;
1558
1559 f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields);
1560 ASSERT((1LL << f) & XFS_SB_MOD_BITS);
1561 last = xfs_sb_info[f + 1].offset - 1;
1562
1563 xfs_trans_log_buf(tp, bp, first, last);
1564}
Yingping Lud210a282006-06-09 14:55:18 +10001565
Yingping Lud210a282006-06-09 14:55:18 +10001566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567/*
1568 * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply
1569 * a delta to a specified field in the in-core superblock. Simply
1570 * switch on the field indicated and apply the delta to that field.
1571 * Fields are not allowed to dip below zero, so if the delta would
1572 * do this do not apply it and return EINVAL.
1573 *
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001574 * The m_sb_lock must be held when this routine is called.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05001576STATIC int
David Chinner20f4ebf2007-02-10 18:36:10 +11001577xfs_mod_incore_sb_unlocked(
1578 xfs_mount_t *mp,
1579 xfs_sb_field_t field,
1580 int64_t delta,
1581 int rsvd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
1583 int scounter; /* short counter for 32 bit fields */
1584 long long lcounter; /* long counter for 64 bit fields */
1585 long long res_used, rem;
1586
1587 /*
1588 * With the in-core superblock spin lock held, switch
1589 * on the indicated field. Apply the delta to the
1590 * proper field. If the fields value would dip below
1591 * 0, then do not apply the delta and return EINVAL.
1592 */
1593 switch (field) {
1594 case XFS_SBS_ICOUNT:
1595 lcounter = (long long)mp->m_sb.sb_icount;
1596 lcounter += delta;
1597 if (lcounter < 0) {
1598 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001599 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 }
1601 mp->m_sb.sb_icount = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001602 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 case XFS_SBS_IFREE:
1604 lcounter = (long long)mp->m_sb.sb_ifree;
1605 lcounter += delta;
1606 if (lcounter < 0) {
1607 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001608 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 }
1610 mp->m_sb.sb_ifree = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001611 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 case XFS_SBS_FDBLOCKS:
David Chinner4be536d2006-09-07 14:26:50 +10001613 lcounter = (long long)
1614 mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
1616
1617 if (delta > 0) { /* Putting blocks back */
1618 if (res_used > delta) {
1619 mp->m_resblks_avail += delta;
1620 } else {
1621 rem = delta - res_used;
1622 mp->m_resblks_avail = mp->m_resblks;
1623 lcounter += rem;
1624 }
1625 } else { /* Taking blocks away */
1626
1627 lcounter += delta;
1628
1629 /*
1630 * If were out of blocks, use any available reserved blocks if
1631 * were allowed to.
1632 */
1633
1634 if (lcounter < 0) {
1635 if (rsvd) {
1636 lcounter = (long long)mp->m_resblks_avail + delta;
1637 if (lcounter < 0) {
Jesper Juhl014c2542006-01-15 02:37:08 +01001638 return XFS_ERROR(ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
1640 mp->m_resblks_avail = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001641 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 } else { /* not reserved */
Jesper Juhl014c2542006-01-15 02:37:08 +01001643 return XFS_ERROR(ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 }
1645 }
1646 }
1647
David Chinner4be536d2006-09-07 14:26:50 +10001648 mp->m_sb.sb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
Jesper Juhl014c2542006-01-15 02:37:08 +01001649 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 case XFS_SBS_FREXTENTS:
1651 lcounter = (long long)mp->m_sb.sb_frextents;
1652 lcounter += delta;
1653 if (lcounter < 0) {
Jesper Juhl014c2542006-01-15 02:37:08 +01001654 return XFS_ERROR(ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 }
1656 mp->m_sb.sb_frextents = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001657 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 case XFS_SBS_DBLOCKS:
1659 lcounter = (long long)mp->m_sb.sb_dblocks;
1660 lcounter += delta;
1661 if (lcounter < 0) {
1662 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001663 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
1665 mp->m_sb.sb_dblocks = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001666 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 case XFS_SBS_AGCOUNT:
1668 scounter = mp->m_sb.sb_agcount;
1669 scounter += delta;
1670 if (scounter < 0) {
1671 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001672 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 }
1674 mp->m_sb.sb_agcount = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001675 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 case XFS_SBS_IMAX_PCT:
1677 scounter = mp->m_sb.sb_imax_pct;
1678 scounter += delta;
1679 if (scounter < 0) {
1680 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001681 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
1683 mp->m_sb.sb_imax_pct = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001684 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 case XFS_SBS_REXTSIZE:
1686 scounter = mp->m_sb.sb_rextsize;
1687 scounter += delta;
1688 if (scounter < 0) {
1689 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001690 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }
1692 mp->m_sb.sb_rextsize = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001693 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 case XFS_SBS_RBMBLOCKS:
1695 scounter = mp->m_sb.sb_rbmblocks;
1696 scounter += delta;
1697 if (scounter < 0) {
1698 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001699 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 }
1701 mp->m_sb.sb_rbmblocks = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001702 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 case XFS_SBS_RBLOCKS:
1704 lcounter = (long long)mp->m_sb.sb_rblocks;
1705 lcounter += delta;
1706 if (lcounter < 0) {
1707 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001708 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710 mp->m_sb.sb_rblocks = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001711 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 case XFS_SBS_REXTENTS:
1713 lcounter = (long long)mp->m_sb.sb_rextents;
1714 lcounter += delta;
1715 if (lcounter < 0) {
1716 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001717 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
1719 mp->m_sb.sb_rextents = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001720 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 case XFS_SBS_REXTSLOG:
1722 scounter = mp->m_sb.sb_rextslog;
1723 scounter += delta;
1724 if (scounter < 0) {
1725 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001726 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 }
1728 mp->m_sb.sb_rextslog = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001729 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 default:
1731 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001732 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 }
1734}
1735
1736/*
1737 * xfs_mod_incore_sb() is used to change a field in the in-core
1738 * superblock structure by the specified delta. This modification
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001739 * is protected by the m_sb_lock. Just use the xfs_mod_incore_sb_unlocked()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 * routine to do the work.
1741 */
1742int
David Chinner20f4ebf2007-02-10 18:36:10 +11001743xfs_mod_incore_sb(
1744 xfs_mount_t *mp,
1745 xfs_sb_field_t field,
1746 int64_t delta,
1747 int rsvd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 int status;
1750
David Chinner8d280b92006-03-14 13:13:09 +11001751 /* check for per-cpu counters */
1752 switch (field) {
1753#ifdef HAVE_PERCPU_SB
1754 case XFS_SBS_ICOUNT:
1755 case XFS_SBS_IFREE:
1756 case XFS_SBS_FDBLOCKS:
1757 if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
1758 status = xfs_icsb_modify_counters(mp, field,
1759 delta, rsvd);
1760 break;
1761 }
1762 /* FALLTHROUGH */
1763#endif
1764 default:
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001765 spin_lock(&mp->m_sb_lock);
David Chinner8d280b92006-03-14 13:13:09 +11001766 status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001767 spin_unlock(&mp->m_sb_lock);
David Chinner8d280b92006-03-14 13:13:09 +11001768 break;
1769 }
1770
Jesper Juhl014c2542006-01-15 02:37:08 +01001771 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772}
1773
1774/*
1775 * xfs_mod_incore_sb_batch() is used to change more than one field
1776 * in the in-core superblock structure at a time. This modification
1777 * is protected by a lock internal to this module. The fields and
1778 * changes to those fields are specified in the array of xfs_mod_sb
1779 * structures passed in.
1780 *
1781 * Either all of the specified deltas will be applied or none of
1782 * them will. If any modified field dips below 0, then all modifications
1783 * will be backed out and EINVAL will be returned.
1784 */
1785int
1786xfs_mod_incore_sb_batch(xfs_mount_t *mp, xfs_mod_sb_t *msb, uint nmsb, int rsvd)
1787{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 int status=0;
1789 xfs_mod_sb_t *msbp;
1790
1791 /*
1792 * Loop through the array of mod structures and apply each
1793 * individually. If any fail, then back out all those
1794 * which have already been applied. Do all of this within
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001795 * the scope of the m_sb_lock so that all of the changes will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 * be atomic.
1797 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001798 spin_lock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 msbp = &msb[0];
1800 for (msbp = &msbp[0]; msbp < (msb + nmsb); msbp++) {
1801 /*
1802 * Apply the delta at index n. If it fails, break
1803 * from the loop so we'll fall into the undo loop
1804 * below.
1805 */
David Chinner8d280b92006-03-14 13:13:09 +11001806 switch (msbp->msb_field) {
1807#ifdef HAVE_PERCPU_SB
1808 case XFS_SBS_ICOUNT:
1809 case XFS_SBS_IFREE:
1810 case XFS_SBS_FDBLOCKS:
1811 if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001812 spin_unlock(&mp->m_sb_lock);
David Chinner20b64282007-02-10 18:35:09 +11001813 status = xfs_icsb_modify_counters(mp,
David Chinner8d280b92006-03-14 13:13:09 +11001814 msbp->msb_field,
1815 msbp->msb_delta, rsvd);
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001816 spin_lock(&mp->m_sb_lock);
David Chinner8d280b92006-03-14 13:13:09 +11001817 break;
1818 }
1819 /* FALLTHROUGH */
1820#endif
1821 default:
1822 status = xfs_mod_incore_sb_unlocked(mp,
1823 msbp->msb_field,
1824 msbp->msb_delta, rsvd);
1825 break;
1826 }
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 if (status != 0) {
1829 break;
1830 }
1831 }
1832
1833 /*
1834 * If we didn't complete the loop above, then back out
1835 * any changes made to the superblock. If you add code
1836 * between the loop above and here, make sure that you
1837 * preserve the value of status. Loop back until
1838 * we step below the beginning of the array. Make sure
1839 * we don't touch anything back there.
1840 */
1841 if (status != 0) {
1842 msbp--;
1843 while (msbp >= msb) {
David Chinner8d280b92006-03-14 13:13:09 +11001844 switch (msbp->msb_field) {
1845#ifdef HAVE_PERCPU_SB
1846 case XFS_SBS_ICOUNT:
1847 case XFS_SBS_IFREE:
1848 case XFS_SBS_FDBLOCKS:
1849 if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001850 spin_unlock(&mp->m_sb_lock);
David Chinner20b64282007-02-10 18:35:09 +11001851 status = xfs_icsb_modify_counters(mp,
David Chinner8d280b92006-03-14 13:13:09 +11001852 msbp->msb_field,
1853 -(msbp->msb_delta),
1854 rsvd);
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001855 spin_lock(&mp->m_sb_lock);
David Chinner8d280b92006-03-14 13:13:09 +11001856 break;
1857 }
1858 /* FALLTHROUGH */
1859#endif
1860 default:
1861 status = xfs_mod_incore_sb_unlocked(mp,
1862 msbp->msb_field,
1863 -(msbp->msb_delta),
1864 rsvd);
1865 break;
1866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 ASSERT(status == 0);
1868 msbp--;
1869 }
1870 }
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001871 spin_unlock(&mp->m_sb_lock);
Jesper Juhl014c2542006-01-15 02:37:08 +01001872 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873}
1874
1875/*
1876 * xfs_getsb() is called to obtain the buffer for the superblock.
1877 * The buffer is returned locked and read in from disk.
1878 * The buffer should be released with a call to xfs_brelse().
1879 *
1880 * If the flags parameter is BUF_TRYLOCK, then we'll only return
1881 * the superblock buffer if it can be locked without sleeping.
1882 * If it can't then we'll return NULL.
1883 */
1884xfs_buf_t *
1885xfs_getsb(
1886 xfs_mount_t *mp,
1887 int flags)
1888{
1889 xfs_buf_t *bp;
1890
1891 ASSERT(mp->m_sb_bp != NULL);
1892 bp = mp->m_sb_bp;
1893 if (flags & XFS_BUF_TRYLOCK) {
1894 if (!XFS_BUF_CPSEMA(bp)) {
1895 return NULL;
1896 }
1897 } else {
1898 XFS_BUF_PSEMA(bp, PRIBIO);
1899 }
1900 XFS_BUF_HOLD(bp);
1901 ASSERT(XFS_BUF_ISDONE(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01001902 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903}
1904
1905/*
1906 * Used to free the superblock along various error paths.
1907 */
1908void
1909xfs_freesb(
1910 xfs_mount_t *mp)
1911{
1912 xfs_buf_t *bp;
1913
1914 /*
1915 * Use xfs_getsb() so that the buffer will be locked
1916 * when we call xfs_buf_relse().
1917 */
1918 bp = xfs_getsb(mp, 0);
1919 XFS_BUF_UNMANAGE(bp);
1920 xfs_buf_relse(bp);
1921 mp->m_sb_bp = NULL;
1922}
1923
1924/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 * Used to log changes to the superblock unit and width fields which could
Eric Sandeene6957ea2008-04-10 12:19:34 +10001926 * be altered by the mount options, as well as any potential sb_features2
1927 * fixup. Only the first superblock is updated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 */
Christoph Hellwig7884bc82009-01-19 02:04:07 +01001929int
David Chinneree1c0902008-03-06 13:45:50 +11001930xfs_mount_log_sb(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 xfs_mount_t *mp,
1932 __int64_t fields)
1933{
1934 xfs_trans_t *tp;
David Chinnere5720ee2008-04-10 12:21:18 +10001935 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
David Chinneree1c0902008-03-06 13:45:50 +11001937 ASSERT(fields & (XFS_SB_UNIT | XFS_SB_WIDTH | XFS_SB_UUID |
David Chinner4b166de2008-05-20 11:30:27 +10001938 XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2 |
1939 XFS_SB_VERSIONNUM));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
1941 tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT);
David Chinnere5720ee2008-04-10 12:21:18 +10001942 error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
1943 XFS_DEFAULT_LOG_COUNT);
1944 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 xfs_trans_cancel(tp, 0);
David Chinnere5720ee2008-04-10 12:21:18 +10001946 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 }
1948 xfs_mod_sb(tp, fields);
David Chinnere5720ee2008-04-10 12:21:18 +10001949 error = xfs_trans_commit(tp, 0);
1950 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951}
David Chinner8d280b92006-03-14 13:13:09 +11001952
1953
1954#ifdef HAVE_PERCPU_SB
1955/*
1956 * Per-cpu incore superblock counters
1957 *
1958 * Simple concept, difficult implementation
1959 *
1960 * Basically, replace the incore superblock counters with a distributed per cpu
1961 * counter for contended fields (e.g. free block count).
1962 *
1963 * Difficulties arise in that the incore sb is used for ENOSPC checking, and
1964 * hence needs to be accurately read when we are running low on space. Hence
1965 * there is a method to enable and disable the per-cpu counters based on how
1966 * much "stuff" is available in them.
1967 *
1968 * Basically, a counter is enabled if there is enough free resource to justify
1969 * running a per-cpu fast-path. If the per-cpu counter runs out (i.e. a local
1970 * ENOSPC), then we disable the counters to synchronise all callers and
1971 * re-distribute the available resources.
1972 *
1973 * If, once we redistributed the available resources, we still get a failure,
1974 * we disable the per-cpu counter and go through the slow path.
1975 *
1976 * The slow path is the current xfs_mod_incore_sb() function. This means that
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001977 * when we disable a per-cpu counter, we need to drain its resources back to
David Chinner8d280b92006-03-14 13:13:09 +11001978 * the global superblock. We do this after disabling the counter to prevent
1979 * more threads from queueing up on the counter.
1980 *
1981 * Essentially, this means that we still need a lock in the fast path to enable
1982 * synchronisation between the global counters and the per-cpu counters. This
1983 * is not a problem because the lock will be local to a CPU almost all the time
1984 * and have little contention except when we get to ENOSPC conditions.
1985 *
1986 * Basically, this lock becomes a barrier that enables us to lock out the fast
1987 * path while we do things like enabling and disabling counters and
1988 * synchronising the counters.
1989 *
1990 * Locking rules:
1991 *
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001992 * 1. m_sb_lock before picking up per-cpu locks
David Chinner8d280b92006-03-14 13:13:09 +11001993 * 2. per-cpu locks always picked up via for_each_online_cpu() order
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001994 * 3. accurate counter sync requires m_sb_lock + per cpu locks
David Chinner8d280b92006-03-14 13:13:09 +11001995 * 4. modifying per-cpu counters requires holding per-cpu lock
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001996 * 5. modifying global counters requires holding m_sb_lock
1997 * 6. enabling or disabling a counter requires holding the m_sb_lock
David Chinner8d280b92006-03-14 13:13:09 +11001998 * and _none_ of the per-cpu locks.
1999 *
2000 * Disabled counters are only ever re-enabled by a balance operation
2001 * that results in more free resources per CPU than a given threshold.
2002 * To ensure counters don't remain disabled, they are rebalanced when
2003 * the global resource goes above a higher threshold (i.e. some hysteresis
2004 * is present to prevent thrashing).
David Chinner8d280b92006-03-14 13:13:09 +11002005 */
David Chinnere8234a62006-03-14 13:23:52 +11002006
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07002007#ifdef CONFIG_HOTPLUG_CPU
David Chinnere8234a62006-03-14 13:23:52 +11002008/*
2009 * hot-plug CPU notifier support.
2010 *
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07002011 * We need a notifier per filesystem as we need to be able to identify
2012 * the filesystem to balance the counters out. This is achieved by
2013 * having a notifier block embedded in the xfs_mount_t and doing pointer
2014 * magic to get the mount pointer from the notifier block address.
David Chinnere8234a62006-03-14 13:23:52 +11002015 */
2016STATIC int
2017xfs_icsb_cpu_notify(
2018 struct notifier_block *nfb,
2019 unsigned long action,
2020 void *hcpu)
2021{
2022 xfs_icsb_cnts_t *cntp;
2023 xfs_mount_t *mp;
David Chinnere8234a62006-03-14 13:23:52 +11002024
2025 mp = (xfs_mount_t *)container_of(nfb, xfs_mount_t, m_icsb_notifier);
2026 cntp = (xfs_icsb_cnts_t *)
2027 per_cpu_ptr(mp->m_sb_cnts, (unsigned long)hcpu);
2028 switch (action) {
2029 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002030 case CPU_UP_PREPARE_FROZEN:
David Chinnere8234a62006-03-14 13:23:52 +11002031 /* Easy Case - initialize the area and locks, and
2032 * then rebalance when online does everything else for us. */
David Chinner01e1b692006-03-14 13:29:16 +11002033 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
David Chinnere8234a62006-03-14 13:23:52 +11002034 break;
2035 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002036 case CPU_ONLINE_FROZEN:
David Chinner03135cf2007-02-10 18:35:15 +11002037 xfs_icsb_lock(mp);
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002038 xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
2039 xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
2040 xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
David Chinner03135cf2007-02-10 18:35:15 +11002041 xfs_icsb_unlock(mp);
David Chinnere8234a62006-03-14 13:23:52 +11002042 break;
2043 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002044 case CPU_DEAD_FROZEN:
David Chinnere8234a62006-03-14 13:23:52 +11002045 /* Disable all the counters, then fold the dead cpu's
2046 * count into the total on the global superblock and
2047 * re-enable the counters. */
David Chinner03135cf2007-02-10 18:35:15 +11002048 xfs_icsb_lock(mp);
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002049 spin_lock(&mp->m_sb_lock);
David Chinnere8234a62006-03-14 13:23:52 +11002050 xfs_icsb_disable_counter(mp, XFS_SBS_ICOUNT);
2051 xfs_icsb_disable_counter(mp, XFS_SBS_IFREE);
2052 xfs_icsb_disable_counter(mp, XFS_SBS_FDBLOCKS);
2053
2054 mp->m_sb.sb_icount += cntp->icsb_icount;
2055 mp->m_sb.sb_ifree += cntp->icsb_ifree;
2056 mp->m_sb.sb_fdblocks += cntp->icsb_fdblocks;
2057
David Chinner01e1b692006-03-14 13:29:16 +11002058 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
David Chinnere8234a62006-03-14 13:23:52 +11002059
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002060 xfs_icsb_balance_counter_locked(mp, XFS_SBS_ICOUNT, 0);
2061 xfs_icsb_balance_counter_locked(mp, XFS_SBS_IFREE, 0);
2062 xfs_icsb_balance_counter_locked(mp, XFS_SBS_FDBLOCKS, 0);
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002063 spin_unlock(&mp->m_sb_lock);
David Chinner03135cf2007-02-10 18:35:15 +11002064 xfs_icsb_unlock(mp);
David Chinnere8234a62006-03-14 13:23:52 +11002065 break;
2066 }
2067
2068 return NOTIFY_OK;
2069}
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07002070#endif /* CONFIG_HOTPLUG_CPU */
David Chinnere8234a62006-03-14 13:23:52 +11002071
David Chinner8d280b92006-03-14 13:13:09 +11002072int
2073xfs_icsb_init_counters(
2074 xfs_mount_t *mp)
2075{
2076 xfs_icsb_cnts_t *cntp;
2077 int i;
2078
2079 mp->m_sb_cnts = alloc_percpu(xfs_icsb_cnts_t);
2080 if (mp->m_sb_cnts == NULL)
2081 return -ENOMEM;
2082
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07002083#ifdef CONFIG_HOTPLUG_CPU
David Chinnere8234a62006-03-14 13:23:52 +11002084 mp->m_icsb_notifier.notifier_call = xfs_icsb_cpu_notify;
2085 mp->m_icsb_notifier.priority = 0;
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07002086 register_hotcpu_notifier(&mp->m_icsb_notifier);
2087#endif /* CONFIG_HOTPLUG_CPU */
David Chinnere8234a62006-03-14 13:23:52 +11002088
David Chinner8d280b92006-03-14 13:13:09 +11002089 for_each_online_cpu(i) {
2090 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
David Chinner01e1b692006-03-14 13:29:16 +11002091 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
David Chinner8d280b92006-03-14 13:13:09 +11002092 }
David Chinner20b64282007-02-10 18:35:09 +11002093
2094 mutex_init(&mp->m_icsb_mutex);
2095
David Chinner8d280b92006-03-14 13:13:09 +11002096 /*
2097 * start with all counters disabled so that the
2098 * initial balance kicks us off correctly
2099 */
2100 mp->m_icsb_counters = -1;
2101 return 0;
2102}
2103
Lachlan McIlroy5478eea2007-02-10 18:36:29 +11002104void
2105xfs_icsb_reinit_counters(
2106 xfs_mount_t *mp)
2107{
2108 xfs_icsb_lock(mp);
2109 /*
2110 * start with all counters disabled so that the
2111 * initial balance kicks us off correctly
2112 */
2113 mp->m_icsb_counters = -1;
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002114 xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
2115 xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
2116 xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
Lachlan McIlroy5478eea2007-02-10 18:36:29 +11002117 xfs_icsb_unlock(mp);
2118}
2119
Christoph Hellwigc962fb72008-05-20 15:10:52 +10002120void
David Chinner8d280b92006-03-14 13:13:09 +11002121xfs_icsb_destroy_counters(
2122 xfs_mount_t *mp)
2123{
David Chinnere8234a62006-03-14 13:23:52 +11002124 if (mp->m_sb_cnts) {
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07002125 unregister_hotcpu_notifier(&mp->m_icsb_notifier);
David Chinner8d280b92006-03-14 13:13:09 +11002126 free_percpu(mp->m_sb_cnts);
David Chinnere8234a62006-03-14 13:23:52 +11002127 }
David Chinner03135cf2007-02-10 18:35:15 +11002128 mutex_destroy(&mp->m_icsb_mutex);
David Chinner8d280b92006-03-14 13:13:09 +11002129}
2130
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00002131STATIC void
David Chinner01e1b692006-03-14 13:29:16 +11002132xfs_icsb_lock_cntr(
2133 xfs_icsb_cnts_t *icsbp)
2134{
2135 while (test_and_set_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags)) {
2136 ndelay(1000);
2137 }
2138}
2139
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00002140STATIC void
David Chinner01e1b692006-03-14 13:29:16 +11002141xfs_icsb_unlock_cntr(
2142 xfs_icsb_cnts_t *icsbp)
2143{
2144 clear_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags);
2145}
2146
David Chinner8d280b92006-03-14 13:13:09 +11002147
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00002148STATIC void
David Chinner8d280b92006-03-14 13:13:09 +11002149xfs_icsb_lock_all_counters(
2150 xfs_mount_t *mp)
2151{
2152 xfs_icsb_cnts_t *cntp;
2153 int i;
2154
2155 for_each_online_cpu(i) {
2156 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
David Chinner01e1b692006-03-14 13:29:16 +11002157 xfs_icsb_lock_cntr(cntp);
David Chinner8d280b92006-03-14 13:13:09 +11002158 }
2159}
2160
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00002161STATIC void
David Chinner8d280b92006-03-14 13:13:09 +11002162xfs_icsb_unlock_all_counters(
2163 xfs_mount_t *mp)
2164{
2165 xfs_icsb_cnts_t *cntp;
2166 int i;
2167
2168 for_each_online_cpu(i) {
2169 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
David Chinner01e1b692006-03-14 13:29:16 +11002170 xfs_icsb_unlock_cntr(cntp);
David Chinner8d280b92006-03-14 13:13:09 +11002171 }
2172}
2173
2174STATIC void
2175xfs_icsb_count(
2176 xfs_mount_t *mp,
2177 xfs_icsb_cnts_t *cnt,
2178 int flags)
2179{
2180 xfs_icsb_cnts_t *cntp;
2181 int i;
2182
2183 memset(cnt, 0, sizeof(xfs_icsb_cnts_t));
2184
2185 if (!(flags & XFS_ICSB_LAZY_COUNT))
2186 xfs_icsb_lock_all_counters(mp);
2187
2188 for_each_online_cpu(i) {
2189 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
2190 cnt->icsb_icount += cntp->icsb_icount;
2191 cnt->icsb_ifree += cntp->icsb_ifree;
2192 cnt->icsb_fdblocks += cntp->icsb_fdblocks;
2193 }
2194
2195 if (!(flags & XFS_ICSB_LAZY_COUNT))
2196 xfs_icsb_unlock_all_counters(mp);
2197}
2198
2199STATIC int
2200xfs_icsb_counter_disabled(
2201 xfs_mount_t *mp,
2202 xfs_sb_field_t field)
2203{
2204 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
2205 return test_bit(field, &mp->m_icsb_counters);
2206}
2207
David Chinner36fbe6e2008-04-10 12:19:56 +10002208STATIC void
David Chinner8d280b92006-03-14 13:13:09 +11002209xfs_icsb_disable_counter(
2210 xfs_mount_t *mp,
2211 xfs_sb_field_t field)
2212{
2213 xfs_icsb_cnts_t cnt;
2214
2215 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
2216
David Chinner20b64282007-02-10 18:35:09 +11002217 /*
2218 * If we are already disabled, then there is nothing to do
2219 * here. We check before locking all the counters to avoid
2220 * the expensive lock operation when being called in the
2221 * slow path and the counter is already disabled. This is
2222 * safe because the only time we set or clear this state is under
2223 * the m_icsb_mutex.
2224 */
2225 if (xfs_icsb_counter_disabled(mp, field))
David Chinner36fbe6e2008-04-10 12:19:56 +10002226 return;
David Chinner20b64282007-02-10 18:35:09 +11002227
David Chinner8d280b92006-03-14 13:13:09 +11002228 xfs_icsb_lock_all_counters(mp);
2229 if (!test_and_set_bit(field, &mp->m_icsb_counters)) {
2230 /* drain back to superblock */
2231
Christoph Hellwigce461932008-04-22 17:34:50 +10002232 xfs_icsb_count(mp, &cnt, XFS_ICSB_LAZY_COUNT);
David Chinner8d280b92006-03-14 13:13:09 +11002233 switch(field) {
2234 case XFS_SBS_ICOUNT:
2235 mp->m_sb.sb_icount = cnt.icsb_icount;
2236 break;
2237 case XFS_SBS_IFREE:
2238 mp->m_sb.sb_ifree = cnt.icsb_ifree;
2239 break;
2240 case XFS_SBS_FDBLOCKS:
2241 mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
2242 break;
2243 default:
2244 BUG();
2245 }
2246 }
2247
2248 xfs_icsb_unlock_all_counters(mp);
David Chinner8d280b92006-03-14 13:13:09 +11002249}
2250
2251STATIC void
2252xfs_icsb_enable_counter(
2253 xfs_mount_t *mp,
2254 xfs_sb_field_t field,
2255 uint64_t count,
2256 uint64_t resid)
2257{
2258 xfs_icsb_cnts_t *cntp;
2259 int i;
2260
2261 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
2262
2263 xfs_icsb_lock_all_counters(mp);
2264 for_each_online_cpu(i) {
2265 cntp = per_cpu_ptr(mp->m_sb_cnts, i);
2266 switch (field) {
2267 case XFS_SBS_ICOUNT:
2268 cntp->icsb_icount = count + resid;
2269 break;
2270 case XFS_SBS_IFREE:
2271 cntp->icsb_ifree = count + resid;
2272 break;
2273 case XFS_SBS_FDBLOCKS:
2274 cntp->icsb_fdblocks = count + resid;
2275 break;
2276 default:
2277 BUG();
2278 break;
2279 }
2280 resid = 0;
2281 }
2282 clear_bit(field, &mp->m_icsb_counters);
2283 xfs_icsb_unlock_all_counters(mp);
2284}
2285
David Chinnerdbcabad2007-02-10 18:36:17 +11002286void
Christoph Hellwigd4d90b52008-04-22 17:34:37 +10002287xfs_icsb_sync_counters_locked(
David Chinner8d280b92006-03-14 13:13:09 +11002288 xfs_mount_t *mp,
2289 int flags)
2290{
2291 xfs_icsb_cnts_t cnt;
David Chinner8d280b92006-03-14 13:13:09 +11002292
David Chinner8d280b92006-03-14 13:13:09 +11002293 xfs_icsb_count(mp, &cnt, flags);
2294
David Chinner8d280b92006-03-14 13:13:09 +11002295 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_ICOUNT))
2296 mp->m_sb.sb_icount = cnt.icsb_icount;
2297 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_IFREE))
2298 mp->m_sb.sb_ifree = cnt.icsb_ifree;
2299 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_FDBLOCKS))
2300 mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
David Chinner8d280b92006-03-14 13:13:09 +11002301}
2302
2303/*
2304 * Accurate update of per-cpu counters to incore superblock
2305 */
Christoph Hellwigd4d90b52008-04-22 17:34:37 +10002306void
David Chinner8d280b92006-03-14 13:13:09 +11002307xfs_icsb_sync_counters(
Christoph Hellwigd4d90b52008-04-22 17:34:37 +10002308 xfs_mount_t *mp,
2309 int flags)
David Chinner8d280b92006-03-14 13:13:09 +11002310{
Christoph Hellwigd4d90b52008-04-22 17:34:37 +10002311 spin_lock(&mp->m_sb_lock);
2312 xfs_icsb_sync_counters_locked(mp, flags);
2313 spin_unlock(&mp->m_sb_lock);
David Chinner8d280b92006-03-14 13:13:09 +11002314}
2315
2316/*
2317 * Balance and enable/disable counters as necessary.
2318 *
David Chinner20b64282007-02-10 18:35:09 +11002319 * Thresholds for re-enabling counters are somewhat magic. inode counts are
2320 * chosen to be the same number as single on disk allocation chunk per CPU, and
2321 * free blocks is something far enough zero that we aren't going thrash when we
2322 * get near ENOSPC. We also need to supply a minimum we require per cpu to
2323 * prevent looping endlessly when xfs_alloc_space asks for more than will
2324 * be distributed to a single CPU but each CPU has enough blocks to be
2325 * reenabled.
2326 *
2327 * Note that we can be called when counters are already disabled.
2328 * xfs_icsb_disable_counter() optimises the counter locking in this case to
2329 * prevent locking every per-cpu counter needlessly.
David Chinner8d280b92006-03-14 13:13:09 +11002330 */
David Chinner20b64282007-02-10 18:35:09 +11002331
2332#define XFS_ICSB_INO_CNTR_REENABLE (uint64_t)64
David Chinner4be536d2006-09-07 14:26:50 +10002333#define XFS_ICSB_FDBLK_CNTR_REENABLE(mp) \
David Chinner20b64282007-02-10 18:35:09 +11002334 (uint64_t)(512 + XFS_ALLOC_SET_ASIDE(mp))
David Chinner8d280b92006-03-14 13:13:09 +11002335STATIC void
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002336xfs_icsb_balance_counter_locked(
David Chinner8d280b92006-03-14 13:13:09 +11002337 xfs_mount_t *mp,
2338 xfs_sb_field_t field,
David Chinner20b64282007-02-10 18:35:09 +11002339 int min_per_cpu)
David Chinner8d280b92006-03-14 13:13:09 +11002340{
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10002341 uint64_t count, resid;
David Chinner8d280b92006-03-14 13:13:09 +11002342 int weight = num_online_cpus();
David Chinner20b64282007-02-10 18:35:09 +11002343 uint64_t min = (uint64_t)min_per_cpu;
David Chinner8d280b92006-03-14 13:13:09 +11002344
David Chinner8d280b92006-03-14 13:13:09 +11002345 /* disable counter and sync counter */
2346 xfs_icsb_disable_counter(mp, field);
2347
2348 /* update counters - first CPU gets residual*/
2349 switch (field) {
2350 case XFS_SBS_ICOUNT:
2351 count = mp->m_sb.sb_icount;
2352 resid = do_div(count, weight);
David Chinner20b64282007-02-10 18:35:09 +11002353 if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002354 return;
David Chinner8d280b92006-03-14 13:13:09 +11002355 break;
2356 case XFS_SBS_IFREE:
2357 count = mp->m_sb.sb_ifree;
2358 resid = do_div(count, weight);
David Chinner20b64282007-02-10 18:35:09 +11002359 if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002360 return;
David Chinner8d280b92006-03-14 13:13:09 +11002361 break;
2362 case XFS_SBS_FDBLOCKS:
2363 count = mp->m_sb.sb_fdblocks;
2364 resid = do_div(count, weight);
David Chinner20b64282007-02-10 18:35:09 +11002365 if (count < max(min, XFS_ICSB_FDBLK_CNTR_REENABLE(mp)))
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002366 return;
David Chinner8d280b92006-03-14 13:13:09 +11002367 break;
2368 default:
2369 BUG();
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10002370 count = resid = 0; /* quiet, gcc */
David Chinner8d280b92006-03-14 13:13:09 +11002371 break;
2372 }
2373
2374 xfs_icsb_enable_counter(mp, field, count, resid);
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002375}
2376
2377STATIC void
2378xfs_icsb_balance_counter(
2379 xfs_mount_t *mp,
2380 xfs_sb_field_t fields,
2381 int min_per_cpu)
2382{
2383 spin_lock(&mp->m_sb_lock);
2384 xfs_icsb_balance_counter_locked(mp, fields, min_per_cpu);
2385 spin_unlock(&mp->m_sb_lock);
David Chinner8d280b92006-03-14 13:13:09 +11002386}
2387
David Chinnera8272ce2007-11-23 16:28:09 +11002388STATIC int
David Chinner20b64282007-02-10 18:35:09 +11002389xfs_icsb_modify_counters(
David Chinner8d280b92006-03-14 13:13:09 +11002390 xfs_mount_t *mp,
2391 xfs_sb_field_t field,
David Chinner20f4ebf2007-02-10 18:36:10 +11002392 int64_t delta,
David Chinner20b64282007-02-10 18:35:09 +11002393 int rsvd)
David Chinner8d280b92006-03-14 13:13:09 +11002394{
2395 xfs_icsb_cnts_t *icsbp;
2396 long long lcounter; /* long counter for 64 bit fields */
Christoph Lameter7a9e02d2009-10-03 19:48:23 +09002397 int ret = 0;
David Chinner8d280b92006-03-14 13:13:09 +11002398
David Chinner20b64282007-02-10 18:35:09 +11002399 might_sleep();
David Chinner8d280b92006-03-14 13:13:09 +11002400again:
Christoph Lameter7a9e02d2009-10-03 19:48:23 +09002401 preempt_disable();
2402 icsbp = this_cpu_ptr(mp->m_sb_cnts);
David Chinner20b64282007-02-10 18:35:09 +11002403
2404 /*
2405 * if the counter is disabled, go to slow path
2406 */
David Chinner8d280b92006-03-14 13:13:09 +11002407 if (unlikely(xfs_icsb_counter_disabled(mp, field)))
2408 goto slow_path;
David Chinner20b64282007-02-10 18:35:09 +11002409 xfs_icsb_lock_cntr(icsbp);
2410 if (unlikely(xfs_icsb_counter_disabled(mp, field))) {
2411 xfs_icsb_unlock_cntr(icsbp);
2412 goto slow_path;
2413 }
David Chinner8d280b92006-03-14 13:13:09 +11002414
2415 switch (field) {
2416 case XFS_SBS_ICOUNT:
2417 lcounter = icsbp->icsb_icount;
2418 lcounter += delta;
2419 if (unlikely(lcounter < 0))
David Chinner20b64282007-02-10 18:35:09 +11002420 goto balance_counter;
David Chinner8d280b92006-03-14 13:13:09 +11002421 icsbp->icsb_icount = lcounter;
2422 break;
2423
2424 case XFS_SBS_IFREE:
2425 lcounter = icsbp->icsb_ifree;
2426 lcounter += delta;
2427 if (unlikely(lcounter < 0))
David Chinner20b64282007-02-10 18:35:09 +11002428 goto balance_counter;
David Chinner8d280b92006-03-14 13:13:09 +11002429 icsbp->icsb_ifree = lcounter;
2430 break;
2431
2432 case XFS_SBS_FDBLOCKS:
2433 BUG_ON((mp->m_resblks - mp->m_resblks_avail) != 0);
2434
David Chinner4be536d2006-09-07 14:26:50 +10002435 lcounter = icsbp->icsb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
David Chinner8d280b92006-03-14 13:13:09 +11002436 lcounter += delta;
2437 if (unlikely(lcounter < 0))
David Chinner20b64282007-02-10 18:35:09 +11002438 goto balance_counter;
David Chinner4be536d2006-09-07 14:26:50 +10002439 icsbp->icsb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
David Chinner8d280b92006-03-14 13:13:09 +11002440 break;
2441 default:
2442 BUG();
2443 break;
2444 }
David Chinner01e1b692006-03-14 13:29:16 +11002445 xfs_icsb_unlock_cntr(icsbp);
Christoph Lameter7a9e02d2009-10-03 19:48:23 +09002446 preempt_enable();
David Chinner8d280b92006-03-14 13:13:09 +11002447 return 0;
2448
David Chinner8d280b92006-03-14 13:13:09 +11002449slow_path:
Christoph Lameter7a9e02d2009-10-03 19:48:23 +09002450 preempt_enable();
David Chinner20b64282007-02-10 18:35:09 +11002451
2452 /*
2453 * serialise with a mutex so we don't burn lots of cpu on
2454 * the superblock lock. We still need to hold the superblock
2455 * lock, however, when we modify the global structures.
2456 */
David Chinner03135cf2007-02-10 18:35:15 +11002457 xfs_icsb_lock(mp);
David Chinner20b64282007-02-10 18:35:09 +11002458
2459 /*
2460 * Now running atomically.
2461 *
2462 * If the counter is enabled, someone has beaten us to rebalancing.
2463 * Drop the lock and try again in the fast path....
2464 */
2465 if (!(xfs_icsb_counter_disabled(mp, field))) {
David Chinner03135cf2007-02-10 18:35:15 +11002466 xfs_icsb_unlock(mp);
David Chinner20b64282007-02-10 18:35:09 +11002467 goto again;
2468 }
2469
2470 /*
2471 * The counter is currently disabled. Because we are
2472 * running atomically here, we know a rebalance cannot
2473 * be in progress. Hence we can go straight to operating
2474 * on the global superblock. We do not call xfs_mod_incore_sb()
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002475 * here even though we need to get the m_sb_lock. Doing so
David Chinner20b64282007-02-10 18:35:09 +11002476 * will cause us to re-enter this function and deadlock.
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002477 * Hence we get the m_sb_lock ourselves and then call
David Chinner20b64282007-02-10 18:35:09 +11002478 * xfs_mod_incore_sb_unlocked() as the unlocked path operates
2479 * directly on the global counters.
2480 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002481 spin_lock(&mp->m_sb_lock);
David Chinner20b64282007-02-10 18:35:09 +11002482 ret = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002483 spin_unlock(&mp->m_sb_lock);
David Chinner20b64282007-02-10 18:35:09 +11002484
2485 /*
2486 * Now that we've modified the global superblock, we
2487 * may be able to re-enable the distributed counters
2488 * (e.g. lots of space just got freed). After that
2489 * we are done.
2490 */
2491 if (ret != ENOSPC)
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002492 xfs_icsb_balance_counter(mp, field, 0);
David Chinner03135cf2007-02-10 18:35:15 +11002493 xfs_icsb_unlock(mp);
David Chinner20b64282007-02-10 18:35:09 +11002494 return ret;
2495
2496balance_counter:
David Chinner01e1b692006-03-14 13:29:16 +11002497 xfs_icsb_unlock_cntr(icsbp);
Christoph Lameter7a9e02d2009-10-03 19:48:23 +09002498 preempt_enable();
David Chinner8d280b92006-03-14 13:13:09 +11002499
David Chinner20b64282007-02-10 18:35:09 +11002500 /*
2501 * We may have multiple threads here if multiple per-cpu
2502 * counters run dry at the same time. This will mean we can
2503 * do more balances than strictly necessary but it is not
2504 * the common slowpath case.
2505 */
David Chinner03135cf2007-02-10 18:35:15 +11002506 xfs_icsb_lock(mp);
David Chinner8d280b92006-03-14 13:13:09 +11002507
David Chinner20b64282007-02-10 18:35:09 +11002508 /*
2509 * running atomically.
2510 *
2511 * This will leave the counter in the correct state for future
2512 * accesses. After the rebalance, we simply try again and our retry
2513 * will either succeed through the fast path or slow path without
2514 * another balance operation being required.
2515 */
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002516 xfs_icsb_balance_counter(mp, field, delta);
David Chinner03135cf2007-02-10 18:35:15 +11002517 xfs_icsb_unlock(mp);
David Chinner20b64282007-02-10 18:35:09 +11002518 goto again;
David Chinner8d280b92006-03-14 13:13:09 +11002519}
2520
David Chinner8d280b92006-03-14 13:13:09 +11002521#endif