]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/xfs/xfs_mount.c
xfs: reshuffle dir2 definitions around for userspace
[mirror_ubuntu-artful-kernel.git] / fs / xfs / xfs_mount.c
CommitLineData
1da177e4 1/*
7b718769
NS
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * 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.
1da177e4 13 *
7b718769
NS
14 * 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
1da177e4 17 */
1da177e4 18#include "xfs.h"
a844f451 19#include "xfs_fs.h"
6ca1c906 20#include "xfs_format.h"
a844f451 21#include "xfs_bit.h"
1da177e4 22#include "xfs_log.h"
a844f451 23#include "xfs_inum.h"
1da177e4 24#include "xfs_trans.h"
211e4d43 25#include "xfs_trans_priv.h"
1da177e4
LT
26#include "xfs_sb.h"
27#include "xfs_ag.h"
1da177e4 28#include "xfs_mount.h"
2b9ab5ab
DC
29#include "xfs_da_btree.h"
30#include "xfs_dir2_format.h"
31#include "xfs_dir2.h"
1da177e4 32#include "xfs_bmap_btree.h"
a844f451 33#include "xfs_alloc_btree.h"
1da177e4 34#include "xfs_ialloc_btree.h"
1da177e4
LT
35#include "xfs_dinode.h"
36#include "xfs_inode.h"
a844f451
NS
37#include "xfs_btree.h"
38#include "xfs_ialloc.h"
1da177e4
LT
39#include "xfs_alloc.h"
40#include "xfs_rtalloc.h"
41#include "xfs_bmap.h"
42#include "xfs_error.h"
1da177e4
LT
43#include "xfs_quota.h"
44#include "xfs_fsops.h"
43355099 45#include "xfs_utils.h"
0b1b213f 46#include "xfs_trace.h"
6d8b79cf 47#include "xfs_icache.h"
04a1e6c5
DC
48#include "xfs_cksum.h"
49#include "xfs_buf_item.h"
0b1b213f 50
1da177e4 51
8d280b98 52#ifdef HAVE_PERCPU_SB
20f4ebf2 53STATIC void xfs_icsb_balance_counter(xfs_mount_t *, xfs_sb_field_t,
45af6c6d
CH
54 int);
55STATIC void xfs_icsb_balance_counter_locked(xfs_mount_t *, xfs_sb_field_t,
56 int);
36fbe6e6 57STATIC void xfs_icsb_disable_counter(xfs_mount_t *, xfs_sb_field_t);
8d280b98
DC
58#else
59
45af6c6d
CH
60#define xfs_icsb_balance_counter(mp, a, b) do { } while (0)
61#define xfs_icsb_balance_counter_locked(mp, a, b) do { } while (0)
8d280b98
DC
62#endif
63
1df84c93 64static const struct {
8d280b98
DC
65 short offset;
66 short type; /* 0 = integer
67 * 1 = binary / string (no translation)
68 */
1da177e4
LT
69} 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 },
ee1c0908 115 { offsetof(xfs_sb_t, sb_bad_features2), 0 },
04a1e6c5
DC
116 { 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 },
e721f504 119 { offsetof(xfs_sb_t, sb_features_log_incompat), 0 },
04a1e6c5 120 { offsetof(xfs_sb_t, sb_crc), 0 },
e721f504 121 { offsetof(xfs_sb_t, sb_pad), 0 },
04a1e6c5
DC
122 { offsetof(xfs_sb_t, sb_pquotino), 0 },
123 { offsetof(xfs_sb_t, sb_lsn), 0 },
1da177e4
LT
124 { sizeof(xfs_sb_t), 0 }
125};
126
27174203
CH
127static 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)) {
0b932ccc 146 xfs_warn(mp, "Filesystem has nil UUID - can't mount");
27174203
CH
147 return XFS_ERROR(EINVAL);
148 }
149
150 mutex_lock(&xfs_uuid_table_mutex);
151 for (i = 0, hole = -1; i < xfs_uuid_table_size; i++) {
152 if (uuid_is_nil(&xfs_uuid_table[i])) {
153 hole = i;
154 continue;
155 }
156 if (uuid_equal(uuid, &xfs_uuid_table[i]))
157 goto out_duplicate;
158 }
159
160 if (hole < 0) {
161 xfs_uuid_table = kmem_realloc(xfs_uuid_table,
162 (xfs_uuid_table_size + 1) * sizeof(*xfs_uuid_table),
163 xfs_uuid_table_size * sizeof(*xfs_uuid_table),
164 KM_SLEEP);
165 hole = xfs_uuid_table_size++;
166 }
167 xfs_uuid_table[hole] = *uuid;
168 mutex_unlock(&xfs_uuid_table_mutex);
169
170 return 0;
171
172 out_duplicate:
173 mutex_unlock(&xfs_uuid_table_mutex);
021000e5 174 xfs_warn(mp, "Filesystem has duplicate UUID %pU - can't mount", uuid);
27174203
CH
175 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
0fa800fb
DC
202/*
203 * Reference counting access wrappers to the perag structures.
e176579e
DC
204 * Because we never free per-ag structures, the only thing we
205 * have to protect against changes is the tree structure itself.
0fa800fb
DC
206 */
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
e176579e 213 rcu_read_lock();
0fa800fb
DC
214 pag = radix_tree_lookup(&mp->m_perag_tree, agno);
215 if (pag) {
216 ASSERT(atomic_read(&pag->pag_ref) >= 0);
0fa800fb
DC
217 ref = atomic_inc_return(&pag->pag_ref);
218 }
e176579e 219 rcu_read_unlock();
0fa800fb
DC
220 trace_xfs_perag_get(mp, agno, ref, _RET_IP_);
221 return pag;
222}
223
65d0f205
DC
224/*
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
0fa800fb
DC
250void
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
e176579e
DC
260STATIC 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
1da177e4 270/*
e176579e 271 * Free up the per-ag resources associated with the mount structure.
1da177e4 272 */
c962fb79 273STATIC void
ff4f038c 274xfs_free_perag(
745f6919 275 xfs_mount_t *mp)
1da177e4 276{
1c1c6ebc
DC
277 xfs_agnumber_t agno;
278 struct xfs_perag *pag;
279
280 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);
e176579e 284 ASSERT(pag);
f83282a8 285 ASSERT(atomic_read(&pag->pag_ref) == 0);
e176579e 286 call_rcu(&pag->rcu_head, __xfs_free_perag);
1da177e4 287 }
1da177e4
LT
288}
289
4cc929ee
NS
290/*
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)
657a4cff 304 return EFBIG;
4cc929ee
NS
305#else /* Limited by UINT_MAX of sectors */
306 if (nblocks << (sbp->sb_blocklog - BBSHIFT) > UINT_MAX)
657a4cff 307 return EFBIG;
4cc929ee
NS
308#endif
309 return 0;
310}
1da177e4
LT
311
312/*
313 * Check the validity of the SB found.
314 */
315STATIC int
316xfs_mount_validate_sb(
317 xfs_mount_t *mp,
764d1f89 318 xfs_sb_t *sbp,
34510185
DC
319 bool check_inprogress,
320 bool check_version)
1da177e4 321{
af34e09d 322
1da177e4
LT
323 /*
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) {
98021821 331 xfs_warn(mp, "bad magic number");
1da177e4
LT
332 return XFS_ERROR(EWRONGFS);
333 }
334
04a1e6c5 335
62118709 336 if (!xfs_sb_good_version(sbp)) {
98021821 337 xfs_warn(mp, "bad version");
1da177e4
LT
338 return XFS_ERROR(EWRONGFS);
339 }
340
04a1e6c5 341 /*
e721f504 342 * Version 5 superblock feature mask validation. Reject combinations the
34510185
DC
343 * kernel cannot support up front before checking anything else. For
344 * write validation, we don't need to check feature masks.
04a1e6c5 345 */
34510185 346 if (check_version && XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5) {
04a1e6c5 347 xfs_alert(mp,
e721f504
DC
348"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 }
04a1e6c5
DC
382 }
383
d892d586
CS
384 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
1da177e4
LT
397 if (unlikely(
398 sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
98021821 399 xfs_warn(mp,
af34e09d
DC
400 "filesystem is marked as having an external log; "
401 "specify logdev on the mount command line.");
764d1f89 402 return XFS_ERROR(EINVAL);
1da177e4
LT
403 }
404
405 if (unlikely(
406 sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
98021821 407 xfs_warn(mp,
af34e09d
DC
408 "filesystem is marked as having an internal log; "
409 "do not specify logdev on the mount command line.");
764d1f89 410 return XFS_ERROR(EINVAL);
1da177e4
LT
411 }
412
413 /*
c0e090ce 414 * More sanity checking. Most of these were stolen directly from
1da177e4
LT
415 * 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 ||
2ac00af7 423 sbp->sb_sectsize != (1 << sbp->sb_sectlog) ||
1da177e4
LT
424 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 ||
2ac00af7 428 sbp->sb_blocksize != (1 << sbp->sb_blocklog) ||
1da177e4
LT
429 sbp->sb_inodesize < XFS_DINODE_MIN_SIZE ||
430 sbp->sb_inodesize > XFS_DINODE_MAX_SIZE ||
9f989c94
NS
431 sbp->sb_inodelog < XFS_DINODE_MIN_LOG ||
432 sbp->sb_inodelog > XFS_DINODE_MAX_LOG ||
2ac00af7 433 sbp->sb_inodesize != (1 << sbp->sb_inodelog) ||
9f989c94 434 (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) ||
1da177e4
LT
435 (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
436 (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
c0e090ce
ES
437 (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))) {
98021821 441 XFS_CORRUPTION_ERROR("SB sanity check failed",
c0e090ce 442 XFS_ERRLEVEL_LOW, mp, sbp);
1da177e4
LT
443 return XFS_ERROR(EFSCORRUPTED);
444 }
445
2edbddd5
LM
446 /*
447 * Until this is fixed only page-sized or smaller data blocks work.
448 */
449 if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
98021821 450 xfs_warn(mp,
af34e09d
DC
451 "File system with blocksize %d bytes. "
452 "Only pagesize (%ld) or less will currently work.",
453 sbp->sb_blocksize, PAGE_SIZE);
2edbddd5
LM
454 return XFS_ERROR(ENOSYS);
455 }
456
1a5902c5
CH
457 /*
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:
98021821 467 xfs_warn(mp, "inode size of %d bytes not supported",
af34e09d 468 sbp->sb_inodesize);
1a5902c5
CH
469 return XFS_ERROR(ENOSYS);
470 }
471
4cc929ee
NS
472 if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) ||
473 xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) {
98021821 474 xfs_warn(mp,
af34e09d 475 "file system too large to be mounted on this system.");
657a4cff 476 return XFS_ERROR(EFBIG);
1da177e4
LT
477 }
478
98021821
DC
479 if (check_inprogress && sbp->sb_inprogress) {
480 xfs_warn(mp, "Offline file system operation in progress!");
1da177e4
LT
481 return XFS_ERROR(EFSCORRUPTED);
482 }
483
de20614b
NS
484 /*
485 * Version 1 directory format has never worked on Linux.
486 */
62118709 487 if (unlikely(!xfs_sb_version_hasdirv2(sbp))) {
98021821 488 xfs_warn(mp, "file system using version 1 directory format");
de20614b
NS
489 return XFS_ERROR(ENOSYS);
490 }
491
1da177e4
LT
492 return 0;
493}
494
1c1c6ebc 495int
c11e2c36 496xfs_initialize_perag(
c11e2c36 497 xfs_mount_t *mp,
1c1c6ebc
DC
498 xfs_agnumber_t agcount,
499 xfs_agnumber_t *maxagi)
1da177e4 500{
2d2194f6 501 xfs_agnumber_t index;
8b26c582 502 xfs_agnumber_t first_initialised = 0;
1da177e4
LT
503 xfs_perag_t *pag;
504 xfs_agino_t agino;
505 xfs_ino_t ino;
506 xfs_sb_t *sbp = &mp->m_sb;
8b26c582 507 int error = -ENOMEM;
1da177e4 508
1c1c6ebc
DC
509 /*
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 }
8b26c582
DC
520 if (!first_initialised)
521 first_initialised = index;
fb3b504a 522
1c1c6ebc
DC
523 pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL);
524 if (!pag)
8b26c582 525 goto out_unwind;
fb3b504a
CH
526 pag->pag_agno = index;
527 pag->pag_mount = mp;
1a427ab0 528 spin_lock_init(&pag->pag_ici_lock);
69b491c2 529 mutex_init(&pag->pag_ici_reclaim_lock);
fb3b504a 530 INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC);
74f75a0c
DC
531 spin_lock_init(&pag->pag_buf_lock);
532 pag->pag_buf_tree = RB_ROOT;
fb3b504a 533
1c1c6ebc 534 if (radix_tree_preload(GFP_NOFS))
8b26c582 535 goto out_unwind;
fb3b504a 536
1c1c6ebc
DC
537 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);
8b26c582
DC
541 radix_tree_preload_end();
542 error = -EEXIST;
543 goto out_unwind;
1c1c6ebc
DC
544 }
545 spin_unlock(&mp->m_perag_lock);
546 radix_tree_preload_end();
547 }
548
fb3b504a
CH
549 /*
550 * If we mount with the inode64 option, or no inode overflows
551 * the legacy 32-bit address space clear the inode32 option.
1da177e4 552 */
fb3b504a
CH
553 agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
554 ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
555
556 if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) && ino > XFS_MAXINUMBER_32)
1da177e4 557 mp->m_flags |= XFS_MOUNT_32BITINODES;
fb3b504a 558 else
1da177e4 559 mp->m_flags &= ~XFS_MOUNT_32BITINODES;
1da177e4 560
2d2194f6
CM
561 if (mp->m_flags & XFS_MOUNT_32BITINODES)
562 index = xfs_set_inode32(mp);
563 else
564 index = xfs_set_inode64(mp);
fb3b504a 565
1c1c6ebc
DC
566 if (maxagi)
567 *maxagi = index;
568 return 0;
8b26c582
DC
569
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;
1da177e4
LT
577}
578
83e782e1
CS
579static void
580xfs_sb_quota_from_disk(struct xfs_sb *sbp)
581{
01026297
CS
582 /*
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
d892d586
CS
600 /*
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
83e782e1
CS
607 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);
d892d586
CS
614
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 }
83e782e1
CS
626}
627
2bdf7cd0
CH
628void
629xfs_sb_from_disk(
98021821 630 struct xfs_sb *to,
2bdf7cd0
CH
631 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);
ee1c0908 678 to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
04a1e6c5
DC
679 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);
e721f504
DC
682 to->sb_features_log_incompat =
683 be32_to_cpu(from->sb_features_log_incompat);
684 to->sb_pad = 0;
04a1e6c5
DC
685 to->sb_pquotino = be64_to_cpu(from->sb_pquotino);
686 to->sb_lsn = be64_to_cpu(from->sb_lsn);
2bdf7cd0
CH
687}
688
83e782e1
CS
689static 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
d892d586
CS
697 /*
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
83e782e1
CS
704 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 }
d892d586
CS
723
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);
83e782e1
CS
738}
739
1da177e4 740/*
2bdf7cd0 741 * Copy in core superblock to ondisk one.
1da177e4 742 *
2bdf7cd0 743 * The fields argument is mask of superblock fields to copy.
1da177e4
LT
744 */
745void
2bdf7cd0
CH
746xfs_sb_to_disk(
747 xfs_dsb_t *to,
748 xfs_sb_t *from,
1da177e4
LT
749 __int64_t fields)
750{
2bdf7cd0
CH
751 xfs_caddr_t to_ptr = (xfs_caddr_t)to;
752 xfs_caddr_t from_ptr = (xfs_caddr_t)from;
1da177e4
LT
753 xfs_sb_field_t f;
754 int first;
755 int size;
756
1da177e4 757 ASSERT(fields);
1da177e4
LT
758 if (!fields)
759 return;
760
83e782e1 761 xfs_sb_quota_to_disk(to, from, &fields);
1da177e4
LT
762 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) {
2bdf7cd0 770 memcpy(to_ptr + first, from_ptr + first, size);
1da177e4
LT
771 } else {
772 switch (size) {
773 case 2:
2bdf7cd0
CH
774 *(__be16 *)(to_ptr + first) =
775 cpu_to_be16(*(__u16 *)(from_ptr + first));
1da177e4
LT
776 break;
777 case 4:
2bdf7cd0
CH
778 *(__be32 *)(to_ptr + first) =
779 cpu_to_be32(*(__u32 *)(from_ptr + first));
1da177e4
LT
780 break;
781 case 8:
2bdf7cd0
CH
782 *(__be64 *)(to_ptr + first) =
783 cpu_to_be64(*(__u64 *)(from_ptr + first));
1da177e4
LT
784 break;
785 default:
786 ASSERT(0);
787 }
788 }
789
790 fields &= ~(1LL << f);
791 }
792}
793
04a1e6c5 794static int
612cfbfe 795xfs_sb_verify(
34510185
DC
796 struct xfs_buf *bp,
797 bool check_version)
98021821
DC
798{
799 struct xfs_mount *mp = bp->b_target->bt_mount;
800 struct xfs_sb sb;
98021821
DC
801
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 */
34510185
DC
808 return xfs_mount_validate_sb(mp, &sb, bp->b_bn == XFS_SB_DADDR,
809 check_version);
612cfbfe
DC
810}
811
04a1e6c5
DC
812/*
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 */
1813dd64 819static void
612cfbfe
DC
820xfs_sb_read_verify(
821 struct xfs_buf *bp)
822{
04a1e6c5
DC
823 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 }
34510185 842 error = xfs_sb_verify(bp, true);
04a1e6c5
DC
843
844out_error:
845 if (error) {
846 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
847 xfs_buf_ioerror(bp, error);
848 }
98021821
DC
849}
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 */
1813dd64 857static void
98021821
DC
858xfs_sb_quiet_read_verify(
859 struct xfs_buf *bp)
860{
04a1e6c5 861 struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp);
98021821 862
98021821 863
04a1e6c5 864 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) {
98021821
DC
865 /* XFS filesystem, verify noisily! */
866 xfs_sb_read_verify(bp);
867 return;
868 }
869 /* quietly fail */
aeb4f20a 870 xfs_buf_ioerror(bp, EWRONGFS);
98021821
DC
871}
872
1813dd64
DC
873static void
874xfs_sb_write_verify(
04a1e6c5 875 struct xfs_buf *bp)
1813dd64 876{
04a1e6c5
DC
877 struct xfs_mount *mp = bp->b_target->bt_mount;
878 struct xfs_buf_log_item *bip = bp->b_fspriv;
879 int error;
880
34510185 881 error = xfs_sb_verify(bp, false);
04a1e6c5
DC
882 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));
1813dd64
DC
896}
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
1da177e4
LT
908/*
909 * xfs_readsb
910 *
911 * Does the initial read of the superblock.
912 */
913int
764d1f89 914xfs_readsb(xfs_mount_t *mp, int flags)
1da177e4
LT
915{
916 unsigned int sector_size;
04a1e6c5
DC
917 struct xfs_buf *bp;
918 struct xfs_sb *sbp = &mp->m_sb;
1da177e4 919 int error;
af34e09d 920 int loud = !(flags & XFS_MFSI_QUIET);
1da177e4
LT
921
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);
26af6552
DC
931
932reread:
e70b73f8 933 bp = xfs_buf_read_uncached(mp->m_ddev_targp, XFS_SB_DADDR,
98021821 934 BTOBB(sector_size), 0,
1813dd64
DC
935 loud ? &xfs_sb_buf_ops
936 : &xfs_sb_quiet_buf_ops);
26af6552 937 if (!bp) {
af34e09d
DC
938 if (loud)
939 xfs_warn(mp, "SB buffer read failed");
26af6552 940 return EIO;
1da177e4 941 }
eab4e633
DC
942 if (bp->b_error) {
943 error = bp->b_error;
944 if (loud)
e721f504 945 xfs_warn(mp, "SB validate failed with error %d.", error);
eab4e633
DC
946 goto release_buf;
947 }
1da177e4
LT
948
949 /*
950 * Initialize the mount structure from the superblock.
1da177e4 951 */
98021821 952 xfs_sb_from_disk(&mp->m_sb, XFS_BUF_TO_SBP(bp));
1da177e4 953
83e782e1 954 xfs_sb_quota_from_disk(&mp->m_sb);
1da177e4
LT
955 /*
956 * We must be able to do sector-sized and sector-aligned IO.
957 */
04a1e6c5 958 if (sector_size > sbp->sb_sectsize) {
af34e09d
DC
959 if (loud)
960 xfs_warn(mp, "device supports %u byte sectors (not %u)",
04a1e6c5 961 sector_size, sbp->sb_sectsize);
1da177e4 962 error = ENOSYS;
26af6552 963 goto release_buf;
1da177e4
LT
964 }
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 */
04a1e6c5 970 if (sector_size < sbp->sb_sectsize) {
1da177e4 971 xfs_buf_relse(bp);
04a1e6c5 972 sector_size = sbp->sb_sectsize;
26af6552 973 goto reread;
1da177e4
LT
974 }
975
5478eead
LM
976 /* Initialize per-cpu counters */
977 xfs_icsb_reinit_counters(mp);
8d280b98 978
04a1e6c5
DC
979 /* no need to be quiet anymore, so reset the buf ops */
980 bp->b_ops = &xfs_sb_buf_ops;
981
1da177e4 982 mp->m_sb_bp = bp;
26af6552 983 xfs_buf_unlock(bp);
1da177e4
LT
984 return 0;
985
26af6552
DC
986release_buf:
987 xfs_buf_relse(bp);
1da177e4
LT
988 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 */
ba0f32d4 999STATIC void
1da177e4
LT
1000xfs_mount_common(xfs_mount_t *mp, xfs_sb_t *sbp)
1001{
1da177e4 1002 mp->m_agfrotor = mp->m_agirotor = 0;
007c61c6 1003 spin_lock_init(&mp->m_agirotor_lock);
1da177e4
LT
1004 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;
1da177e4
LT
1010 mp->m_blockmask = sbp->sb_blocksize - 1;
1011 mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
1012 mp->m_blockwmask = mp->m_blockwsize - 1;
1da177e4 1013
60197e8d
CH
1014 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;
1da177e4
LT
1028
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}
92821e2b
DC
1034
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;
92821e2b
DC
1055
1056 for (index = 0; index < agcount; index++) {
1057 /*
1058 * read the agf, then the agi. This gets us
9da096fd 1059 * all the information we need and populates the
92821e2b
DC
1060 * 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;
44b56e0a 1069 pag = xfs_perag_get(mp, index);
92821e2b
DC
1070 ifree += pag->pagi_freecount;
1071 ialloc += pag->pagi_count;
1072 bfree += pag->pagf_freeblks;
1073 bfreelst += pag->pagf_flcount;
1074 btree += pag->pagf_btreeblks;
44b56e0a 1075 xfs_perag_put(pag);
92821e2b
DC
1076 }
1077 /*
1078 * Overwrite incore superblock counters with just-read data
1079 */
3685c2a1 1080 spin_lock(&mp->m_sb_lock);
92821e2b
DC
1081 sbp->sb_ifree = ifree;
1082 sbp->sb_icount = ialloc;
1083 sbp->sb_fdblocks = bfree + bfreelst + btree;
3685c2a1 1084 spin_unlock(&mp->m_sb_lock);
92821e2b
DC
1085
1086 /* Fixup the per-cpu counters as well. */
1087 xfs_icsb_reinit_counters(mp);
1088
1089 return 0;
1090}
1091
1da177e4 1092/*
0771fb45 1093 * Update alignment values based on mount options and sb values
1da177e4 1094 */
0771fb45 1095STATIC int
7884bc86 1096xfs_update_alignment(xfs_mount_t *mp)
1da177e4 1097{
1da177e4 1098 xfs_sb_t *sbp = &(mp->m_sb);
1da177e4 1099
4249023a 1100 if (mp->m_dalign) {
1da177e4
LT
1101 /*
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)) {
39a45d84
JL
1107 xfs_warn(mp,
1108 "alignment check failed: sunit/swidth vs. blocksize(%d)",
1109 sbp->sb_blocksize);
1110 return XFS_ERROR(EINVAL);
1da177e4
LT
1111 } 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)) {
53487786 1117 xfs_warn(mp,
39a45d84
JL
1118 "alignment check failed: sunit/swidth vs. agsize(%d)",
1119 sbp->sb_agblocks);
1120 return XFS_ERROR(EINVAL);
1da177e4
LT
1121 } else if (mp->m_dalign) {
1122 mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
1123 } else {
39a45d84
JL
1124 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);
1da177e4
LT
1128 }
1129 }
1130
1131 /*
1132 * Update superblock with new values
1133 * and log changes
1134 */
62118709 1135 if (xfs_sb_version_hasdalign(sbp)) {
1da177e4
LT
1136 if (sbp->sb_unit != mp->m_dalign) {
1137 sbp->sb_unit = mp->m_dalign;
7884bc86 1138 mp->m_update_flags |= XFS_SB_UNIT;
1da177e4
LT
1139 }
1140 if (sbp->sb_width != mp->m_swidth) {
1141 sbp->sb_width = mp->m_swidth;
7884bc86 1142 mp->m_update_flags |= XFS_SB_WIDTH;
1da177e4 1143 }
34d7f603
JL
1144 } else {
1145 xfs_warn(mp,
1146 "cannot change alignment: superblock does not support data alignment");
1147 return XFS_ERROR(EINVAL);
1da177e4
LT
1148 }
1149 } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
62118709 1150 xfs_sb_version_hasdalign(&mp->m_sb)) {
1da177e4
LT
1151 mp->m_dalign = sbp->sb_unit;
1152 mp->m_swidth = sbp->sb_width;
1153 }
1154
0771fb45
ES
1155 return 0;
1156}
1da177e4 1157
0771fb45
ES
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;
1da177e4 1166
0771fb45
ES
1167 if (sbp->sb_imax_pct) {
1168 /*
1169 * Make sure the maximum inode count is a multiple
1170 * of the units we allocate inodes in.
1da177e4 1171 */
1da177e4
LT
1172 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;
0771fb45 1177 } else {
1da177e4 1178 mp->m_maxicount = 0;
1da177e4 1179 }
0771fb45
ES
1180}
1181
1182/*
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;
1da177e4 1193
1da177e4
LT
1194 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
1da177e4
LT
1207 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);
0771fb45 1219}
1da177e4 1220
055388a3
DC
1221/*
1222 * 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
0771fb45
ES
1239/*
1240 * Set whether we're using inode alignment.
1241 */
1242STATIC void
1243xfs_set_inoalignment(xfs_mount_t *mp)
1244{
62118709 1245 if (xfs_sb_version_hasalign(&mp->m_sb) &&
1da177e4
LT
1246 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;
0771fb45
ES
1260}
1261
1262/*
1263 * Check that the data (and log if separate) are an ok size.
1264 */
1265STATIC int
4249023a 1266xfs_check_sizes(xfs_mount_t *mp)
0771fb45
ES
1267{
1268 xfs_buf_t *bp;
1269 xfs_daddr_t d;
0771fb45 1270
1da177e4
LT
1271 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) {
0b932ccc 1273 xfs_warn(mp, "filesystem size mismatch detected");
657a4cff 1274 return XFS_ERROR(EFBIG);
1da177e4 1275 }
e70b73f8 1276 bp = xfs_buf_read_uncached(mp->m_ddev_targp,
1922c949 1277 d - XFS_FSS_TO_BB(mp, 1),
c3f8fc73 1278 XFS_FSS_TO_BB(mp, 1), 0, NULL);
1922c949 1279 if (!bp) {
0b932ccc 1280 xfs_warn(mp, "last sector read failed");
1922c949 1281 return EIO;
1da177e4 1282 }
1922c949 1283 xfs_buf_relse(bp);
1da177e4 1284
4249023a 1285 if (mp->m_logdev_targp != mp->m_ddev_targp) {
1da177e4
LT
1286 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) {
0b932ccc 1288 xfs_warn(mp, "log size mismatch detected");
657a4cff 1289 return XFS_ERROR(EFBIG);
1da177e4 1290 }
e70b73f8 1291 bp = xfs_buf_read_uncached(mp->m_logdev_targp,
1922c949 1292 d - XFS_FSB_TO_BB(mp, 1),
c3f8fc73 1293 XFS_FSB_TO_BB(mp, 1), 0, NULL);
1922c949 1294 if (!bp) {
0b932ccc 1295 xfs_warn(mp, "log device read failed");
1922c949 1296 return EIO;
0771fb45 1297 }
1922c949 1298 xfs_buf_relse(bp);
0771fb45
ES
1299 }
1300 return 0;
1301}
1302
7d095257
CH
1303/*
1304 * 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
7d095257 1332 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SBCHANGE);
b0c10b98
JL
1333 error = xfs_trans_reserve(tp, 0, XFS_QM_SBCHANGE_LOG_RES(mp),
1334 0, 0, XFS_DEFAULT_LOG_COUNT);
7d095257
CH
1335 if (error) {
1336 xfs_trans_cancel(tp, 0);
53487786 1337 xfs_alert(mp, "%s: Superblock update failed!", __func__);
7d095257
CH
1338 return error;
1339 }
1340
1341 xfs_mod_sb(tp, XFS_SB_QFLAGS);
1342 return xfs_trans_commit(tp, 0);
1343}
1344
d5db0f97
ES
1345__uint64_t
1346xfs_default_resblks(xfs_mount_t *mp)
1347{
1348 __uint64_t resblks;
1349
1350 /*
8babd8a2
DC
1351 * 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.
d5db0f97
ES
1356 */
1357 resblks = mp->m_sb.sb_dblocks;
1358 do_div(resblks, 20);
8babd8a2 1359 resblks = min_t(__uint64_t, resblks, 8192);
d5db0f97
ES
1360 return resblks;
1361}
1362
0771fb45 1363/*
0771fb45
ES
1364 * 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(
4249023a 1375 xfs_mount_t *mp)
0771fb45
ES
1376{
1377 xfs_sb_t *sbp = &(mp->m_sb);
1378 xfs_inode_t *rip;
0771fb45 1379 __uint64_t resblks;
7d095257
CH
1380 uint quotamount = 0;
1381 uint quotaflags = 0;
0771fb45
ES
1382 int error = 0;
1383
0771fb45
ES
1384 xfs_mount_common(mp, sbp);
1385
ee1c0908 1386 /*
e6957ea4
ES
1387 * 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.
ee1c0908 1392 *
e6957ea4
ES
1393 * 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.
ee1c0908 1401 */
e6957ea4 1402 if (xfs_sb_has_mismatched_features2(sbp)) {
0b932ccc 1403 xfs_warn(mp, "correcting sb_features alignment problem");
ee1c0908 1404 sbp->sb_features2 |= sbp->sb_bad_features2;
e6957ea4 1405 sbp->sb_bad_features2 = sbp->sb_features2;
7884bc86 1406 mp->m_update_flags |= XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2;
e6957ea4
ES
1407
1408 /*
1409 * Re-check for ATTR2 in case it was found in bad_features2
1410 * slot.
1411 */
7c12f296
TS
1412 if (xfs_sb_version_hasattr2(&mp->m_sb) &&
1413 !(mp->m_flags & XFS_MOUNT_NOATTR2))
e6957ea4 1414 mp->m_flags |= XFS_MOUNT_ATTR2;
7c12f296
TS
1415 }
1416
1417 if (xfs_sb_version_hasattr2(&mp->m_sb) &&
1418 (mp->m_flags & XFS_MOUNT_NOATTR2)) {
1419 xfs_sb_version_removeattr2(&mp->m_sb);
7884bc86 1420 mp->m_update_flags |= XFS_SB_FEATURES2;
e6957ea4 1421
7c12f296
TS
1422 /* update sb_versionnum for the clearing of the morebits */
1423 if (!sbp->sb_features2)
7884bc86 1424 mp->m_update_flags |= XFS_SB_VERSIONNUM;
ee1c0908
DC
1425 }
1426
0771fb45
ES
1427 /*
1428 * 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 */
7884bc86 1433 error = xfs_update_alignment(mp);
0771fb45 1434 if (error)
f9057e3d 1435 goto out;
0771fb45
ES
1436
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
27174203
CH
1444 error = xfs_uuid_mount(mp);
1445 if (error)
1446 goto out;
1da177e4 1447
0771fb45
ES
1448 /*
1449 * Set the minimum read and write sizes
1450 */
1451 xfs_set_rw_sizes(mp);
1452
055388a3
DC
1453 /* set the low space thresholds for dynamic preallocation */
1454 xfs_set_low_space_thresholds(mp);
1455
0771fb45
ES
1456 /*
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 */
4249023a 1471 error = xfs_check_sizes(mp);
0771fb45 1472 if (error)
f9057e3d 1473 goto out_remove_uuid;
0771fb45 1474
1da177e4
LT
1475 /*
1476 * Initialize realtime fields in the mount structure
1477 */
0771fb45
ES
1478 error = xfs_rtmount_init(mp);
1479 if (error) {
0b932ccc 1480 xfs_warn(mp, "RT mount failed");
f9057e3d 1481 goto out_remove_uuid;
1da177e4
LT
1482 }
1483
1da177e4
LT
1484 /*
1485 * 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
1da177e4
LT
1490 mp->m_dmevmask = 0; /* not persistent; set after each mount */
1491
f6c2d1fa 1492 xfs_dir_mount(mp);
1da177e4
LT
1493
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
1da177e4
LT
1504 /*
1505 * Allocate and initialize the per-ag data.
1506 */
1c1c6ebc 1507 spin_lock_init(&mp->m_perag_lock);
9b98b6f3 1508 INIT_RADIX_TREE(&mp->m_perag_tree, GFP_ATOMIC);
1c1c6ebc
DC
1509 error = xfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi);
1510 if (error) {
0b932ccc 1511 xfs_warn(mp, "Failed per-ag init: %d", error);
f9057e3d 1512 goto out_remove_uuid;
1c1c6ebc 1513 }
1da177e4 1514
f9057e3d 1515 if (!sbp->sb_logblocks) {
0b932ccc 1516 xfs_warn(mp, "no log defined");
f9057e3d
CH
1517 XFS_ERROR_REPORT("xfs_mountfs", XFS_ERRLEVEL_LOW, mp);
1518 error = XFS_ERROR(EFSCORRUPTED);
1519 goto out_free_perag;
1520 }
1521
1da177e4
LT
1522 /*
1523 * log's mount-time initialization. Perform 1st part recovery if needed
1524 */
f9057e3d
CH
1525 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) {
0b932ccc 1529 xfs_warn(mp, "log mount failed");
d4f3512b 1530 goto out_fail_wait;
1da177e4
LT
1531 }
1532
92821e2b
DC
1533 /*
1534 * 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 */
92821e2b
DC
1552 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);
f9057e3d 1556 if (error)
d4f3512b 1557 goto out_fail_wait;
92821e2b 1558 }
f9057e3d 1559
1da177e4
LT
1560 /*
1561 * Get and sanity-check the root inode.
1562 * Save the pointer to it in the mount structure.
1563 */
7b6259e7 1564 error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip);
1da177e4 1565 if (error) {
0b932ccc 1566 xfs_warn(mp, "failed to read root inode");
f9057e3d 1567 goto out_log_dealloc;
1da177e4
LT
1568 }
1569
1570 ASSERT(rip != NULL);
1da177e4 1571
abbede1b 1572 if (unlikely(!S_ISDIR(rip->i_d.di_mode))) {
0b932ccc 1573 xfs_warn(mp, "corrupted root inode %llu: not a directory",
b6574520 1574 (unsigned long long)rip->i_ino);
1da177e4
LT
1575 xfs_iunlock(rip, XFS_ILOCK_EXCL);
1576 XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
1577 mp);
1578 error = XFS_ERROR(EFSCORRUPTED);
f9057e3d 1579 goto out_rele_rip;
1da177e4
LT
1580 }
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 */
0771fb45
ES
1588 error = xfs_rtmount_inodes(mp);
1589 if (error) {
1da177e4
LT
1590 /*
1591 * Free up the root inode.
1592 */
0b932ccc 1593 xfs_warn(mp, "failed to read RT inodes");
f9057e3d 1594 goto out_rele_rip;
1da177e4
LT
1595 }
1596
1597 /*
7884bc86
CH
1598 * 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.
1da177e4 1601 */
7884bc86
CH
1602 if (mp->m_update_flags && !(mp->m_flags & XFS_MOUNT_RDONLY)) {
1603 error = xfs_mount_log_sb(mp, mp->m_update_flags);
e5720eec 1604 if (error) {
0b932ccc 1605 xfs_warn(mp, "failed to write sb changes");
b93b6e43 1606 goto out_rtunmount;
e5720eec
DC
1607 }
1608 }
1da177e4
LT
1609
1610 /*
1611 * Initialise the XFS quota management subsystem for this mount
1612 */
7d095257
CH
1613 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) {
0b932ccc 1626 xfs_notice(mp, "resetting quota flags");
7d095257
CH
1627 error = xfs_mount_reset_sbqflags(mp);
1628 if (error)
1629 return error;
1630 }
1631 }
1da177e4
LT
1632
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 */
4249023a 1638 error = xfs_log_mount_finish(mp);
1da177e4 1639 if (error) {
0b932ccc 1640 xfs_warn(mp, "log mount finish failed");
b93b6e43 1641 goto out_rtunmount;
1da177e4
LT
1642 }
1643
1644 /*
1645 * Complete the quota initialisation, post-log-replay component.
1646 */
7d095257
CH
1647 if (quotamount) {
1648 ASSERT(mp->m_qflags == 0);
1649 mp->m_qflags = quotaflags;
1650
1651 xfs_qm_mount_quotas(mp);
1652 }
1653
84e1e99f
DC
1654 /*
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.
8babd8a2
DC
1661 *
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.
84e1e99f 1664 */
d5db0f97
ES
1665 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
1666 resblks = xfs_default_resblks(mp);
1667 error = xfs_reserve_blocks(mp, &resblks, NULL);
1668 if (error)
0b932ccc
DC
1669 xfs_warn(mp,
1670 "Unable to allocate reserve blocks. Continuing without reserve pool.");
d5db0f97 1671 }
84e1e99f 1672
1da177e4
LT
1673 return 0;
1674
b93b6e43
CH
1675 out_rtunmount:
1676 xfs_rtunmount_inodes(mp);
f9057e3d 1677 out_rele_rip:
43355099 1678 IRELE(rip);
f9057e3d 1679 out_log_dealloc:
21b699c8 1680 xfs_log_unmount(mp);
d4f3512b
DC
1681 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);
f9057e3d 1685 out_free_perag:
ff4f038c 1686 xfs_free_perag(mp);
f9057e3d 1687 out_remove_uuid:
27174203 1688 xfs_uuid_unmount(mp);
f9057e3d 1689 out:
1da177e4
LT
1690 return error;
1691}
1692
1693/*
1da177e4
LT
1694 * This flushes out the inodes,dquots and the superblock, unmounts the
1695 * log and makes sure that incore structures are freed.
1696 */
41b5c2e7
CH
1697void
1698xfs_unmountfs(
1699 struct xfs_mount *mp)
1da177e4 1700{
41b5c2e7
CH
1701 __uint64_t resblks;
1702 int error;
1da177e4 1703
579b62fa
BF
1704 cancel_delayed_work_sync(&mp->m_eofblocks_work);
1705
7d095257 1706 xfs_qm_unmount_quotas(mp);
b93b6e43 1707 xfs_rtunmount_inodes(mp);
77508ec8
CH
1708 IRELE(mp->m_rootip);
1709
641c56fb
DC
1710 /*
1711 * We can potentially deadlock here if we have an inode cluster
9da096fd 1712 * that has been freed has its buffer still pinned in memory because
641c56fb
DC
1713 * 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 */
a14a348b 1720 xfs_log_force(mp, XFS_LOG_SYNC);
c854363e
DC
1721
1722 /*
211e4d43
CH
1723 * Flush all pending changes from the AIL.
1724 */
1725 xfs_ail_push_all_sync(mp->m_ail);
1726
1727 /*
1728 * And reclaim all inodes. At this point there should be no dirty
7e18530b
DC
1729 * 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.
c854363e 1732 */
7e18530b 1733 cancel_delayed_work_sync(&mp->m_reclaim_work);
c854363e 1734 xfs_reclaim_inodes(mp, SYNC_WAIT);
1da177e4 1735
7d095257 1736 xfs_qm_unmount(mp);
a357a121 1737
84e1e99f
DC
1738 /*
1739 * 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
9da096fd 1742 * counters to be absolutely correct on clean unmount.
84e1e99f
DC
1743 *
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;
714082bc
DC
1753 error = xfs_reserve_blocks(mp, &resblks, NULL);
1754 if (error)
0b932ccc 1755 xfs_warn(mp, "Unable to free reserved block pool. "
714082bc
DC
1756 "Freespace may not be correct on next mount.");
1757
adab0f67 1758 error = xfs_log_sbcount(mp);
e5720eec 1759 if (error)
0b932ccc 1760 xfs_warn(mp, "Unable to update superblock counters. "
e5720eec 1761 "Freespace may not be correct on next mount.");
87c7bec7 1762
21b699c8 1763 xfs_log_unmount(mp);
27174203 1764 xfs_uuid_unmount(mp);
1da177e4 1765
1550d0b0 1766#if defined(DEBUG)
0ce4cfd4 1767 xfs_errortag_clearall(mp, 0);
1da177e4 1768#endif
ff4f038c 1769 xfs_free_perag(mp);
1da177e4
LT
1770}
1771
92821e2b
DC
1772int
1773xfs_fs_writable(xfs_mount_t *mp)
1774{
d9457dc0 1775 return !(mp->m_super->s_writers.frozen || XFS_FORCED_SHUTDOWN(mp) ||
bd186aa9 1776 (mp->m_flags & XFS_MOUNT_RDONLY));
92821e2b
DC
1777}
1778
1779/*
b2ce3974
AE
1780 * xfs_log_sbcount
1781 *
adab0f67 1782 * Sync the superblock counters to disk.
b2ce3974
AE
1783 *
1784 * Note this code can be called during the process of freezing, so
adab0f67 1785 * we may need to use the transaction allocator which does not
b2ce3974 1786 * block when the transaction subsystem is in its frozen state.
92821e2b
DC
1787 */
1788int
adab0f67 1789xfs_log_sbcount(xfs_mount_t *mp)
92821e2b
DC
1790{
1791 xfs_trans_t *tp;
1792 int error;
1793
1794 if (!xfs_fs_writable(mp))
1795 return 0;
1796
d4d90b57 1797 xfs_icsb_sync_counters(mp, 0);
92821e2b
DC
1798
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
b2ce3974 1806 tp = _xfs_trans_alloc(mp, XFS_TRANS_SB_COUNT, KM_SLEEP);
e457274b
JL
1807 error = xfs_trans_reserve(tp, 0, XFS_SB_LOG_RES(mp), 0, 0,
1808 XFS_DEFAULT_LOG_COUNT);
92821e2b
DC
1809 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);
adab0f67 1815 xfs_trans_set_sync(tp);
e5720eec
DC
1816 error = xfs_trans_commit(tp, 0);
1817 return error;
92821e2b
DC
1818}
1819
1da177e4
LT
1820/*
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;
1da177e4
LT
1834 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);
1da177e4
LT
1841 first = sizeof(xfs_sb_t);
1842 last = 0;
1843
1844 /* translate/copy */
1845
2bdf7cd0 1846 xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb, fields);
1da177e4
LT
1847
1848 /* find modified range */
587aa0fe
DC
1849 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;
1da177e4
LT
1852
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
04a1e6c5 1857 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
1da177e4
LT
1858 xfs_trans_log_buf(tp, bp, first, last);
1859}
d210a28c 1860
d210a28c 1861
1da177e4
LT
1862/*
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 *
3685c2a1 1869 * The m_sb_lock must be held when this routine is called.
1da177e4 1870 */
d96f8f89 1871STATIC int
20f4ebf2
DC
1872xfs_mod_incore_sb_unlocked(
1873 xfs_mount_t *mp,
1874 xfs_sb_field_t field,
1875 int64_t delta,
1876 int rsvd)
1da177e4
LT
1877{
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);
014c2544 1894 return XFS_ERROR(EINVAL);
1da177e4
LT
1895 }
1896 mp->m_sb.sb_icount = lcounter;
014c2544 1897 return 0;
1da177e4
LT
1898 case XFS_SBS_IFREE:
1899 lcounter = (long long)mp->m_sb.sb_ifree;
1900 lcounter += delta;
1901 if (lcounter < 0) {
1902 ASSERT(0);
014c2544 1903 return XFS_ERROR(EINVAL);
1da177e4
LT
1904 }
1905 mp->m_sb.sb_ifree = lcounter;
014c2544 1906 return 0;
1da177e4 1907 case XFS_SBS_FDBLOCKS:
4be536de
DC
1908 lcounter = (long long)
1909 mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
1da177e4
LT
1910 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 */
1da177e4 1921 lcounter += delta;
8babd8a2
DC
1922 if (lcounter >= 0) {
1923 mp->m_sb.sb_fdblocks = lcounter +
1924 XFS_ALLOC_SET_ASIDE(mp);
1925 return 0;
1926 }
1da177e4 1927
8babd8a2
DC
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);
1da177e4 1934
8babd8a2
DC
1935 lcounter = (long long)mp->m_resblks_avail + delta;
1936 if (lcounter >= 0) {
1937 mp->m_resblks_avail = lcounter;
1938 return 0;
1da177e4 1939 }
8babd8a2
DC
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);
1da177e4
LT
1945 }
1946
4be536de 1947 mp->m_sb.sb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
014c2544 1948 return 0;
1da177e4
LT
1949 case XFS_SBS_FREXTENTS:
1950 lcounter = (long long)mp->m_sb.sb_frextents;
1951 lcounter += delta;
1952 if (lcounter < 0) {
014c2544 1953 return XFS_ERROR(ENOSPC);
1da177e4
LT
1954 }
1955 mp->m_sb.sb_frextents = lcounter;
014c2544 1956 return 0;
1da177e4
LT
1957 case XFS_SBS_DBLOCKS:
1958 lcounter = (long long)mp->m_sb.sb_dblocks;
1959 lcounter += delta;
1960 if (lcounter < 0) {
1961 ASSERT(0);
014c2544 1962 return XFS_ERROR(EINVAL);
1da177e4
LT
1963 }
1964 mp->m_sb.sb_dblocks = lcounter;
014c2544 1965 return 0;
1da177e4
LT
1966 case XFS_SBS_AGCOUNT:
1967 scounter = mp->m_sb.sb_agcount;
1968 scounter += delta;
1969 if (scounter < 0) {
1970 ASSERT(0);
014c2544 1971 return XFS_ERROR(EINVAL);
1da177e4
LT
1972 }
1973 mp->m_sb.sb_agcount = scounter;
014c2544 1974 return 0;
1da177e4
LT
1975 case XFS_SBS_IMAX_PCT:
1976 scounter = mp->m_sb.sb_imax_pct;
1977 scounter += delta;
1978 if (scounter < 0) {
1979 ASSERT(0);
014c2544 1980 return XFS_ERROR(EINVAL);
1da177e4
LT
1981 }
1982 mp->m_sb.sb_imax_pct = scounter;
014c2544 1983 return 0;
1da177e4
LT
1984 case XFS_SBS_REXTSIZE:
1985 scounter = mp->m_sb.sb_rextsize;
1986 scounter += delta;
1987 if (scounter < 0) {
1988 ASSERT(0);
014c2544 1989 return XFS_ERROR(EINVAL);
1da177e4
LT
1990 }
1991 mp->m_sb.sb_rextsize = scounter;
014c2544 1992 return 0;
1da177e4
LT
1993 case XFS_SBS_RBMBLOCKS:
1994 scounter = mp->m_sb.sb_rbmblocks;
1995 scounter += delta;
1996 if (scounter < 0) {
1997 ASSERT(0);
014c2544 1998 return XFS_ERROR(EINVAL);
1da177e4
LT
1999 }
2000 mp->m_sb.sb_rbmblocks = scounter;
014c2544 2001 return 0;
1da177e4
LT
2002 case XFS_SBS_RBLOCKS:
2003 lcounter = (long long)mp->m_sb.sb_rblocks;
2004 lcounter += delta;
2005 if (lcounter < 0) {
2006 ASSERT(0);
014c2544 2007 return XFS_ERROR(EINVAL);
1da177e4
LT
2008 }
2009 mp->m_sb.sb_rblocks = lcounter;
014c2544 2010 return 0;
1da177e4
LT
2011 case XFS_SBS_REXTENTS:
2012 lcounter = (long long)mp->m_sb.sb_rextents;
2013 lcounter += delta;
2014 if (lcounter < 0) {
2015 ASSERT(0);
014c2544 2016 return XFS_ERROR(EINVAL);
1da177e4
LT
2017 }
2018 mp->m_sb.sb_rextents = lcounter;
014c2544 2019 return 0;
1da177e4
LT
2020 case XFS_SBS_REXTSLOG:
2021 scounter = mp->m_sb.sb_rextslog;
2022 scounter += delta;
2023 if (scounter < 0) {
2024 ASSERT(0);
014c2544 2025 return XFS_ERROR(EINVAL);
1da177e4
LT
2026 }
2027 mp->m_sb.sb_rextslog = scounter;
014c2544 2028 return 0;
1da177e4
LT
2029 default:
2030 ASSERT(0);
014c2544 2031 return XFS_ERROR(EINVAL);
1da177e4
LT
2032 }
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
3685c2a1 2038 * is protected by the m_sb_lock. Just use the xfs_mod_incore_sb_unlocked()
1da177e4
LT
2039 * routine to do the work.
2040 */
2041int
20f4ebf2 2042xfs_mod_incore_sb(
96540c78
CH
2043 struct xfs_mount *mp,
2044 xfs_sb_field_t field,
2045 int64_t delta,
2046 int rsvd)
1da177e4 2047{
96540c78 2048 int status;
1da177e4 2049
8d280b98 2050#ifdef HAVE_PERCPU_SB
96540c78 2051 ASSERT(field < XFS_SBS_ICOUNT || field > XFS_SBS_FDBLOCKS);
8d280b98 2052#endif
96540c78
CH
2053 spin_lock(&mp->m_sb_lock);
2054 status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
2055 spin_unlock(&mp->m_sb_lock);
8d280b98 2056
014c2544 2057 return status;
1da177e4
LT
2058}
2059
2060/*
1b040712 2061 * Change more than one field in the in-core superblock structure at a time.
1da177e4 2062 *
1b040712
CH
2063 * 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.
1da177e4
LT
2071 */
2072int
1b040712
CH
2073xfs_mod_incore_sb_batch(
2074 struct xfs_mount *mp,
2075 xfs_mod_sb_t *msb,
2076 uint nmsb,
2077 int rsvd)
1da177e4 2078{
45c51b99 2079 xfs_mod_sb_t *msbp;
1b040712 2080 int error = 0;
1da177e4
LT
2081
2082 /*
1b040712
CH
2083 * 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.
1da177e4 2087 */
3685c2a1 2088 spin_lock(&mp->m_sb_lock);
45c51b99 2089 for (msbp = msb; msbp < (msb + nmsb); msbp++) {
1b040712
CH
2090 ASSERT(msbp->msb_field < XFS_SBS_ICOUNT ||
2091 msbp->msb_field > XFS_SBS_FDBLOCKS);
8d280b98 2092
1b040712
CH
2093 error = xfs_mod_incore_sb_unlocked(mp, msbp->msb_field,
2094 msbp->msb_delta, rsvd);
2095 if (error)
2096 goto unwind;
1da177e4 2097 }
1b040712
CH
2098 spin_unlock(&mp->m_sb_lock);
2099 return 0;
1da177e4 2100
1b040712
CH
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);
1da177e4 2106 }
3685c2a1 2107 spin_unlock(&mp->m_sb_lock);
1b040712 2108 return error;
1da177e4
LT
2109}
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 */
0c842ad4 2120struct xfs_buf *
1da177e4 2121xfs_getsb(
0c842ad4
CH
2122 struct xfs_mount *mp,
2123 int flags)
1da177e4 2124{
0c842ad4 2125 struct xfs_buf *bp = mp->m_sb_bp;
1da177e4 2126
0c842ad4
CH
2127 if (!xfs_buf_trylock(bp)) {
2128 if (flags & XBF_TRYLOCK)
1da177e4 2129 return NULL;
0c842ad4 2130 xfs_buf_lock(bp);
1da177e4 2131 }
0c842ad4 2132
72790aa1 2133 xfs_buf_hold(bp);
1da177e4 2134 ASSERT(XFS_BUF_ISDONE(bp));
014c2544 2135 return bp;
1da177e4
LT
2136}
2137
2138/*
2139 * Used to free the superblock along various error paths.
2140 */
2141void
2142xfs_freesb(
26af6552 2143 struct xfs_mount *mp)
1da177e4 2144{
26af6552 2145 struct xfs_buf *bp = mp->m_sb_bp;
1da177e4 2146
26af6552 2147 xfs_buf_lock(bp);
1da177e4 2148 mp->m_sb_bp = NULL;
26af6552 2149 xfs_buf_relse(bp);
1da177e4
LT
2150}
2151
1da177e4
LT
2152/*
2153 * Used to log changes to the superblock unit and width fields which could
e6957ea4
ES
2154 * be altered by the mount options, as well as any potential sb_features2
2155 * fixup. Only the first superblock is updated.
1da177e4 2156 */
7884bc86 2157int
ee1c0908 2158xfs_mount_log_sb(
1da177e4
LT
2159 xfs_mount_t *mp,
2160 __int64_t fields)
2161{
2162 xfs_trans_t *tp;
e5720eec 2163 int error;
1da177e4 2164
ee1c0908 2165 ASSERT(fields & (XFS_SB_UNIT | XFS_SB_WIDTH | XFS_SB_UUID |
4b166de0
DC
2166 XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2 |
2167 XFS_SB_VERSIONNUM));
1da177e4
LT
2168
2169 tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT);
5166ab06
JL
2170 error = xfs_trans_reserve(tp, 0, XFS_SB_LOG_RES(mp), 0, 0,
2171 XFS_DEFAULT_LOG_COUNT);
e5720eec 2172 if (error) {
1da177e4 2173 xfs_trans_cancel(tp, 0);
e5720eec 2174 return error;
1da177e4
LT
2175 }
2176 xfs_mod_sb(tp, fields);
e5720eec
DC
2177 error = xfs_trans_commit(tp, 0);
2178 return error;
1da177e4 2179}
8d280b98 2180
dda35b8f
CH
2181/*
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))) {
0b932ccc
DC
2193 xfs_notice(mp, "%s required on read-only device.", message);
2194 xfs_notice(mp, "write access unavailable, cannot proceed.");
dda35b8f
CH
2195 return EROFS;
2196 }
2197 return 0;
2198}
8d280b98
DC
2199
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
9da096fd 2223 * when we disable a per-cpu counter, we need to drain its resources back to
8d280b98
DC
2224 * 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 *
3685c2a1 2238 * 1. m_sb_lock before picking up per-cpu locks
8d280b98 2239 * 2. per-cpu locks always picked up via for_each_online_cpu() order
3685c2a1 2240 * 3. accurate counter sync requires m_sb_lock + per cpu locks
8d280b98 2241 * 4. modifying per-cpu counters requires holding per-cpu lock
3685c2a1
ES
2242 * 5. modifying global counters requires holding m_sb_lock
2243 * 6. enabling or disabling a counter requires holding the m_sb_lock
8d280b98
DC
2244 * 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).
e8234a68
DC
2251 */
2252
5a67e4c5 2253#ifdef CONFIG_HOTPLUG_CPU
e8234a68
DC
2254/*
2255 * hot-plug CPU notifier support.
8d280b98 2256 *
5a67e4c5
CS
2257 * 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.
8d280b98 2261 */
e8234a68
DC
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;
e8234a68
DC
2270
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:
8bb78442 2276 case CPU_UP_PREPARE_FROZEN:
e8234a68
DC
2277 /* Easy Case - initialize the area and locks, and
2278 * then rebalance when online does everything else for us. */
01e1b69c 2279 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
e8234a68
DC
2280 break;
2281 case CPU_ONLINE:
8bb78442 2282 case CPU_ONLINE_FROZEN:
03135cf7 2283 xfs_icsb_lock(mp);
45af6c6d
CH
2284 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);
03135cf7 2287 xfs_icsb_unlock(mp);
e8234a68
DC
2288 break;
2289 case CPU_DEAD:
8bb78442 2290 case CPU_DEAD_FROZEN:
e8234a68
DC
2291 /* 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. */
03135cf7 2294 xfs_icsb_lock(mp);
3685c2a1 2295 spin_lock(&mp->m_sb_lock);
e8234a68
DC
2296 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
01e1b69c 2304 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
e8234a68 2305
45af6c6d
CH
2306 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);
3685c2a1 2309 spin_unlock(&mp->m_sb_lock);
03135cf7 2310 xfs_icsb_unlock(mp);
e8234a68
DC
2311 break;
2312 }
2313
2314 return NOTIFY_OK;
2315}
5a67e4c5 2316#endif /* CONFIG_HOTPLUG_CPU */
e8234a68 2317
8d280b98
DC
2318int
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
5a67e4c5 2329#ifdef CONFIG_HOTPLUG_CPU
e8234a68
DC
2330 mp->m_icsb_notifier.notifier_call = xfs_icsb_cpu_notify;
2331 mp->m_icsb_notifier.priority = 0;
5a67e4c5
CS
2332 register_hotcpu_notifier(&mp->m_icsb_notifier);
2333#endif /* CONFIG_HOTPLUG_CPU */
e8234a68 2334
8d280b98
DC
2335 for_each_online_cpu(i) {
2336 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
01e1b69c 2337 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
8d280b98 2338 }
20b64285
DC
2339
2340 mutex_init(&mp->m_icsb_mutex);
2341
8d280b98
DC
2342 /*
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
5478eead
LM
2350void
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;
45af6c6d
CH
2360 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);
5478eead
LM
2363 xfs_icsb_unlock(mp);
2364}
2365
c962fb79 2366void
8d280b98
DC
2367xfs_icsb_destroy_counters(
2368 xfs_mount_t *mp)
2369{
e8234a68 2370 if (mp->m_sb_cnts) {
5a67e4c5 2371 unregister_hotcpu_notifier(&mp->m_icsb_notifier);
8d280b98 2372 free_percpu(mp->m_sb_cnts);
e8234a68 2373 }
03135cf7 2374 mutex_destroy(&mp->m_icsb_mutex);
8d280b98
DC
2375}
2376
b8f82a4a 2377STATIC void
01e1b69c
DC
2378xfs_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
b8f82a4a 2386STATIC void
01e1b69c
DC
2387xfs_icsb_unlock_cntr(
2388 xfs_icsb_cnts_t *icsbp)
2389{
2390 clear_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags);
2391}
2392
8d280b98 2393
b8f82a4a 2394STATIC void
8d280b98
DC
2395xfs_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);
01e1b69c 2403 xfs_icsb_lock_cntr(cntp);
8d280b98
DC
2404 }
2405}
2406
b8f82a4a 2407STATIC void
8d280b98
DC
2408xfs_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);
01e1b69c 2416 xfs_icsb_unlock_cntr(cntp);
8d280b98
DC
2417 }
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
36fbe6e6 2454STATIC void
8d280b98
DC
2455xfs_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
20b64285
DC
2463 /*
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))
36fbe6e6 2472 return;
20b64285 2473
8d280b98
DC
2474 xfs_icsb_lock_all_counters(mp);
2475 if (!test_and_set_bit(field, &mp->m_icsb_counters)) {
2476 /* drain back to superblock */
2477
ce46193b 2478 xfs_icsb_count(mp, &cnt, XFS_ICSB_LAZY_COUNT);
8d280b98
DC
2479 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);
8d280b98
DC
2495}
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
dbcabad1 2532void
d4d90b57 2533xfs_icsb_sync_counters_locked(
8d280b98
DC
2534 xfs_mount_t *mp,
2535 int flags)
2536{
2537 xfs_icsb_cnts_t cnt;
8d280b98 2538
8d280b98
DC
2539 xfs_icsb_count(mp, &cnt, flags);
2540
8d280b98
DC
2541 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;
8d280b98
DC
2547}
2548
2549/*
2550 * Accurate update of per-cpu counters to incore superblock
2551 */
d4d90b57 2552void
8d280b98 2553xfs_icsb_sync_counters(
d4d90b57
CH
2554 xfs_mount_t *mp,
2555 int flags)
8d280b98 2556{
d4d90b57
CH
2557 spin_lock(&mp->m_sb_lock);
2558 xfs_icsb_sync_counters_locked(mp, flags);
2559 spin_unlock(&mp->m_sb_lock);
8d280b98
DC
2560}
2561
2562/*
2563 * Balance and enable/disable counters as necessary.
2564 *
20b64285
DC
2565 * 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.
8d280b98 2576 */
20b64285
DC
2577
2578#define XFS_ICSB_INO_CNTR_REENABLE (uint64_t)64
4be536de 2579#define XFS_ICSB_FDBLK_CNTR_REENABLE(mp) \
20b64285 2580 (uint64_t)(512 + XFS_ALLOC_SET_ASIDE(mp))
8d280b98 2581STATIC void
45af6c6d 2582xfs_icsb_balance_counter_locked(
8d280b98
DC
2583 xfs_mount_t *mp,
2584 xfs_sb_field_t field,
20b64285 2585 int min_per_cpu)
8d280b98 2586{
6fdf8ccc 2587 uint64_t count, resid;
8d280b98 2588 int weight = num_online_cpus();
20b64285 2589 uint64_t min = (uint64_t)min_per_cpu;
8d280b98 2590
8d280b98
DC
2591 /* 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);
20b64285 2599 if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
45af6c6d 2600 return;
8d280b98
DC
2601 break;
2602 case XFS_SBS_IFREE:
2603 count = mp->m_sb.sb_ifree;
2604 resid = do_div(count, weight);
20b64285 2605 if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
45af6c6d 2606 return;
8d280b98
DC
2607 break;
2608 case XFS_SBS_FDBLOCKS:
2609 count = mp->m_sb.sb_fdblocks;
2610 resid = do_div(count, weight);
20b64285 2611 if (count < max(min, XFS_ICSB_FDBLK_CNTR_REENABLE(mp)))
45af6c6d 2612 return;
8d280b98
DC
2613 break;
2614 default:
2615 BUG();
6fdf8ccc 2616 count = resid = 0; /* quiet, gcc */
8d280b98
DC
2617 break;
2618 }
2619
2620 xfs_icsb_enable_counter(mp, field, count, resid);
45af6c6d
CH
2621}
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);
8d280b98
DC
2632}
2633
1b040712 2634int
20b64285 2635xfs_icsb_modify_counters(
8d280b98
DC
2636 xfs_mount_t *mp,
2637 xfs_sb_field_t field,
20f4ebf2 2638 int64_t delta,
20b64285 2639 int rsvd)
8d280b98
DC
2640{
2641 xfs_icsb_cnts_t *icsbp;
2642 long long lcounter; /* long counter for 64 bit fields */
7a9e02d6 2643 int ret = 0;
8d280b98 2644
20b64285 2645 might_sleep();
8d280b98 2646again:
7a9e02d6
CL
2647 preempt_disable();
2648 icsbp = this_cpu_ptr(mp->m_sb_cnts);
20b64285
DC
2649
2650 /*
2651 * if the counter is disabled, go to slow path
2652 */
8d280b98
DC
2653 if (unlikely(xfs_icsb_counter_disabled(mp, field)))
2654 goto slow_path;
20b64285
DC
2655 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 }
8d280b98
DC
2660
2661 switch (field) {
2662 case XFS_SBS_ICOUNT:
2663 lcounter = icsbp->icsb_icount;
2664 lcounter += delta;
2665 if (unlikely(lcounter < 0))
20b64285 2666 goto balance_counter;
8d280b98
DC
2667 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))
20b64285 2674 goto balance_counter;
8d280b98
DC
2675 icsbp->icsb_ifree = lcounter;
2676 break;
2677
2678 case XFS_SBS_FDBLOCKS:
2679 BUG_ON((mp->m_resblks - mp->m_resblks_avail) != 0);
2680
4be536de 2681 lcounter = icsbp->icsb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
8d280b98
DC
2682 lcounter += delta;
2683 if (unlikely(lcounter < 0))
20b64285 2684 goto balance_counter;
4be536de 2685 icsbp->icsb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
8d280b98
DC
2686 break;
2687 default:
2688 BUG();
2689 break;
2690 }
01e1b69c 2691 xfs_icsb_unlock_cntr(icsbp);
7a9e02d6 2692 preempt_enable();
8d280b98
DC
2693 return 0;
2694
8d280b98 2695slow_path:
7a9e02d6 2696 preempt_enable();
8d280b98 2697
20b64285
DC
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 */
03135cf7 2703 xfs_icsb_lock(mp);
20b64285
DC
2704
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))) {
03135cf7 2712 xfs_icsb_unlock(mp);
8d280b98 2713 goto again;
8d280b98
DC
2714 }
2715
20b64285
DC
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()
3685c2a1 2721 * here even though we need to get the m_sb_lock. Doing so
20b64285 2722 * will cause us to re-enter this function and deadlock.
3685c2a1 2723 * Hence we get the m_sb_lock ourselves and then call
20b64285
DC
2724 * xfs_mod_incore_sb_unlocked() as the unlocked path operates
2725 * directly on the global counters.
2726 */
3685c2a1 2727 spin_lock(&mp->m_sb_lock);
8d280b98 2728 ret = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
3685c2a1 2729 spin_unlock(&mp->m_sb_lock);
8d280b98 2730
20b64285
DC
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)
45af6c6d 2738 xfs_icsb_balance_counter(mp, field, 0);
03135cf7 2739 xfs_icsb_unlock(mp);
8d280b98 2740 return ret;
8d280b98 2741
20b64285
DC
2742balance_counter:
2743 xfs_icsb_unlock_cntr(icsbp);
7a9e02d6 2744 preempt_enable();
8d280b98 2745
20b64285
DC
2746 /*
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 */
03135cf7 2752 xfs_icsb_lock(mp);
20b64285
DC
2753
2754 /*
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 */
45af6c6d 2762 xfs_icsb_balance_counter(mp, field, delta);
03135cf7 2763 xfs_icsb_unlock(mp);
20b64285 2764 goto again;
8d280b98 2765}
20b64285 2766
8d280b98 2767#endif