blob: b07ca4be3941d7f24a1acedb393cccfa871a96d0 [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"
Dave Chinner6ca1c902013-08-12 20:49:26 +100020#include "xfs_format.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"
Christoph Hellwig211e4d42012-04-23 15:58:34 +100025#include "xfs_trans_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_sb.h"
27#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_mount.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100029#include "xfs_da_btree.h"
30#include "xfs_dir2_format.h"
31#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_ialloc_btree.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_quota.h"
44#include "xfs_fsops.h"
Christoph Hellwig43355092008-03-27 18:01:08 +110045#include "xfs_utils.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000046#include "xfs_trace.h"
Dave Chinner6d8b79c2012-10-08 21:56:09 +110047#include "xfs_icache.h"
Dave Chinner04a1e6c2013-04-03 16:11:31 +110048#include "xfs_cksum.h"
49#include "xfs_buf_item.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
David Chinner8d280b92006-03-14 13:13:09 +110052#ifdef HAVE_PERCPU_SB
David Chinner20f4ebf2007-02-10 18:36:10 +110053STATIC void xfs_icsb_balance_counter(xfs_mount_t *, xfs_sb_field_t,
Christoph Hellwig45af6c62008-04-22 17:34:44 +100054 int);
55STATIC void xfs_icsb_balance_counter_locked(xfs_mount_t *, xfs_sb_field_t,
56 int);
David Chinner36fbe6e2008-04-10 12:19:56 +100057STATIC void xfs_icsb_disable_counter(xfs_mount_t *, xfs_sb_field_t);
David Chinner8d280b92006-03-14 13:13:09 +110058#else
59
Christoph Hellwig45af6c62008-04-22 17:34:44 +100060#define xfs_icsb_balance_counter(mp, a, b) do { } while (0)
61#define xfs_icsb_balance_counter_locked(mp, a, b) do { } while (0)
David Chinner8d280b92006-03-14 13:13:09 +110062#endif
63
Christoph Hellwig1df84c92006-01-11 15:29:52 +110064static const struct {
David Chinner8d280b92006-03-14 13:13:09 +110065 short offset;
66 short type; /* 0 = integer
67 * 1 = binary / string (no translation)
68 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069} xfs_sb_info[] = {
70 { offsetof(xfs_sb_t, sb_magicnum), 0 },
71 { offsetof(xfs_sb_t, sb_blocksize), 0 },
72 { offsetof(xfs_sb_t, sb_dblocks), 0 },
73 { offsetof(xfs_sb_t, sb_rblocks), 0 },
74 { offsetof(xfs_sb_t, sb_rextents), 0 },
75 { offsetof(xfs_sb_t, sb_uuid), 1 },
76 { offsetof(xfs_sb_t, sb_logstart), 0 },
77 { offsetof(xfs_sb_t, sb_rootino), 0 },
78 { offsetof(xfs_sb_t, sb_rbmino), 0 },
79 { offsetof(xfs_sb_t, sb_rsumino), 0 },
80 { offsetof(xfs_sb_t, sb_rextsize), 0 },
81 { offsetof(xfs_sb_t, sb_agblocks), 0 },
82 { offsetof(xfs_sb_t, sb_agcount), 0 },
83 { offsetof(xfs_sb_t, sb_rbmblocks), 0 },
84 { offsetof(xfs_sb_t, sb_logblocks), 0 },
85 { offsetof(xfs_sb_t, sb_versionnum), 0 },
86 { offsetof(xfs_sb_t, sb_sectsize), 0 },
87 { offsetof(xfs_sb_t, sb_inodesize), 0 },
88 { offsetof(xfs_sb_t, sb_inopblock), 0 },
89 { offsetof(xfs_sb_t, sb_fname[0]), 1 },
90 { offsetof(xfs_sb_t, sb_blocklog), 0 },
91 { offsetof(xfs_sb_t, sb_sectlog), 0 },
92 { offsetof(xfs_sb_t, sb_inodelog), 0 },
93 { offsetof(xfs_sb_t, sb_inopblog), 0 },
94 { offsetof(xfs_sb_t, sb_agblklog), 0 },
95 { offsetof(xfs_sb_t, sb_rextslog), 0 },
96 { offsetof(xfs_sb_t, sb_inprogress), 0 },
97 { offsetof(xfs_sb_t, sb_imax_pct), 0 },
98 { offsetof(xfs_sb_t, sb_icount), 0 },
99 { offsetof(xfs_sb_t, sb_ifree), 0 },
100 { offsetof(xfs_sb_t, sb_fdblocks), 0 },
101 { offsetof(xfs_sb_t, sb_frextents), 0 },
102 { offsetof(xfs_sb_t, sb_uquotino), 0 },
103 { offsetof(xfs_sb_t, sb_gquotino), 0 },
104 { offsetof(xfs_sb_t, sb_qflags), 0 },
105 { offsetof(xfs_sb_t, sb_flags), 0 },
106 { offsetof(xfs_sb_t, sb_shared_vn), 0 },
107 { offsetof(xfs_sb_t, sb_inoalignmt), 0 },
108 { offsetof(xfs_sb_t, sb_unit), 0 },
109 { offsetof(xfs_sb_t, sb_width), 0 },
110 { offsetof(xfs_sb_t, sb_dirblklog), 0 },
111 { offsetof(xfs_sb_t, sb_logsectlog), 0 },
112 { offsetof(xfs_sb_t, sb_logsectsize),0 },
113 { offsetof(xfs_sb_t, sb_logsunit), 0 },
114 { offsetof(xfs_sb_t, sb_features2), 0 },
David Chinneree1c0902008-03-06 13:45:50 +1100115 { offsetof(xfs_sb_t, sb_bad_features2), 0 },
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100116 { offsetof(xfs_sb_t, sb_features_compat), 0 },
117 { offsetof(xfs_sb_t, sb_features_ro_compat), 0 },
118 { offsetof(xfs_sb_t, sb_features_incompat), 0 },
Dave Chinnere721f502013-04-03 16:11:32 +1100119 { offsetof(xfs_sb_t, sb_features_log_incompat), 0 },
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100120 { offsetof(xfs_sb_t, sb_crc), 0 },
Dave Chinnere721f502013-04-03 16:11:32 +1100121 { offsetof(xfs_sb_t, sb_pad), 0 },
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100122 { offsetof(xfs_sb_t, sb_pquotino), 0 },
123 { offsetof(xfs_sb_t, sb_lsn), 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 { sizeof(xfs_sb_t), 0 }
125};
126
Christoph Hellwig27174202009-03-30 10:21:31 +0200127static DEFINE_MUTEX(xfs_uuid_table_mutex);
128static int xfs_uuid_table_size;
129static uuid_t *xfs_uuid_table;
130
131/*
132 * See if the UUID is unique among mounted XFS filesystems.
133 * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
134 */
135STATIC int
136xfs_uuid_mount(
137 struct xfs_mount *mp)
138{
139 uuid_t *uuid = &mp->m_sb.sb_uuid;
140 int hole, i;
141
142 if (mp->m_flags & XFS_MOUNT_NOUUID)
143 return 0;
144
145 if (uuid_is_nil(uuid)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +1100146 xfs_warn(mp, "Filesystem has nil UUID - can't mount");
Christoph Hellwig27174202009-03-30 10:21:31 +0200147 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);
Mitsuo Hayasaka021000e2012-01-13 05:58:39 +0000174 xfs_warn(mp, "Filesystem has duplicate UUID %pU - can't mount", uuid);
Christoph Hellwig27174202009-03-30 10:21:31 +0200175 return XFS_ERROR(EINVAL);
176}
177
178STATIC void
179xfs_uuid_unmount(
180 struct xfs_mount *mp)
181{
182 uuid_t *uuid = &mp->m_sb.sb_uuid;
183 int i;
184
185 if (mp->m_flags & XFS_MOUNT_NOUUID)
186 return;
187
188 mutex_lock(&xfs_uuid_table_mutex);
189 for (i = 0; i < xfs_uuid_table_size; i++) {
190 if (uuid_is_nil(&xfs_uuid_table[i]))
191 continue;
192 if (!uuid_equal(uuid, &xfs_uuid_table[i]))
193 continue;
194 memset(&xfs_uuid_table[i], 0, sizeof(uuid_t));
195 break;
196 }
197 ASSERT(i < xfs_uuid_table_size);
198 mutex_unlock(&xfs_uuid_table_mutex);
199}
200
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/*
Dave Chinner0fa800f2010-01-11 11:47:46 +0000203 * Reference counting access wrappers to the perag structures.
Dave Chinnere1765792010-09-22 10:47:20 +1000204 * Because we never free per-ag structures, the only thing we
205 * have to protect against changes is the tree structure itself.
Dave Chinner0fa800f2010-01-11 11:47:46 +0000206 */
207struct xfs_perag *
208xfs_perag_get(struct xfs_mount *mp, xfs_agnumber_t agno)
209{
210 struct xfs_perag *pag;
211 int ref = 0;
212
Dave Chinnere1765792010-09-22 10:47:20 +1000213 rcu_read_lock();
Dave Chinner0fa800f2010-01-11 11:47:46 +0000214 pag = radix_tree_lookup(&mp->m_perag_tree, agno);
215 if (pag) {
216 ASSERT(atomic_read(&pag->pag_ref) >= 0);
Dave Chinner0fa800f2010-01-11 11:47:46 +0000217 ref = atomic_inc_return(&pag->pag_ref);
218 }
Dave Chinnere1765792010-09-22 10:47:20 +1000219 rcu_read_unlock();
Dave Chinner0fa800f2010-01-11 11:47:46 +0000220 trace_xfs_perag_get(mp, agno, ref, _RET_IP_);
221 return pag;
222}
223
Dave Chinner65d0f202010-09-24 18:40:15 +1000224/*
225 * search from @first to find the next perag with the given tag set.
226 */
227struct xfs_perag *
228xfs_perag_get_tag(
229 struct xfs_mount *mp,
230 xfs_agnumber_t first,
231 int tag)
232{
233 struct xfs_perag *pag;
234 int found;
235 int ref;
236
237 rcu_read_lock();
238 found = radix_tree_gang_lookup_tag(&mp->m_perag_tree,
239 (void **)&pag, first, 1, tag);
240 if (found <= 0) {
241 rcu_read_unlock();
242 return NULL;
243 }
244 ref = atomic_inc_return(&pag->pag_ref);
245 rcu_read_unlock();
246 trace_xfs_perag_get_tag(mp, pag->pag_agno, ref, _RET_IP_);
247 return pag;
248}
249
Dave Chinner0fa800f2010-01-11 11:47:46 +0000250void
251xfs_perag_put(struct xfs_perag *pag)
252{
253 int ref;
254
255 ASSERT(atomic_read(&pag->pag_ref) > 0);
256 ref = atomic_dec_return(&pag->pag_ref);
257 trace_xfs_perag_put(pag->pag_mount, pag->pag_agno, ref, _RET_IP_);
258}
259
Dave Chinnere1765792010-09-22 10:47:20 +1000260STATIC void
261__xfs_free_perag(
262 struct rcu_head *head)
263{
264 struct xfs_perag *pag = container_of(head, struct xfs_perag, rcu_head);
265
266 ASSERT(atomic_read(&pag->pag_ref) == 0);
267 kmem_free(pag);
268}
269
Dave Chinner0fa800f2010-01-11 11:47:46 +0000270/*
Dave Chinnere1765792010-09-22 10:47:20 +1000271 * Free up the per-ag resources associated with the mount structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 */
Christoph Hellwigc962fb72008-05-20 15:10:52 +1000273STATIC void
Christoph Hellwigff4f0382008-08-13 16:50:47 +1000274xfs_free_perag(
Christoph Hellwig745f6912007-08-30 17:20:39 +1000275 xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000277 xfs_agnumber_t agno;
278 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000280 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
281 spin_lock(&mp->m_perag_lock);
282 pag = radix_tree_delete(&mp->m_perag_tree, agno);
283 spin_unlock(&mp->m_perag_lock);
Dave Chinnere1765792010-09-22 10:47:20 +1000284 ASSERT(pag);
Dave Chinnerf83282a2010-11-08 08:55:04 +0000285 ASSERT(atomic_read(&pag->pag_ref) == 0);
Dave Chinnere1765792010-09-22 10:47:20 +1000286 call_rcu(&pag->rcu_head, __xfs_free_perag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
Nathan Scott4cc929e2007-05-14 18:24:02 +1000290/*
291 * Check size of device based on the (data/realtime) block count.
292 * Note: this check is used by the growfs code as well as mount.
293 */
294int
295xfs_sb_validate_fsb_count(
296 xfs_sb_t *sbp,
297 __uint64_t nblocks)
298{
299 ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
300 ASSERT(sbp->sb_blocklog >= BBSHIFT);
301
302#if XFS_BIG_BLKNOS /* Limited by ULONG_MAX of page cache index */
303 if (nblocks >> (PAGE_CACHE_SHIFT - sbp->sb_blocklog) > ULONG_MAX)
Eric Sandeen657a4cf2010-04-30 03:42:49 +0000304 return EFBIG;
Nathan Scott4cc929e2007-05-14 18:24:02 +1000305#else /* Limited by UINT_MAX of sectors */
306 if (nblocks << (sbp->sb_blocklog - BBSHIFT) > UINT_MAX)
Eric Sandeen657a4cf2010-04-30 03:42:49 +0000307 return EFBIG;
Nathan Scott4cc929e2007-05-14 18:24:02 +1000308#endif
309 return 0;
310}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312/*
313 * Check the validity of the SB found.
314 */
315STATIC int
316xfs_mount_validate_sb(
317 xfs_mount_t *mp,
Nathan Scott764d1f82006-03-31 13:04:17 +1000318 xfs_sb_t *sbp,
Dave Chinner34510182013-05-27 16:38:19 +1000319 bool check_inprogress,
320 bool check_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Dave Chinneraf34e092011-03-07 10:04:35 +1100322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 /*
324 * If the log device and data device have the
325 * same device number, the log is internal.
326 * Consequently, the sb_logstart should be non-zero. If
327 * we have a zero sb_logstart in this case, we may be trying to mount
328 * a volume filesystem in a non-volume manner.
329 */
330 if (sbp->sb_magicnum != XFS_SB_MAGIC) {
Dave Chinner98021822012-11-12 22:54:03 +1100331 xfs_warn(mp, "bad magic number");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return XFS_ERROR(EWRONGFS);
333 }
334
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100335
Eric Sandeen62118702008-03-06 13:44:28 +1100336 if (!xfs_sb_good_version(sbp)) {
Dave Chinner98021822012-11-12 22:54:03 +1100337 xfs_warn(mp, "bad version");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return XFS_ERROR(EWRONGFS);
339 }
340
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100341 /*
Dave Chinnere721f502013-04-03 16:11:32 +1100342 * Version 5 superblock feature mask validation. Reject combinations the
Dave Chinner34510182013-05-27 16:38:19 +1000343 * kernel cannot support up front before checking anything else. For
344 * write validation, we don't need to check feature masks.
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100345 */
Dave Chinner34510182013-05-27 16:38:19 +1000346 if (check_version && XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5) {
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100347 xfs_alert(mp,
Dave Chinnere721f502013-04-03 16:11:32 +1100348"Version 5 superblock detected. This kernel has EXPERIMENTAL support enabled!\n"
349"Use of these features in this kernel is at your own risk!");
350
351 if (xfs_sb_has_compat_feature(sbp,
352 XFS_SB_FEAT_COMPAT_UNKNOWN)) {
353 xfs_warn(mp,
354"Superblock has unknown compatible features (0x%x) enabled.\n"
355"Using a more recent kernel is recommended.",
356 (sbp->sb_features_compat &
357 XFS_SB_FEAT_COMPAT_UNKNOWN));
358 }
359
360 if (xfs_sb_has_ro_compat_feature(sbp,
361 XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
362 xfs_alert(mp,
363"Superblock has unknown read-only compatible features (0x%x) enabled.",
364 (sbp->sb_features_ro_compat &
365 XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
366 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
367 xfs_warn(mp,
368"Attempted to mount read-only compatible filesystem read-write.\n"
369"Filesystem can only be safely mounted read only.");
370 return XFS_ERROR(EINVAL);
371 }
372 }
373 if (xfs_sb_has_incompat_feature(sbp,
374 XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
375 xfs_warn(mp,
376"Superblock has unknown incompatible features (0x%x) enabled.\n"
377"Filesystem can not be safely mounted by this kernel.",
378 (sbp->sb_features_incompat &
379 XFS_SB_FEAT_INCOMPAT_UNKNOWN));
380 return XFS_ERROR(EINVAL);
381 }
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100382 }
383
Chandra Seetharamand892d582013-07-19 17:36:02 -0500384 if (xfs_sb_version_has_pquotino(sbp)) {
385 if (sbp->sb_qflags & (XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD)) {
386 xfs_notice(mp,
387 "Version 5 of Super block has XFS_OQUOTA bits.\n");
388 return XFS_ERROR(EFSCORRUPTED);
389 }
390 } else if (sbp->sb_qflags & (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD |
391 XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) {
392 xfs_notice(mp,
393"Superblock earlier than Version 5 has XFS_[PQ]UOTA_{ENFD|CHKD} bits.\n");
394 return XFS_ERROR(EFSCORRUPTED);
395 }
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (unlikely(
398 sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
Dave Chinner98021822012-11-12 22:54:03 +1100399 xfs_warn(mp,
Dave Chinneraf34e092011-03-07 10:04:35 +1100400 "filesystem is marked as having an external log; "
401 "specify logdev on the mount command line.");
Nathan Scott764d1f82006-03-31 13:04:17 +1000402 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404
405 if (unlikely(
406 sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
Dave Chinner98021822012-11-12 22:54:03 +1100407 xfs_warn(mp,
Dave Chinneraf34e092011-03-07 10:04:35 +1100408 "filesystem is marked as having an internal log; "
409 "do not specify logdev on the mount command line.");
Nathan Scott764d1f82006-03-31 13:04:17 +1000410 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412
413 /*
Eric Sandeenc0e090c2011-05-20 21:52:17 +0000414 * More sanity checking. Most of these were stolen directly from
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 * xfs_repair.
416 */
417 if (unlikely(
418 sbp->sb_agcount <= 0 ||
419 sbp->sb_sectsize < XFS_MIN_SECTORSIZE ||
420 sbp->sb_sectsize > XFS_MAX_SECTORSIZE ||
421 sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG ||
422 sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG ||
Olaf Weber2ac00af2009-04-17 16:12:45 -0500423 sbp->sb_sectsize != (1 << sbp->sb_sectlog) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 sbp->sb_blocksize < XFS_MIN_BLOCKSIZE ||
425 sbp->sb_blocksize > XFS_MAX_BLOCKSIZE ||
426 sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG ||
427 sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
Olaf Weber2ac00af2009-04-17 16:12:45 -0500428 sbp->sb_blocksize != (1 << sbp->sb_blocklog) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 sbp->sb_inodesize < XFS_DINODE_MIN_SIZE ||
430 sbp->sb_inodesize > XFS_DINODE_MAX_SIZE ||
Nathan Scott9f989c92006-03-14 13:29:32 +1100431 sbp->sb_inodelog < XFS_DINODE_MIN_LOG ||
432 sbp->sb_inodelog > XFS_DINODE_MAX_LOG ||
Olaf Weber2ac00af2009-04-17 16:12:45 -0500433 sbp->sb_inodesize != (1 << sbp->sb_inodelog) ||
Nathan Scott9f989c92006-03-14 13:29:32 +1100434 (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
436 (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
Eric Sandeenc0e090c2011-05-20 21:52:17 +0000437 (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */) ||
438 sbp->sb_dblocks == 0 ||
439 sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp) ||
440 sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp))) {
Dave Chinner98021822012-11-12 22:54:03 +1100441 XFS_CORRUPTION_ERROR("SB sanity check failed",
Eric Sandeenc0e090c2011-05-20 21:52:17 +0000442 XFS_ERRLEVEL_LOW, mp, sbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return XFS_ERROR(EFSCORRUPTED);
444 }
445
Lachlan McIlroy2edbddd2008-06-27 13:34:34 +1000446 /*
447 * Until this is fixed only page-sized or smaller data blocks work.
448 */
449 if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
Dave Chinner98021822012-11-12 22:54:03 +1100450 xfs_warn(mp,
Dave Chinneraf34e092011-03-07 10:04:35 +1100451 "File system with blocksize %d bytes. "
452 "Only pagesize (%ld) or less will currently work.",
453 sbp->sb_blocksize, PAGE_SIZE);
Lachlan McIlroy2edbddd2008-06-27 13:34:34 +1000454 return XFS_ERROR(ENOSYS);
455 }
456
Christoph Hellwig1a5902c2009-03-29 19:26:46 +0200457 /*
458 * Currently only very few inode sizes are supported.
459 */
460 switch (sbp->sb_inodesize) {
461 case 256:
462 case 512:
463 case 1024:
464 case 2048:
465 break;
466 default:
Dave Chinner98021822012-11-12 22:54:03 +1100467 xfs_warn(mp, "inode size of %d bytes not supported",
Dave Chinneraf34e092011-03-07 10:04:35 +1100468 sbp->sb_inodesize);
Christoph Hellwig1a5902c2009-03-29 19:26:46 +0200469 return XFS_ERROR(ENOSYS);
470 }
471
Nathan Scott4cc929e2007-05-14 18:24:02 +1000472 if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) ||
473 xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) {
Dave Chinner98021822012-11-12 22:54:03 +1100474 xfs_warn(mp,
Dave Chinneraf34e092011-03-07 10:04:35 +1100475 "file system too large to be mounted on this system.");
Eric Sandeen657a4cf2010-04-30 03:42:49 +0000476 return XFS_ERROR(EFBIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478
Dave Chinner98021822012-11-12 22:54:03 +1100479 if (check_inprogress && sbp->sb_inprogress) {
480 xfs_warn(mp, "Offline file system operation in progress!");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return XFS_ERROR(EFSCORRUPTED);
482 }
483
484 /*
Nathan Scottde206142005-05-05 13:24:13 -0700485 * Version 1 directory format has never worked on Linux.
486 */
Eric Sandeen62118702008-03-06 13:44:28 +1100487 if (unlikely(!xfs_sb_version_hasdirv2(sbp))) {
Dave Chinner98021822012-11-12 22:54:03 +1100488 xfs_warn(mp, "file system using version 1 directory format");
Nathan Scottde206142005-05-05 13:24:13 -0700489 return XFS_ERROR(ENOSYS);
490 }
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return 0;
493}
494
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000495int
Nathan Scottc11e2c32005-11-02 15:11:45 +1100496xfs_initialize_perag(
Nathan Scottc11e2c32005-11-02 15:11:45 +1100497 xfs_mount_t *mp,
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000498 xfs_agnumber_t agcount,
499 xfs_agnumber_t *maxagi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Carlos Maiolino2d2194f2012-09-20 10:32:38 -0300501 xfs_agnumber_t index;
Dave Chinner8b26c582010-01-11 11:47:48 +0000502 xfs_agnumber_t first_initialised = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 xfs_perag_t *pag;
504 xfs_agino_t agino;
505 xfs_ino_t ino;
506 xfs_sb_t *sbp = &mp->m_sb;
Dave Chinner8b26c582010-01-11 11:47:48 +0000507 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000509 /*
510 * Walk the current per-ag tree so we don't try to initialise AGs
511 * that already exist (growfs case). Allocate and insert all the
512 * AGs we don't find ready for initialisation.
513 */
514 for (index = 0; index < agcount; index++) {
515 pag = xfs_perag_get(mp, index);
516 if (pag) {
517 xfs_perag_put(pag);
518 continue;
519 }
Dave Chinner8b26c582010-01-11 11:47:48 +0000520 if (!first_initialised)
521 first_initialised = index;
Christoph Hellwigfb3b5042010-05-28 19:03:10 +0000522
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000523 pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL);
524 if (!pag)
Dave Chinner8b26c582010-01-11 11:47:48 +0000525 goto out_unwind;
Christoph Hellwigfb3b5042010-05-28 19:03:10 +0000526 pag->pag_agno = index;
527 pag->pag_mount = mp;
Dave Chinner1a427ab2010-12-16 17:08:41 +1100528 spin_lock_init(&pag->pag_ici_lock);
Dave Chinner69b491c2010-09-27 11:09:51 +1000529 mutex_init(&pag->pag_ici_reclaim_lock);
Christoph Hellwigfb3b5042010-05-28 19:03:10 +0000530 INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC);
Dave Chinner74f75a02010-09-24 19:59:04 +1000531 spin_lock_init(&pag->pag_buf_lock);
532 pag->pag_buf_tree = RB_ROOT;
Christoph Hellwigfb3b5042010-05-28 19:03:10 +0000533
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000534 if (radix_tree_preload(GFP_NOFS))
Dave Chinner8b26c582010-01-11 11:47:48 +0000535 goto out_unwind;
Christoph Hellwigfb3b5042010-05-28 19:03:10 +0000536
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000537 spin_lock(&mp->m_perag_lock);
538 if (radix_tree_insert(&mp->m_perag_tree, index, pag)) {
539 BUG();
540 spin_unlock(&mp->m_perag_lock);
Dave Chinner8b26c582010-01-11 11:47:48 +0000541 radix_tree_preload_end();
542 error = -EEXIST;
543 goto out_unwind;
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000544 }
545 spin_unlock(&mp->m_perag_lock);
546 radix_tree_preload_end();
547 }
548
Christoph Hellwigfb3b5042010-05-28 19:03:10 +0000549 /*
550 * If we mount with the inode64 option, or no inode overflows
551 * the legacy 32-bit address space clear the inode32 option.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 */
Christoph Hellwigfb3b5042010-05-28 19:03:10 +0000553 agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
554 ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Christoph Hellwigfb3b5042010-05-28 19:03:10 +0000556 if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) && ino > XFS_MAXINUMBER_32)
557 mp->m_flags |= XFS_MOUNT_32BITINODES;
558 else
559 mp->m_flags &= ~XFS_MOUNT_32BITINODES;
560
Carlos Maiolino2d2194f2012-09-20 10:32:38 -0300561 if (mp->m_flags & XFS_MOUNT_32BITINODES)
562 index = xfs_set_inode32(mp);
563 else
564 index = xfs_set_inode64(mp);
Christoph Hellwigfb3b5042010-05-28 19:03:10 +0000565
Dave Chinner1c1c6eb2010-01-11 11:47:44 +0000566 if (maxagi)
567 *maxagi = index;
568 return 0;
Dave Chinner8b26c582010-01-11 11:47:48 +0000569
570out_unwind:
571 kmem_free(pag);
572 for (; index > first_initialised; index--) {
573 pag = radix_tree_delete(&mp->m_perag_tree, index);
574 kmem_free(pag);
575 }
576 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500579static void
580xfs_sb_quota_from_disk(struct xfs_sb *sbp)
581{
Chandra Seetharaman01026292013-07-19 17:32:55 -0500582 /*
583 * older mkfs doesn't initialize quota inodes to NULLFSINO. This
584 * leads to in-core values having two different values for a quota
585 * inode to be invalid: 0 and NULLFSINO. Change it to a single value
586 * NULLFSINO.
587 *
588 * Note that this change affect only the in-core values. These
589 * values are not written back to disk unless any quota information
590 * is written to the disk. Even in that case, sb_pquotino field is
591 * not written to disk unless the superblock supports pquotino.
592 */
593 if (sbp->sb_uquotino == 0)
594 sbp->sb_uquotino = NULLFSINO;
595 if (sbp->sb_gquotino == 0)
596 sbp->sb_gquotino = NULLFSINO;
597 if (sbp->sb_pquotino == 0)
598 sbp->sb_pquotino = NULLFSINO;
599
Chandra Seetharamand892d582013-07-19 17:36:02 -0500600 /*
601 * We need to do these manipilations only if we are working
602 * with an older version of on-disk superblock.
603 */
604 if (xfs_sb_version_has_pquotino(sbp))
605 return;
606
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500607 if (sbp->sb_qflags & XFS_OQUOTA_ENFD)
608 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
609 XFS_PQUOTA_ENFD : XFS_GQUOTA_ENFD;
610 if (sbp->sb_qflags & XFS_OQUOTA_CHKD)
611 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
612 XFS_PQUOTA_CHKD : XFS_GQUOTA_CHKD;
613 sbp->sb_qflags &= ~(XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD);
Chandra Seetharamand892d582013-07-19 17:36:02 -0500614
615 if (sbp->sb_qflags & XFS_PQUOTA_ACCT) {
616 /*
617 * In older version of superblock, on-disk superblock only
618 * has sb_gquotino, and in-core superblock has both sb_gquotino
619 * and sb_pquotino. But, only one of them is supported at any
620 * point of time. So, if PQUOTA is set in disk superblock,
621 * copy over sb_gquotino to sb_pquotino.
622 */
623 sbp->sb_pquotino = sbp->sb_gquotino;
624 sbp->sb_gquotino = NULLFSINO;
625 }
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500626}
627
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000628void
629xfs_sb_from_disk(
Dave Chinner98021822012-11-12 22:54:03 +1100630 struct xfs_sb *to,
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000631 xfs_dsb_t *from)
632{
633 to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
634 to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
635 to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
636 to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
637 to->sb_rextents = be64_to_cpu(from->sb_rextents);
638 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
639 to->sb_logstart = be64_to_cpu(from->sb_logstart);
640 to->sb_rootino = be64_to_cpu(from->sb_rootino);
641 to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
642 to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
643 to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
644 to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
645 to->sb_agcount = be32_to_cpu(from->sb_agcount);
646 to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
647 to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
648 to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
649 to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
650 to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
651 to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
652 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
653 to->sb_blocklog = from->sb_blocklog;
654 to->sb_sectlog = from->sb_sectlog;
655 to->sb_inodelog = from->sb_inodelog;
656 to->sb_inopblog = from->sb_inopblog;
657 to->sb_agblklog = from->sb_agblklog;
658 to->sb_rextslog = from->sb_rextslog;
659 to->sb_inprogress = from->sb_inprogress;
660 to->sb_imax_pct = from->sb_imax_pct;
661 to->sb_icount = be64_to_cpu(from->sb_icount);
662 to->sb_ifree = be64_to_cpu(from->sb_ifree);
663 to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
664 to->sb_frextents = be64_to_cpu(from->sb_frextents);
665 to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
666 to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
667 to->sb_qflags = be16_to_cpu(from->sb_qflags);
668 to->sb_flags = from->sb_flags;
669 to->sb_shared_vn = from->sb_shared_vn;
670 to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
671 to->sb_unit = be32_to_cpu(from->sb_unit);
672 to->sb_width = be32_to_cpu(from->sb_width);
673 to->sb_dirblklog = from->sb_dirblklog;
674 to->sb_logsectlog = from->sb_logsectlog;
675 to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
676 to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
677 to->sb_features2 = be32_to_cpu(from->sb_features2);
David Chinneree1c0902008-03-06 13:45:50 +1100678 to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100679 to->sb_features_compat = be32_to_cpu(from->sb_features_compat);
680 to->sb_features_ro_compat = be32_to_cpu(from->sb_features_ro_compat);
681 to->sb_features_incompat = be32_to_cpu(from->sb_features_incompat);
Dave Chinnere721f502013-04-03 16:11:32 +1100682 to->sb_features_log_incompat =
683 be32_to_cpu(from->sb_features_log_incompat);
684 to->sb_pad = 0;
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100685 to->sb_pquotino = be64_to_cpu(from->sb_pquotino);
686 to->sb_lsn = be64_to_cpu(from->sb_lsn);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000687}
688
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500689static inline void
690xfs_sb_quota_to_disk(
691 xfs_dsb_t *to,
692 xfs_sb_t *from,
693 __int64_t *fields)
694{
695 __uint16_t qflags = from->sb_qflags;
696
Chandra Seetharamand892d582013-07-19 17:36:02 -0500697 /*
698 * We need to do these manipilations only if we are working
699 * with an older version of on-disk superblock.
700 */
701 if (xfs_sb_version_has_pquotino(from))
702 return;
703
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500704 if (*fields & XFS_SB_QFLAGS) {
705 /*
706 * The in-core version of sb_qflags do not have
707 * XFS_OQUOTA_* flags, whereas the on-disk version
708 * does. So, convert incore XFS_{PG}QUOTA_* flags
709 * to on-disk XFS_OQUOTA_* flags.
710 */
711 qflags &= ~(XFS_PQUOTA_ENFD | XFS_PQUOTA_CHKD |
712 XFS_GQUOTA_ENFD | XFS_GQUOTA_CHKD);
713
714 if (from->sb_qflags &
715 (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD))
716 qflags |= XFS_OQUOTA_ENFD;
717 if (from->sb_qflags &
718 (XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD))
719 qflags |= XFS_OQUOTA_CHKD;
720 to->sb_qflags = cpu_to_be16(qflags);
721 *fields &= ~XFS_SB_QFLAGS;
722 }
Chandra Seetharamand892d582013-07-19 17:36:02 -0500723
724 /*
725 * GQUOTINO and PQUOTINO cannot be used together in versions
726 * of superblock that do not have pquotino. from->sb_flags
727 * tells us which quota is active and should be copied to
728 * disk.
729 */
730 if ((*fields & XFS_SB_GQUOTINO) &&
731 (from->sb_qflags & XFS_GQUOTA_ACCT))
732 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
733 else if ((*fields & XFS_SB_PQUOTINO) &&
734 (from->sb_qflags & XFS_PQUOTA_ACCT))
735 to->sb_gquotino = cpu_to_be64(from->sb_pquotino);
736
737 *fields &= ~(XFS_SB_PQUOTINO | XFS_SB_GQUOTINO);
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500738}
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740/*
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000741 * Copy in core superblock to ondisk one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 *
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000743 * The fields argument is mask of superblock fields to copy.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 */
745void
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000746xfs_sb_to_disk(
747 xfs_dsb_t *to,
748 xfs_sb_t *from,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 __int64_t fields)
750{
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000751 xfs_caddr_t to_ptr = (xfs_caddr_t)to;
752 xfs_caddr_t from_ptr = (xfs_caddr_t)from;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 xfs_sb_field_t f;
754 int first;
755 int size;
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 ASSERT(fields);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (!fields)
759 return;
760
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500761 xfs_sb_quota_to_disk(to, from, &fields);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 while (fields) {
763 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
764 first = xfs_sb_info[f].offset;
765 size = xfs_sb_info[f + 1].offset - first;
766
767 ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1);
768
769 if (size == 1 || xfs_sb_info[f].type == 1) {
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000770 memcpy(to_ptr + first, from_ptr + first, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 } else {
772 switch (size) {
773 case 2:
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000774 *(__be16 *)(to_ptr + first) =
775 cpu_to_be16(*(__u16 *)(from_ptr + first));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 break;
777 case 4:
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000778 *(__be32 *)(to_ptr + first) =
779 cpu_to_be32(*(__u32 *)(from_ptr + first));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 break;
781 case 8:
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000782 *(__be64 *)(to_ptr + first) =
783 cpu_to_be64(*(__u64 *)(from_ptr + first));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 break;
785 default:
786 ASSERT(0);
787 }
788 }
789
790 fields &= ~(1LL << f);
791 }
792}
793
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100794static int
Dave Chinner612cfbf2012-11-14 17:52:32 +1100795xfs_sb_verify(
Dave Chinner34510182013-05-27 16:38:19 +1000796 struct xfs_buf *bp,
797 bool check_version)
Dave Chinner98021822012-11-12 22:54:03 +1100798{
799 struct xfs_mount *mp = bp->b_target->bt_mount;
800 struct xfs_sb sb;
Dave Chinner98021822012-11-12 22:54:03 +1100801
802 xfs_sb_from_disk(&sb, XFS_BUF_TO_SBP(bp));
803
804 /*
805 * Only check the in progress field for the primary superblock as
806 * mkfs.xfs doesn't clear it from secondary superblocks.
807 */
Dave Chinner34510182013-05-27 16:38:19 +1000808 return xfs_mount_validate_sb(mp, &sb, bp->b_bn == XFS_SB_DADDR,
809 check_version);
Dave Chinner612cfbf2012-11-14 17:52:32 +1100810}
811
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100812/*
813 * If the superblock has the CRC feature bit set or the CRC field is non-null,
814 * check that the CRC is valid. We check the CRC field is non-null because a
815 * single bit error could clear the feature bit and unused parts of the
816 * superblock are supposed to be zero. Hence a non-null crc field indicates that
817 * we've potentially lost a feature bit and we should check it anyway.
818 */
Dave Chinner1813dd62012-11-14 17:54:40 +1100819static void
Dave Chinner612cfbf2012-11-14 17:52:32 +1100820xfs_sb_read_verify(
821 struct xfs_buf *bp)
822{
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100823 struct xfs_mount *mp = bp->b_target->bt_mount;
824 struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp);
825 int error;
826
827 /*
828 * open code the version check to avoid needing to convert the entire
829 * superblock from disk order just to check the version number
830 */
831 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC) &&
832 (((be16_to_cpu(dsb->sb_versionnum) & XFS_SB_VERSION_NUMBITS) ==
833 XFS_SB_VERSION_5) ||
834 dsb->sb_crc != 0)) {
835
836 if (!xfs_verify_cksum(bp->b_addr, be16_to_cpu(dsb->sb_sectsize),
837 offsetof(struct xfs_sb, sb_crc))) {
838 error = EFSCORRUPTED;
839 goto out_error;
840 }
841 }
Dave Chinner34510182013-05-27 16:38:19 +1000842 error = xfs_sb_verify(bp, true);
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100843
844out_error:
845 if (error) {
846 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
847 xfs_buf_ioerror(bp, error);
848 }
Dave Chinner98021822012-11-12 22:54:03 +1100849}
850
851/*
852 * We may be probed for a filesystem match, so we may not want to emit
853 * messages when the superblock buffer is not actually an XFS superblock.
854 * If we find an XFS superblock, the run a normal, noisy mount because we are
855 * really going to mount it and want to know about errors.
856 */
Dave Chinner1813dd62012-11-14 17:54:40 +1100857static void
Dave Chinner98021822012-11-12 22:54:03 +1100858xfs_sb_quiet_read_verify(
859 struct xfs_buf *bp)
860{
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100861 struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp);
Dave Chinner98021822012-11-12 22:54:03 +1100862
Dave Chinner98021822012-11-12 22:54:03 +1100863
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100864 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) {
Dave Chinner98021822012-11-12 22:54:03 +1100865 /* XFS filesystem, verify noisily! */
866 xfs_sb_read_verify(bp);
867 return;
868 }
869 /* quietly fail */
Eric Sandeenaeb4f202013-01-16 17:33:53 -0600870 xfs_buf_ioerror(bp, EWRONGFS);
Dave Chinner98021822012-11-12 22:54:03 +1100871}
872
Dave Chinner1813dd62012-11-14 17:54:40 +1100873static void
874xfs_sb_write_verify(
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100875 struct xfs_buf *bp)
Dave Chinner1813dd62012-11-14 17:54:40 +1100876{
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100877 struct xfs_mount *mp = bp->b_target->bt_mount;
878 struct xfs_buf_log_item *bip = bp->b_fspriv;
879 int error;
880
Dave Chinner34510182013-05-27 16:38:19 +1000881 error = xfs_sb_verify(bp, false);
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100882 if (error) {
883 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
884 xfs_buf_ioerror(bp, error);
885 return;
886 }
887
888 if (!xfs_sb_version_hascrc(&mp->m_sb))
889 return;
890
891 if (bip)
892 XFS_BUF_TO_SBP(bp)->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
893
894 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
895 offsetof(struct xfs_sb, sb_crc));
Dave Chinner1813dd62012-11-14 17:54:40 +1100896}
897
898const struct xfs_buf_ops xfs_sb_buf_ops = {
899 .verify_read = xfs_sb_read_verify,
900 .verify_write = xfs_sb_write_verify,
901};
902
903static const struct xfs_buf_ops xfs_sb_quiet_buf_ops = {
904 .verify_read = xfs_sb_quiet_read_verify,
905 .verify_write = xfs_sb_write_verify,
906};
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908/*
909 * xfs_readsb
910 *
911 * Does the initial read of the superblock.
912 */
913int
Nathan Scott764d1f82006-03-31 13:04:17 +1000914xfs_readsb(xfs_mount_t *mp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
916 unsigned int sector_size;
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100917 struct xfs_buf *bp;
918 struct xfs_sb *sbp = &mp->m_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 int error;
Dave Chinneraf34e092011-03-07 10:04:35 +1100920 int loud = !(flags & XFS_MFSI_QUIET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 ASSERT(mp->m_sb_bp == NULL);
923 ASSERT(mp->m_ddev_targp != NULL);
924
925 /*
926 * Allocate a (locked) buffer to hold the superblock.
927 * This will be kept around at all times to optimize
928 * access to the superblock.
929 */
930 sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Dave Chinner26af6552010-09-22 10:47:20 +1000932reread:
Dave Chinnere70b73f2012-04-23 15:58:49 +1000933 bp = xfs_buf_read_uncached(mp->m_ddev_targp, XFS_SB_DADDR,
Dave Chinner98021822012-11-12 22:54:03 +1100934 BTOBB(sector_size), 0,
Dave Chinner1813dd62012-11-14 17:54:40 +1100935 loud ? &xfs_sb_buf_ops
936 : &xfs_sb_quiet_buf_ops);
Dave Chinner26af6552010-09-22 10:47:20 +1000937 if (!bp) {
Dave Chinneraf34e092011-03-07 10:04:35 +1100938 if (loud)
939 xfs_warn(mp, "SB buffer read failed");
Dave Chinner26af6552010-09-22 10:47:20 +1000940 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
Dave Chinnereab4e632012-11-12 22:54:02 +1100942 if (bp->b_error) {
943 error = bp->b_error;
944 if (loud)
Dave Chinnere721f502013-04-03 16:11:32 +1100945 xfs_warn(mp, "SB validate failed with error %d.", error);
Dave Chinnereab4e632012-11-12 22:54:02 +1100946 goto release_buf;
947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 /*
950 * Initialize the mount structure from the superblock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 */
Dave Chinner98021822012-11-12 22:54:03 +1100952 xfs_sb_from_disk(&mp->m_sb, XFS_BUF_TO_SBP(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500954 xfs_sb_quota_from_disk(&mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 /*
956 * We must be able to do sector-sized and sector-aligned IO.
957 */
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100958 if (sector_size > sbp->sb_sectsize) {
Dave Chinneraf34e092011-03-07 10:04:35 +1100959 if (loud)
960 xfs_warn(mp, "device supports %u byte sectors (not %u)",
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100961 sector_size, sbp->sb_sectsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 error = ENOSYS;
Dave Chinner26af6552010-09-22 10:47:20 +1000963 goto release_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
965
966 /*
967 * If device sector size is smaller than the superblock size,
968 * re-read the superblock so the buffer is correctly sized.
969 */
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100970 if (sector_size < sbp->sb_sectsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 xfs_buf_relse(bp);
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100972 sector_size = sbp->sb_sectsize;
Dave Chinner26af6552010-09-22 10:47:20 +1000973 goto reread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
975
Lachlan McIlroy5478eea2007-02-10 18:36:29 +1100976 /* Initialize per-cpu counters */
977 xfs_icsb_reinit_counters(mp);
David Chinner8d280b92006-03-14 13:13:09 +1100978
Dave Chinner04a1e6c2013-04-03 16:11:31 +1100979 /* no need to be quiet anymore, so reset the buf ops */
980 bp->b_ops = &xfs_sb_buf_ops;
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 mp->m_sb_bp = bp;
Dave Chinner26af6552010-09-22 10:47:20 +1000983 xfs_buf_unlock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return 0;
985
Dave Chinner26af6552010-09-22 10:47:20 +1000986release_buf:
987 xfs_buf_relse(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return error;
989}
990
991
992/*
993 * xfs_mount_common
994 *
995 * Mount initialization code establishing various mount
996 * fields from the superblock associated with the given
997 * mount structure
998 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000999STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000xfs_mount_common(xfs_mount_t *mp, xfs_sb_t *sbp)
1001{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 mp->m_agfrotor = mp->m_agirotor = 0;
Eric Sandeen007c61c2007-10-11 17:43:56 +10001003 spin_lock_init(&mp->m_agirotor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 mp->m_maxagi = mp->m_sb.sb_agcount;
1005 mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
1006 mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
1007 mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
1008 mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
1009 mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 mp->m_blockmask = sbp->sb_blocksize - 1;
1011 mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
1012 mp->m_blockwmask = mp->m_blockwsize - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Christoph Hellwig60197e82008-10-30 17:11:19 +11001014 mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
1015 mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
1016 mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
1017 mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
1018
1019 mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
1020 mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
1021 mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2;
1022 mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2;
1023
1024 mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
1025 mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
1026 mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
1027 mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
1030 mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
1031 sbp->sb_inopblock);
1032 mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
1033}
David Chinner92821e22007-05-24 15:26:31 +10001034
1035/*
1036 * xfs_initialize_perag_data
1037 *
1038 * Read in each per-ag structure so we can count up the number of
1039 * allocated inodes, free inodes and used filesystem blocks as this
1040 * information is no longer persistent in the superblock. Once we have
1041 * this information, write it into the in-core superblock structure.
1042 */
1043STATIC int
1044xfs_initialize_perag_data(xfs_mount_t *mp, xfs_agnumber_t agcount)
1045{
1046 xfs_agnumber_t index;
1047 xfs_perag_t *pag;
1048 xfs_sb_t *sbp = &mp->m_sb;
1049 uint64_t ifree = 0;
1050 uint64_t ialloc = 0;
1051 uint64_t bfree = 0;
1052 uint64_t bfreelst = 0;
1053 uint64_t btree = 0;
1054 int error;
David Chinner92821e22007-05-24 15:26:31 +10001055
1056 for (index = 0; index < agcount; index++) {
1057 /*
1058 * read the agf, then the agi. This gets us
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001059 * all the information we need and populates the
David Chinner92821e22007-05-24 15:26:31 +10001060 * per-ag structures for us.
1061 */
1062 error = xfs_alloc_pagf_init(mp, NULL, index, 0);
1063 if (error)
1064 return error;
1065
1066 error = xfs_ialloc_pagi_init(mp, NULL, index);
1067 if (error)
1068 return error;
Dave Chinner44b56e02010-01-11 11:47:43 +00001069 pag = xfs_perag_get(mp, index);
David Chinner92821e22007-05-24 15:26:31 +10001070 ifree += pag->pagi_freecount;
1071 ialloc += pag->pagi_count;
1072 bfree += pag->pagf_freeblks;
1073 bfreelst += pag->pagf_flcount;
1074 btree += pag->pagf_btreeblks;
Dave Chinner44b56e02010-01-11 11:47:43 +00001075 xfs_perag_put(pag);
David Chinner92821e22007-05-24 15:26:31 +10001076 }
1077 /*
1078 * Overwrite incore superblock counters with just-read data
1079 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001080 spin_lock(&mp->m_sb_lock);
David Chinner92821e22007-05-24 15:26:31 +10001081 sbp->sb_ifree = ifree;
1082 sbp->sb_icount = ialloc;
1083 sbp->sb_fdblocks = bfree + bfreelst + btree;
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001084 spin_unlock(&mp->m_sb_lock);
David Chinner92821e22007-05-24 15:26:31 +10001085
1086 /* Fixup the per-cpu counters as well. */
1087 xfs_icsb_reinit_counters(mp);
1088
1089 return 0;
1090}
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092/*
Eric Sandeen0771fb42007-10-12 11:03:40 +10001093 * Update alignment values based on mount options and sb values
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 */
Eric Sandeen0771fb42007-10-12 11:03:40 +10001095STATIC int
Christoph Hellwig7884bc82009-01-19 02:04:07 +01001096xfs_update_alignment(xfs_mount_t *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 xfs_sb_t *sbp = &(mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Christoph Hellwig42490232008-08-13 16:49:32 +10001100 if (mp->m_dalign) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 /*
1102 * If stripe unit and stripe width are not multiples
1103 * of the fs blocksize turn off alignment.
1104 */
1105 if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
1106 (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
Jie Liu39a45d82013-05-02 19:27:47 +08001107 xfs_warn(mp,
1108 "alignment check failed: sunit/swidth vs. blocksize(%d)",
1109 sbp->sb_blocksize);
1110 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 } else {
1112 /*
1113 * Convert the stripe unit and width to FSBs.
1114 */
1115 mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
1116 if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
Dave Chinner53487782011-03-07 10:05:35 +11001117 xfs_warn(mp,
Jie Liu39a45d82013-05-02 19:27:47 +08001118 "alignment check failed: sunit/swidth vs. agsize(%d)",
1119 sbp->sb_agblocks);
1120 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 } else if (mp->m_dalign) {
1122 mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
1123 } else {
Jie Liu39a45d82013-05-02 19:27:47 +08001124 xfs_warn(mp,
1125 "alignment check failed: sunit(%d) less than bsize(%d)",
1126 mp->m_dalign, sbp->sb_blocksize);
1127 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
1129 }
1130
1131 /*
1132 * Update superblock with new values
1133 * and log changes
1134 */
Eric Sandeen62118702008-03-06 13:44:28 +11001135 if (xfs_sb_version_hasdalign(sbp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 if (sbp->sb_unit != mp->m_dalign) {
1137 sbp->sb_unit = mp->m_dalign;
Christoph Hellwig7884bc82009-01-19 02:04:07 +01001138 mp->m_update_flags |= XFS_SB_UNIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
1140 if (sbp->sb_width != mp->m_swidth) {
1141 sbp->sb_width = mp->m_swidth;
Christoph Hellwig7884bc82009-01-19 02:04:07 +01001142 mp->m_update_flags |= XFS_SB_WIDTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
Jie Liu34d7f602013-05-02 19:27:53 +08001144 } else {
1145 xfs_warn(mp,
1146 "cannot change alignment: superblock does not support data alignment");
1147 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
1149 } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
Eric Sandeen62118702008-03-06 13:44:28 +11001150 xfs_sb_version_hasdalign(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 mp->m_dalign = sbp->sb_unit;
1152 mp->m_swidth = sbp->sb_width;
1153 }
1154
Eric Sandeen0771fb42007-10-12 11:03:40 +10001155 return 0;
1156}
1157
1158/*
1159 * Set the maximum inode count for this filesystem
1160 */
1161STATIC void
1162xfs_set_maxicount(xfs_mount_t *mp)
1163{
1164 xfs_sb_t *sbp = &(mp->m_sb);
1165 __uint64_t icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 if (sbp->sb_imax_pct) {
Eric Sandeen0771fb42007-10-12 11:03:40 +10001168 /*
1169 * Make sure the maximum inode count is a multiple
1170 * of the units we allocate inodes in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
1173 do_div(icount, 100);
1174 do_div(icount, mp->m_ialloc_blks);
1175 mp->m_maxicount = (icount * mp->m_ialloc_blks) <<
1176 sbp->sb_inopblog;
Eric Sandeen0771fb42007-10-12 11:03:40 +10001177 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 mp->m_maxicount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
Eric Sandeen0771fb42007-10-12 11:03:40 +10001180}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Eric Sandeen0771fb42007-10-12 11:03:40 +10001182/*
1183 * Set the default minimum read and write sizes unless
1184 * already specified in a mount option.
1185 * We use smaller I/O sizes when the file system
1186 * is being used for NFS service (wsync mount option).
1187 */
1188STATIC void
1189xfs_set_rw_sizes(xfs_mount_t *mp)
1190{
1191 xfs_sb_t *sbp = &(mp->m_sb);
1192 int readio_log, writeio_log;
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
1195 if (mp->m_flags & XFS_MOUNT_WSYNC) {
1196 readio_log = XFS_WSYNC_READIO_LOG;
1197 writeio_log = XFS_WSYNC_WRITEIO_LOG;
1198 } else {
1199 readio_log = XFS_READIO_LOG_LARGE;
1200 writeio_log = XFS_WRITEIO_LOG_LARGE;
1201 }
1202 } else {
1203 readio_log = mp->m_readio_log;
1204 writeio_log = mp->m_writeio_log;
1205 }
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (sbp->sb_blocklog > readio_log) {
1208 mp->m_readio_log = sbp->sb_blocklog;
1209 } else {
1210 mp->m_readio_log = readio_log;
1211 }
1212 mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
1213 if (sbp->sb_blocklog > writeio_log) {
1214 mp->m_writeio_log = sbp->sb_blocklog;
1215 } else {
1216 mp->m_writeio_log = writeio_log;
1217 }
1218 mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
Eric Sandeen0771fb42007-10-12 11:03:40 +10001219}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Eric Sandeen0771fb42007-10-12 11:03:40 +10001221/*
Dave Chinner055388a2011-01-04 11:35:03 +11001222 * precalculate the low space thresholds for dynamic speculative preallocation.
1223 */
1224void
1225xfs_set_low_space_thresholds(
1226 struct xfs_mount *mp)
1227{
1228 int i;
1229
1230 for (i = 0; i < XFS_LOWSP_MAX; i++) {
1231 __uint64_t space = mp->m_sb.sb_dblocks;
1232
1233 do_div(space, 100);
1234 mp->m_low_space[i] = space * (i + 1);
1235 }
1236}
1237
1238
1239/*
Eric Sandeen0771fb42007-10-12 11:03:40 +10001240 * Set whether we're using inode alignment.
1241 */
1242STATIC void
1243xfs_set_inoalignment(xfs_mount_t *mp)
1244{
Eric Sandeen62118702008-03-06 13:44:28 +11001245 if (xfs_sb_version_hasalign(&mp->m_sb) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 mp->m_sb.sb_inoalignmt >=
1247 XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
1248 mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
1249 else
1250 mp->m_inoalign_mask = 0;
1251 /*
1252 * If we are using stripe alignment, check whether
1253 * the stripe unit is a multiple of the inode alignment
1254 */
1255 if (mp->m_dalign && mp->m_inoalign_mask &&
1256 !(mp->m_dalign & mp->m_inoalign_mask))
1257 mp->m_sinoalign = mp->m_dalign;
1258 else
1259 mp->m_sinoalign = 0;
Eric Sandeen0771fb42007-10-12 11:03:40 +10001260}
1261
1262/*
1263 * Check that the data (and log if separate) are an ok size.
1264 */
1265STATIC int
Christoph Hellwig42490232008-08-13 16:49:32 +10001266xfs_check_sizes(xfs_mount_t *mp)
Eric Sandeen0771fb42007-10-12 11:03:40 +10001267{
1268 xfs_buf_t *bp;
1269 xfs_daddr_t d;
Eric Sandeen0771fb42007-10-12 11:03:40 +10001270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
1272 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001273 xfs_warn(mp, "filesystem size mismatch detected");
Eric Sandeen657a4cf2010-04-30 03:42:49 +00001274 return XFS_ERROR(EFBIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
Dave Chinnere70b73f2012-04-23 15:58:49 +10001276 bp = xfs_buf_read_uncached(mp->m_ddev_targp,
Dave Chinner1922c942010-09-22 10:47:20 +10001277 d - XFS_FSS_TO_BB(mp, 1),
Dave Chinnerc3f8fc72012-11-12 22:54:01 +11001278 XFS_FSS_TO_BB(mp, 1), 0, NULL);
Dave Chinner1922c942010-09-22 10:47:20 +10001279 if (!bp) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001280 xfs_warn(mp, "last sector read failed");
Dave Chinner1922c942010-09-22 10:47:20 +10001281 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
Dave Chinner1922c942010-09-22 10:47:20 +10001283 xfs_buf_relse(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Christoph Hellwig42490232008-08-13 16:49:32 +10001285 if (mp->m_logdev_targp != mp->m_ddev_targp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
1287 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001288 xfs_warn(mp, "log size mismatch detected");
Eric Sandeen657a4cf2010-04-30 03:42:49 +00001289 return XFS_ERROR(EFBIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 }
Dave Chinnere70b73f2012-04-23 15:58:49 +10001291 bp = xfs_buf_read_uncached(mp->m_logdev_targp,
Dave Chinner1922c942010-09-22 10:47:20 +10001292 d - XFS_FSB_TO_BB(mp, 1),
Dave Chinnerc3f8fc72012-11-12 22:54:01 +11001293 XFS_FSB_TO_BB(mp, 1), 0, NULL);
Dave Chinner1922c942010-09-22 10:47:20 +10001294 if (!bp) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001295 xfs_warn(mp, "log device read failed");
Dave Chinner1922c942010-09-22 10:47:20 +10001296 return EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
Dave Chinner1922c942010-09-22 10:47:20 +10001298 xfs_buf_relse(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 }
Eric Sandeen0771fb42007-10-12 11:03:40 +10001300 return 0;
1301}
1302
1303/*
Christoph Hellwig7d095252009-06-08 15:33:32 +02001304 * Clear the quotaflags in memory and in the superblock.
1305 */
1306int
1307xfs_mount_reset_sbqflags(
1308 struct xfs_mount *mp)
1309{
1310 int error;
1311 struct xfs_trans *tp;
1312
1313 mp->m_qflags = 0;
1314
1315 /*
1316 * It is OK to look at sb_qflags here in mount path,
1317 * without m_sb_lock.
1318 */
1319 if (mp->m_sb.sb_qflags == 0)
1320 return 0;
1321 spin_lock(&mp->m_sb_lock);
1322 mp->m_sb.sb_qflags = 0;
1323 spin_unlock(&mp->m_sb_lock);
1324
1325 /*
1326 * If the fs is readonly, let the incore superblock run
1327 * with quotas off but don't flush the update out to disk
1328 */
1329 if (mp->m_flags & XFS_MOUNT_RDONLY)
1330 return 0;
1331
Christoph Hellwig7d095252009-06-08 15:33:32 +02001332 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SBCHANGE);
Jeff Liub0c10b982013-01-28 21:26:16 +08001333 error = xfs_trans_reserve(tp, 0, XFS_QM_SBCHANGE_LOG_RES(mp),
1334 0, 0, XFS_DEFAULT_LOG_COUNT);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001335 if (error) {
1336 xfs_trans_cancel(tp, 0);
Dave Chinner53487782011-03-07 10:05:35 +11001337 xfs_alert(mp, "%s: Superblock update failed!", __func__);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001338 return error;
1339 }
1340
1341 xfs_mod_sb(tp, XFS_SB_QFLAGS);
1342 return xfs_trans_commit(tp, 0);
1343}
1344
Eric Sandeend5db0f92010-02-05 22:59:53 +00001345__uint64_t
1346xfs_default_resblks(xfs_mount_t *mp)
1347{
1348 __uint64_t resblks;
1349
1350 /*
Dave Chinner8babd8a2010-03-04 01:46:25 +00001351 * We default to 5% or 8192 fsbs of space reserved, whichever is
1352 * smaller. This is intended to cover concurrent allocation
1353 * transactions when we initially hit enospc. These each require a 4
1354 * block reservation. Hence by default we cover roughly 2000 concurrent
1355 * allocation reservations.
Eric Sandeend5db0f92010-02-05 22:59:53 +00001356 */
1357 resblks = mp->m_sb.sb_dblocks;
1358 do_div(resblks, 20);
Dave Chinner8babd8a2010-03-04 01:46:25 +00001359 resblks = min_t(__uint64_t, resblks, 8192);
Eric Sandeend5db0f92010-02-05 22:59:53 +00001360 return resblks;
1361}
1362
Christoph Hellwig7d095252009-06-08 15:33:32 +02001363/*
Eric Sandeen0771fb42007-10-12 11:03:40 +10001364 * This function does the following on an initial mount of a file system:
1365 * - reads the superblock from disk and init the mount struct
1366 * - if we're a 32-bit kernel, do a size check on the superblock
1367 * so we don't mount terabyte filesystems
1368 * - init mount struct realtime fields
1369 * - allocate inode hash table for fs
1370 * - init directory manager
1371 * - perform recovery and init the log manager
1372 */
1373int
1374xfs_mountfs(
Christoph Hellwig42490232008-08-13 16:49:32 +10001375 xfs_mount_t *mp)
Eric Sandeen0771fb42007-10-12 11:03:40 +10001376{
1377 xfs_sb_t *sbp = &(mp->m_sb);
1378 xfs_inode_t *rip;
Eric Sandeen0771fb42007-10-12 11:03:40 +10001379 __uint64_t resblks;
Christoph Hellwig7d095252009-06-08 15:33:32 +02001380 uint quotamount = 0;
1381 uint quotaflags = 0;
Eric Sandeen0771fb42007-10-12 11:03:40 +10001382 int error = 0;
1383
Eric Sandeen0771fb42007-10-12 11:03:40 +10001384 xfs_mount_common(mp, sbp);
1385
1386 /*
Eric Sandeene6957ea2008-04-10 12:19:34 +10001387 * Check for a mismatched features2 values. Older kernels
1388 * read & wrote into the wrong sb offset for sb_features2
1389 * on some platforms due to xfs_sb_t not being 64bit size aligned
1390 * when sb_features2 was added, which made older superblock
1391 * reading/writing routines swap it as a 64-bit value.
David Chinneree1c0902008-03-06 13:45:50 +11001392 *
Eric Sandeene6957ea2008-04-10 12:19:34 +10001393 * For backwards compatibility, we make both slots equal.
1394 *
1395 * If we detect a mismatched field, we OR the set bits into the
1396 * existing features2 field in case it has already been modified; we
1397 * don't want to lose any features. We then update the bad location
1398 * with the ORed value so that older kernels will see any features2
1399 * flags, and mark the two fields as needing updates once the
1400 * transaction subsystem is online.
David Chinneree1c0902008-03-06 13:45:50 +11001401 */
Eric Sandeene6957ea2008-04-10 12:19:34 +10001402 if (xfs_sb_has_mismatched_features2(sbp)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001403 xfs_warn(mp, "correcting sb_features alignment problem");
David Chinneree1c0902008-03-06 13:45:50 +11001404 sbp->sb_features2 |= sbp->sb_bad_features2;
Eric Sandeene6957ea2008-04-10 12:19:34 +10001405 sbp->sb_bad_features2 = sbp->sb_features2;
Christoph Hellwig7884bc82009-01-19 02:04:07 +01001406 mp->m_update_flags |= XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2;
Eric Sandeene6957ea2008-04-10 12:19:34 +10001407
1408 /*
1409 * Re-check for ATTR2 in case it was found in bad_features2
1410 * slot.
1411 */
Tim Shimmin7c12f292008-04-30 18:15:28 +10001412 if (xfs_sb_version_hasattr2(&mp->m_sb) &&
1413 !(mp->m_flags & XFS_MOUNT_NOATTR2))
Eric Sandeene6957ea2008-04-10 12:19:34 +10001414 mp->m_flags |= XFS_MOUNT_ATTR2;
Tim Shimmin7c12f292008-04-30 18:15:28 +10001415 }
Eric Sandeene6957ea2008-04-10 12:19:34 +10001416
Tim Shimmin7c12f292008-04-30 18:15:28 +10001417 if (xfs_sb_version_hasattr2(&mp->m_sb) &&
1418 (mp->m_flags & XFS_MOUNT_NOATTR2)) {
1419 xfs_sb_version_removeattr2(&mp->m_sb);
Christoph Hellwig7884bc82009-01-19 02:04:07 +01001420 mp->m_update_flags |= XFS_SB_FEATURES2;
Tim Shimmin7c12f292008-04-30 18:15:28 +10001421
1422 /* update sb_versionnum for the clearing of the morebits */
1423 if (!sbp->sb_features2)
Christoph Hellwig7884bc82009-01-19 02:04:07 +01001424 mp->m_update_flags |= XFS_SB_VERSIONNUM;
David Chinneree1c0902008-03-06 13:45:50 +11001425 }
1426
1427 /*
Eric Sandeen0771fb42007-10-12 11:03:40 +10001428 * Check if sb_agblocks is aligned at stripe boundary
1429 * If sb_agblocks is NOT aligned turn off m_dalign since
1430 * allocator alignment is within an ag, therefore ag has
1431 * to be aligned at stripe boundary.
1432 */
Christoph Hellwig7884bc82009-01-19 02:04:07 +01001433 error = xfs_update_alignment(mp);
Eric Sandeen0771fb42007-10-12 11:03:40 +10001434 if (error)
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001435 goto out;
Eric Sandeen0771fb42007-10-12 11:03:40 +10001436
1437 xfs_alloc_compute_maxlevels(mp);
1438 xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
1439 xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
1440 xfs_ialloc_compute_maxlevels(mp);
1441
1442 xfs_set_maxicount(mp);
1443
Christoph Hellwig27174202009-03-30 10:21:31 +02001444 error = xfs_uuid_mount(mp);
1445 if (error)
1446 goto out;
Eric Sandeen0771fb42007-10-12 11:03:40 +10001447
1448 /*
1449 * Set the minimum read and write sizes
1450 */
1451 xfs_set_rw_sizes(mp);
1452
Dave Chinner055388a2011-01-04 11:35:03 +11001453 /* set the low space thresholds for dynamic preallocation */
1454 xfs_set_low_space_thresholds(mp);
1455
Eric Sandeen0771fb42007-10-12 11:03:40 +10001456 /*
1457 * Set the inode cluster size.
1458 * This may still be overridden by the file system
1459 * block size if it is larger than the chosen cluster size.
1460 */
1461 mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
1462
1463 /*
1464 * Set inode alignment fields
1465 */
1466 xfs_set_inoalignment(mp);
1467
1468 /*
1469 * Check that the data (and log if separate) are an ok size.
1470 */
Christoph Hellwig42490232008-08-13 16:49:32 +10001471 error = xfs_check_sizes(mp);
Eric Sandeen0771fb42007-10-12 11:03:40 +10001472 if (error)
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001473 goto out_remove_uuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 /*
1476 * Initialize realtime fields in the mount structure
1477 */
Eric Sandeen0771fb42007-10-12 11:03:40 +10001478 error = xfs_rtmount_init(mp);
1479 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001480 xfs_warn(mp, "RT mount failed");
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001481 goto out_remove_uuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
1483
1484 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 * Copies the low order bits of the timestamp and the randomly
1486 * set "sequence" number out of a UUID.
1487 */
1488 uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid);
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 mp->m_dmevmask = 0; /* not persistent; set after each mount */
1491
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001492 xfs_dir_mount(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
1494 /*
1495 * Initialize the attribute manager's entries.
1496 */
1497 mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100;
1498
1499 /*
1500 * Initialize the precomputed transaction reservations values.
1501 */
1502 xfs_trans_init(mp);
1503
1504 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 * Allocate and initialize the per-ag data.
1506 */
Dave Chinner1c1c6eb2010-01-11 11:47:44 +00001507 spin_lock_init(&mp->m_perag_lock);
Dave Chinner9b98b6f2010-05-27 01:58:13 +00001508 INIT_RADIX_TREE(&mp->m_perag_tree, GFP_ATOMIC);
Dave Chinner1c1c6eb2010-01-11 11:47:44 +00001509 error = xfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi);
1510 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001511 xfs_warn(mp, "Failed per-ag init: %d", error);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001512 goto out_remove_uuid;
Dave Chinner1c1c6eb2010-01-11 11:47:44 +00001513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001515 if (!sbp->sb_logblocks) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001516 xfs_warn(mp, "no log defined");
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001517 XFS_ERROR_REPORT("xfs_mountfs", XFS_ERRLEVEL_LOW, mp);
1518 error = XFS_ERROR(EFSCORRUPTED);
1519 goto out_free_perag;
1520 }
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 /*
1523 * log's mount-time initialization. Perform 1st part recovery if needed
1524 */
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001525 error = xfs_log_mount(mp, mp->m_logdev_targp,
1526 XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
1527 XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
1528 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001529 xfs_warn(mp, "log mount failed");
Dave Chinnerd4f35122012-04-23 15:59:06 +10001530 goto out_fail_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
1532
1533 /*
David Chinner92821e22007-05-24 15:26:31 +10001534 * Now the log is mounted, we know if it was an unclean shutdown or
1535 * not. If it was, with the first phase of recovery has completed, we
1536 * have consistent AG blocks on disk. We have not recovered EFIs yet,
1537 * but they are recovered transactionally in the second recovery phase
1538 * later.
1539 *
1540 * Hence we can safely re-initialise incore superblock counters from
1541 * the per-ag data. These may not be correct if the filesystem was not
1542 * cleanly unmounted, so we need to wait for recovery to finish before
1543 * doing this.
1544 *
1545 * If the filesystem was cleanly unmounted, then we can trust the
1546 * values in the superblock to be correct and we don't need to do
1547 * anything here.
1548 *
1549 * If we are currently making the filesystem, the initialisation will
1550 * fail as the perag data is in an undefined state.
1551 */
David Chinner92821e22007-05-24 15:26:31 +10001552 if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
1553 !XFS_LAST_UNMOUNT_WAS_CLEAN(mp) &&
1554 !mp->m_sb.sb_inprogress) {
1555 error = xfs_initialize_perag_data(mp, sbp->sb_agcount);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001556 if (error)
Dave Chinnerd4f35122012-04-23 15:59:06 +10001557 goto out_fail_wait;
David Chinner92821e22007-05-24 15:26:31 +10001558 }
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001559
David Chinner92821e22007-05-24 15:26:31 +10001560 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 * Get and sanity-check the root inode.
1562 * Save the pointer to it in the mount structure.
1563 */
Dave Chinner7b6259e2010-06-24 11:35:17 +10001564 error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001566 xfs_warn(mp, "failed to read root inode");
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001567 goto out_log_dealloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
1569
1570 ASSERT(rip != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Al Viroabbede12011-07-26 02:31:30 -04001572 if (unlikely(!S_ISDIR(rip->i_d.di_mode))) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001573 xfs_warn(mp, "corrupted root inode %llu: not a directory",
Nathan Scottb6574522006-06-09 15:29:40 +10001574 (unsigned long long)rip->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 xfs_iunlock(rip, XFS_ILOCK_EXCL);
1576 XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
1577 mp);
1578 error = XFS_ERROR(EFSCORRUPTED);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001579 goto out_rele_rip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 }
1581 mp->m_rootip = rip; /* save it */
1582
1583 xfs_iunlock(rip, XFS_ILOCK_EXCL);
1584
1585 /*
1586 * Initialize realtime inode pointers in the mount structure
1587 */
Eric Sandeen0771fb42007-10-12 11:03:40 +10001588 error = xfs_rtmount_inodes(mp);
1589 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 /*
1591 * Free up the root inode.
1592 */
Dave Chinner0b932cc2011-03-07 10:08:35 +11001593 xfs_warn(mp, "failed to read RT inodes");
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001594 goto out_rele_rip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 }
1596
1597 /*
Christoph Hellwig7884bc82009-01-19 02:04:07 +01001598 * If this is a read-only mount defer the superblock updates until
1599 * the next remount into writeable mode. Otherwise we would never
1600 * perform the update e.g. for the root filesystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 */
Christoph Hellwig7884bc82009-01-19 02:04:07 +01001602 if (mp->m_update_flags && !(mp->m_flags & XFS_MOUNT_RDONLY)) {
1603 error = xfs_mount_log_sb(mp, mp->m_update_flags);
David Chinnere5720ee2008-04-10 12:21:18 +10001604 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001605 xfs_warn(mp, "failed to write sb changes");
Christoph Hellwigb93b6e42009-02-04 09:33:58 +01001606 goto out_rtunmount;
David Chinnere5720ee2008-04-10 12:21:18 +10001607 }
1608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 /*
1611 * Initialise the XFS quota management subsystem for this mount
1612 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001613 if (XFS_IS_QUOTA_RUNNING(mp)) {
1614 error = xfs_qm_newmount(mp, &quotamount, &quotaflags);
1615 if (error)
1616 goto out_rtunmount;
1617 } else {
1618 ASSERT(!XFS_IS_QUOTA_ON(mp));
1619
1620 /*
1621 * If a file system had quotas running earlier, but decided to
1622 * mount without -o uquota/pquota/gquota options, revoke the
1623 * quotachecked license.
1624 */
1625 if (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001626 xfs_notice(mp, "resetting quota flags");
Christoph Hellwig7d095252009-06-08 15:33:32 +02001627 error = xfs_mount_reset_sbqflags(mp);
1628 if (error)
1629 return error;
1630 }
1631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 /*
1634 * Finish recovering the file system. This part needed to be
1635 * delayed until after the root and real-time bitmap inodes
1636 * were consistently read in.
1637 */
Christoph Hellwig42490232008-08-13 16:49:32 +10001638 error = xfs_log_mount_finish(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001640 xfs_warn(mp, "log mount finish failed");
Christoph Hellwigb93b6e42009-02-04 09:33:58 +01001641 goto out_rtunmount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 }
1643
1644 /*
1645 * Complete the quota initialisation, post-log-replay component.
1646 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02001647 if (quotamount) {
1648 ASSERT(mp->m_qflags == 0);
1649 mp->m_qflags = quotaflags;
1650
1651 xfs_qm_mount_quotas(mp);
1652 }
1653
David Chinner84e1e992007-06-18 16:50:27 +10001654 /*
1655 * Now we are mounted, reserve a small amount of unused space for
1656 * privileged transactions. This is needed so that transaction
1657 * space required for critical operations can dip into this pool
1658 * when at ENOSPC. This is needed for operations like create with
1659 * attr, unwritten extent conversion at ENOSPC, etc. Data allocations
1660 * are not allowed to use this reserved space.
Dave Chinner8babd8a2010-03-04 01:46:25 +00001661 *
1662 * This may drive us straight to ENOSPC on mount, but that implies
1663 * we were already there on the last unmount. Warn if this occurs.
David Chinner84e1e992007-06-18 16:50:27 +10001664 */
Eric Sandeend5db0f92010-02-05 22:59:53 +00001665 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
1666 resblks = xfs_default_resblks(mp);
1667 error = xfs_reserve_blocks(mp, &resblks, NULL);
1668 if (error)
Dave Chinner0b932cc2011-03-07 10:08:35 +11001669 xfs_warn(mp,
1670 "Unable to allocate reserve blocks. Continuing without reserve pool.");
Eric Sandeend5db0f92010-02-05 22:59:53 +00001671 }
David Chinner84e1e992007-06-18 16:50:27 +10001672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 return 0;
1674
Christoph Hellwigb93b6e42009-02-04 09:33:58 +01001675 out_rtunmount:
1676 xfs_rtunmount_inodes(mp);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001677 out_rele_rip:
Christoph Hellwig43355092008-03-27 18:01:08 +11001678 IRELE(rip);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001679 out_log_dealloc:
Christoph Hellwig21b699c2009-03-16 08:19:29 +01001680 xfs_log_unmount(mp);
Dave Chinnerd4f35122012-04-23 15:59:06 +10001681 out_fail_wait:
1682 if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp)
1683 xfs_wait_buftarg(mp->m_logdev_targp);
1684 xfs_wait_buftarg(mp->m_ddev_targp);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001685 out_free_perag:
Christoph Hellwigff4f0382008-08-13 16:50:47 +10001686 xfs_free_perag(mp);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001687 out_remove_uuid:
Christoph Hellwig27174202009-03-30 10:21:31 +02001688 xfs_uuid_unmount(mp);
Christoph Hellwigf9057e32009-02-04 09:31:52 +01001689 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return error;
1691}
1692
1693/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 * This flushes out the inodes,dquots and the superblock, unmounts the
1695 * log and makes sure that incore structures are freed.
1696 */
Christoph Hellwig41b5c2e2008-08-13 16:49:57 +10001697void
1698xfs_unmountfs(
1699 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
Christoph Hellwig41b5c2e2008-08-13 16:49:57 +10001701 __uint64_t resblks;
1702 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Brian Foster579b62f2012-11-06 09:50:47 -05001704 cancel_delayed_work_sync(&mp->m_eofblocks_work);
1705
Christoph Hellwig7d095252009-06-08 15:33:32 +02001706 xfs_qm_unmount_quotas(mp);
Christoph Hellwigb93b6e42009-02-04 09:33:58 +01001707 xfs_rtunmount_inodes(mp);
Christoph Hellwig77508ec2008-08-13 16:49:04 +10001708 IRELE(mp->m_rootip);
1709
David Chinner641c56f2007-06-18 16:50:17 +10001710 /*
1711 * We can potentially deadlock here if we have an inode cluster
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001712 * that has been freed has its buffer still pinned in memory because
David Chinner641c56f2007-06-18 16:50:17 +10001713 * the transaction is still sitting in a iclog. The stale inodes
1714 * on that buffer will have their flush locks held until the
1715 * transaction hits the disk and the callbacks run. the inode
1716 * flush takes the flush lock unconditionally and with nothing to
1717 * push out the iclog we will never get that unlocked. hence we
1718 * need to force the log first.
1719 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00001720 xfs_log_force(mp, XFS_LOG_SYNC);
Dave Chinnerc8543632010-02-06 12:39:36 +11001721
1722 /*
Christoph Hellwig211e4d42012-04-23 15:58:34 +10001723 * Flush all pending changes from the AIL.
Dave Chinnerc8543632010-02-06 12:39:36 +11001724 */
Christoph Hellwig211e4d42012-04-23 15:58:34 +10001725 xfs_ail_push_all_sync(mp->m_ail);
1726
1727 /*
1728 * And reclaim all inodes. At this point there should be no dirty
Dave Chinner7e185302012-10-08 21:56:00 +11001729 * inodes and none should be pinned or locked, but use synchronous
1730 * reclaim just to be sure. We can stop background inode reclaim
1731 * here as well if it is still running.
Christoph Hellwig211e4d42012-04-23 15:58:34 +10001732 */
Dave Chinner7e185302012-10-08 21:56:00 +11001733 cancel_delayed_work_sync(&mp->m_reclaim_work);
Dave Chinnerc8543632010-02-06 12:39:36 +11001734 xfs_reclaim_inodes(mp, SYNC_WAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Christoph Hellwig7d095252009-06-08 15:33:32 +02001736 xfs_qm_unmount(mp);
Lachlan McIlroya357a122008-10-30 16:53:25 +11001737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 /*
David Chinner84e1e992007-06-18 16:50:27 +10001739 * Unreserve any blocks we have so that when we unmount we don't account
1740 * the reserved free space as used. This is really only necessary for
1741 * lazy superblock counting because it trusts the incore superblock
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001742 * counters to be absolutely correct on clean unmount.
David Chinner84e1e992007-06-18 16:50:27 +10001743 *
1744 * We don't bother correcting this elsewhere for lazy superblock
1745 * counting because on mount of an unclean filesystem we reconstruct the
1746 * correct counter value and this is irrelevant.
1747 *
1748 * For non-lazy counter filesystems, this doesn't matter at all because
1749 * we only every apply deltas to the superblock and hence the incore
1750 * value does not matter....
1751 */
1752 resblks = 0;
David Chinner714082b2008-04-10 12:20:03 +10001753 error = xfs_reserve_blocks(mp, &resblks, NULL);
1754 if (error)
Dave Chinner0b932cc2011-03-07 10:08:35 +11001755 xfs_warn(mp, "Unable to free reserved block pool. "
David Chinner714082b2008-04-10 12:20:03 +10001756 "Freespace may not be correct on next mount.");
1757
Chandra Seetharamanadab0f62011-06-29 22:10:14 +00001758 error = xfs_log_sbcount(mp);
David Chinnere5720ee2008-04-10 12:21:18 +10001759 if (error)
Dave Chinner0b932cc2011-03-07 10:08:35 +11001760 xfs_warn(mp, "Unable to update superblock counters. "
David Chinnere5720ee2008-04-10 12:21:18 +10001761 "Freespace may not be correct on next mount.");
Christoph Hellwig87c7bec2011-09-14 14:08:26 +00001762
Christoph Hellwig21b699c2009-03-16 08:19:29 +01001763 xfs_log_unmount(mp);
Christoph Hellwig27174202009-03-30 10:21:31 +02001764 xfs_uuid_unmount(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Christoph Hellwig1550d0b2008-08-13 16:17:37 +10001766#if defined(DEBUG)
Christoph Hellwig0ce4cfd2007-08-30 17:20:53 +10001767 xfs_errortag_clearall(mp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768#endif
Christoph Hellwigff4f0382008-08-13 16:50:47 +10001769 xfs_free_perag(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772int
David Chinner92821e22007-05-24 15:26:31 +10001773xfs_fs_writable(xfs_mount_t *mp)
1774{
Jan Karad9457dc2012-06-12 16:20:39 +02001775 return !(mp->m_super->s_writers.frozen || XFS_FORCED_SHUTDOWN(mp) ||
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001776 (mp->m_flags & XFS_MOUNT_RDONLY));
David Chinner92821e22007-05-24 15:26:31 +10001777}
1778
1779/*
Alex Elderb2ce3972011-07-11 09:51:44 -05001780 * xfs_log_sbcount
1781 *
Chandra Seetharamanadab0f62011-06-29 22:10:14 +00001782 * Sync the superblock counters to disk.
Alex Elderb2ce3972011-07-11 09:51:44 -05001783 *
1784 * Note this code can be called during the process of freezing, so
Chandra Seetharamanadab0f62011-06-29 22:10:14 +00001785 * we may need to use the transaction allocator which does not
Alex Elderb2ce3972011-07-11 09:51:44 -05001786 * block when the transaction subsystem is in its frozen state.
David Chinner92821e22007-05-24 15:26:31 +10001787 */
1788int
Chandra Seetharamanadab0f62011-06-29 22:10:14 +00001789xfs_log_sbcount(xfs_mount_t *mp)
David Chinner92821e22007-05-24 15:26:31 +10001790{
1791 xfs_trans_t *tp;
1792 int error;
1793
1794 if (!xfs_fs_writable(mp))
1795 return 0;
1796
Christoph Hellwigd4d90b52008-04-22 17:34:37 +10001797 xfs_icsb_sync_counters(mp, 0);
David Chinner92821e22007-05-24 15:26:31 +10001798
1799 /*
1800 * we don't need to do this if we are updating the superblock
1801 * counters on every modification.
1802 */
1803 if (!xfs_sb_version_haslazysbcount(&mp->m_sb))
1804 return 0;
1805
Alex Elderb2ce3972011-07-11 09:51:44 -05001806 tp = _xfs_trans_alloc(mp, XFS_TRANS_SB_COUNT, KM_SLEEP);
Jeff Liue4572742013-01-28 21:27:31 +08001807 error = xfs_trans_reserve(tp, 0, XFS_SB_LOG_RES(mp), 0, 0,
1808 XFS_DEFAULT_LOG_COUNT);
David Chinner92821e22007-05-24 15:26:31 +10001809 if (error) {
1810 xfs_trans_cancel(tp, 0);
1811 return error;
1812 }
1813
1814 xfs_mod_sb(tp, XFS_SB_IFREE | XFS_SB_ICOUNT | XFS_SB_FDBLOCKS);
Chandra Seetharamanadab0f62011-06-29 22:10:14 +00001815 xfs_trans_set_sync(tp);
David Chinnere5720ee2008-04-10 12:21:18 +10001816 error = xfs_trans_commit(tp, 0);
1817 return error;
David Chinner92821e22007-05-24 15:26:31 +10001818}
1819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820/*
1821 * xfs_mod_sb() can be used to copy arbitrary changes to the
1822 * in-core superblock into the superblock buffer to be logged.
1823 * It does not provide the higher level of locking that is
1824 * needed to protect the in-core superblock from concurrent
1825 * access.
1826 */
1827void
1828xfs_mod_sb(xfs_trans_t *tp, __int64_t fields)
1829{
1830 xfs_buf_t *bp;
1831 int first;
1832 int last;
1833 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 xfs_sb_field_t f;
1835
1836 ASSERT(fields);
1837 if (!fields)
1838 return;
1839 mp = tp->t_mountp;
1840 bp = xfs_trans_getsb(tp, mp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 first = sizeof(xfs_sb_t);
1842 last = 0;
1843
1844 /* translate/copy */
1845
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +10001846 xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb, fields);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
1848 /* find modified range */
Dave Chinner587aa0f2010-01-20 12:04:53 +11001849 f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields);
1850 ASSERT((1LL << f) & XFS_SB_MOD_BITS);
1851 last = xfs_sb_info[f + 1].offset - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
1854 ASSERT((1LL << f) & XFS_SB_MOD_BITS);
1855 first = xfs_sb_info[f].offset;
1856
Dave Chinner04a1e6c2013-04-03 16:11:31 +11001857 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 xfs_trans_log_buf(tp, bp, first, last);
1859}
Yingping Lud210a282006-06-09 14:55:18 +10001860
Yingping Lud210a282006-06-09 14:55:18 +10001861
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862/*
1863 * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply
1864 * a delta to a specified field in the in-core superblock. Simply
1865 * switch on the field indicated and apply the delta to that field.
1866 * Fields are not allowed to dip below zero, so if the delta would
1867 * do this do not apply it and return EINVAL.
1868 *
Eric Sandeen3685c2a2007-10-11 17:42:32 +10001869 * The m_sb_lock must be held when this routine is called.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05001871STATIC int
David Chinner20f4ebf2007-02-10 18:36:10 +11001872xfs_mod_incore_sb_unlocked(
1873 xfs_mount_t *mp,
1874 xfs_sb_field_t field,
1875 int64_t delta,
1876 int rsvd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
1878 int scounter; /* short counter for 32 bit fields */
1879 long long lcounter; /* long counter for 64 bit fields */
1880 long long res_used, rem;
1881
1882 /*
1883 * With the in-core superblock spin lock held, switch
1884 * on the indicated field. Apply the delta to the
1885 * proper field. If the fields value would dip below
1886 * 0, then do not apply the delta and return EINVAL.
1887 */
1888 switch (field) {
1889 case XFS_SBS_ICOUNT:
1890 lcounter = (long long)mp->m_sb.sb_icount;
1891 lcounter += delta;
1892 if (lcounter < 0) {
1893 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001894 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
1896 mp->m_sb.sb_icount = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001897 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 case XFS_SBS_IFREE:
1899 lcounter = (long long)mp->m_sb.sb_ifree;
1900 lcounter += delta;
1901 if (lcounter < 0) {
1902 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001903 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 }
1905 mp->m_sb.sb_ifree = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001906 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 case XFS_SBS_FDBLOCKS:
David Chinner4be536d2006-09-07 14:26:50 +10001908 lcounter = (long long)
1909 mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
1911
1912 if (delta > 0) { /* Putting blocks back */
1913 if (res_used > delta) {
1914 mp->m_resblks_avail += delta;
1915 } else {
1916 rem = delta - res_used;
1917 mp->m_resblks_avail = mp->m_resblks;
1918 lcounter += rem;
1919 }
1920 } else { /* Taking blocks away */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 lcounter += delta;
Dave Chinner8babd8a2010-03-04 01:46:25 +00001922 if (lcounter >= 0) {
1923 mp->m_sb.sb_fdblocks = lcounter +
1924 XFS_ALLOC_SET_ASIDE(mp);
1925 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 }
Dave Chinner8babd8a2010-03-04 01:46:25 +00001927
1928 /*
1929 * We are out of blocks, use any available reserved
1930 * blocks if were allowed to.
1931 */
1932 if (!rsvd)
1933 return XFS_ERROR(ENOSPC);
1934
1935 lcounter = (long long)mp->m_resblks_avail + delta;
1936 if (lcounter >= 0) {
1937 mp->m_resblks_avail = lcounter;
1938 return 0;
1939 }
1940 printk_once(KERN_WARNING
1941 "Filesystem \"%s\": reserve blocks depleted! "
1942 "Consider increasing reserve pool size.",
1943 mp->m_fsname);
1944 return XFS_ERROR(ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
1946
David Chinner4be536d2006-09-07 14:26:50 +10001947 mp->m_sb.sb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
Jesper Juhl014c2542006-01-15 02:37:08 +01001948 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 case XFS_SBS_FREXTENTS:
1950 lcounter = (long long)mp->m_sb.sb_frextents;
1951 lcounter += delta;
1952 if (lcounter < 0) {
Jesper Juhl014c2542006-01-15 02:37:08 +01001953 return XFS_ERROR(ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
1955 mp->m_sb.sb_frextents = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001956 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 case XFS_SBS_DBLOCKS:
1958 lcounter = (long long)mp->m_sb.sb_dblocks;
1959 lcounter += delta;
1960 if (lcounter < 0) {
1961 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001962 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 }
1964 mp->m_sb.sb_dblocks = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001965 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 case XFS_SBS_AGCOUNT:
1967 scounter = mp->m_sb.sb_agcount;
1968 scounter += delta;
1969 if (scounter < 0) {
1970 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001971 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 }
1973 mp->m_sb.sb_agcount = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001974 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 case XFS_SBS_IMAX_PCT:
1976 scounter = mp->m_sb.sb_imax_pct;
1977 scounter += delta;
1978 if (scounter < 0) {
1979 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001980 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 }
1982 mp->m_sb.sb_imax_pct = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001983 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 case XFS_SBS_REXTSIZE:
1985 scounter = mp->m_sb.sb_rextsize;
1986 scounter += delta;
1987 if (scounter < 0) {
1988 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001989 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 }
1991 mp->m_sb.sb_rextsize = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01001992 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 case XFS_SBS_RBMBLOCKS:
1994 scounter = mp->m_sb.sb_rbmblocks;
1995 scounter += delta;
1996 if (scounter < 0) {
1997 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001998 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 }
2000 mp->m_sb.sb_rbmblocks = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01002001 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 case XFS_SBS_RBLOCKS:
2003 lcounter = (long long)mp->m_sb.sb_rblocks;
2004 lcounter += delta;
2005 if (lcounter < 0) {
2006 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01002007 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 }
2009 mp->m_sb.sb_rblocks = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01002010 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 case XFS_SBS_REXTENTS:
2012 lcounter = (long long)mp->m_sb.sb_rextents;
2013 lcounter += delta;
2014 if (lcounter < 0) {
2015 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01002016 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 }
2018 mp->m_sb.sb_rextents = lcounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01002019 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 case XFS_SBS_REXTSLOG:
2021 scounter = mp->m_sb.sb_rextslog;
2022 scounter += delta;
2023 if (scounter < 0) {
2024 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01002025 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
2027 mp->m_sb.sb_rextslog = scounter;
Jesper Juhl014c2542006-01-15 02:37:08 +01002028 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 default:
2030 ASSERT(0);
Jesper Juhl014c2542006-01-15 02:37:08 +01002031 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 }
2033}
2034
2035/*
2036 * xfs_mod_incore_sb() is used to change a field in the in-core
2037 * superblock structure by the specified delta. This modification
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002038 * is protected by the m_sb_lock. Just use the xfs_mod_incore_sb_unlocked()
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 * routine to do the work.
2040 */
2041int
David Chinner20f4ebf2007-02-10 18:36:10 +11002042xfs_mod_incore_sb(
Christoph Hellwig96540c72010-09-30 02:25:55 +00002043 struct xfs_mount *mp,
2044 xfs_sb_field_t field,
2045 int64_t delta,
2046 int rsvd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
Christoph Hellwig96540c72010-09-30 02:25:55 +00002048 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
David Chinner8d280b92006-03-14 13:13:09 +11002050#ifdef HAVE_PERCPU_SB
Christoph Hellwig96540c72010-09-30 02:25:55 +00002051 ASSERT(field < XFS_SBS_ICOUNT || field > XFS_SBS_FDBLOCKS);
David Chinner8d280b92006-03-14 13:13:09 +11002052#endif
Christoph Hellwig96540c72010-09-30 02:25:55 +00002053 spin_lock(&mp->m_sb_lock);
2054 status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
2055 spin_unlock(&mp->m_sb_lock);
David Chinner8d280b92006-03-14 13:13:09 +11002056
Jesper Juhl014c2542006-01-15 02:37:08 +01002057 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058}
2059
2060/*
Christoph Hellwig1b040712010-09-30 02:25:56 +00002061 * Change more than one field in the in-core superblock structure at a time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 *
Christoph Hellwig1b040712010-09-30 02:25:56 +00002063 * The fields and changes to those fields are specified in the array of
2064 * xfs_mod_sb structures passed in. Either all of the specified deltas
2065 * will be applied or none of them will. If any modified field dips below 0,
2066 * then all modifications will be backed out and EINVAL will be returned.
2067 *
2068 * Note that this function may not be used for the superblock values that
2069 * are tracked with the in-memory per-cpu counters - a direct call to
2070 * xfs_icsb_modify_counters is required for these.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 */
2072int
Christoph Hellwig1b040712010-09-30 02:25:56 +00002073xfs_mod_incore_sb_batch(
2074 struct xfs_mount *mp,
2075 xfs_mod_sb_t *msb,
2076 uint nmsb,
2077 int rsvd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
David Sterba45c51b92011-04-13 22:03:28 +00002079 xfs_mod_sb_t *msbp;
Christoph Hellwig1b040712010-09-30 02:25:56 +00002080 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 /*
Christoph Hellwig1b040712010-09-30 02:25:56 +00002083 * Loop through the array of mod structures and apply each individually.
2084 * If any fail, then back out all those which have already been applied.
2085 * Do all of this within the scope of the m_sb_lock so that all of the
2086 * changes will be atomic.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002088 spin_lock(&mp->m_sb_lock);
David Sterba45c51b92011-04-13 22:03:28 +00002089 for (msbp = msb; msbp < (msb + nmsb); msbp++) {
Christoph Hellwig1b040712010-09-30 02:25:56 +00002090 ASSERT(msbp->msb_field < XFS_SBS_ICOUNT ||
2091 msbp->msb_field > XFS_SBS_FDBLOCKS);
David Chinner8d280b92006-03-14 13:13:09 +11002092
Christoph Hellwig1b040712010-09-30 02:25:56 +00002093 error = xfs_mod_incore_sb_unlocked(mp, msbp->msb_field,
2094 msbp->msb_delta, rsvd);
2095 if (error)
2096 goto unwind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 }
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002098 spin_unlock(&mp->m_sb_lock);
Christoph Hellwig1b040712010-09-30 02:25:56 +00002099 return 0;
2100
2101unwind:
2102 while (--msbp >= msb) {
2103 error = xfs_mod_incore_sb_unlocked(mp, msbp->msb_field,
2104 -msbp->msb_delta, rsvd);
2105 ASSERT(error == 0);
2106 }
2107 spin_unlock(&mp->m_sb_lock);
2108 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109}
2110
2111/*
2112 * xfs_getsb() is called to obtain the buffer for the superblock.
2113 * The buffer is returned locked and read in from disk.
2114 * The buffer should be released with a call to xfs_brelse().
2115 *
2116 * If the flags parameter is BUF_TRYLOCK, then we'll only return
2117 * the superblock buffer if it can be locked without sleeping.
2118 * If it can't then we'll return NULL.
2119 */
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02002120struct xfs_buf *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121xfs_getsb(
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02002122 struct xfs_mount *mp,
2123 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02002125 struct xfs_buf *bp = mp->m_sb_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02002127 if (!xfs_buf_trylock(bp)) {
2128 if (flags & XBF_TRYLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 return NULL;
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02002130 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 }
Christoph Hellwig0c842ad2011-07-08 14:36:19 +02002132
Chandra Seetharaman72790aa2011-07-22 23:40:04 +00002133 xfs_buf_hold(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 ASSERT(XFS_BUF_ISDONE(bp));
Jesper Juhl014c2542006-01-15 02:37:08 +01002135 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136}
2137
2138/*
2139 * Used to free the superblock along various error paths.
2140 */
2141void
2142xfs_freesb(
Dave Chinner26af6552010-09-22 10:47:20 +10002143 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
Dave Chinner26af6552010-09-22 10:47:20 +10002145 struct xfs_buf *bp = mp->m_sb_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Dave Chinner26af6552010-09-22 10:47:20 +10002147 xfs_buf_lock(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 mp->m_sb_bp = NULL;
Dave Chinner26af6552010-09-22 10:47:20 +10002149 xfs_buf_relse(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
2151
2152/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 * Used to log changes to the superblock unit and width fields which could
Eric Sandeene6957ea2008-04-10 12:19:34 +10002154 * be altered by the mount options, as well as any potential sb_features2
2155 * fixup. Only the first superblock is updated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 */
Christoph Hellwig7884bc82009-01-19 02:04:07 +01002157int
David Chinneree1c0902008-03-06 13:45:50 +11002158xfs_mount_log_sb(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 xfs_mount_t *mp,
2160 __int64_t fields)
2161{
2162 xfs_trans_t *tp;
David Chinnere5720ee2008-04-10 12:21:18 +10002163 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
David Chinneree1c0902008-03-06 13:45:50 +11002165 ASSERT(fields & (XFS_SB_UNIT | XFS_SB_WIDTH | XFS_SB_UUID |
David Chinner4b166de2008-05-20 11:30:27 +10002166 XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2 |
2167 XFS_SB_VERSIONNUM));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
2169 tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT);
Jeff Liu5166ab02013-01-28 21:27:39 +08002170 error = xfs_trans_reserve(tp, 0, XFS_SB_LOG_RES(mp), 0, 0,
2171 XFS_DEFAULT_LOG_COUNT);
David Chinnere5720ee2008-04-10 12:21:18 +10002172 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 xfs_trans_cancel(tp, 0);
David Chinnere5720ee2008-04-10 12:21:18 +10002174 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 }
2176 xfs_mod_sb(tp, fields);
David Chinnere5720ee2008-04-10 12:21:18 +10002177 error = xfs_trans_commit(tp, 0);
2178 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
David Chinner8d280b92006-03-14 13:13:09 +11002180
Christoph Hellwigdda35b82010-02-15 09:44:46 +00002181/*
2182 * If the underlying (data/log/rt) device is readonly, there are some
2183 * operations that cannot proceed.
2184 */
2185int
2186xfs_dev_is_read_only(
2187 struct xfs_mount *mp,
2188 char *message)
2189{
2190 if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
2191 xfs_readonly_buftarg(mp->m_logdev_targp) ||
2192 (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11002193 xfs_notice(mp, "%s required on read-only device.", message);
2194 xfs_notice(mp, "write access unavailable, cannot proceed.");
Christoph Hellwigdda35b82010-02-15 09:44:46 +00002195 return EROFS;
2196 }
2197 return 0;
2198}
David Chinner8d280b92006-03-14 13:13:09 +11002199
2200#ifdef HAVE_PERCPU_SB
2201/*
2202 * Per-cpu incore superblock counters
2203 *
2204 * Simple concept, difficult implementation
2205 *
2206 * Basically, replace the incore superblock counters with a distributed per cpu
2207 * counter for contended fields (e.g. free block count).
2208 *
2209 * Difficulties arise in that the incore sb is used for ENOSPC checking, and
2210 * hence needs to be accurately read when we are running low on space. Hence
2211 * there is a method to enable and disable the per-cpu counters based on how
2212 * much "stuff" is available in them.
2213 *
2214 * Basically, a counter is enabled if there is enough free resource to justify
2215 * running a per-cpu fast-path. If the per-cpu counter runs out (i.e. a local
2216 * ENOSPC), then we disable the counters to synchronise all callers and
2217 * re-distribute the available resources.
2218 *
2219 * If, once we redistributed the available resources, we still get a failure,
2220 * we disable the per-cpu counter and go through the slow path.
2221 *
2222 * The slow path is the current xfs_mod_incore_sb() function. This means that
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002223 * when we disable a per-cpu counter, we need to drain its resources back to
David Chinner8d280b92006-03-14 13:13:09 +11002224 * the global superblock. We do this after disabling the counter to prevent
2225 * more threads from queueing up on the counter.
2226 *
2227 * Essentially, this means that we still need a lock in the fast path to enable
2228 * synchronisation between the global counters and the per-cpu counters. This
2229 * is not a problem because the lock will be local to a CPU almost all the time
2230 * and have little contention except when we get to ENOSPC conditions.
2231 *
2232 * Basically, this lock becomes a barrier that enables us to lock out the fast
2233 * path while we do things like enabling and disabling counters and
2234 * synchronising the counters.
2235 *
2236 * Locking rules:
2237 *
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002238 * 1. m_sb_lock before picking up per-cpu locks
David Chinner8d280b92006-03-14 13:13:09 +11002239 * 2. per-cpu locks always picked up via for_each_online_cpu() order
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002240 * 3. accurate counter sync requires m_sb_lock + per cpu locks
David Chinner8d280b92006-03-14 13:13:09 +11002241 * 4. modifying per-cpu counters requires holding per-cpu lock
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002242 * 5. modifying global counters requires holding m_sb_lock
2243 * 6. enabling or disabling a counter requires holding the m_sb_lock
David Chinner8d280b92006-03-14 13:13:09 +11002244 * and _none_ of the per-cpu locks.
2245 *
2246 * Disabled counters are only ever re-enabled by a balance operation
2247 * that results in more free resources per CPU than a given threshold.
2248 * To ensure counters don't remain disabled, they are rebalanced when
2249 * the global resource goes above a higher threshold (i.e. some hysteresis
2250 * is present to prevent thrashing).
David Chinner8d280b92006-03-14 13:13:09 +11002251 */
David Chinnere8234a62006-03-14 13:23:52 +11002252
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07002253#ifdef CONFIG_HOTPLUG_CPU
David Chinnere8234a62006-03-14 13:23:52 +11002254/*
2255 * hot-plug CPU notifier support.
2256 *
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07002257 * We need a notifier per filesystem as we need to be able to identify
2258 * the filesystem to balance the counters out. This is achieved by
2259 * having a notifier block embedded in the xfs_mount_t and doing pointer
2260 * magic to get the mount pointer from the notifier block address.
David Chinnere8234a62006-03-14 13:23:52 +11002261 */
2262STATIC int
2263xfs_icsb_cpu_notify(
2264 struct notifier_block *nfb,
2265 unsigned long action,
2266 void *hcpu)
2267{
2268 xfs_icsb_cnts_t *cntp;
2269 xfs_mount_t *mp;
David Chinnere8234a62006-03-14 13:23:52 +11002270
2271 mp = (xfs_mount_t *)container_of(nfb, xfs_mount_t, m_icsb_notifier);
2272 cntp = (xfs_icsb_cnts_t *)
2273 per_cpu_ptr(mp->m_sb_cnts, (unsigned long)hcpu);
2274 switch (action) {
2275 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002276 case CPU_UP_PREPARE_FROZEN:
David Chinnere8234a62006-03-14 13:23:52 +11002277 /* Easy Case - initialize the area and locks, and
2278 * then rebalance when online does everything else for us. */
David Chinner01e1b692006-03-14 13:29:16 +11002279 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
David Chinnere8234a62006-03-14 13:23:52 +11002280 break;
2281 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002282 case CPU_ONLINE_FROZEN:
David Chinner03135cf2007-02-10 18:35:15 +11002283 xfs_icsb_lock(mp);
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002284 xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
2285 xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
2286 xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
David Chinner03135cf2007-02-10 18:35:15 +11002287 xfs_icsb_unlock(mp);
David Chinnere8234a62006-03-14 13:23:52 +11002288 break;
2289 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002290 case CPU_DEAD_FROZEN:
David Chinnere8234a62006-03-14 13:23:52 +11002291 /* Disable all the counters, then fold the dead cpu's
2292 * count into the total on the global superblock and
2293 * re-enable the counters. */
David Chinner03135cf2007-02-10 18:35:15 +11002294 xfs_icsb_lock(mp);
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002295 spin_lock(&mp->m_sb_lock);
David Chinnere8234a62006-03-14 13:23:52 +11002296 xfs_icsb_disable_counter(mp, XFS_SBS_ICOUNT);
2297 xfs_icsb_disable_counter(mp, XFS_SBS_IFREE);
2298 xfs_icsb_disable_counter(mp, XFS_SBS_FDBLOCKS);
2299
2300 mp->m_sb.sb_icount += cntp->icsb_icount;
2301 mp->m_sb.sb_ifree += cntp->icsb_ifree;
2302 mp->m_sb.sb_fdblocks += cntp->icsb_fdblocks;
2303
David Chinner01e1b692006-03-14 13:29:16 +11002304 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
David Chinnere8234a62006-03-14 13:23:52 +11002305
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002306 xfs_icsb_balance_counter_locked(mp, XFS_SBS_ICOUNT, 0);
2307 xfs_icsb_balance_counter_locked(mp, XFS_SBS_IFREE, 0);
2308 xfs_icsb_balance_counter_locked(mp, XFS_SBS_FDBLOCKS, 0);
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002309 spin_unlock(&mp->m_sb_lock);
David Chinner03135cf2007-02-10 18:35:15 +11002310 xfs_icsb_unlock(mp);
David Chinnere8234a62006-03-14 13:23:52 +11002311 break;
2312 }
2313
2314 return NOTIFY_OK;
2315}
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07002316#endif /* CONFIG_HOTPLUG_CPU */
David Chinnere8234a62006-03-14 13:23:52 +11002317
David Chinner8d280b92006-03-14 13:13:09 +11002318int
2319xfs_icsb_init_counters(
2320 xfs_mount_t *mp)
2321{
2322 xfs_icsb_cnts_t *cntp;
2323 int i;
2324
2325 mp->m_sb_cnts = alloc_percpu(xfs_icsb_cnts_t);
2326 if (mp->m_sb_cnts == NULL)
2327 return -ENOMEM;
2328
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07002329#ifdef CONFIG_HOTPLUG_CPU
David Chinnere8234a62006-03-14 13:23:52 +11002330 mp->m_icsb_notifier.notifier_call = xfs_icsb_cpu_notify;
2331 mp->m_icsb_notifier.priority = 0;
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07002332 register_hotcpu_notifier(&mp->m_icsb_notifier);
2333#endif /* CONFIG_HOTPLUG_CPU */
David Chinnere8234a62006-03-14 13:23:52 +11002334
David Chinner8d280b92006-03-14 13:13:09 +11002335 for_each_online_cpu(i) {
2336 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
David Chinner01e1b692006-03-14 13:29:16 +11002337 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
David Chinner8d280b92006-03-14 13:13:09 +11002338 }
David Chinner20b64282007-02-10 18:35:09 +11002339
2340 mutex_init(&mp->m_icsb_mutex);
2341
David Chinner8d280b92006-03-14 13:13:09 +11002342 /*
2343 * start with all counters disabled so that the
2344 * initial balance kicks us off correctly
2345 */
2346 mp->m_icsb_counters = -1;
2347 return 0;
2348}
2349
Lachlan McIlroy5478eea2007-02-10 18:36:29 +11002350void
2351xfs_icsb_reinit_counters(
2352 xfs_mount_t *mp)
2353{
2354 xfs_icsb_lock(mp);
2355 /*
2356 * start with all counters disabled so that the
2357 * initial balance kicks us off correctly
2358 */
2359 mp->m_icsb_counters = -1;
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002360 xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
2361 xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
2362 xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
Lachlan McIlroy5478eea2007-02-10 18:36:29 +11002363 xfs_icsb_unlock(mp);
2364}
2365
Christoph Hellwigc962fb72008-05-20 15:10:52 +10002366void
David Chinner8d280b92006-03-14 13:13:09 +11002367xfs_icsb_destroy_counters(
2368 xfs_mount_t *mp)
2369{
David Chinnere8234a62006-03-14 13:23:52 +11002370 if (mp->m_sb_cnts) {
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07002371 unregister_hotcpu_notifier(&mp->m_icsb_notifier);
David Chinner8d280b92006-03-14 13:13:09 +11002372 free_percpu(mp->m_sb_cnts);
David Chinnere8234a62006-03-14 13:23:52 +11002373 }
David Chinner03135cf2007-02-10 18:35:15 +11002374 mutex_destroy(&mp->m_icsb_mutex);
David Chinner8d280b92006-03-14 13:13:09 +11002375}
2376
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00002377STATIC void
David Chinner01e1b692006-03-14 13:29:16 +11002378xfs_icsb_lock_cntr(
2379 xfs_icsb_cnts_t *icsbp)
2380{
2381 while (test_and_set_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags)) {
2382 ndelay(1000);
2383 }
2384}
2385
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00002386STATIC void
David Chinner01e1b692006-03-14 13:29:16 +11002387xfs_icsb_unlock_cntr(
2388 xfs_icsb_cnts_t *icsbp)
2389{
2390 clear_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags);
2391}
2392
David Chinner8d280b92006-03-14 13:13:09 +11002393
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00002394STATIC void
David Chinner8d280b92006-03-14 13:13:09 +11002395xfs_icsb_lock_all_counters(
2396 xfs_mount_t *mp)
2397{
2398 xfs_icsb_cnts_t *cntp;
2399 int i;
2400
2401 for_each_online_cpu(i) {
2402 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
David Chinner01e1b692006-03-14 13:29:16 +11002403 xfs_icsb_lock_cntr(cntp);
David Chinner8d280b92006-03-14 13:13:09 +11002404 }
2405}
2406
Christoph Hellwigb8f82a42009-11-14 16:17:22 +00002407STATIC void
David Chinner8d280b92006-03-14 13:13:09 +11002408xfs_icsb_unlock_all_counters(
2409 xfs_mount_t *mp)
2410{
2411 xfs_icsb_cnts_t *cntp;
2412 int i;
2413
2414 for_each_online_cpu(i) {
2415 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
David Chinner01e1b692006-03-14 13:29:16 +11002416 xfs_icsb_unlock_cntr(cntp);
David Chinner8d280b92006-03-14 13:13:09 +11002417 }
2418}
2419
2420STATIC void
2421xfs_icsb_count(
2422 xfs_mount_t *mp,
2423 xfs_icsb_cnts_t *cnt,
2424 int flags)
2425{
2426 xfs_icsb_cnts_t *cntp;
2427 int i;
2428
2429 memset(cnt, 0, sizeof(xfs_icsb_cnts_t));
2430
2431 if (!(flags & XFS_ICSB_LAZY_COUNT))
2432 xfs_icsb_lock_all_counters(mp);
2433
2434 for_each_online_cpu(i) {
2435 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
2436 cnt->icsb_icount += cntp->icsb_icount;
2437 cnt->icsb_ifree += cntp->icsb_ifree;
2438 cnt->icsb_fdblocks += cntp->icsb_fdblocks;
2439 }
2440
2441 if (!(flags & XFS_ICSB_LAZY_COUNT))
2442 xfs_icsb_unlock_all_counters(mp);
2443}
2444
2445STATIC int
2446xfs_icsb_counter_disabled(
2447 xfs_mount_t *mp,
2448 xfs_sb_field_t field)
2449{
2450 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
2451 return test_bit(field, &mp->m_icsb_counters);
2452}
2453
David Chinner36fbe6e2008-04-10 12:19:56 +10002454STATIC void
David Chinner8d280b92006-03-14 13:13:09 +11002455xfs_icsb_disable_counter(
2456 xfs_mount_t *mp,
2457 xfs_sb_field_t field)
2458{
2459 xfs_icsb_cnts_t cnt;
2460
2461 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
2462
David Chinner20b64282007-02-10 18:35:09 +11002463 /*
2464 * If we are already disabled, then there is nothing to do
2465 * here. We check before locking all the counters to avoid
2466 * the expensive lock operation when being called in the
2467 * slow path and the counter is already disabled. This is
2468 * safe because the only time we set or clear this state is under
2469 * the m_icsb_mutex.
2470 */
2471 if (xfs_icsb_counter_disabled(mp, field))
David Chinner36fbe6e2008-04-10 12:19:56 +10002472 return;
David Chinner20b64282007-02-10 18:35:09 +11002473
David Chinner8d280b92006-03-14 13:13:09 +11002474 xfs_icsb_lock_all_counters(mp);
2475 if (!test_and_set_bit(field, &mp->m_icsb_counters)) {
2476 /* drain back to superblock */
2477
Christoph Hellwigce461932008-04-22 17:34:50 +10002478 xfs_icsb_count(mp, &cnt, XFS_ICSB_LAZY_COUNT);
David Chinner8d280b92006-03-14 13:13:09 +11002479 switch(field) {
2480 case XFS_SBS_ICOUNT:
2481 mp->m_sb.sb_icount = cnt.icsb_icount;
2482 break;
2483 case XFS_SBS_IFREE:
2484 mp->m_sb.sb_ifree = cnt.icsb_ifree;
2485 break;
2486 case XFS_SBS_FDBLOCKS:
2487 mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
2488 break;
2489 default:
2490 BUG();
2491 }
2492 }
2493
2494 xfs_icsb_unlock_all_counters(mp);
David Chinner8d280b92006-03-14 13:13:09 +11002495}
2496
2497STATIC void
2498xfs_icsb_enable_counter(
2499 xfs_mount_t *mp,
2500 xfs_sb_field_t field,
2501 uint64_t count,
2502 uint64_t resid)
2503{
2504 xfs_icsb_cnts_t *cntp;
2505 int i;
2506
2507 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
2508
2509 xfs_icsb_lock_all_counters(mp);
2510 for_each_online_cpu(i) {
2511 cntp = per_cpu_ptr(mp->m_sb_cnts, i);
2512 switch (field) {
2513 case XFS_SBS_ICOUNT:
2514 cntp->icsb_icount = count + resid;
2515 break;
2516 case XFS_SBS_IFREE:
2517 cntp->icsb_ifree = count + resid;
2518 break;
2519 case XFS_SBS_FDBLOCKS:
2520 cntp->icsb_fdblocks = count + resid;
2521 break;
2522 default:
2523 BUG();
2524 break;
2525 }
2526 resid = 0;
2527 }
2528 clear_bit(field, &mp->m_icsb_counters);
2529 xfs_icsb_unlock_all_counters(mp);
2530}
2531
David Chinnerdbcabad2007-02-10 18:36:17 +11002532void
Christoph Hellwigd4d90b52008-04-22 17:34:37 +10002533xfs_icsb_sync_counters_locked(
David Chinner8d280b92006-03-14 13:13:09 +11002534 xfs_mount_t *mp,
2535 int flags)
2536{
2537 xfs_icsb_cnts_t cnt;
David Chinner8d280b92006-03-14 13:13:09 +11002538
David Chinner8d280b92006-03-14 13:13:09 +11002539 xfs_icsb_count(mp, &cnt, flags);
2540
David Chinner8d280b92006-03-14 13:13:09 +11002541 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_ICOUNT))
2542 mp->m_sb.sb_icount = cnt.icsb_icount;
2543 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_IFREE))
2544 mp->m_sb.sb_ifree = cnt.icsb_ifree;
2545 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_FDBLOCKS))
2546 mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
David Chinner8d280b92006-03-14 13:13:09 +11002547}
2548
2549/*
2550 * Accurate update of per-cpu counters to incore superblock
2551 */
Christoph Hellwigd4d90b52008-04-22 17:34:37 +10002552void
David Chinner8d280b92006-03-14 13:13:09 +11002553xfs_icsb_sync_counters(
Christoph Hellwigd4d90b52008-04-22 17:34:37 +10002554 xfs_mount_t *mp,
2555 int flags)
David Chinner8d280b92006-03-14 13:13:09 +11002556{
Christoph Hellwigd4d90b52008-04-22 17:34:37 +10002557 spin_lock(&mp->m_sb_lock);
2558 xfs_icsb_sync_counters_locked(mp, flags);
2559 spin_unlock(&mp->m_sb_lock);
David Chinner8d280b92006-03-14 13:13:09 +11002560}
2561
2562/*
2563 * Balance and enable/disable counters as necessary.
2564 *
David Chinner20b64282007-02-10 18:35:09 +11002565 * Thresholds for re-enabling counters are somewhat magic. inode counts are
2566 * chosen to be the same number as single on disk allocation chunk per CPU, and
2567 * free blocks is something far enough zero that we aren't going thrash when we
2568 * get near ENOSPC. We also need to supply a minimum we require per cpu to
2569 * prevent looping endlessly when xfs_alloc_space asks for more than will
2570 * be distributed to a single CPU but each CPU has enough blocks to be
2571 * reenabled.
2572 *
2573 * Note that we can be called when counters are already disabled.
2574 * xfs_icsb_disable_counter() optimises the counter locking in this case to
2575 * prevent locking every per-cpu counter needlessly.
David Chinner8d280b92006-03-14 13:13:09 +11002576 */
David Chinner20b64282007-02-10 18:35:09 +11002577
2578#define XFS_ICSB_INO_CNTR_REENABLE (uint64_t)64
David Chinner4be536d2006-09-07 14:26:50 +10002579#define XFS_ICSB_FDBLK_CNTR_REENABLE(mp) \
David Chinner20b64282007-02-10 18:35:09 +11002580 (uint64_t)(512 + XFS_ALLOC_SET_ASIDE(mp))
David Chinner8d280b92006-03-14 13:13:09 +11002581STATIC void
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002582xfs_icsb_balance_counter_locked(
David Chinner8d280b92006-03-14 13:13:09 +11002583 xfs_mount_t *mp,
2584 xfs_sb_field_t field,
David Chinner20b64282007-02-10 18:35:09 +11002585 int min_per_cpu)
David Chinner8d280b92006-03-14 13:13:09 +11002586{
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10002587 uint64_t count, resid;
David Chinner8d280b92006-03-14 13:13:09 +11002588 int weight = num_online_cpus();
David Chinner20b64282007-02-10 18:35:09 +11002589 uint64_t min = (uint64_t)min_per_cpu;
David Chinner8d280b92006-03-14 13:13:09 +11002590
David Chinner8d280b92006-03-14 13:13:09 +11002591 /* disable counter and sync counter */
2592 xfs_icsb_disable_counter(mp, field);
2593
2594 /* update counters - first CPU gets residual*/
2595 switch (field) {
2596 case XFS_SBS_ICOUNT:
2597 count = mp->m_sb.sb_icount;
2598 resid = do_div(count, weight);
David Chinner20b64282007-02-10 18:35:09 +11002599 if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002600 return;
David Chinner8d280b92006-03-14 13:13:09 +11002601 break;
2602 case XFS_SBS_IFREE:
2603 count = mp->m_sb.sb_ifree;
2604 resid = do_div(count, weight);
David Chinner20b64282007-02-10 18:35:09 +11002605 if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002606 return;
David Chinner8d280b92006-03-14 13:13:09 +11002607 break;
2608 case XFS_SBS_FDBLOCKS:
2609 count = mp->m_sb.sb_fdblocks;
2610 resid = do_div(count, weight);
David Chinner20b64282007-02-10 18:35:09 +11002611 if (count < max(min, XFS_ICSB_FDBLK_CNTR_REENABLE(mp)))
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002612 return;
David Chinner8d280b92006-03-14 13:13:09 +11002613 break;
2614 default:
2615 BUG();
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10002616 count = resid = 0; /* quiet, gcc */
David Chinner8d280b92006-03-14 13:13:09 +11002617 break;
2618 }
2619
2620 xfs_icsb_enable_counter(mp, field, count, resid);
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002621}
2622
2623STATIC void
2624xfs_icsb_balance_counter(
2625 xfs_mount_t *mp,
2626 xfs_sb_field_t fields,
2627 int min_per_cpu)
2628{
2629 spin_lock(&mp->m_sb_lock);
2630 xfs_icsb_balance_counter_locked(mp, fields, min_per_cpu);
2631 spin_unlock(&mp->m_sb_lock);
David Chinner8d280b92006-03-14 13:13:09 +11002632}
2633
Christoph Hellwig1b040712010-09-30 02:25:56 +00002634int
David Chinner20b64282007-02-10 18:35:09 +11002635xfs_icsb_modify_counters(
David Chinner8d280b92006-03-14 13:13:09 +11002636 xfs_mount_t *mp,
2637 xfs_sb_field_t field,
David Chinner20f4ebf2007-02-10 18:36:10 +11002638 int64_t delta,
David Chinner20b64282007-02-10 18:35:09 +11002639 int rsvd)
David Chinner8d280b92006-03-14 13:13:09 +11002640{
2641 xfs_icsb_cnts_t *icsbp;
2642 long long lcounter; /* long counter for 64 bit fields */
Christoph Lameter7a9e02d2009-10-03 19:48:23 +09002643 int ret = 0;
David Chinner8d280b92006-03-14 13:13:09 +11002644
David Chinner20b64282007-02-10 18:35:09 +11002645 might_sleep();
David Chinner8d280b92006-03-14 13:13:09 +11002646again:
Christoph Lameter7a9e02d2009-10-03 19:48:23 +09002647 preempt_disable();
2648 icsbp = this_cpu_ptr(mp->m_sb_cnts);
David Chinner20b64282007-02-10 18:35:09 +11002649
2650 /*
2651 * if the counter is disabled, go to slow path
2652 */
David Chinner8d280b92006-03-14 13:13:09 +11002653 if (unlikely(xfs_icsb_counter_disabled(mp, field)))
2654 goto slow_path;
David Chinner20b64282007-02-10 18:35:09 +11002655 xfs_icsb_lock_cntr(icsbp);
2656 if (unlikely(xfs_icsb_counter_disabled(mp, field))) {
2657 xfs_icsb_unlock_cntr(icsbp);
2658 goto slow_path;
2659 }
David Chinner8d280b92006-03-14 13:13:09 +11002660
2661 switch (field) {
2662 case XFS_SBS_ICOUNT:
2663 lcounter = icsbp->icsb_icount;
2664 lcounter += delta;
2665 if (unlikely(lcounter < 0))
David Chinner20b64282007-02-10 18:35:09 +11002666 goto balance_counter;
David Chinner8d280b92006-03-14 13:13:09 +11002667 icsbp->icsb_icount = lcounter;
2668 break;
2669
2670 case XFS_SBS_IFREE:
2671 lcounter = icsbp->icsb_ifree;
2672 lcounter += delta;
2673 if (unlikely(lcounter < 0))
David Chinner20b64282007-02-10 18:35:09 +11002674 goto balance_counter;
David Chinner8d280b92006-03-14 13:13:09 +11002675 icsbp->icsb_ifree = lcounter;
2676 break;
2677
2678 case XFS_SBS_FDBLOCKS:
2679 BUG_ON((mp->m_resblks - mp->m_resblks_avail) != 0);
2680
David Chinner4be536d2006-09-07 14:26:50 +10002681 lcounter = icsbp->icsb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
David Chinner8d280b92006-03-14 13:13:09 +11002682 lcounter += delta;
2683 if (unlikely(lcounter < 0))
David Chinner20b64282007-02-10 18:35:09 +11002684 goto balance_counter;
David Chinner4be536d2006-09-07 14:26:50 +10002685 icsbp->icsb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
David Chinner8d280b92006-03-14 13:13:09 +11002686 break;
2687 default:
2688 BUG();
2689 break;
2690 }
David Chinner01e1b692006-03-14 13:29:16 +11002691 xfs_icsb_unlock_cntr(icsbp);
Christoph Lameter7a9e02d2009-10-03 19:48:23 +09002692 preempt_enable();
David Chinner8d280b92006-03-14 13:13:09 +11002693 return 0;
2694
David Chinner8d280b92006-03-14 13:13:09 +11002695slow_path:
Christoph Lameter7a9e02d2009-10-03 19:48:23 +09002696 preempt_enable();
David Chinner20b64282007-02-10 18:35:09 +11002697
2698 /*
2699 * serialise with a mutex so we don't burn lots of cpu on
2700 * the superblock lock. We still need to hold the superblock
2701 * lock, however, when we modify the global structures.
2702 */
David Chinner03135cf2007-02-10 18:35:15 +11002703 xfs_icsb_lock(mp);
David Chinner20b64282007-02-10 18:35:09 +11002704
2705 /*
2706 * Now running atomically.
2707 *
2708 * If the counter is enabled, someone has beaten us to rebalancing.
2709 * Drop the lock and try again in the fast path....
2710 */
2711 if (!(xfs_icsb_counter_disabled(mp, field))) {
David Chinner03135cf2007-02-10 18:35:15 +11002712 xfs_icsb_unlock(mp);
David Chinner20b64282007-02-10 18:35:09 +11002713 goto again;
2714 }
2715
2716 /*
2717 * The counter is currently disabled. Because we are
2718 * running atomically here, we know a rebalance cannot
2719 * be in progress. Hence we can go straight to operating
2720 * on the global superblock. We do not call xfs_mod_incore_sb()
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002721 * here even though we need to get the m_sb_lock. Doing so
David Chinner20b64282007-02-10 18:35:09 +11002722 * will cause us to re-enter this function and deadlock.
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002723 * Hence we get the m_sb_lock ourselves and then call
David Chinner20b64282007-02-10 18:35:09 +11002724 * xfs_mod_incore_sb_unlocked() as the unlocked path operates
2725 * directly on the global counters.
2726 */
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002727 spin_lock(&mp->m_sb_lock);
David Chinner20b64282007-02-10 18:35:09 +11002728 ret = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
Eric Sandeen3685c2a2007-10-11 17:42:32 +10002729 spin_unlock(&mp->m_sb_lock);
David Chinner20b64282007-02-10 18:35:09 +11002730
2731 /*
2732 * Now that we've modified the global superblock, we
2733 * may be able to re-enable the distributed counters
2734 * (e.g. lots of space just got freed). After that
2735 * we are done.
2736 */
2737 if (ret != ENOSPC)
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002738 xfs_icsb_balance_counter(mp, field, 0);
David Chinner03135cf2007-02-10 18:35:15 +11002739 xfs_icsb_unlock(mp);
David Chinner20b64282007-02-10 18:35:09 +11002740 return ret;
2741
2742balance_counter:
David Chinner01e1b692006-03-14 13:29:16 +11002743 xfs_icsb_unlock_cntr(icsbp);
Christoph Lameter7a9e02d2009-10-03 19:48:23 +09002744 preempt_enable();
David Chinner8d280b92006-03-14 13:13:09 +11002745
David Chinner20b64282007-02-10 18:35:09 +11002746 /*
2747 * We may have multiple threads here if multiple per-cpu
2748 * counters run dry at the same time. This will mean we can
2749 * do more balances than strictly necessary but it is not
2750 * the common slowpath case.
2751 */
David Chinner03135cf2007-02-10 18:35:15 +11002752 xfs_icsb_lock(mp);
David Chinner8d280b92006-03-14 13:13:09 +11002753
David Chinner20b64282007-02-10 18:35:09 +11002754 /*
2755 * running atomically.
2756 *
2757 * This will leave the counter in the correct state for future
2758 * accesses. After the rebalance, we simply try again and our retry
2759 * will either succeed through the fast path or slow path without
2760 * another balance operation being required.
2761 */
Christoph Hellwig45af6c62008-04-22 17:34:44 +10002762 xfs_icsb_balance_counter(mp, field, delta);
David Chinner03135cf2007-02-10 18:35:15 +11002763 xfs_icsb_unlock(mp);
David Chinner20b64282007-02-10 18:35:09 +11002764 goto again;
David Chinner8d280b92006-03-14 13:13:09 +11002765}
2766
David Chinner8d280b92006-03-14 13:13:09 +11002767#endif