]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/xfs/xfs_mount.c
xfs: remove m_attroffset
[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"
1da177e4 20#include "xfs_types.h"
a844f451 21#include "xfs_bit.h"
1da177e4 22#include "xfs_log.h"
a844f451 23#include "xfs_inum.h"
1da177e4
LT
24#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
1da177e4
LT
27#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
1da177e4 30#include "xfs_bmap_btree.h"
a844f451 31#include "xfs_alloc_btree.h"
1da177e4 32#include "xfs_ialloc_btree.h"
1da177e4 33#include "xfs_dir2_sf.h"
a844f451 34#include "xfs_attr_sf.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_rw.h"
44#include "xfs_quota.h"
45#include "xfs_fsops.h"
43355099 46#include "xfs_utils.h"
1da177e4 47
1da177e4 48STATIC int xfs_uuid_mount(xfs_mount_t *);
ba0f32d4 49STATIC void xfs_unmountfs_wait(xfs_mount_t *);
1da177e4 50
8d280b98
DC
51
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);
8d280b98 57STATIC int xfs_icsb_modify_counters(xfs_mount_t *, xfs_sb_field_t,
20f4ebf2 58 int64_t, int);
36fbe6e6 59STATIC void xfs_icsb_disable_counter(xfs_mount_t *, xfs_sb_field_t);
8d280b98
DC
60
61#else
62
45af6c6d
CH
63#define xfs_icsb_balance_counter(mp, a, b) do { } while (0)
64#define xfs_icsb_balance_counter_locked(mp, a, b) do { } while (0)
8d280b98 65#define xfs_icsb_modify_counters(mp, a, b, c) do { } while (0)
8d280b98
DC
66
67#endif
68
1df84c93 69static const struct {
8d280b98
DC
70 short offset;
71 short type; /* 0 = integer
72 * 1 = binary / string (no translation)
73 */
1da177e4
LT
74} xfs_sb_info[] = {
75 { offsetof(xfs_sb_t, sb_magicnum), 0 },
76 { offsetof(xfs_sb_t, sb_blocksize), 0 },
77 { offsetof(xfs_sb_t, sb_dblocks), 0 },
78 { offsetof(xfs_sb_t, sb_rblocks), 0 },
79 { offsetof(xfs_sb_t, sb_rextents), 0 },
80 { offsetof(xfs_sb_t, sb_uuid), 1 },
81 { offsetof(xfs_sb_t, sb_logstart), 0 },
82 { offsetof(xfs_sb_t, sb_rootino), 0 },
83 { offsetof(xfs_sb_t, sb_rbmino), 0 },
84 { offsetof(xfs_sb_t, sb_rsumino), 0 },
85 { offsetof(xfs_sb_t, sb_rextsize), 0 },
86 { offsetof(xfs_sb_t, sb_agblocks), 0 },
87 { offsetof(xfs_sb_t, sb_agcount), 0 },
88 { offsetof(xfs_sb_t, sb_rbmblocks), 0 },
89 { offsetof(xfs_sb_t, sb_logblocks), 0 },
90 { offsetof(xfs_sb_t, sb_versionnum), 0 },
91 { offsetof(xfs_sb_t, sb_sectsize), 0 },
92 { offsetof(xfs_sb_t, sb_inodesize), 0 },
93 { offsetof(xfs_sb_t, sb_inopblock), 0 },
94 { offsetof(xfs_sb_t, sb_fname[0]), 1 },
95 { offsetof(xfs_sb_t, sb_blocklog), 0 },
96 { offsetof(xfs_sb_t, sb_sectlog), 0 },
97 { offsetof(xfs_sb_t, sb_inodelog), 0 },
98 { offsetof(xfs_sb_t, sb_inopblog), 0 },
99 { offsetof(xfs_sb_t, sb_agblklog), 0 },
100 { offsetof(xfs_sb_t, sb_rextslog), 0 },
101 { offsetof(xfs_sb_t, sb_inprogress), 0 },
102 { offsetof(xfs_sb_t, sb_imax_pct), 0 },
103 { offsetof(xfs_sb_t, sb_icount), 0 },
104 { offsetof(xfs_sb_t, sb_ifree), 0 },
105 { offsetof(xfs_sb_t, sb_fdblocks), 0 },
106 { offsetof(xfs_sb_t, sb_frextents), 0 },
107 { offsetof(xfs_sb_t, sb_uquotino), 0 },
108 { offsetof(xfs_sb_t, sb_gquotino), 0 },
109 { offsetof(xfs_sb_t, sb_qflags), 0 },
110 { offsetof(xfs_sb_t, sb_flags), 0 },
111 { offsetof(xfs_sb_t, sb_shared_vn), 0 },
112 { offsetof(xfs_sb_t, sb_inoalignmt), 0 },
113 { offsetof(xfs_sb_t, sb_unit), 0 },
114 { offsetof(xfs_sb_t, sb_width), 0 },
115 { offsetof(xfs_sb_t, sb_dirblklog), 0 },
116 { offsetof(xfs_sb_t, sb_logsectlog), 0 },
117 { offsetof(xfs_sb_t, sb_logsectsize),0 },
118 { offsetof(xfs_sb_t, sb_logsunit), 0 },
119 { offsetof(xfs_sb_t, sb_features2), 0 },
ee1c0908 120 { offsetof(xfs_sb_t, sb_bad_features2), 0 },
1da177e4
LT
121 { sizeof(xfs_sb_t), 0 }
122};
123
1da177e4
LT
124/*
125 * Free up the resources associated with a mount structure. Assume that
126 * the structure was initially zeroed, so we can tell which fields got
127 * initialized.
128 */
c962fb79 129STATIC void
ff4f038c 130xfs_free_perag(
745f6919 131 xfs_mount_t *mp)
1da177e4 132{
1da177e4
LT
133 if (mp->m_perag) {
134 int agno;
135
136 for (agno = 0; agno < mp->m_maxagi; agno++)
137 if (mp->m_perag[agno].pagb_list)
f0e2d93c
DV
138 kmem_free(mp->m_perag[agno].pagb_list);
139 kmem_free(mp->m_perag);
1da177e4 140 }
1da177e4
LT
141}
142
4cc929ee
NS
143/*
144 * Check size of device based on the (data/realtime) block count.
145 * Note: this check is used by the growfs code as well as mount.
146 */
147int
148xfs_sb_validate_fsb_count(
149 xfs_sb_t *sbp,
150 __uint64_t nblocks)
151{
152 ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
153 ASSERT(sbp->sb_blocklog >= BBSHIFT);
154
155#if XFS_BIG_BLKNOS /* Limited by ULONG_MAX of page cache index */
156 if (nblocks >> (PAGE_CACHE_SHIFT - sbp->sb_blocklog) > ULONG_MAX)
157 return E2BIG;
158#else /* Limited by UINT_MAX of sectors */
159 if (nblocks << (sbp->sb_blocklog - BBSHIFT) > UINT_MAX)
160 return E2BIG;
161#endif
162 return 0;
163}
1da177e4
LT
164
165/*
166 * Check the validity of the SB found.
167 */
168STATIC int
169xfs_mount_validate_sb(
170 xfs_mount_t *mp,
764d1f89
NS
171 xfs_sb_t *sbp,
172 int flags)
1da177e4
LT
173{
174 /*
175 * If the log device and data device have the
176 * same device number, the log is internal.
177 * Consequently, the sb_logstart should be non-zero. If
178 * we have a zero sb_logstart in this case, we may be trying to mount
179 * a volume filesystem in a non-volume manner.
180 */
181 if (sbp->sb_magicnum != XFS_SB_MAGIC) {
764d1f89 182 xfs_fs_mount_cmn_err(flags, "bad magic number");
1da177e4
LT
183 return XFS_ERROR(EWRONGFS);
184 }
185
62118709 186 if (!xfs_sb_good_version(sbp)) {
764d1f89 187 xfs_fs_mount_cmn_err(flags, "bad version");
1da177e4
LT
188 return XFS_ERROR(EWRONGFS);
189 }
190
191 if (unlikely(
192 sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
764d1f89
NS
193 xfs_fs_mount_cmn_err(flags,
194 "filesystem is marked as having an external log; "
195 "specify logdev on the\nmount command line.");
196 return XFS_ERROR(EINVAL);
1da177e4
LT
197 }
198
199 if (unlikely(
200 sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
764d1f89
NS
201 xfs_fs_mount_cmn_err(flags,
202 "filesystem is marked as having an internal log; "
203 "do not specify logdev on\nthe mount command line.");
204 return XFS_ERROR(EINVAL);
1da177e4
LT
205 }
206
207 /*
208 * More sanity checking. These were stolen directly from
209 * xfs_repair.
210 */
211 if (unlikely(
212 sbp->sb_agcount <= 0 ||
213 sbp->sb_sectsize < XFS_MIN_SECTORSIZE ||
214 sbp->sb_sectsize > XFS_MAX_SECTORSIZE ||
215 sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG ||
216 sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG ||
217 sbp->sb_blocksize < XFS_MIN_BLOCKSIZE ||
218 sbp->sb_blocksize > XFS_MAX_BLOCKSIZE ||
219 sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG ||
220 sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
221 sbp->sb_inodesize < XFS_DINODE_MIN_SIZE ||
222 sbp->sb_inodesize > XFS_DINODE_MAX_SIZE ||
9f989c94
NS
223 sbp->sb_inodelog < XFS_DINODE_MIN_LOG ||
224 sbp->sb_inodelog > XFS_DINODE_MAX_LOG ||
225 (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) ||
1da177e4
LT
226 (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
227 (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
e50bd16f 228 (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */))) {
764d1f89 229 xfs_fs_mount_cmn_err(flags, "SB sanity check 1 failed");
1da177e4
LT
230 return XFS_ERROR(EFSCORRUPTED);
231 }
232
233 /*
234 * Sanity check AG count, size fields against data size field
235 */
236 if (unlikely(
237 sbp->sb_dblocks == 0 ||
238 sbp->sb_dblocks >
239 (xfs_drfsbno_t)sbp->sb_agcount * sbp->sb_agblocks ||
240 sbp->sb_dblocks < (xfs_drfsbno_t)(sbp->sb_agcount - 1) *
241 sbp->sb_agblocks + XFS_MIN_AG_BLOCKS)) {
764d1f89 242 xfs_fs_mount_cmn_err(flags, "SB sanity check 2 failed");
1da177e4
LT
243 return XFS_ERROR(EFSCORRUPTED);
244 }
245
2edbddd5
LM
246 /*
247 * Until this is fixed only page-sized or smaller data blocks work.
248 */
249 if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
250 xfs_fs_mount_cmn_err(flags,
251 "file system with blocksize %d bytes",
252 sbp->sb_blocksize);
253 xfs_fs_mount_cmn_err(flags,
254 "only pagesize (%ld) or less will currently work.",
255 PAGE_SIZE);
256 return XFS_ERROR(ENOSYS);
257 }
258
1a5902c5
CH
259 /*
260 * Currently only very few inode sizes are supported.
261 */
262 switch (sbp->sb_inodesize) {
263 case 256:
264 case 512:
265 case 1024:
266 case 2048:
267 break;
268 default:
269 xfs_fs_mount_cmn_err(flags,
270 "inode size of %d bytes not supported",
271 sbp->sb_inodesize);
272 return XFS_ERROR(ENOSYS);
273 }
274
4cc929ee
NS
275 if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) ||
276 xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) {
764d1f89
NS
277 xfs_fs_mount_cmn_err(flags,
278 "file system too large to be mounted on this system.");
1da177e4
LT
279 return XFS_ERROR(E2BIG);
280 }
281
282 if (unlikely(sbp->sb_inprogress)) {
764d1f89 283 xfs_fs_mount_cmn_err(flags, "file system busy");
1da177e4
LT
284 return XFS_ERROR(EFSCORRUPTED);
285 }
286
de20614b
NS
287 /*
288 * Version 1 directory format has never worked on Linux.
289 */
62118709 290 if (unlikely(!xfs_sb_version_hasdirv2(sbp))) {
764d1f89
NS
291 xfs_fs_mount_cmn_err(flags,
292 "file system using version 1 directory format");
de20614b
NS
293 return XFS_ERROR(ENOSYS);
294 }
295
1da177e4
LT
296 return 0;
297}
298
da353b0d
DC
299STATIC void
300xfs_initialize_perag_icache(
301 xfs_perag_t *pag)
302{
303 if (!pag->pag_ici_init) {
304 rwlock_init(&pag->pag_ici_lock);
305 INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC);
306 pag->pag_ici_init = 1;
307 }
308}
309
1da177e4 310xfs_agnumber_t
c11e2c36 311xfs_initialize_perag(
c11e2c36
NS
312 xfs_mount_t *mp,
313 xfs_agnumber_t agcount)
1da177e4
LT
314{
315 xfs_agnumber_t index, max_metadata;
316 xfs_perag_t *pag;
317 xfs_agino_t agino;
318 xfs_ino_t ino;
319 xfs_sb_t *sbp = &mp->m_sb;
320 xfs_ino_t max_inum = XFS_MAXINUMBER_32;
321
322 /* Check to see if the filesystem can overflow 32 bit inodes */
323 agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
324 ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
325
326 /* Clear the mount flag if no inode can overflow 32 bits
327 * on this filesystem, or if specifically requested..
328 */
bd186aa9 329 if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) && ino > max_inum) {
1da177e4
LT
330 mp->m_flags |= XFS_MOUNT_32BITINODES;
331 } else {
332 mp->m_flags &= ~XFS_MOUNT_32BITINODES;
333 }
334
335 /* If we can overflow then setup the ag headers accordingly */
336 if (mp->m_flags & XFS_MOUNT_32BITINODES) {
337 /* Calculate how much should be reserved for inodes to
338 * meet the max inode percentage.
339 */
340 if (mp->m_maxicount) {
341 __uint64_t icount;
342
343 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
344 do_div(icount, 100);
345 icount += sbp->sb_agblocks - 1;
a749ee86 346 do_div(icount, sbp->sb_agblocks);
1da177e4
LT
347 max_metadata = icount;
348 } else {
349 max_metadata = agcount;
350 }
351 for (index = 0; index < agcount; index++) {
352 ino = XFS_AGINO_TO_INO(mp, index, agino);
353 if (ino > max_inum) {
354 index++;
355 break;
356 }
357
c41564b5 358 /* This ag is preferred for inodes */
1da177e4
LT
359 pag = &mp->m_perag[index];
360 pag->pagi_inodeok = 1;
361 if (index < max_metadata)
362 pag->pagf_metadata = 1;
da353b0d 363 xfs_initialize_perag_icache(pag);
1da177e4
LT
364 }
365 } else {
366 /* Setup default behavior for smaller filesystems */
367 for (index = 0; index < agcount; index++) {
368 pag = &mp->m_perag[index];
369 pag->pagi_inodeok = 1;
da353b0d 370 xfs_initialize_perag_icache(pag);
1da177e4
LT
371 }
372 }
373 return index;
374}
375
2bdf7cd0
CH
376void
377xfs_sb_from_disk(
378 xfs_sb_t *to,
379 xfs_dsb_t *from)
380{
381 to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
382 to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
383 to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
384 to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
385 to->sb_rextents = be64_to_cpu(from->sb_rextents);
386 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
387 to->sb_logstart = be64_to_cpu(from->sb_logstart);
388 to->sb_rootino = be64_to_cpu(from->sb_rootino);
389 to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
390 to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
391 to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
392 to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
393 to->sb_agcount = be32_to_cpu(from->sb_agcount);
394 to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
395 to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
396 to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
397 to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
398 to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
399 to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
400 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
401 to->sb_blocklog = from->sb_blocklog;
402 to->sb_sectlog = from->sb_sectlog;
403 to->sb_inodelog = from->sb_inodelog;
404 to->sb_inopblog = from->sb_inopblog;
405 to->sb_agblklog = from->sb_agblklog;
406 to->sb_rextslog = from->sb_rextslog;
407 to->sb_inprogress = from->sb_inprogress;
408 to->sb_imax_pct = from->sb_imax_pct;
409 to->sb_icount = be64_to_cpu(from->sb_icount);
410 to->sb_ifree = be64_to_cpu(from->sb_ifree);
411 to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
412 to->sb_frextents = be64_to_cpu(from->sb_frextents);
413 to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
414 to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
415 to->sb_qflags = be16_to_cpu(from->sb_qflags);
416 to->sb_flags = from->sb_flags;
417 to->sb_shared_vn = from->sb_shared_vn;
418 to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
419 to->sb_unit = be32_to_cpu(from->sb_unit);
420 to->sb_width = be32_to_cpu(from->sb_width);
421 to->sb_dirblklog = from->sb_dirblklog;
422 to->sb_logsectlog = from->sb_logsectlog;
423 to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
424 to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
425 to->sb_features2 = be32_to_cpu(from->sb_features2);
ee1c0908 426 to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
2bdf7cd0
CH
427}
428
1da177e4 429/*
2bdf7cd0 430 * Copy in core superblock to ondisk one.
1da177e4 431 *
2bdf7cd0 432 * The fields argument is mask of superblock fields to copy.
1da177e4
LT
433 */
434void
2bdf7cd0
CH
435xfs_sb_to_disk(
436 xfs_dsb_t *to,
437 xfs_sb_t *from,
1da177e4
LT
438 __int64_t fields)
439{
2bdf7cd0
CH
440 xfs_caddr_t to_ptr = (xfs_caddr_t)to;
441 xfs_caddr_t from_ptr = (xfs_caddr_t)from;
1da177e4
LT
442 xfs_sb_field_t f;
443 int first;
444 int size;
445
1da177e4 446 ASSERT(fields);
1da177e4
LT
447 if (!fields)
448 return;
449
1da177e4
LT
450 while (fields) {
451 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
452 first = xfs_sb_info[f].offset;
453 size = xfs_sb_info[f + 1].offset - first;
454
455 ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1);
456
457 if (size == 1 || xfs_sb_info[f].type == 1) {
2bdf7cd0 458 memcpy(to_ptr + first, from_ptr + first, size);
1da177e4
LT
459 } else {
460 switch (size) {
461 case 2:
2bdf7cd0
CH
462 *(__be16 *)(to_ptr + first) =
463 cpu_to_be16(*(__u16 *)(from_ptr + first));
1da177e4
LT
464 break;
465 case 4:
2bdf7cd0
CH
466 *(__be32 *)(to_ptr + first) =
467 cpu_to_be32(*(__u32 *)(from_ptr + first));
1da177e4
LT
468 break;
469 case 8:
2bdf7cd0
CH
470 *(__be64 *)(to_ptr + first) =
471 cpu_to_be64(*(__u64 *)(from_ptr + first));
1da177e4
LT
472 break;
473 default:
474 ASSERT(0);
475 }
476 }
477
478 fields &= ~(1LL << f);
479 }
480}
481
482/*
483 * xfs_readsb
484 *
485 * Does the initial read of the superblock.
486 */
487int
764d1f89 488xfs_readsb(xfs_mount_t *mp, int flags)
1da177e4
LT
489{
490 unsigned int sector_size;
491 unsigned int extra_flags;
492 xfs_buf_t *bp;
1da177e4
LT
493 int error;
494
495 ASSERT(mp->m_sb_bp == NULL);
496 ASSERT(mp->m_ddev_targp != NULL);
497
498 /*
499 * Allocate a (locked) buffer to hold the superblock.
500 * This will be kept around at all times to optimize
501 * access to the superblock.
502 */
503 sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
504 extra_flags = XFS_BUF_LOCK | XFS_BUF_MANAGE | XFS_BUF_MAPPED;
505
506 bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR,
507 BTOBB(sector_size), extra_flags);
508 if (!bp || XFS_BUF_ISERROR(bp)) {
764d1f89 509 xfs_fs_mount_cmn_err(flags, "SB read failed");
1da177e4
LT
510 error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
511 goto fail;
512 }
513 ASSERT(XFS_BUF_ISBUSY(bp));
514 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
515
516 /*
517 * Initialize the mount structure from the superblock.
518 * But first do some basic consistency checking.
519 */
2bdf7cd0 520 xfs_sb_from_disk(&mp->m_sb, XFS_BUF_TO_SBP(bp));
1da177e4 521
764d1f89 522 error = xfs_mount_validate_sb(mp, &(mp->m_sb), flags);
1da177e4 523 if (error) {
764d1f89 524 xfs_fs_mount_cmn_err(flags, "SB validate failed");
1da177e4
LT
525 goto fail;
526 }
527
528 /*
529 * We must be able to do sector-sized and sector-aligned IO.
530 */
531 if (sector_size > mp->m_sb.sb_sectsize) {
764d1f89
NS
532 xfs_fs_mount_cmn_err(flags,
533 "device supports only %u byte sectors (not %u)",
1da177e4
LT
534 sector_size, mp->m_sb.sb_sectsize);
535 error = ENOSYS;
536 goto fail;
537 }
538
539 /*
540 * If device sector size is smaller than the superblock size,
541 * re-read the superblock so the buffer is correctly sized.
542 */
543 if (sector_size < mp->m_sb.sb_sectsize) {
544 XFS_BUF_UNMANAGE(bp);
545 xfs_buf_relse(bp);
546 sector_size = mp->m_sb.sb_sectsize;
547 bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR,
548 BTOBB(sector_size), extra_flags);
549 if (!bp || XFS_BUF_ISERROR(bp)) {
764d1f89 550 xfs_fs_mount_cmn_err(flags, "SB re-read failed");
1da177e4
LT
551 error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
552 goto fail;
553 }
554 ASSERT(XFS_BUF_ISBUSY(bp));
555 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
556 }
557
5478eead
LM
558 /* Initialize per-cpu counters */
559 xfs_icsb_reinit_counters(mp);
8d280b98 560
1da177e4
LT
561 mp->m_sb_bp = bp;
562 xfs_buf_relse(bp);
563 ASSERT(XFS_BUF_VALUSEMA(bp) > 0);
564 return 0;
565
566 fail:
567 if (bp) {
568 XFS_BUF_UNMANAGE(bp);
569 xfs_buf_relse(bp);
570 }
571 return error;
572}
573
574
575/*
576 * xfs_mount_common
577 *
578 * Mount initialization code establishing various mount
579 * fields from the superblock associated with the given
580 * mount structure
581 */
ba0f32d4 582STATIC void
1da177e4
LT
583xfs_mount_common(xfs_mount_t *mp, xfs_sb_t *sbp)
584{
1da177e4 585 mp->m_agfrotor = mp->m_agirotor = 0;
007c61c6 586 spin_lock_init(&mp->m_agirotor_lock);
1da177e4
LT
587 mp->m_maxagi = mp->m_sb.sb_agcount;
588 mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
589 mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
590 mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
591 mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
592 mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
1da177e4
LT
593 mp->m_blockmask = sbp->sb_blocksize - 1;
594 mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
595 mp->m_blockwmask = mp->m_blockwsize - 1;
1da177e4 596
60197e8d
CH
597 mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
598 mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
599 mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
600 mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
601
602 mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
603 mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
604 mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2;
605 mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2;
606
607 mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
608 mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
609 mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
610 mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
1da177e4
LT
611
612 mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
613 mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
614 sbp->sb_inopblock);
615 mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
616}
92821e2b
DC
617
618/*
619 * xfs_initialize_perag_data
620 *
621 * Read in each per-ag structure so we can count up the number of
622 * allocated inodes, free inodes and used filesystem blocks as this
623 * information is no longer persistent in the superblock. Once we have
624 * this information, write it into the in-core superblock structure.
625 */
626STATIC int
627xfs_initialize_perag_data(xfs_mount_t *mp, xfs_agnumber_t agcount)
628{
629 xfs_agnumber_t index;
630 xfs_perag_t *pag;
631 xfs_sb_t *sbp = &mp->m_sb;
632 uint64_t ifree = 0;
633 uint64_t ialloc = 0;
634 uint64_t bfree = 0;
635 uint64_t bfreelst = 0;
636 uint64_t btree = 0;
637 int error;
92821e2b
DC
638
639 for (index = 0; index < agcount; index++) {
640 /*
641 * read the agf, then the agi. This gets us
9da096fd 642 * all the information we need and populates the
92821e2b
DC
643 * per-ag structures for us.
644 */
645 error = xfs_alloc_pagf_init(mp, NULL, index, 0);
646 if (error)
647 return error;
648
649 error = xfs_ialloc_pagi_init(mp, NULL, index);
650 if (error)
651 return error;
652 pag = &mp->m_perag[index];
653 ifree += pag->pagi_freecount;
654 ialloc += pag->pagi_count;
655 bfree += pag->pagf_freeblks;
656 bfreelst += pag->pagf_flcount;
657 btree += pag->pagf_btreeblks;
658 }
659 /*
660 * Overwrite incore superblock counters with just-read data
661 */
3685c2a1 662 spin_lock(&mp->m_sb_lock);
92821e2b
DC
663 sbp->sb_ifree = ifree;
664 sbp->sb_icount = ialloc;
665 sbp->sb_fdblocks = bfree + bfreelst + btree;
3685c2a1 666 spin_unlock(&mp->m_sb_lock);
92821e2b
DC
667
668 /* Fixup the per-cpu counters as well. */
669 xfs_icsb_reinit_counters(mp);
670
671 return 0;
672}
673
1da177e4 674/*
0771fb45 675 * Update alignment values based on mount options and sb values
1da177e4 676 */
0771fb45 677STATIC int
7884bc86 678xfs_update_alignment(xfs_mount_t *mp)
1da177e4 679{
1da177e4 680 xfs_sb_t *sbp = &(mp->m_sb);
1da177e4 681
4249023a 682 if (mp->m_dalign) {
1da177e4
LT
683 /*
684 * If stripe unit and stripe width are not multiples
685 * of the fs blocksize turn off alignment.
686 */
687 if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
688 (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
689 if (mp->m_flags & XFS_MOUNT_RETERR) {
690 cmn_err(CE_WARN,
691 "XFS: alignment check 1 failed");
0771fb45 692 return XFS_ERROR(EINVAL);
1da177e4
LT
693 }
694 mp->m_dalign = mp->m_swidth = 0;
695 } else {
696 /*
697 * Convert the stripe unit and width to FSBs.
698 */
699 mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
700 if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
701 if (mp->m_flags & XFS_MOUNT_RETERR) {
0771fb45 702 return XFS_ERROR(EINVAL);
1da177e4
LT
703 }
704 xfs_fs_cmn_err(CE_WARN, mp,
705"stripe alignment turned off: sunit(%d)/swidth(%d) incompatible with agsize(%d)",
706 mp->m_dalign, mp->m_swidth,
707 sbp->sb_agblocks);
708
709 mp->m_dalign = 0;
710 mp->m_swidth = 0;
711 } else if (mp->m_dalign) {
712 mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
713 } else {
714 if (mp->m_flags & XFS_MOUNT_RETERR) {
715 xfs_fs_cmn_err(CE_WARN, mp,
716"stripe alignment turned off: sunit(%d) less than bsize(%d)",
717 mp->m_dalign,
718 mp->m_blockmask +1);
0771fb45 719 return XFS_ERROR(EINVAL);
1da177e4
LT
720 }
721 mp->m_swidth = 0;
722 }
723 }
724
725 /*
726 * Update superblock with new values
727 * and log changes
728 */
62118709 729 if (xfs_sb_version_hasdalign(sbp)) {
1da177e4
LT
730 if (sbp->sb_unit != mp->m_dalign) {
731 sbp->sb_unit = mp->m_dalign;
7884bc86 732 mp->m_update_flags |= XFS_SB_UNIT;
1da177e4
LT
733 }
734 if (sbp->sb_width != mp->m_swidth) {
735 sbp->sb_width = mp->m_swidth;
7884bc86 736 mp->m_update_flags |= XFS_SB_WIDTH;
1da177e4
LT
737 }
738 }
739 } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
62118709 740 xfs_sb_version_hasdalign(&mp->m_sb)) {
1da177e4
LT
741 mp->m_dalign = sbp->sb_unit;
742 mp->m_swidth = sbp->sb_width;
743 }
744
0771fb45
ES
745 return 0;
746}
1da177e4 747
0771fb45
ES
748/*
749 * Set the maximum inode count for this filesystem
750 */
751STATIC void
752xfs_set_maxicount(xfs_mount_t *mp)
753{
754 xfs_sb_t *sbp = &(mp->m_sb);
755 __uint64_t icount;
1da177e4 756
0771fb45
ES
757 if (sbp->sb_imax_pct) {
758 /*
759 * Make sure the maximum inode count is a multiple
760 * of the units we allocate inodes in.
1da177e4 761 */
1da177e4
LT
762 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
763 do_div(icount, 100);
764 do_div(icount, mp->m_ialloc_blks);
765 mp->m_maxicount = (icount * mp->m_ialloc_blks) <<
766 sbp->sb_inopblog;
0771fb45 767 } else {
1da177e4 768 mp->m_maxicount = 0;
1da177e4 769 }
0771fb45
ES
770}
771
772/*
773 * Set the default minimum read and write sizes unless
774 * already specified in a mount option.
775 * We use smaller I/O sizes when the file system
776 * is being used for NFS service (wsync mount option).
777 */
778STATIC void
779xfs_set_rw_sizes(xfs_mount_t *mp)
780{
781 xfs_sb_t *sbp = &(mp->m_sb);
782 int readio_log, writeio_log;
1da177e4 783
1da177e4
LT
784 if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
785 if (mp->m_flags & XFS_MOUNT_WSYNC) {
786 readio_log = XFS_WSYNC_READIO_LOG;
787 writeio_log = XFS_WSYNC_WRITEIO_LOG;
788 } else {
789 readio_log = XFS_READIO_LOG_LARGE;
790 writeio_log = XFS_WRITEIO_LOG_LARGE;
791 }
792 } else {
793 readio_log = mp->m_readio_log;
794 writeio_log = mp->m_writeio_log;
795 }
796
1da177e4
LT
797 if (sbp->sb_blocklog > readio_log) {
798 mp->m_readio_log = sbp->sb_blocklog;
799 } else {
800 mp->m_readio_log = readio_log;
801 }
802 mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
803 if (sbp->sb_blocklog > writeio_log) {
804 mp->m_writeio_log = sbp->sb_blocklog;
805 } else {
806 mp->m_writeio_log = writeio_log;
807 }
808 mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
0771fb45 809}
1da177e4 810
0771fb45
ES
811/*
812 * Set whether we're using inode alignment.
813 */
814STATIC void
815xfs_set_inoalignment(xfs_mount_t *mp)
816{
62118709 817 if (xfs_sb_version_hasalign(&mp->m_sb) &&
1da177e4
LT
818 mp->m_sb.sb_inoalignmt >=
819 XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
820 mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
821 else
822 mp->m_inoalign_mask = 0;
823 /*
824 * If we are using stripe alignment, check whether
825 * the stripe unit is a multiple of the inode alignment
826 */
827 if (mp->m_dalign && mp->m_inoalign_mask &&
828 !(mp->m_dalign & mp->m_inoalign_mask))
829 mp->m_sinoalign = mp->m_dalign;
830 else
831 mp->m_sinoalign = 0;
0771fb45
ES
832}
833
834/*
835 * Check that the data (and log if separate) are an ok size.
836 */
837STATIC int
4249023a 838xfs_check_sizes(xfs_mount_t *mp)
0771fb45
ES
839{
840 xfs_buf_t *bp;
841 xfs_daddr_t d;
842 int error;
843
1da177e4
LT
844 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
845 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
846 cmn_err(CE_WARN, "XFS: size check 1 failed");
0771fb45 847 return XFS_ERROR(E2BIG);
1da177e4
LT
848 }
849 error = xfs_read_buf(mp, mp->m_ddev_targp,
850 d - XFS_FSS_TO_BB(mp, 1),
851 XFS_FSS_TO_BB(mp, 1), 0, &bp);
852 if (!error) {
853 xfs_buf_relse(bp);
854 } else {
855 cmn_err(CE_WARN, "XFS: size check 2 failed");
0771fb45 856 if (error == ENOSPC)
1da177e4 857 error = XFS_ERROR(E2BIG);
0771fb45 858 return error;
1da177e4
LT
859 }
860
4249023a 861 if (mp->m_logdev_targp != mp->m_ddev_targp) {
1da177e4
LT
862 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
863 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
864 cmn_err(CE_WARN, "XFS: size check 3 failed");
0771fb45 865 return XFS_ERROR(E2BIG);
1da177e4
LT
866 }
867 error = xfs_read_buf(mp, mp->m_logdev_targp,
868 d - XFS_FSB_TO_BB(mp, 1),
869 XFS_FSB_TO_BB(mp, 1), 0, &bp);
870 if (!error) {
871 xfs_buf_relse(bp);
872 } else {
873 cmn_err(CE_WARN, "XFS: size check 3 failed");
0771fb45 874 if (error == ENOSPC)
1da177e4 875 error = XFS_ERROR(E2BIG);
0771fb45
ES
876 return error;
877 }
878 }
879 return 0;
880}
881
882/*
0771fb45
ES
883 * This function does the following on an initial mount of a file system:
884 * - reads the superblock from disk and init the mount struct
885 * - if we're a 32-bit kernel, do a size check on the superblock
886 * so we don't mount terabyte filesystems
887 * - init mount struct realtime fields
888 * - allocate inode hash table for fs
889 * - init directory manager
890 * - perform recovery and init the log manager
891 */
892int
893xfs_mountfs(
4249023a 894 xfs_mount_t *mp)
0771fb45
ES
895{
896 xfs_sb_t *sbp = &(mp->m_sb);
897 xfs_inode_t *rip;
0771fb45 898 __uint64_t resblks;
0771fb45 899 uint quotamount, quotaflags;
0771fb45
ES
900 int error = 0;
901
0771fb45
ES
902 xfs_mount_common(mp, sbp);
903
ee1c0908 904 /*
e6957ea4
ES
905 * Check for a mismatched features2 values. Older kernels
906 * read & wrote into the wrong sb offset for sb_features2
907 * on some platforms due to xfs_sb_t not being 64bit size aligned
908 * when sb_features2 was added, which made older superblock
909 * reading/writing routines swap it as a 64-bit value.
ee1c0908 910 *
e6957ea4
ES
911 * For backwards compatibility, we make both slots equal.
912 *
913 * If we detect a mismatched field, we OR the set bits into the
914 * existing features2 field in case it has already been modified; we
915 * don't want to lose any features. We then update the bad location
916 * with the ORed value so that older kernels will see any features2
917 * flags, and mark the two fields as needing updates once the
918 * transaction subsystem is online.
ee1c0908 919 */
e6957ea4 920 if (xfs_sb_has_mismatched_features2(sbp)) {
ee1c0908
DC
921 cmn_err(CE_WARN,
922 "XFS: correcting sb_features alignment problem");
923 sbp->sb_features2 |= sbp->sb_bad_features2;
e6957ea4 924 sbp->sb_bad_features2 = sbp->sb_features2;
7884bc86 925 mp->m_update_flags |= XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2;
e6957ea4
ES
926
927 /*
928 * Re-check for ATTR2 in case it was found in bad_features2
929 * slot.
930 */
7c12f296
TS
931 if (xfs_sb_version_hasattr2(&mp->m_sb) &&
932 !(mp->m_flags & XFS_MOUNT_NOATTR2))
e6957ea4 933 mp->m_flags |= XFS_MOUNT_ATTR2;
7c12f296
TS
934 }
935
936 if (xfs_sb_version_hasattr2(&mp->m_sb) &&
937 (mp->m_flags & XFS_MOUNT_NOATTR2)) {
938 xfs_sb_version_removeattr2(&mp->m_sb);
7884bc86 939 mp->m_update_flags |= XFS_SB_FEATURES2;
e6957ea4 940
7c12f296
TS
941 /* update sb_versionnum for the clearing of the morebits */
942 if (!sbp->sb_features2)
7884bc86 943 mp->m_update_flags |= XFS_SB_VERSIONNUM;
ee1c0908
DC
944 }
945
0771fb45
ES
946 /*
947 * Check if sb_agblocks is aligned at stripe boundary
948 * If sb_agblocks is NOT aligned turn off m_dalign since
949 * allocator alignment is within an ag, therefore ag has
950 * to be aligned at stripe boundary.
951 */
7884bc86 952 error = xfs_update_alignment(mp);
0771fb45 953 if (error)
f9057e3d 954 goto out;
0771fb45
ES
955
956 xfs_alloc_compute_maxlevels(mp);
957 xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
958 xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
959 xfs_ialloc_compute_maxlevels(mp);
960
961 xfs_set_maxicount(mp);
962
963 mp->m_maxioffset = xfs_max_file_offset(sbp->sb_blocklog);
964
965 /*
966 * XFS uses the uuid from the superblock as the unique
967 * identifier for fsid. We can not use the uuid from the volume
968 * since a single partition filesystem is identical to a single
969 * partition volume/filesystem.
970 */
f9057e3d 971 if (!(mp->m_flags & XFS_MOUNT_NOUUID)) {
0771fb45
ES
972 if (xfs_uuid_mount(mp)) {
973 error = XFS_ERROR(EINVAL);
f9057e3d 974 goto out;
1da177e4
LT
975 }
976 }
977
0771fb45
ES
978 /*
979 * Set the minimum read and write sizes
980 */
981 xfs_set_rw_sizes(mp);
982
983 /*
984 * Set the inode cluster size.
985 * This may still be overridden by the file system
986 * block size if it is larger than the chosen cluster size.
987 */
988 mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
989
990 /*
991 * Set inode alignment fields
992 */
993 xfs_set_inoalignment(mp);
994
995 /*
996 * Check that the data (and log if separate) are an ok size.
997 */
4249023a 998 error = xfs_check_sizes(mp);
0771fb45 999 if (error)
f9057e3d 1000 goto out_remove_uuid;
0771fb45 1001
1da177e4
LT
1002 /*
1003 * Initialize realtime fields in the mount structure
1004 */
0771fb45
ES
1005 error = xfs_rtmount_init(mp);
1006 if (error) {
1da177e4 1007 cmn_err(CE_WARN, "XFS: RT mount failed");
f9057e3d 1008 goto out_remove_uuid;
1da177e4
LT
1009 }
1010
1da177e4
LT
1011 /*
1012 * Copies the low order bits of the timestamp and the randomly
1013 * set "sequence" number out of a UUID.
1014 */
1015 uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid);
1016
1da177e4
LT
1017 mp->m_dmevmask = 0; /* not persistent; set after each mount */
1018
f6c2d1fa 1019 xfs_dir_mount(mp);
1da177e4
LT
1020
1021 /*
1022 * Initialize the attribute manager's entries.
1023 */
1024 mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100;
1025
1026 /*
1027 * Initialize the precomputed transaction reservations values.
1028 */
1029 xfs_trans_init(mp);
1030
1da177e4
LT
1031 /*
1032 * Allocate and initialize the per-ag data.
1033 */
1034 init_rwsem(&mp->m_peraglock);
d62c251f
CH
1035 mp->m_perag = kmem_zalloc(sbp->sb_agcount * sizeof(xfs_perag_t),
1036 KM_MAYFAIL);
1037 if (!mp->m_perag)
f9057e3d 1038 goto out_remove_uuid;
1da177e4 1039
b267ce99 1040 mp->m_maxagi = xfs_initialize_perag(mp, sbp->sb_agcount);
1da177e4 1041
f9057e3d
CH
1042 if (!sbp->sb_logblocks) {
1043 cmn_err(CE_WARN, "XFS: no log defined");
1044 XFS_ERROR_REPORT("xfs_mountfs", XFS_ERRLEVEL_LOW, mp);
1045 error = XFS_ERROR(EFSCORRUPTED);
1046 goto out_free_perag;
1047 }
1048
1da177e4
LT
1049 /*
1050 * log's mount-time initialization. Perform 1st part recovery if needed
1051 */
f9057e3d
CH
1052 error = xfs_log_mount(mp, mp->m_logdev_targp,
1053 XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
1054 XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
1055 if (error) {
1056 cmn_err(CE_WARN, "XFS: log mount failed");
1057 goto out_free_perag;
1da177e4
LT
1058 }
1059
92821e2b
DC
1060 /*
1061 * Now the log is mounted, we know if it was an unclean shutdown or
1062 * not. If it was, with the first phase of recovery has completed, we
1063 * have consistent AG blocks on disk. We have not recovered EFIs yet,
1064 * but they are recovered transactionally in the second recovery phase
1065 * later.
1066 *
1067 * Hence we can safely re-initialise incore superblock counters from
1068 * the per-ag data. These may not be correct if the filesystem was not
1069 * cleanly unmounted, so we need to wait for recovery to finish before
1070 * doing this.
1071 *
1072 * If the filesystem was cleanly unmounted, then we can trust the
1073 * values in the superblock to be correct and we don't need to do
1074 * anything here.
1075 *
1076 * If we are currently making the filesystem, the initialisation will
1077 * fail as the perag data is in an undefined state.
1078 */
92821e2b
DC
1079 if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
1080 !XFS_LAST_UNMOUNT_WAS_CLEAN(mp) &&
1081 !mp->m_sb.sb_inprogress) {
1082 error = xfs_initialize_perag_data(mp, sbp->sb_agcount);
f9057e3d
CH
1083 if (error)
1084 goto out_free_perag;
92821e2b 1085 }
f9057e3d 1086
1da177e4
LT
1087 /*
1088 * Get and sanity-check the root inode.
1089 * Save the pointer to it in the mount structure.
1090 */
1091 error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip, 0);
1092 if (error) {
1093 cmn_err(CE_WARN, "XFS: failed to read root inode");
f9057e3d 1094 goto out_log_dealloc;
1da177e4
LT
1095 }
1096
1097 ASSERT(rip != NULL);
1da177e4
LT
1098
1099 if (unlikely((rip->i_d.di_mode & S_IFMT) != S_IFDIR)) {
1100 cmn_err(CE_WARN, "XFS: corrupted root inode");
b6574520
NS
1101 cmn_err(CE_WARN, "Device %s - root %llu is not a directory",
1102 XFS_BUFTARG_NAME(mp->m_ddev_targp),
1103 (unsigned long long)rip->i_ino);
1da177e4
LT
1104 xfs_iunlock(rip, XFS_ILOCK_EXCL);
1105 XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
1106 mp);
1107 error = XFS_ERROR(EFSCORRUPTED);
f9057e3d 1108 goto out_rele_rip;
1da177e4
LT
1109 }
1110 mp->m_rootip = rip; /* save it */
1111
1112 xfs_iunlock(rip, XFS_ILOCK_EXCL);
1113
1114 /*
1115 * Initialize realtime inode pointers in the mount structure
1116 */
0771fb45
ES
1117 error = xfs_rtmount_inodes(mp);
1118 if (error) {
1da177e4
LT
1119 /*
1120 * Free up the root inode.
1121 */
1122 cmn_err(CE_WARN, "XFS: failed to read RT inodes");
f9057e3d 1123 goto out_rele_rip;
1da177e4
LT
1124 }
1125
1126 /*
7884bc86
CH
1127 * If this is a read-only mount defer the superblock updates until
1128 * the next remount into writeable mode. Otherwise we would never
1129 * perform the update e.g. for the root filesystem.
1da177e4 1130 */
7884bc86
CH
1131 if (mp->m_update_flags && !(mp->m_flags & XFS_MOUNT_RDONLY)) {
1132 error = xfs_mount_log_sb(mp, mp->m_update_flags);
e5720eec
DC
1133 if (error) {
1134 cmn_err(CE_WARN, "XFS: failed to write sb changes");
b93b6e43 1135 goto out_rtunmount;
e5720eec
DC
1136 }
1137 }
1da177e4
LT
1138
1139 /*
1140 * Initialise the XFS quota management subsystem for this mount
1141 */
0771fb45
ES
1142 error = XFS_QM_INIT(mp, &quotamount, &quotaflags);
1143 if (error)
b93b6e43 1144 goto out_rtunmount;
1da177e4
LT
1145
1146 /*
1147 * Finish recovering the file system. This part needed to be
1148 * delayed until after the root and real-time bitmap inodes
1149 * were consistently read in.
1150 */
4249023a 1151 error = xfs_log_mount_finish(mp);
1da177e4
LT
1152 if (error) {
1153 cmn_err(CE_WARN, "XFS: log mount finish failed");
b93b6e43 1154 goto out_rtunmount;
1da177e4
LT
1155 }
1156
1157 /*
1158 * Complete the quota initialisation, post-log-replay component.
1159 */
4249023a 1160 error = XFS_QM_MOUNT(mp, quotamount, quotaflags);
0771fb45 1161 if (error)
b93b6e43 1162 goto out_rtunmount;
1da177e4 1163
84e1e99f
DC
1164 /*
1165 * Now we are mounted, reserve a small amount of unused space for
1166 * privileged transactions. This is needed so that transaction
1167 * space required for critical operations can dip into this pool
1168 * when at ENOSPC. This is needed for operations like create with
1169 * attr, unwritten extent conversion at ENOSPC, etc. Data allocations
1170 * are not allowed to use this reserved space.
1171 *
1172 * We default to 5% or 1024 fsbs of space reserved, whichever is smaller.
1173 * This may drive us straight to ENOSPC on mount, but that implies
714082bc 1174 * we were already there on the last unmount. Warn if this occurs.
84e1e99f 1175 */
39726be2
CH
1176 resblks = mp->m_sb.sb_dblocks;
1177 do_div(resblks, 20);
1178 resblks = min_t(__uint64_t, resblks, 1024);
714082bc
DC
1179 error = xfs_reserve_blocks(mp, &resblks, NULL);
1180 if (error)
1181 cmn_err(CE_WARN, "XFS: Unable to allocate reserve blocks. "
1182 "Continuing without a reserve pool.");
84e1e99f 1183
1da177e4
LT
1184 return 0;
1185
b93b6e43
CH
1186 out_rtunmount:
1187 xfs_rtunmount_inodes(mp);
f9057e3d 1188 out_rele_rip:
43355099 1189 IRELE(rip);
f9057e3d 1190 out_log_dealloc:
21b699c8 1191 xfs_log_unmount(mp);
f9057e3d 1192 out_free_perag:
ff4f038c 1193 xfs_free_perag(mp);
f9057e3d
CH
1194 out_remove_uuid:
1195 if (!(mp->m_flags & XFS_MOUNT_NOUUID))
fa6adbe0 1196 uuid_table_remove(&mp->m_sb.sb_uuid);
f9057e3d 1197 out:
1da177e4
LT
1198 return error;
1199}
1200
1201/*
1da177e4
LT
1202 * This flushes out the inodes,dquots and the superblock, unmounts the
1203 * log and makes sure that incore structures are freed.
1204 */
41b5c2e7
CH
1205void
1206xfs_unmountfs(
1207 struct xfs_mount *mp)
1da177e4 1208{
41b5c2e7
CH
1209 __uint64_t resblks;
1210 int error;
1da177e4 1211
f95099ba
CH
1212 /*
1213 * Release dquot that rootinode, rbmino and rsumino might be holding,
1214 * and release the quota inodes.
1215 */
1216 XFS_QM_UNMOUNT(mp);
1217
b93b6e43 1218 xfs_rtunmount_inodes(mp);
77508ec8
CH
1219 IRELE(mp->m_rootip);
1220
641c56fb
DC
1221 /*
1222 * We can potentially deadlock here if we have an inode cluster
9da096fd 1223 * that has been freed has its buffer still pinned in memory because
641c56fb
DC
1224 * the transaction is still sitting in a iclog. The stale inodes
1225 * on that buffer will have their flush locks held until the
1226 * transaction hits the disk and the callbacks run. the inode
1227 * flush takes the flush lock unconditionally and with nothing to
1228 * push out the iclog we will never get that unlocked. hence we
1229 * need to force the log first.
1230 */
1231 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1dc3318a 1232 xfs_reclaim_inodes(mp, 0, XFS_IFLUSH_ASYNC);
1da177e4 1233
ee2a4f7c 1234 XFS_QM_DQPURGEALL(mp, XFS_QMOPT_QUOTALL | XFS_QMOPT_UMOUNTING);
1da177e4 1235
a357a121
LM
1236 if (mp->m_quotainfo)
1237 XFS_QM_DONE(mp);
1238
1da177e4
LT
1239 /*
1240 * Flush out the log synchronously so that we know for sure
1241 * that nothing is pinned. This is important because bflush()
1242 * will skip pinned buffers.
1243 */
1244 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1245
1246 xfs_binval(mp->m_ddev_targp);
1247 if (mp->m_rtdev_targp) {
1248 xfs_binval(mp->m_rtdev_targp);
1249 }
1250
84e1e99f
DC
1251 /*
1252 * Unreserve any blocks we have so that when we unmount we don't account
1253 * the reserved free space as used. This is really only necessary for
1254 * lazy superblock counting because it trusts the incore superblock
9da096fd 1255 * counters to be absolutely correct on clean unmount.
84e1e99f
DC
1256 *
1257 * We don't bother correcting this elsewhere for lazy superblock
1258 * counting because on mount of an unclean filesystem we reconstruct the
1259 * correct counter value and this is irrelevant.
1260 *
1261 * For non-lazy counter filesystems, this doesn't matter at all because
1262 * we only every apply deltas to the superblock and hence the incore
1263 * value does not matter....
1264 */
1265 resblks = 0;
714082bc
DC
1266 error = xfs_reserve_blocks(mp, &resblks, NULL);
1267 if (error)
1268 cmn_err(CE_WARN, "XFS: Unable to free reserved block pool. "
1269 "Freespace may not be correct on next mount.");
1270
e5720eec
DC
1271 error = xfs_log_sbcount(mp, 1);
1272 if (error)
1273 cmn_err(CE_WARN, "XFS: Unable to update superblock counters. "
1274 "Freespace may not be correct on next mount.");
1da177e4 1275 xfs_unmountfs_writesb(mp);
1da177e4 1276 xfs_unmountfs_wait(mp); /* wait for async bufs */
21b699c8
CH
1277 xfs_log_unmount_write(mp);
1278 xfs_log_unmount(mp);
1da177e4 1279
1da177e4 1280 if ((mp->m_flags & XFS_MOUNT_NOUUID) == 0)
fa6adbe0 1281 uuid_table_remove(&mp->m_sb.sb_uuid);
1da177e4 1282
1550d0b0 1283#if defined(DEBUG)
0ce4cfd4 1284 xfs_errortag_clearall(mp, 0);
1da177e4 1285#endif
ff4f038c 1286 xfs_free_perag(mp);
1da177e4
LT
1287}
1288
ba0f32d4 1289STATIC void
1da177e4
LT
1290xfs_unmountfs_wait(xfs_mount_t *mp)
1291{
1292 if (mp->m_logdev_targp != mp->m_ddev_targp)
1293 xfs_wait_buftarg(mp->m_logdev_targp);
1294 if (mp->m_rtdev_targp)
1295 xfs_wait_buftarg(mp->m_rtdev_targp);
1296 xfs_wait_buftarg(mp->m_ddev_targp);
1297}
1298
92821e2b
DC
1299int
1300xfs_fs_writable(xfs_mount_t *mp)
1301{
b267ce99 1302 return !(xfs_test_for_freeze(mp) || XFS_FORCED_SHUTDOWN(mp) ||
bd186aa9 1303 (mp->m_flags & XFS_MOUNT_RDONLY));
92821e2b
DC
1304}
1305
1306/*
1307 * xfs_log_sbcount
1308 *
1309 * Called either periodically to keep the on disk superblock values
1310 * roughly up to date or from unmount to make sure the values are
1311 * correct on a clean unmount.
1312 *
1313 * Note this code can be called during the process of freezing, so
1314 * we may need to use the transaction allocator which does not not
1315 * block when the transaction subsystem is in its frozen state.
1316 */
1317int
1318xfs_log_sbcount(
1319 xfs_mount_t *mp,
1320 uint sync)
1321{
1322 xfs_trans_t *tp;
1323 int error;
1324
1325 if (!xfs_fs_writable(mp))
1326 return 0;
1327
d4d90b57 1328 xfs_icsb_sync_counters(mp, 0);
92821e2b
DC
1329
1330 /*
1331 * we don't need to do this if we are updating the superblock
1332 * counters on every modification.
1333 */
1334 if (!xfs_sb_version_haslazysbcount(&mp->m_sb))
1335 return 0;
1336
1337 tp = _xfs_trans_alloc(mp, XFS_TRANS_SB_COUNT);
1338 error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
1339 XFS_DEFAULT_LOG_COUNT);
1340 if (error) {
1341 xfs_trans_cancel(tp, 0);
1342 return error;
1343 }
1344
1345 xfs_mod_sb(tp, XFS_SB_IFREE | XFS_SB_ICOUNT | XFS_SB_FDBLOCKS);
1346 if (sync)
1347 xfs_trans_set_sync(tp);
e5720eec
DC
1348 error = xfs_trans_commit(tp, 0);
1349 return error;
92821e2b
DC
1350}
1351
1da177e4
LT
1352int
1353xfs_unmountfs_writesb(xfs_mount_t *mp)
1354{
1355 xfs_buf_t *sbp;
1da177e4
LT
1356 int error = 0;
1357
1358 /*
1359 * skip superblock write if fs is read-only, or
1360 * if we are doing a forced umount.
1361 */
bd186aa9 1362 if (!((mp->m_flags & XFS_MOUNT_RDONLY) ||
1da177e4 1363 XFS_FORCED_SHUTDOWN(mp))) {
8d280b98 1364
92821e2b 1365 sbp = xfs_getsb(mp, 0);
8d280b98 1366
1da177e4
LT
1367 XFS_BUF_UNDONE(sbp);
1368 XFS_BUF_UNREAD(sbp);
1369 XFS_BUF_UNDELAYWRITE(sbp);
1370 XFS_BUF_WRITE(sbp);
1371 XFS_BUF_UNASYNC(sbp);
1372 ASSERT(XFS_BUF_TARGET(sbp) == mp->m_ddev_targp);
1373 xfsbdstrat(mp, sbp);
1da177e4
LT
1374 error = xfs_iowait(sbp);
1375 if (error)
1376 xfs_ioerror_alert("xfs_unmountfs_writesb",
1377 mp, sbp, XFS_BUF_ADDR(sbp));
92821e2b 1378 xfs_buf_relse(sbp);
1da177e4 1379 }
014c2544 1380 return error;
1da177e4
LT
1381}
1382
1383/*
1384 * xfs_mod_sb() can be used to copy arbitrary changes to the
1385 * in-core superblock into the superblock buffer to be logged.
1386 * It does not provide the higher level of locking that is
1387 * needed to protect the in-core superblock from concurrent
1388 * access.
1389 */
1390void
1391xfs_mod_sb(xfs_trans_t *tp, __int64_t fields)
1392{
1393 xfs_buf_t *bp;
1394 int first;
1395 int last;
1396 xfs_mount_t *mp;
1da177e4
LT
1397 xfs_sb_field_t f;
1398
1399 ASSERT(fields);
1400 if (!fields)
1401 return;
1402 mp = tp->t_mountp;
1403 bp = xfs_trans_getsb(tp, mp, 0);
1da177e4
LT
1404 first = sizeof(xfs_sb_t);
1405 last = 0;
1406
1407 /* translate/copy */
1408
2bdf7cd0 1409 xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb, fields);
1da177e4
LT
1410
1411 /* find modified range */
1412
1413 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
1414 ASSERT((1LL << f) & XFS_SB_MOD_BITS);
1415 first = xfs_sb_info[f].offset;
1416
1417 f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields);
1418 ASSERT((1LL << f) & XFS_SB_MOD_BITS);
1419 last = xfs_sb_info[f + 1].offset - 1;
1420
1421 xfs_trans_log_buf(tp, bp, first, last);
1422}
d210a28c 1423
d210a28c 1424
1da177e4
LT
1425/*
1426 * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply
1427 * a delta to a specified field in the in-core superblock. Simply
1428 * switch on the field indicated and apply the delta to that field.
1429 * Fields are not allowed to dip below zero, so if the delta would
1430 * do this do not apply it and return EINVAL.
1431 *
3685c2a1 1432 * The m_sb_lock must be held when this routine is called.
1da177e4 1433 */
8d280b98 1434int
20f4ebf2
DC
1435xfs_mod_incore_sb_unlocked(
1436 xfs_mount_t *mp,
1437 xfs_sb_field_t field,
1438 int64_t delta,
1439 int rsvd)
1da177e4
LT
1440{
1441 int scounter; /* short counter for 32 bit fields */
1442 long long lcounter; /* long counter for 64 bit fields */
1443 long long res_used, rem;
1444
1445 /*
1446 * With the in-core superblock spin lock held, switch
1447 * on the indicated field. Apply the delta to the
1448 * proper field. If the fields value would dip below
1449 * 0, then do not apply the delta and return EINVAL.
1450 */
1451 switch (field) {
1452 case XFS_SBS_ICOUNT:
1453 lcounter = (long long)mp->m_sb.sb_icount;
1454 lcounter += delta;
1455 if (lcounter < 0) {
1456 ASSERT(0);
014c2544 1457 return XFS_ERROR(EINVAL);
1da177e4
LT
1458 }
1459 mp->m_sb.sb_icount = lcounter;
014c2544 1460 return 0;
1da177e4
LT
1461 case XFS_SBS_IFREE:
1462 lcounter = (long long)mp->m_sb.sb_ifree;
1463 lcounter += delta;
1464 if (lcounter < 0) {
1465 ASSERT(0);
014c2544 1466 return XFS_ERROR(EINVAL);
1da177e4
LT
1467 }
1468 mp->m_sb.sb_ifree = lcounter;
014c2544 1469 return 0;
1da177e4 1470 case XFS_SBS_FDBLOCKS:
4be536de
DC
1471 lcounter = (long long)
1472 mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
1da177e4
LT
1473 res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
1474
1475 if (delta > 0) { /* Putting blocks back */
1476 if (res_used > delta) {
1477 mp->m_resblks_avail += delta;
1478 } else {
1479 rem = delta - res_used;
1480 mp->m_resblks_avail = mp->m_resblks;
1481 lcounter += rem;
1482 }
1483 } else { /* Taking blocks away */
1484
1485 lcounter += delta;
1486
1487 /*
1488 * If were out of blocks, use any available reserved blocks if
1489 * were allowed to.
1490 */
1491
1492 if (lcounter < 0) {
1493 if (rsvd) {
1494 lcounter = (long long)mp->m_resblks_avail + delta;
1495 if (lcounter < 0) {
014c2544 1496 return XFS_ERROR(ENOSPC);
1da177e4
LT
1497 }
1498 mp->m_resblks_avail = lcounter;
014c2544 1499 return 0;
1da177e4 1500 } else { /* not reserved */
014c2544 1501 return XFS_ERROR(ENOSPC);
1da177e4
LT
1502 }
1503 }
1504 }
1505
4be536de 1506 mp->m_sb.sb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
014c2544 1507 return 0;
1da177e4
LT
1508 case XFS_SBS_FREXTENTS:
1509 lcounter = (long long)mp->m_sb.sb_frextents;
1510 lcounter += delta;
1511 if (lcounter < 0) {
014c2544 1512 return XFS_ERROR(ENOSPC);
1da177e4
LT
1513 }
1514 mp->m_sb.sb_frextents = lcounter;
014c2544 1515 return 0;
1da177e4
LT
1516 case XFS_SBS_DBLOCKS:
1517 lcounter = (long long)mp->m_sb.sb_dblocks;
1518 lcounter += delta;
1519 if (lcounter < 0) {
1520 ASSERT(0);
014c2544 1521 return XFS_ERROR(EINVAL);
1da177e4
LT
1522 }
1523 mp->m_sb.sb_dblocks = lcounter;
014c2544 1524 return 0;
1da177e4
LT
1525 case XFS_SBS_AGCOUNT:
1526 scounter = mp->m_sb.sb_agcount;
1527 scounter += delta;
1528 if (scounter < 0) {
1529 ASSERT(0);
014c2544 1530 return XFS_ERROR(EINVAL);
1da177e4
LT
1531 }
1532 mp->m_sb.sb_agcount = scounter;
014c2544 1533 return 0;
1da177e4
LT
1534 case XFS_SBS_IMAX_PCT:
1535 scounter = mp->m_sb.sb_imax_pct;
1536 scounter += delta;
1537 if (scounter < 0) {
1538 ASSERT(0);
014c2544 1539 return XFS_ERROR(EINVAL);
1da177e4
LT
1540 }
1541 mp->m_sb.sb_imax_pct = scounter;
014c2544 1542 return 0;
1da177e4
LT
1543 case XFS_SBS_REXTSIZE:
1544 scounter = mp->m_sb.sb_rextsize;
1545 scounter += delta;
1546 if (scounter < 0) {
1547 ASSERT(0);
014c2544 1548 return XFS_ERROR(EINVAL);
1da177e4
LT
1549 }
1550 mp->m_sb.sb_rextsize = scounter;
014c2544 1551 return 0;
1da177e4
LT
1552 case XFS_SBS_RBMBLOCKS:
1553 scounter = mp->m_sb.sb_rbmblocks;
1554 scounter += delta;
1555 if (scounter < 0) {
1556 ASSERT(0);
014c2544 1557 return XFS_ERROR(EINVAL);
1da177e4
LT
1558 }
1559 mp->m_sb.sb_rbmblocks = scounter;
014c2544 1560 return 0;
1da177e4
LT
1561 case XFS_SBS_RBLOCKS:
1562 lcounter = (long long)mp->m_sb.sb_rblocks;
1563 lcounter += delta;
1564 if (lcounter < 0) {
1565 ASSERT(0);
014c2544 1566 return XFS_ERROR(EINVAL);
1da177e4
LT
1567 }
1568 mp->m_sb.sb_rblocks = lcounter;
014c2544 1569 return 0;
1da177e4
LT
1570 case XFS_SBS_REXTENTS:
1571 lcounter = (long long)mp->m_sb.sb_rextents;
1572 lcounter += delta;
1573 if (lcounter < 0) {
1574 ASSERT(0);
014c2544 1575 return XFS_ERROR(EINVAL);
1da177e4
LT
1576 }
1577 mp->m_sb.sb_rextents = lcounter;
014c2544 1578 return 0;
1da177e4
LT
1579 case XFS_SBS_REXTSLOG:
1580 scounter = mp->m_sb.sb_rextslog;
1581 scounter += delta;
1582 if (scounter < 0) {
1583 ASSERT(0);
014c2544 1584 return XFS_ERROR(EINVAL);
1da177e4
LT
1585 }
1586 mp->m_sb.sb_rextslog = scounter;
014c2544 1587 return 0;
1da177e4
LT
1588 default:
1589 ASSERT(0);
014c2544 1590 return XFS_ERROR(EINVAL);
1da177e4
LT
1591 }
1592}
1593
1594/*
1595 * xfs_mod_incore_sb() is used to change a field in the in-core
1596 * superblock structure by the specified delta. This modification
3685c2a1 1597 * is protected by the m_sb_lock. Just use the xfs_mod_incore_sb_unlocked()
1da177e4
LT
1598 * routine to do the work.
1599 */
1600int
20f4ebf2
DC
1601xfs_mod_incore_sb(
1602 xfs_mount_t *mp,
1603 xfs_sb_field_t field,
1604 int64_t delta,
1605 int rsvd)
1da177e4 1606{
1da177e4
LT
1607 int status;
1608
8d280b98
DC
1609 /* check for per-cpu counters */
1610 switch (field) {
1611#ifdef HAVE_PERCPU_SB
1612 case XFS_SBS_ICOUNT:
1613 case XFS_SBS_IFREE:
1614 case XFS_SBS_FDBLOCKS:
1615 if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
1616 status = xfs_icsb_modify_counters(mp, field,
1617 delta, rsvd);
1618 break;
1619 }
1620 /* FALLTHROUGH */
1621#endif
1622 default:
3685c2a1 1623 spin_lock(&mp->m_sb_lock);
8d280b98 1624 status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
3685c2a1 1625 spin_unlock(&mp->m_sb_lock);
8d280b98
DC
1626 break;
1627 }
1628
014c2544 1629 return status;
1da177e4
LT
1630}
1631
1632/*
1633 * xfs_mod_incore_sb_batch() is used to change more than one field
1634 * in the in-core superblock structure at a time. This modification
1635 * is protected by a lock internal to this module. The fields and
1636 * changes to those fields are specified in the array of xfs_mod_sb
1637 * structures passed in.
1638 *
1639 * Either all of the specified deltas will be applied or none of
1640 * them will. If any modified field dips below 0, then all modifications
1641 * will be backed out and EINVAL will be returned.
1642 */
1643int
1644xfs_mod_incore_sb_batch(xfs_mount_t *mp, xfs_mod_sb_t *msb, uint nmsb, int rsvd)
1645{
1da177e4
LT
1646 int status=0;
1647 xfs_mod_sb_t *msbp;
1648
1649 /*
1650 * Loop through the array of mod structures and apply each
1651 * individually. If any fail, then back out all those
1652 * which have already been applied. Do all of this within
3685c2a1 1653 * the scope of the m_sb_lock so that all of the changes will
1da177e4
LT
1654 * be atomic.
1655 */
3685c2a1 1656 spin_lock(&mp->m_sb_lock);
1da177e4
LT
1657 msbp = &msb[0];
1658 for (msbp = &msbp[0]; msbp < (msb + nmsb); msbp++) {
1659 /*
1660 * Apply the delta at index n. If it fails, break
1661 * from the loop so we'll fall into the undo loop
1662 * below.
1663 */
8d280b98
DC
1664 switch (msbp->msb_field) {
1665#ifdef HAVE_PERCPU_SB
1666 case XFS_SBS_ICOUNT:
1667 case XFS_SBS_IFREE:
1668 case XFS_SBS_FDBLOCKS:
1669 if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
3685c2a1 1670 spin_unlock(&mp->m_sb_lock);
20b64285 1671 status = xfs_icsb_modify_counters(mp,
8d280b98
DC
1672 msbp->msb_field,
1673 msbp->msb_delta, rsvd);
3685c2a1 1674 spin_lock(&mp->m_sb_lock);
8d280b98
DC
1675 break;
1676 }
1677 /* FALLTHROUGH */
1678#endif
1679 default:
1680 status = xfs_mod_incore_sb_unlocked(mp,
1681 msbp->msb_field,
1682 msbp->msb_delta, rsvd);
1683 break;
1684 }
1685
1da177e4
LT
1686 if (status != 0) {
1687 break;
1688 }
1689 }
1690
1691 /*
1692 * If we didn't complete the loop above, then back out
1693 * any changes made to the superblock. If you add code
1694 * between the loop above and here, make sure that you
1695 * preserve the value of status. Loop back until
1696 * we step below the beginning of the array. Make sure
1697 * we don't touch anything back there.
1698 */
1699 if (status != 0) {
1700 msbp--;
1701 while (msbp >= msb) {
8d280b98
DC
1702 switch (msbp->msb_field) {
1703#ifdef HAVE_PERCPU_SB
1704 case XFS_SBS_ICOUNT:
1705 case XFS_SBS_IFREE:
1706 case XFS_SBS_FDBLOCKS:
1707 if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
3685c2a1 1708 spin_unlock(&mp->m_sb_lock);
20b64285 1709 status = xfs_icsb_modify_counters(mp,
8d280b98
DC
1710 msbp->msb_field,
1711 -(msbp->msb_delta),
1712 rsvd);
3685c2a1 1713 spin_lock(&mp->m_sb_lock);
8d280b98
DC
1714 break;
1715 }
1716 /* FALLTHROUGH */
1717#endif
1718 default:
1719 status = xfs_mod_incore_sb_unlocked(mp,
1720 msbp->msb_field,
1721 -(msbp->msb_delta),
1722 rsvd);
1723 break;
1724 }
1da177e4
LT
1725 ASSERT(status == 0);
1726 msbp--;
1727 }
1728 }
3685c2a1 1729 spin_unlock(&mp->m_sb_lock);
014c2544 1730 return status;
1da177e4
LT
1731}
1732
1733/*
1734 * xfs_getsb() is called to obtain the buffer for the superblock.
1735 * The buffer is returned locked and read in from disk.
1736 * The buffer should be released with a call to xfs_brelse().
1737 *
1738 * If the flags parameter is BUF_TRYLOCK, then we'll only return
1739 * the superblock buffer if it can be locked without sleeping.
1740 * If it can't then we'll return NULL.
1741 */
1742xfs_buf_t *
1743xfs_getsb(
1744 xfs_mount_t *mp,
1745 int flags)
1746{
1747 xfs_buf_t *bp;
1748
1749 ASSERT(mp->m_sb_bp != NULL);
1750 bp = mp->m_sb_bp;
1751 if (flags & XFS_BUF_TRYLOCK) {
1752 if (!XFS_BUF_CPSEMA(bp)) {
1753 return NULL;
1754 }
1755 } else {
1756 XFS_BUF_PSEMA(bp, PRIBIO);
1757 }
1758 XFS_BUF_HOLD(bp);
1759 ASSERT(XFS_BUF_ISDONE(bp));
014c2544 1760 return bp;
1da177e4
LT
1761}
1762
1763/*
1764 * Used to free the superblock along various error paths.
1765 */
1766void
1767xfs_freesb(
1768 xfs_mount_t *mp)
1769{
1770 xfs_buf_t *bp;
1771
1772 /*
1773 * Use xfs_getsb() so that the buffer will be locked
1774 * when we call xfs_buf_relse().
1775 */
1776 bp = xfs_getsb(mp, 0);
1777 XFS_BUF_UNMANAGE(bp);
1778 xfs_buf_relse(bp);
1779 mp->m_sb_bp = NULL;
1780}
1781
1782/*
1783 * See if the UUID is unique among mounted XFS filesystems.
1784 * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
1785 */
1786STATIC int
1787xfs_uuid_mount(
1788 xfs_mount_t *mp)
1789{
1790 if (uuid_is_nil(&mp->m_sb.sb_uuid)) {
1791 cmn_err(CE_WARN,
1792 "XFS: Filesystem %s has nil UUID - can't mount",
1793 mp->m_fsname);
1794 return -1;
1795 }
1796 if (!uuid_table_insert(&mp->m_sb.sb_uuid)) {
1797 cmn_err(CE_WARN,
1798 "XFS: Filesystem %s has duplicate UUID - can't mount",
1799 mp->m_fsname);
1800 return -1;
1801 }
1802 return 0;
1803}
1804
1da177e4
LT
1805/*
1806 * Used to log changes to the superblock unit and width fields which could
e6957ea4
ES
1807 * be altered by the mount options, as well as any potential sb_features2
1808 * fixup. Only the first superblock is updated.
1da177e4 1809 */
7884bc86 1810int
ee1c0908 1811xfs_mount_log_sb(
1da177e4
LT
1812 xfs_mount_t *mp,
1813 __int64_t fields)
1814{
1815 xfs_trans_t *tp;
e5720eec 1816 int error;
1da177e4 1817
ee1c0908 1818 ASSERT(fields & (XFS_SB_UNIT | XFS_SB_WIDTH | XFS_SB_UUID |
4b166de0
DC
1819 XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2 |
1820 XFS_SB_VERSIONNUM));
1da177e4
LT
1821
1822 tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT);
e5720eec
DC
1823 error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
1824 XFS_DEFAULT_LOG_COUNT);
1825 if (error) {
1da177e4 1826 xfs_trans_cancel(tp, 0);
e5720eec 1827 return error;
1da177e4
LT
1828 }
1829 xfs_mod_sb(tp, fields);
e5720eec
DC
1830 error = xfs_trans_commit(tp, 0);
1831 return error;
1da177e4 1832}
8d280b98
DC
1833
1834
1835#ifdef HAVE_PERCPU_SB
1836/*
1837 * Per-cpu incore superblock counters
1838 *
1839 * Simple concept, difficult implementation
1840 *
1841 * Basically, replace the incore superblock counters with a distributed per cpu
1842 * counter for contended fields (e.g. free block count).
1843 *
1844 * Difficulties arise in that the incore sb is used for ENOSPC checking, and
1845 * hence needs to be accurately read when we are running low on space. Hence
1846 * there is a method to enable and disable the per-cpu counters based on how
1847 * much "stuff" is available in them.
1848 *
1849 * Basically, a counter is enabled if there is enough free resource to justify
1850 * running a per-cpu fast-path. If the per-cpu counter runs out (i.e. a local
1851 * ENOSPC), then we disable the counters to synchronise all callers and
1852 * re-distribute the available resources.
1853 *
1854 * If, once we redistributed the available resources, we still get a failure,
1855 * we disable the per-cpu counter and go through the slow path.
1856 *
1857 * The slow path is the current xfs_mod_incore_sb() function. This means that
9da096fd 1858 * when we disable a per-cpu counter, we need to drain its resources back to
8d280b98
DC
1859 * the global superblock. We do this after disabling the counter to prevent
1860 * more threads from queueing up on the counter.
1861 *
1862 * Essentially, this means that we still need a lock in the fast path to enable
1863 * synchronisation between the global counters and the per-cpu counters. This
1864 * is not a problem because the lock will be local to a CPU almost all the time
1865 * and have little contention except when we get to ENOSPC conditions.
1866 *
1867 * Basically, this lock becomes a barrier that enables us to lock out the fast
1868 * path while we do things like enabling and disabling counters and
1869 * synchronising the counters.
1870 *
1871 * Locking rules:
1872 *
3685c2a1 1873 * 1. m_sb_lock before picking up per-cpu locks
8d280b98 1874 * 2. per-cpu locks always picked up via for_each_online_cpu() order
3685c2a1 1875 * 3. accurate counter sync requires m_sb_lock + per cpu locks
8d280b98 1876 * 4. modifying per-cpu counters requires holding per-cpu lock
3685c2a1
ES
1877 * 5. modifying global counters requires holding m_sb_lock
1878 * 6. enabling or disabling a counter requires holding the m_sb_lock
8d280b98
DC
1879 * and _none_ of the per-cpu locks.
1880 *
1881 * Disabled counters are only ever re-enabled by a balance operation
1882 * that results in more free resources per CPU than a given threshold.
1883 * To ensure counters don't remain disabled, they are rebalanced when
1884 * the global resource goes above a higher threshold (i.e. some hysteresis
1885 * is present to prevent thrashing).
e8234a68
DC
1886 */
1887
5a67e4c5 1888#ifdef CONFIG_HOTPLUG_CPU
e8234a68
DC
1889/*
1890 * hot-plug CPU notifier support.
8d280b98 1891 *
5a67e4c5
CS
1892 * We need a notifier per filesystem as we need to be able to identify
1893 * the filesystem to balance the counters out. This is achieved by
1894 * having a notifier block embedded in the xfs_mount_t and doing pointer
1895 * magic to get the mount pointer from the notifier block address.
8d280b98 1896 */
e8234a68
DC
1897STATIC int
1898xfs_icsb_cpu_notify(
1899 struct notifier_block *nfb,
1900 unsigned long action,
1901 void *hcpu)
1902{
1903 xfs_icsb_cnts_t *cntp;
1904 xfs_mount_t *mp;
e8234a68
DC
1905
1906 mp = (xfs_mount_t *)container_of(nfb, xfs_mount_t, m_icsb_notifier);
1907 cntp = (xfs_icsb_cnts_t *)
1908 per_cpu_ptr(mp->m_sb_cnts, (unsigned long)hcpu);
1909 switch (action) {
1910 case CPU_UP_PREPARE:
8bb78442 1911 case CPU_UP_PREPARE_FROZEN:
e8234a68
DC
1912 /* Easy Case - initialize the area and locks, and
1913 * then rebalance when online does everything else for us. */
01e1b69c 1914 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
e8234a68
DC
1915 break;
1916 case CPU_ONLINE:
8bb78442 1917 case CPU_ONLINE_FROZEN:
03135cf7 1918 xfs_icsb_lock(mp);
45af6c6d
CH
1919 xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
1920 xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
1921 xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
03135cf7 1922 xfs_icsb_unlock(mp);
e8234a68
DC
1923 break;
1924 case CPU_DEAD:
8bb78442 1925 case CPU_DEAD_FROZEN:
e8234a68
DC
1926 /* Disable all the counters, then fold the dead cpu's
1927 * count into the total on the global superblock and
1928 * re-enable the counters. */
03135cf7 1929 xfs_icsb_lock(mp);
3685c2a1 1930 spin_lock(&mp->m_sb_lock);
e8234a68
DC
1931 xfs_icsb_disable_counter(mp, XFS_SBS_ICOUNT);
1932 xfs_icsb_disable_counter(mp, XFS_SBS_IFREE);
1933 xfs_icsb_disable_counter(mp, XFS_SBS_FDBLOCKS);
1934
1935 mp->m_sb.sb_icount += cntp->icsb_icount;
1936 mp->m_sb.sb_ifree += cntp->icsb_ifree;
1937 mp->m_sb.sb_fdblocks += cntp->icsb_fdblocks;
1938
01e1b69c 1939 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
e8234a68 1940
45af6c6d
CH
1941 xfs_icsb_balance_counter_locked(mp, XFS_SBS_ICOUNT, 0);
1942 xfs_icsb_balance_counter_locked(mp, XFS_SBS_IFREE, 0);
1943 xfs_icsb_balance_counter_locked(mp, XFS_SBS_FDBLOCKS, 0);
3685c2a1 1944 spin_unlock(&mp->m_sb_lock);
03135cf7 1945 xfs_icsb_unlock(mp);
e8234a68
DC
1946 break;
1947 }
1948
1949 return NOTIFY_OK;
1950}
5a67e4c5 1951#endif /* CONFIG_HOTPLUG_CPU */
e8234a68 1952
8d280b98
DC
1953int
1954xfs_icsb_init_counters(
1955 xfs_mount_t *mp)
1956{
1957 xfs_icsb_cnts_t *cntp;
1958 int i;
1959
1960 mp->m_sb_cnts = alloc_percpu(xfs_icsb_cnts_t);
1961 if (mp->m_sb_cnts == NULL)
1962 return -ENOMEM;
1963
5a67e4c5 1964#ifdef CONFIG_HOTPLUG_CPU
e8234a68
DC
1965 mp->m_icsb_notifier.notifier_call = xfs_icsb_cpu_notify;
1966 mp->m_icsb_notifier.priority = 0;
5a67e4c5
CS
1967 register_hotcpu_notifier(&mp->m_icsb_notifier);
1968#endif /* CONFIG_HOTPLUG_CPU */
e8234a68 1969
8d280b98
DC
1970 for_each_online_cpu(i) {
1971 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
01e1b69c 1972 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
8d280b98 1973 }
20b64285
DC
1974
1975 mutex_init(&mp->m_icsb_mutex);
1976
8d280b98
DC
1977 /*
1978 * start with all counters disabled so that the
1979 * initial balance kicks us off correctly
1980 */
1981 mp->m_icsb_counters = -1;
1982 return 0;
1983}
1984
5478eead
LM
1985void
1986xfs_icsb_reinit_counters(
1987 xfs_mount_t *mp)
1988{
1989 xfs_icsb_lock(mp);
1990 /*
1991 * start with all counters disabled so that the
1992 * initial balance kicks us off correctly
1993 */
1994 mp->m_icsb_counters = -1;
45af6c6d
CH
1995 xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
1996 xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
1997 xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
5478eead
LM
1998 xfs_icsb_unlock(mp);
1999}
2000
c962fb79 2001void
8d280b98
DC
2002xfs_icsb_destroy_counters(
2003 xfs_mount_t *mp)
2004{
e8234a68 2005 if (mp->m_sb_cnts) {
5a67e4c5 2006 unregister_hotcpu_notifier(&mp->m_icsb_notifier);
8d280b98 2007 free_percpu(mp->m_sb_cnts);
e8234a68 2008 }
03135cf7 2009 mutex_destroy(&mp->m_icsb_mutex);
8d280b98
DC
2010}
2011
7989cb8e 2012STATIC_INLINE void
01e1b69c
DC
2013xfs_icsb_lock_cntr(
2014 xfs_icsb_cnts_t *icsbp)
2015{
2016 while (test_and_set_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags)) {
2017 ndelay(1000);
2018 }
2019}
2020
7989cb8e 2021STATIC_INLINE void
01e1b69c
DC
2022xfs_icsb_unlock_cntr(
2023 xfs_icsb_cnts_t *icsbp)
2024{
2025 clear_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags);
2026}
2027
8d280b98 2028
7989cb8e 2029STATIC_INLINE void
8d280b98
DC
2030xfs_icsb_lock_all_counters(
2031 xfs_mount_t *mp)
2032{
2033 xfs_icsb_cnts_t *cntp;
2034 int i;
2035
2036 for_each_online_cpu(i) {
2037 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
01e1b69c 2038 xfs_icsb_lock_cntr(cntp);
8d280b98
DC
2039 }
2040}
2041
7989cb8e 2042STATIC_INLINE void
8d280b98
DC
2043xfs_icsb_unlock_all_counters(
2044 xfs_mount_t *mp)
2045{
2046 xfs_icsb_cnts_t *cntp;
2047 int i;
2048
2049 for_each_online_cpu(i) {
2050 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
01e1b69c 2051 xfs_icsb_unlock_cntr(cntp);
8d280b98
DC
2052 }
2053}
2054
2055STATIC void
2056xfs_icsb_count(
2057 xfs_mount_t *mp,
2058 xfs_icsb_cnts_t *cnt,
2059 int flags)
2060{
2061 xfs_icsb_cnts_t *cntp;
2062 int i;
2063
2064 memset(cnt, 0, sizeof(xfs_icsb_cnts_t));
2065
2066 if (!(flags & XFS_ICSB_LAZY_COUNT))
2067 xfs_icsb_lock_all_counters(mp);
2068
2069 for_each_online_cpu(i) {
2070 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
2071 cnt->icsb_icount += cntp->icsb_icount;
2072 cnt->icsb_ifree += cntp->icsb_ifree;
2073 cnt->icsb_fdblocks += cntp->icsb_fdblocks;
2074 }
2075
2076 if (!(flags & XFS_ICSB_LAZY_COUNT))
2077 xfs_icsb_unlock_all_counters(mp);
2078}
2079
2080STATIC int
2081xfs_icsb_counter_disabled(
2082 xfs_mount_t *mp,
2083 xfs_sb_field_t field)
2084{
2085 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
2086 return test_bit(field, &mp->m_icsb_counters);
2087}
2088
36fbe6e6 2089STATIC void
8d280b98
DC
2090xfs_icsb_disable_counter(
2091 xfs_mount_t *mp,
2092 xfs_sb_field_t field)
2093{
2094 xfs_icsb_cnts_t cnt;
2095
2096 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
2097
20b64285
DC
2098 /*
2099 * If we are already disabled, then there is nothing to do
2100 * here. We check before locking all the counters to avoid
2101 * the expensive lock operation when being called in the
2102 * slow path and the counter is already disabled. This is
2103 * safe because the only time we set or clear this state is under
2104 * the m_icsb_mutex.
2105 */
2106 if (xfs_icsb_counter_disabled(mp, field))
36fbe6e6 2107 return;
20b64285 2108
8d280b98
DC
2109 xfs_icsb_lock_all_counters(mp);
2110 if (!test_and_set_bit(field, &mp->m_icsb_counters)) {
2111 /* drain back to superblock */
2112
ce46193b 2113 xfs_icsb_count(mp, &cnt, XFS_ICSB_LAZY_COUNT);
8d280b98
DC
2114 switch(field) {
2115 case XFS_SBS_ICOUNT:
2116 mp->m_sb.sb_icount = cnt.icsb_icount;
2117 break;
2118 case XFS_SBS_IFREE:
2119 mp->m_sb.sb_ifree = cnt.icsb_ifree;
2120 break;
2121 case XFS_SBS_FDBLOCKS:
2122 mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
2123 break;
2124 default:
2125 BUG();
2126 }
2127 }
2128
2129 xfs_icsb_unlock_all_counters(mp);
8d280b98
DC
2130}
2131
2132STATIC void
2133xfs_icsb_enable_counter(
2134 xfs_mount_t *mp,
2135 xfs_sb_field_t field,
2136 uint64_t count,
2137 uint64_t resid)
2138{
2139 xfs_icsb_cnts_t *cntp;
2140 int i;
2141
2142 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
2143
2144 xfs_icsb_lock_all_counters(mp);
2145 for_each_online_cpu(i) {
2146 cntp = per_cpu_ptr(mp->m_sb_cnts, i);
2147 switch (field) {
2148 case XFS_SBS_ICOUNT:
2149 cntp->icsb_icount = count + resid;
2150 break;
2151 case XFS_SBS_IFREE:
2152 cntp->icsb_ifree = count + resid;
2153 break;
2154 case XFS_SBS_FDBLOCKS:
2155 cntp->icsb_fdblocks = count + resid;
2156 break;
2157 default:
2158 BUG();
2159 break;
2160 }
2161 resid = 0;
2162 }
2163 clear_bit(field, &mp->m_icsb_counters);
2164 xfs_icsb_unlock_all_counters(mp);
2165}
2166
dbcabad1 2167void
d4d90b57 2168xfs_icsb_sync_counters_locked(
8d280b98
DC
2169 xfs_mount_t *mp,
2170 int flags)
2171{
2172 xfs_icsb_cnts_t cnt;
8d280b98 2173
8d280b98
DC
2174 xfs_icsb_count(mp, &cnt, flags);
2175
8d280b98
DC
2176 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_ICOUNT))
2177 mp->m_sb.sb_icount = cnt.icsb_icount;
2178 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_IFREE))
2179 mp->m_sb.sb_ifree = cnt.icsb_ifree;
2180 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_FDBLOCKS))
2181 mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
8d280b98
DC
2182}
2183
2184/*
2185 * Accurate update of per-cpu counters to incore superblock
2186 */
d4d90b57 2187void
8d280b98 2188xfs_icsb_sync_counters(
d4d90b57
CH
2189 xfs_mount_t *mp,
2190 int flags)
8d280b98 2191{
d4d90b57
CH
2192 spin_lock(&mp->m_sb_lock);
2193 xfs_icsb_sync_counters_locked(mp, flags);
2194 spin_unlock(&mp->m_sb_lock);
8d280b98
DC
2195}
2196
2197/*
2198 * Balance and enable/disable counters as necessary.
2199 *
20b64285
DC
2200 * Thresholds for re-enabling counters are somewhat magic. inode counts are
2201 * chosen to be the same number as single on disk allocation chunk per CPU, and
2202 * free blocks is something far enough zero that we aren't going thrash when we
2203 * get near ENOSPC. We also need to supply a minimum we require per cpu to
2204 * prevent looping endlessly when xfs_alloc_space asks for more than will
2205 * be distributed to a single CPU but each CPU has enough blocks to be
2206 * reenabled.
2207 *
2208 * Note that we can be called when counters are already disabled.
2209 * xfs_icsb_disable_counter() optimises the counter locking in this case to
2210 * prevent locking every per-cpu counter needlessly.
8d280b98 2211 */
20b64285
DC
2212
2213#define XFS_ICSB_INO_CNTR_REENABLE (uint64_t)64
4be536de 2214#define XFS_ICSB_FDBLK_CNTR_REENABLE(mp) \
20b64285 2215 (uint64_t)(512 + XFS_ALLOC_SET_ASIDE(mp))
8d280b98 2216STATIC void
45af6c6d 2217xfs_icsb_balance_counter_locked(
8d280b98
DC
2218 xfs_mount_t *mp,
2219 xfs_sb_field_t field,
20b64285 2220 int min_per_cpu)
8d280b98 2221{
6fdf8ccc 2222 uint64_t count, resid;
8d280b98 2223 int weight = num_online_cpus();
20b64285 2224 uint64_t min = (uint64_t)min_per_cpu;
8d280b98 2225
8d280b98
DC
2226 /* disable counter and sync counter */
2227 xfs_icsb_disable_counter(mp, field);
2228
2229 /* update counters - first CPU gets residual*/
2230 switch (field) {
2231 case XFS_SBS_ICOUNT:
2232 count = mp->m_sb.sb_icount;
2233 resid = do_div(count, weight);
20b64285 2234 if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
45af6c6d 2235 return;
8d280b98
DC
2236 break;
2237 case XFS_SBS_IFREE:
2238 count = mp->m_sb.sb_ifree;
2239 resid = do_div(count, weight);
20b64285 2240 if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
45af6c6d 2241 return;
8d280b98
DC
2242 break;
2243 case XFS_SBS_FDBLOCKS:
2244 count = mp->m_sb.sb_fdblocks;
2245 resid = do_div(count, weight);
20b64285 2246 if (count < max(min, XFS_ICSB_FDBLK_CNTR_REENABLE(mp)))
45af6c6d 2247 return;
8d280b98
DC
2248 break;
2249 default:
2250 BUG();
6fdf8ccc 2251 count = resid = 0; /* quiet, gcc */
8d280b98
DC
2252 break;
2253 }
2254
2255 xfs_icsb_enable_counter(mp, field, count, resid);
45af6c6d
CH
2256}
2257
2258STATIC void
2259xfs_icsb_balance_counter(
2260 xfs_mount_t *mp,
2261 xfs_sb_field_t fields,
2262 int min_per_cpu)
2263{
2264 spin_lock(&mp->m_sb_lock);
2265 xfs_icsb_balance_counter_locked(mp, fields, min_per_cpu);
2266 spin_unlock(&mp->m_sb_lock);
8d280b98
DC
2267}
2268
a8272ce0 2269STATIC int
20b64285 2270xfs_icsb_modify_counters(
8d280b98
DC
2271 xfs_mount_t *mp,
2272 xfs_sb_field_t field,
20f4ebf2 2273 int64_t delta,
20b64285 2274 int rsvd)
8d280b98
DC
2275{
2276 xfs_icsb_cnts_t *icsbp;
2277 long long lcounter; /* long counter for 64 bit fields */
007c61c6 2278 int cpu, ret = 0;
8d280b98 2279
20b64285 2280 might_sleep();
8d280b98
DC
2281again:
2282 cpu = get_cpu();
20b64285
DC
2283 icsbp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, cpu);
2284
2285 /*
2286 * if the counter is disabled, go to slow path
2287 */
8d280b98
DC
2288 if (unlikely(xfs_icsb_counter_disabled(mp, field)))
2289 goto slow_path;
20b64285
DC
2290 xfs_icsb_lock_cntr(icsbp);
2291 if (unlikely(xfs_icsb_counter_disabled(mp, field))) {
2292 xfs_icsb_unlock_cntr(icsbp);
2293 goto slow_path;
2294 }
8d280b98
DC
2295
2296 switch (field) {
2297 case XFS_SBS_ICOUNT:
2298 lcounter = icsbp->icsb_icount;
2299 lcounter += delta;
2300 if (unlikely(lcounter < 0))
20b64285 2301 goto balance_counter;
8d280b98
DC
2302 icsbp->icsb_icount = lcounter;
2303 break;
2304
2305 case XFS_SBS_IFREE:
2306 lcounter = icsbp->icsb_ifree;
2307 lcounter += delta;
2308 if (unlikely(lcounter < 0))
20b64285 2309 goto balance_counter;
8d280b98
DC
2310 icsbp->icsb_ifree = lcounter;
2311 break;
2312
2313 case XFS_SBS_FDBLOCKS:
2314 BUG_ON((mp->m_resblks - mp->m_resblks_avail) != 0);
2315
4be536de 2316 lcounter = icsbp->icsb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
8d280b98
DC
2317 lcounter += delta;
2318 if (unlikely(lcounter < 0))
20b64285 2319 goto balance_counter;
4be536de 2320 icsbp->icsb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
8d280b98
DC
2321 break;
2322 default:
2323 BUG();
2324 break;
2325 }
01e1b69c 2326 xfs_icsb_unlock_cntr(icsbp);
8d280b98 2327 put_cpu();
8d280b98
DC
2328 return 0;
2329
8d280b98 2330slow_path:
8d280b98
DC
2331 put_cpu();
2332
20b64285
DC
2333 /*
2334 * serialise with a mutex so we don't burn lots of cpu on
2335 * the superblock lock. We still need to hold the superblock
2336 * lock, however, when we modify the global structures.
2337 */
03135cf7 2338 xfs_icsb_lock(mp);
20b64285
DC
2339
2340 /*
2341 * Now running atomically.
2342 *
2343 * If the counter is enabled, someone has beaten us to rebalancing.
2344 * Drop the lock and try again in the fast path....
2345 */
2346 if (!(xfs_icsb_counter_disabled(mp, field))) {
03135cf7 2347 xfs_icsb_unlock(mp);
8d280b98 2348 goto again;
8d280b98
DC
2349 }
2350
20b64285
DC
2351 /*
2352 * The counter is currently disabled. Because we are
2353 * running atomically here, we know a rebalance cannot
2354 * be in progress. Hence we can go straight to operating
2355 * on the global superblock. We do not call xfs_mod_incore_sb()
3685c2a1 2356 * here even though we need to get the m_sb_lock. Doing so
20b64285 2357 * will cause us to re-enter this function and deadlock.
3685c2a1 2358 * Hence we get the m_sb_lock ourselves and then call
20b64285
DC
2359 * xfs_mod_incore_sb_unlocked() as the unlocked path operates
2360 * directly on the global counters.
2361 */
3685c2a1 2362 spin_lock(&mp->m_sb_lock);
8d280b98 2363 ret = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
3685c2a1 2364 spin_unlock(&mp->m_sb_lock);
8d280b98 2365
20b64285
DC
2366 /*
2367 * Now that we've modified the global superblock, we
2368 * may be able to re-enable the distributed counters
2369 * (e.g. lots of space just got freed). After that
2370 * we are done.
2371 */
2372 if (ret != ENOSPC)
45af6c6d 2373 xfs_icsb_balance_counter(mp, field, 0);
03135cf7 2374 xfs_icsb_unlock(mp);
8d280b98 2375 return ret;
8d280b98 2376
20b64285
DC
2377balance_counter:
2378 xfs_icsb_unlock_cntr(icsbp);
2379 put_cpu();
8d280b98 2380
20b64285
DC
2381 /*
2382 * We may have multiple threads here if multiple per-cpu
2383 * counters run dry at the same time. This will mean we can
2384 * do more balances than strictly necessary but it is not
2385 * the common slowpath case.
2386 */
03135cf7 2387 xfs_icsb_lock(mp);
20b64285
DC
2388
2389 /*
2390 * running atomically.
2391 *
2392 * This will leave the counter in the correct state for future
2393 * accesses. After the rebalance, we simply try again and our retry
2394 * will either succeed through the fast path or slow path without
2395 * another balance operation being required.
2396 */
45af6c6d 2397 xfs_icsb_balance_counter(mp, field, delta);
03135cf7 2398 xfs_icsb_unlock(mp);
20b64285 2399 goto again;
8d280b98 2400}
20b64285 2401
8d280b98 2402#endif