1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
10 #include "libxfs_priv.h"
12 #include "xfs_shared.h"
13 #include "xfs_format.h"
14 #include "xfs_log_format.h"
15 #include "xfs_trans_resv.h"
16 #include "xfs_mount.h"
17 #include "xfs_defer.h"
18 #include "xfs_inode_buf.h"
19 #include "xfs_inode_fork.h"
20 #include "xfs_inode.h"
21 #include "xfs_trans.h"
22 #include "xfs_rmap_btree.h"
23 #include "xfs_refcount_btree.h"
24 #include "xfs_metafile.h"
25 #include "libfrog/platform.h"
26 #include "libfrog/util.h"
27 #include "libxfs/xfile.h"
28 #include "libxfs/buf_mem.h"
30 #include "xfs_format.h"
31 #include "xfs_da_format.h"
32 #include "xfs_log_format.h"
33 #include "xfs_ondisk.h"
35 #include "libxfs.h" /* for now */
36 #include "xfs_rtgroup.h"
38 #ifndef HAVE_LIBURCU_ATOMIC64
39 pthread_mutex_t atomic64_lock
= PTHREAD_MUTEX_INITIALIZER
;
42 char *progname
= "libxfs"; /* default, changed by each tool */
44 int libxfs_bhash_size
; /* #buckets in bcache */
46 int use_xfs_buf_lock
; /* global flag: use xfs_buf locks for MT */
48 static int nextfakedev
= -1; /* device number to give to next fake device */
50 unsigned int PAGE_SHIFT
;
53 * Checks whether a given device has a mounted, writable
54 * filesystem, returns 1 if it does & fatal (just warns
55 * if not fatal, but allows us to proceed).
57 * Useful to tools which will produce uncertain results
58 * if the filesystem is active - repair, check, logprint.
61 check_isactive(char *name
, char *block
, int fatal
)
65 if (stat(block
, &st
) < 0)
67 if ((st
.st_mode
& S_IFMT
) != S_IFBLK
)
69 if (platform_check_ismounted(name
, block
, &st
, 0) == 0)
71 if (platform_check_iswritable(name
, block
, &st
))
78 struct libxfs_init
*xi
,
79 struct libxfs_dev
*dev
)
83 if (stat(dev
->name
, &stbuf
) < 0) {
87 if (!(xi
->flags
& LIBXFS_ISREADONLY
) &&
88 !(xi
->flags
& LIBXFS_ISINACTIVE
) &&
89 platform_check_ismounted(dev
->name
, dev
->name
, NULL
, 1))
92 if ((xi
->flags
& LIBXFS_ISINACTIVE
) &&
93 check_isactive(dev
->name
, dev
->name
, !!(xi
->flags
&
94 (LIBXFS_ISREADONLY
| LIBXFS_DANGEROUSLY
))))
102 struct libxfs_init
*xi
,
103 struct libxfs_dev
*dev
)
112 if (!dev
->isfile
&& !check_open(xi
, dev
))
115 if (xi
->flags
& LIBXFS_ISREADONLY
)
121 flags
|= O_CREAT
| O_TRUNC
;
123 if (xi
->flags
& LIBXFS_EXCLUSIVELY
)
125 if ((xi
->flags
& LIBXFS_DIRECT
) && platform_direct_blockdev())
130 dev
->fd
= open(dev
->name
, flags
, 0666);
132 if (errno
== EINVAL
&& (flags
& O_DIRECT
)) {
136 fprintf(stderr
, _("%s: cannot open %s: %s\n"),
137 progname
, dev
->name
, strerror(errno
));
141 if (fstat(dev
->fd
, &statb
) < 0) {
142 fprintf(stderr
, _("%s: cannot stat %s: %s\n"),
143 progname
, dev
->name
, strerror(errno
));
147 if (!(xi
->flags
& LIBXFS_ISREADONLY
) &&
149 (statb
.st_mode
& S_IFMT
) == S_IFBLK
) {
151 * Try to use the given explicit blocksize. Failure to set the
152 * block size is only fatal for direct I/O.
154 platform_set_blocksize(dev
->fd
, dev
->name
, statb
.st_rdev
,
155 xi
->setblksize
, flags
& O_DIRECT
);
159 * Get the device number from the stat buf - unless we're not opening a
160 * real device, in which case choose a new fake device number.
163 dev
->dev
= statb
.st_rdev
;
165 dev
->dev
= nextfakedev
--;
166 platform_findsizes(dev
->name
, dev
->fd
, &dev
->size
, &dev
->bsize
);
172 struct libxfs_dev
*dev
)
176 ret
= platform_flush_device(dev
->fd
, dev
->dev
);
180 _("%s: flush of device %s failed, err=%d"),
181 progname
, dev
->name
, ret
);
190 * Initialize/destroy all of the cache allocators we use.
197 /* initialise cache allocation */
198 xfs_buf_cache
= kmem_cache_init(sizeof(struct xfs_buf
), "xfs_buffer");
199 xfs_inode_cache
= kmem_cache_init(sizeof(struct xfs_inode
), "xfs_inode");
200 xfs_ifork_cache
= kmem_cache_init(sizeof(struct xfs_ifork
), "xfs_ifork");
201 xfs_ili_cache
= kmem_cache_init(
202 sizeof(struct xfs_inode_log_item
),"xfs_inode_log_item");
203 xfs_buf_item_cache
= kmem_cache_init(
204 sizeof(struct xfs_buf_log_item
), "xfs_buf_log_item");
205 error
= xfs_defer_init_item_caches();
207 fprintf(stderr
, "Could not allocate defer init item caches.\n");
210 xfs_da_state_cache
= kmem_cache_init(
211 sizeof(struct xfs_da_state
), "xfs_da_state");
212 error
= xfs_btree_init_cur_caches();
214 fprintf(stderr
, "Could not allocate btree cursor caches.\n");
217 xfs_extfree_item_cache
= kmem_cache_init(
218 sizeof(struct xfs_extent_free_item
),
220 xfs_trans_cache
= kmem_cache_init(
221 sizeof(struct xfs_trans
), "xfs_trans");
222 xfs_parent_args_cache
= kmem_cache_init(
223 sizeof(struct xfs_parent_args
), "xfs_parent_args");
231 leaked
+= kmem_cache_destroy(xfs_buf_cache
);
232 leaked
+= kmem_cache_destroy(xfs_ili_cache
);
233 leaked
+= kmem_cache_destroy(xfs_inode_cache
);
234 leaked
+= kmem_cache_destroy(xfs_ifork_cache
);
235 leaked
+= kmem_cache_destroy(xfs_buf_item_cache
);
236 leaked
+= kmem_cache_destroy(xfs_da_state_cache
);
237 xfs_defer_destroy_item_caches();
238 xfs_btree_destroy_cur_caches();
239 leaked
+= kmem_cache_destroy(xfs_extfree_item_cache
);
240 leaked
+= kmem_cache_destroy(xfs_trans_cache
);
241 leaked
+= kmem_cache_destroy(xfs_parent_args_cache
);
247 libxfs_close_devices(
248 struct libxfs_init
*li
)
251 libxfs_device_close(&li
->data
);
252 if (li
->log
.dev
&& li
->log
.dev
!= li
->data
.dev
)
253 libxfs_device_close(&li
->log
);
254 if (li
->rt
.dev
&& li
->rt
.dev
!= li
->data
.dev
)
255 libxfs_device_close(&li
->rt
);
259 * libxfs initialization.
260 * Caller gets a 0 on failure (and we print a message), 1 on success.
263 libxfs_init(struct libxfs_init
*a
)
266 PAGE_SHIFT
= log2_roundup(PAGE_SIZE
);
267 xfs_check_ondisk_structs();
270 rcu_register_thread();
273 if (!libxfs_device_open(a
, &a
->data
))
275 if (!libxfs_device_open(a
, &a
->log
))
277 if (!libxfs_device_open(a
, &a
->rt
))
280 if (!libxfs_bhash_size
)
281 libxfs_bhash_size
= LIBXFS_BHASHSIZE(sbp
);
282 use_xfs_buf_lock
= a
->flags
& LIBXFS_USEBUFLOCK
;
288 libxfs_close_devices(a
);
289 rcu_unregister_thread();
295 * Initialize realtime fields in the mount structure.
299 xfs_mount_t
*mp
) /* file system mount structure */
301 struct xfs_buf
*bp
; /* buffer for last block of subvolume */
302 xfs_daddr_t d
; /* address of last block of subvolume */
305 if (mp
->m_sb
.sb_rblocks
== 0)
308 if (xfs_has_reflink(mp
) && mp
->m_sb
.sb_rextsize
> 1) {
310 _("%s: Reflink not compatible with realtime extent size > 1. Please try a newer xfsprogs.\n"),
315 if (mp
->m_rtdev_targp
->bt_bdev
== 0 && !xfs_is_debugger(mp
)) {
316 fprintf(stderr
, _("%s: filesystem has a realtime subvolume\n"),
320 mp
->m_rsumblocks
= xfs_rtsummary_blockcount(mp
, &mp
->m_rsumlevels
);
323 * Allow debugger to be run without the realtime device present.
325 if (xfs_is_debugger(mp
))
329 * Check that the realtime section is an ok size.
331 d
= (xfs_daddr_t
)XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_rblocks
);
332 if (XFS_BB_TO_FSB(mp
, d
) != mp
->m_sb
.sb_rblocks
) {
333 fprintf(stderr
, _("%s: realtime init - %llu != %llu\n"),
334 progname
, (unsigned long long) XFS_BB_TO_FSB(mp
, d
),
335 (unsigned long long) mp
->m_sb
.sb_rblocks
);
338 error
= libxfs_buf_read(mp
->m_rtdev
, d
- XFS_FSB_TO_BB(mp
, 1),
339 XFS_FSB_TO_BB(mp
, 1), 0, &bp
, NULL
);
341 fprintf(stderr
, _("%s: realtime size check failed\n"),
345 libxfs_buf_relse(bp
);
350 xfs_set_inode_alloc_perag(
351 struct xfs_perag
*pag
,
353 xfs_agnumber_t max_metadata
)
355 if (!xfs_is_inode32(pag_mount(pag
))) {
356 set_bit(XFS_AGSTATE_ALLOWS_INODES
, &pag
->pag_opstate
);
357 clear_bit(XFS_AGSTATE_PREFERS_METADATA
, &pag
->pag_opstate
);
361 if (ino
> XFS_MAXINUMBER_32
) {
362 clear_bit(XFS_AGSTATE_ALLOWS_INODES
, &pag
->pag_opstate
);
363 clear_bit(XFS_AGSTATE_PREFERS_METADATA
, &pag
->pag_opstate
);
367 set_bit(XFS_AGSTATE_ALLOWS_INODES
, &pag
->pag_opstate
);
368 if (pag_agno(pag
) < max_metadata
)
369 set_bit(XFS_AGSTATE_PREFERS_METADATA
, &pag
->pag_opstate
);
371 clear_bit(XFS_AGSTATE_PREFERS_METADATA
, &pag
->pag_opstate
);
376 * Set parameters for inode allocation heuristics, taking into account
377 * filesystem size and inode32/inode64 mount options; i.e. specifically
378 * whether or not XFS_MOUNT_SMALL_INUMS is set.
380 * Inode allocation patterns are altered only if inode32 is requested
381 * (XFS_MOUNT_SMALL_INUMS), and the filesystem is sufficiently large.
382 * If altered, XFS_MOUNT_32BITINODES is set as well.
384 * An agcount independent of that in the mount structure is provided
385 * because in the growfs case, mp->m_sb.sb_agcount is not yet updated
386 * to the potentially higher ag count.
388 * Returns the maximum AG index which may contain inodes.
390 * NOTE: userspace has no concept of "inode32" and so xfs_has_small_inums
391 * is always false, and much of this code is a no-op.
395 struct xfs_mount
*mp
,
396 xfs_agnumber_t agcount
)
398 xfs_agnumber_t index
;
399 xfs_agnumber_t maxagi
= 0;
400 xfs_sb_t
*sbp
= &mp
->m_sb
;
401 xfs_agnumber_t max_metadata
;
406 * Calculate how much should be reserved for inodes to meet
407 * the max inode percentage. Used only for inode32.
409 if (M_IGEO(mp
)->maxicount
) {
412 icount
= sbp
->sb_dblocks
* sbp
->sb_imax_pct
;
414 icount
+= sbp
->sb_agblocks
- 1;
415 do_div(icount
, sbp
->sb_agblocks
);
416 max_metadata
= icount
;
418 max_metadata
= agcount
;
421 /* Get the last possible inode in the filesystem */
422 agino
= XFS_AGB_TO_AGINO(mp
, sbp
->sb_agblocks
- 1);
423 ino
= XFS_AGINO_TO_INO(mp
, agcount
- 1, agino
);
426 * If user asked for no more than 32-bit inodes, and the fs is
427 * sufficiently large, set XFS_MOUNT_32BITINODES if we must alter
428 * the allocator to accommodate the request.
430 if (xfs_has_small_inums(mp
) && ino
> XFS_MAXINUMBER_32
)
431 set_bit(XFS_OPSTATE_INODE32
, &mp
->m_opstate
);
433 clear_bit(XFS_OPSTATE_INODE32
, &mp
->m_opstate
);
435 for (index
= 0; index
< agcount
; index
++) {
436 struct xfs_perag
*pag
;
438 ino
= XFS_AGINO_TO_INO(mp
, index
, agino
);
440 pag
= xfs_perag_get(mp
, index
);
441 if (xfs_set_inode_alloc_perag(pag
, ino
, max_metadata
))
446 return xfs_is_inode32(mp
) ? maxagi
: agcount
;
449 static struct xfs_buftarg
*
450 libxfs_buftarg_alloc(
451 struct xfs_mount
*mp
,
452 struct libxfs_init
*xi
,
453 struct libxfs_dev
*dev
,
454 unsigned long write_fails
)
456 struct xfs_buftarg
*btp
;
458 btp
= malloc(sizeof(*btp
));
460 fprintf(stderr
, _("%s: buftarg init failed\n"),
465 btp
->bt_bdev
= dev
->dev
;
466 btp
->bt_bdev_fd
= dev
->fd
;
467 btp
->bt_xfile
= NULL
;
470 btp
->writes_left
= write_fails
;
471 btp
->flags
|= XFS_BUFTARG_INJECT_WRITE_FAIL
;
473 pthread_mutex_init(&btp
->lock
, NULL
);
475 btp
->bcache
= cache_init(xi
->bcache_flags
, libxfs_bhash_size
,
476 &libxfs_bcache_operations
);
481 enum libxfs_write_failure_nums
{
488 static char *wf_opts
[] = {
492 [WF_MAX_OPTS
] = NULL
,
497 struct xfs_mount
*mp
,
498 struct libxfs_init
*xi
)
500 char *p
= getenv("LIBXFS_DEBUG_WRITE_CRASH");
501 unsigned long dfail
= 0, lfail
= 0, rfail
= 0;
503 /* Simulate utility crash after a certain number of writes. */
507 switch (getsubopt(&p
, wf_opts
, &val
)) {
511 _("ddev write fail requires a parameter\n"));
514 dfail
= strtoul(val
, NULL
, 0);
519 _("logdev write fail requires a parameter\n"));
522 lfail
= strtoul(val
, NULL
, 0);
527 _("rtdev write fail requires a parameter\n"));
530 rfail
= strtoul(val
, NULL
, 0);
533 fprintf(stderr
, _("unknown write fail type %s\n"),
540 if (mp
->m_ddev_targp
) {
541 /* should already have all buftargs initialised */
542 if (mp
->m_ddev_targp
->bt_bdev
!= xi
->data
.dev
||
543 mp
->m_ddev_targp
->bt_mount
!= mp
) {
545 _("%s: bad buftarg reinit, ddev\n"),
549 if (!xi
->log
.dev
|| xi
->log
.dev
== xi
->data
.dev
) {
550 if (mp
->m_logdev_targp
!= mp
->m_ddev_targp
) {
552 _("%s: bad buftarg reinit, ldev mismatch\n"),
556 } else if (mp
->m_logdev_targp
->bt_bdev
!= xi
->log
.dev
||
557 mp
->m_logdev_targp
->bt_mount
!= mp
) {
559 _("%s: bad buftarg reinit, logdev\n"),
563 if ((xi
->rt
.dev
|| xi
->rt
.dev
== xi
->data
.dev
) &&
564 (mp
->m_rtdev_targp
->bt_bdev
!= xi
->rt
.dev
||
565 mp
->m_rtdev_targp
->bt_mount
!= mp
)) {
567 _("%s: bad buftarg reinit, rtdev\n"),
574 mp
->m_ddev_targp
= libxfs_buftarg_alloc(mp
, xi
, &xi
->data
, dfail
);
575 if (!xi
->log
.dev
|| xi
->log
.dev
== xi
->data
.dev
)
576 mp
->m_logdev_targp
= mp
->m_ddev_targp
;
578 mp
->m_logdev_targp
= libxfs_buftarg_alloc(mp
, xi
, &xi
->log
,
580 if (!xi
->rt
.dev
|| xi
->rt
.dev
== xi
->data
.dev
)
581 mp
->m_rtdev_targp
= mp
->m_ddev_targp
;
583 mp
->m_rtdev_targp
= libxfs_buftarg_alloc(mp
, xi
, &xi
->rt
,
587 /* Compute maximum possible height for per-AG btree types for this fs. */
589 xfs_agbtree_compute_maxlevels(
590 struct xfs_mount
*mp
)
594 levels
= max(mp
->m_alloc_maxlevels
, M_IGEO(mp
)->inobt_maxlevels
);
595 levels
= max(levels
, mp
->m_rmap_maxlevels
);
596 mp
->m_agbtree_maxlevels
= max(levels
, mp
->m_refc_maxlevels
);
599 /* Compute maximum possible height for realtime btree types for this fs. */
601 xfs_rtbtree_compute_maxlevels(
602 struct xfs_mount
*mp
)
604 mp
->m_rtbtree_maxlevels
= max(mp
->m_rtrmap_maxlevels
,
605 mp
->m_rtrefc_maxlevels
);
608 /* Compute maximum possible height of all btrees. */
610 libxfs_compute_all_maxlevels(
611 struct xfs_mount
*mp
)
613 struct xfs_ino_geometry
*igeo
= M_IGEO(mp
);
615 xfs_alloc_compute_maxlevels(mp
);
616 xfs_bmap_compute_maxlevels(mp
, XFS_DATA_FORK
);
617 xfs_bmap_compute_maxlevels(mp
, XFS_ATTR_FORK
);
618 igeo
->attr_fork_offset
= xfs_bmap_compute_attr_offset(mp
);
619 xfs_ialloc_setup_geometry(mp
);
620 xfs_rmapbt_compute_maxlevels(mp
);
621 xfs_rtrmapbt_compute_maxlevels(mp
);
622 xfs_refcountbt_compute_maxlevels(mp
);
623 xfs_rtrefcountbt_compute_maxlevels(mp
);
625 xfs_agbtree_compute_maxlevels(mp
);
626 xfs_rtbtree_compute_maxlevels(mp
);
629 /* Mount the metadata files under the metadata directory tree. */
631 libxfs_mount_setup_metadir(
632 struct xfs_mount
*mp
)
636 /* Ignore filesystems that are under construction. */
637 if (mp
->m_sb
.sb_inprogress
)
640 error
= -libxfs_metafile_iget(mp
, mp
->m_sb
.sb_metadirino
,
641 XFS_METAFILE_DIR
, &mp
->m_metadirip
);
644 _("%s: Failed to load metadir root directory, error %d\n"),
651 * precalculate the low space thresholds for dynamic speculative preallocation.
654 xfs_set_low_space_thresholds(
655 struct xfs_mount
*mp
)
657 uint64_t dblocks
= mp
->m_sb
.sb_dblocks
;
660 do_div(dblocks
, 100);
662 for (i
= 0; i
< XFS_LOWSP_MAX
; i
++)
663 mp
->m_low_space
[i
] = dblocks
* (i
+ 1);
667 * libxfs_initialize_rtgroup will allocate a rtgroup structure for each
668 * rtgroup. If rgcount is corrupted and insanely high, this will OOM the box.
669 * Try to read what would be the last rtgroup superblock. If that fails, read
670 * the first one and let the user know to check the geometry.
674 struct xfs_mount
*mp
,
681 if (!mp
->m_rtdev
->bt_bdev
) {
682 fprintf(stderr
, _("%s: no rt device, ignoring rgcount %u\n"),
683 progname
, sbp
->sb_rgcount
);
684 if (!xfs_is_debugger(mp
))
691 d
= (xfs_daddr_t
)XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_rblocks
);
692 error
= libxfs_buf_read(mp
->m_rtdev
, d
- XFS_FSB_TO_BB(mp
, 1), 1, 0,
695 libxfs_buf_relse(bp
);
699 fprintf(stderr
, _("%s: read of rtgroup %u failed\n"), progname
,
700 sbp
->sb_rgcount
- 1);
701 if (!xfs_is_debugger(mp
))
704 fprintf(stderr
, _("%s: limiting reads to rtgroup 0\n"), progname
);
710 * Mount structure initialization, provides a filled-in xfs_mount_t
711 * such that the numerous XFS_* macros can be used. If dev is zero,
712 * no IO will be performed (no size checks, read root inodes).
716 struct xfs_mount
*mp
,
718 struct libxfs_init
*xi
,
727 mp
->m_features
= xfs_sb_version_to_features(sb
);
728 if (flags
& LIBXFS_MOUNT_DEBUGGER
)
729 xfs_set_debugger(mp
);
730 if (flags
& LIBXFS_MOUNT_REPORT_CORRUPTION
)
731 xfs_set_reporting_corruption(mp
);
732 libxfs_buftarg_init(mp
, xi
);
735 mp
->m_fsname
= strdup(xi
->data
.name
);
739 mp
->m_finobt_nores
= true;
742 for (i
= 0; i
< XG_TYPE_MAX
; i
++)
743 xa_init(&mp
->m_groups
[i
].xa
);
745 spin_lock_init(&mp
->m_sb_lock
);
746 spin_lock_init(&mp
->m_agirotor_lock
);
748 xfs_sb_mount_common(mp
, sb
);
751 * Set whether we're using stripe alignment.
753 if (xfs_has_dalign(mp
)) {
754 mp
->m_dalign
= sbp
->sb_unit
;
755 mp
->m_swidth
= sbp
->sb_width
;
758 libxfs_compute_all_maxlevels(mp
);
761 * Check that the data (and log if separate) are an ok size.
763 d
= (xfs_daddr_t
) XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_dblocks
);
764 if (XFS_BB_TO_FSB(mp
, d
) != mp
->m_sb
.sb_dblocks
) {
765 fprintf(stderr
, _("%s: size check failed\n"), progname
);
766 if (!xfs_is_debugger(mp
))
771 * We automatically convert v1 inodes to v2 inodes now, so if
772 * the NLINK bit is not set we can't operate on the filesystem.
774 if (!(sbp
->sb_versionnum
& XFS_SB_VERSION_NLINKBIT
)) {
777 "%s: V1 inodes unsupported. Please try an older xfsprogs.\n"),
782 /* Check for supported directory formats */
783 if (!(sbp
->sb_versionnum
& XFS_SB_VERSION_DIRV2BIT
)) {
786 "%s: V1 directories unsupported. Please try an older xfsprogs.\n"),
791 /* check for unsupported other features */
792 if (!xfs_sb_good_version(sbp
)) {
794 "%s: Unsupported features detected. Please try a newer xfsprogs.\n"),
801 /* Initialize the precomputed transaction reservations values */
804 if (xi
->data
.dev
== 0) /* maxtrres, we have no device so leave now */
807 /* device size checks must pass unless we're a debugger. */
808 error
= libxfs_buf_read(mp
->m_dev
, d
- XFS_FSS_TO_BB(mp
, 1),
809 XFS_FSS_TO_BB(mp
, 1), 0, &bp
, NULL
);
811 fprintf(stderr
, _("%s: data size check failed\n"), progname
);
812 if (!xfs_is_debugger(mp
))
815 libxfs_buf_relse(bp
);
817 if (mp
->m_logdev_targp
->bt_bdev
&&
818 mp
->m_logdev_targp
->bt_bdev
!= mp
->m_ddev_targp
->bt_bdev
) {
819 d
= (xfs_daddr_t
) XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_logblocks
);
820 if (XFS_BB_TO_FSB(mp
, d
) != mp
->m_sb
.sb_logblocks
||
821 libxfs_buf_read(mp
->m_logdev_targp
,
822 d
- XFS_FSB_TO_BB(mp
, 1), XFS_FSB_TO_BB(mp
, 1),
824 fprintf(stderr
, _("%s: log size checks failed\n"),
826 if (!xfs_is_debugger(mp
))
830 libxfs_buf_relse(bp
);
833 xfs_set_low_space_thresholds(mp
);
835 /* Initialize realtime fields in the mount structure */
836 if (rtmount_init(mp
)) {
837 fprintf(stderr
, _("%s: realtime device init failed\n"),
843 * libxfs_initialize_perag will allocate a perag structure for each ag.
844 * If agcount is corrupted and insanely high, this will OOM the box.
845 * If the agount seems (arbitrarily) high, try to read what would be
846 * the last AG, and if that fails for a relatively high agcount, just
847 * read the first one and let the user know to check the geometry.
849 if (sbp
->sb_agcount
> 1000000) {
850 error
= libxfs_buf_read(mp
->m_dev
,
851 XFS_AG_DADDR(mp
, sbp
->sb_agcount
- 1, 0), 1,
854 fprintf(stderr
, _("%s: read of AG %u failed\n"),
855 progname
, sbp
->sb_agcount
);
856 if (!xfs_is_debugger(mp
))
858 fprintf(stderr
, _("%s: limiting reads to AG 0\n"),
862 libxfs_buf_relse(bp
);
865 if (sbp
->sb_rgcount
> 1000000 && !check_many_rtgroups(mp
, sbp
))
868 error
= libxfs_initialize_perag(mp
, 0, sbp
->sb_agcount
,
869 sbp
->sb_dblocks
, &mp
->m_maxagi
);
871 fprintf(stderr
, _("%s: perag init failed\n"),
875 xfs_set_perag_data_loaded(mp
);
877 if (xfs_has_metadir(mp
))
878 libxfs_mount_setup_metadir(mp
);
880 error
= libxfs_initialize_rtgroups(mp
, 0, sbp
->sb_rgcount
,
883 fprintf(stderr
, _("%s: rtgroup init failed\n"),
888 xfs_set_rtgroup_data_loaded(mp
);
897 libxfs_rtmount_destroy(
898 struct xfs_mount
*mp
)
900 struct xfs_rtgroup
*rtg
= NULL
;
903 while ((rtg
= xfs_rtgroup_next(mp
, rtg
))) {
904 for (i
= 0; i
< XFS_RTGI_MAX
; i
++)
905 libxfs_rtginode_irele(&rtg
->rtg_inodes
[i
]);
906 kvfree(rtg
->rtg_rsum_cache
);
908 libxfs_rtginode_irele(&mp
->m_rtdirip
);
911 /* Flush a device and report on writes that didn't make it to stable storage. */
913 libxfs_flush_buftarg(
914 struct xfs_buftarg
*btp
,
915 const char *buftarg_descr
)
921 * Write verifier failures are evidence of a buggy program. Make sure
922 * that this state is always reported to the caller.
924 if (btp
->flags
& XFS_BUFTARG_CORRUPT_WRITE
) {
926 _("%s: Refusing to write a corrupt buffer to the %s!\n"),
927 progname
, buftarg_descr
);
928 error
= -EFSCORRUPTED
;
931 if (btp
->flags
& XFS_BUFTARG_LOST_WRITE
) {
933 _("%s: Lost a write to the %s!\n"),
934 progname
, buftarg_descr
);
939 err2
= libxfs_blkdev_issue_flush(btp
);
942 _("%s: Flushing the %s failed, err=%d!\n"),
943 progname
, buftarg_descr
, -err2
);
952 * Flush all dirty buffers to stable storage and report on writes that didn't
953 * make it to stable storage.
957 struct xfs_mount
*mp
)
963 * Flush the buffer cache to write all dirty buffers to disk. Buffers
964 * that fail write verification will cause the CORRUPT_WRITE flag to be
965 * set in the buftarg. Buffers that cannot be written will cause the
966 * LOST_WRITE flag to be set in the buftarg. Once that's done,
967 * instruct the disks to persist their write caches.
969 libxfs_bcache_flush(mp
);
971 /* Flush all kernel and disk write caches, and report failures. */
972 if (mp
->m_ddev_targp
) {
973 err2
= libxfs_flush_buftarg(mp
->m_ddev_targp
, _("data device"));
978 if (mp
->m_logdev_targp
&& mp
->m_logdev_targp
!= mp
->m_ddev_targp
) {
979 err2
= libxfs_flush_buftarg(mp
->m_logdev_targp
,
985 if (mp
->m_rtdev_targp
&& mp
->m_rtdev_targp
!= mp
->m_ddev_targp
) {
986 err2
= libxfs_flush_buftarg(mp
->m_rtdev_targp
,
987 _("realtime device"));
997 struct xfs_buftarg
*btp
)
999 cache_destroy(btp
->bcache
);
1004 * Release any resource obtained during a mount.
1008 struct xfs_mount
*mp
)
1012 libxfs_rtmount_destroy(mp
);
1013 if (mp
->m_metadirip
)
1014 libxfs_irele(mp
->m_metadirip
);
1017 * Purge the buffer cache to write all dirty buffers to disk and free
1018 * all incore buffers, then pick up the outcome when we tell the disks
1019 * to persist their write caches.
1021 libxfs_bcache_purge(mp
);
1022 error
= libxfs_flush_mount(mp
);
1025 * Only try to free the per-AG structures if we set them up in the
1028 if (xfs_is_rtgroup_data_loaded(mp
))
1029 libxfs_free_rtgroups(mp
, 0, mp
->m_sb
.sb_rgcount
);
1030 if (xfs_is_perag_data_loaded(mp
))
1031 libxfs_free_perag_range(mp
, 0, mp
->m_sb
.sb_agcount
);
1036 mp
->m_fsname
= NULL
;
1038 if (mp
->m_rtdev_targp
!= mp
->m_ddev_targp
)
1039 libxfs_buftarg_free(mp
->m_rtdev_targp
);
1040 if (mp
->m_logdev_targp
!= mp
->m_ddev_targp
)
1041 libxfs_buftarg_free(mp
->m_logdev_targp
);
1042 libxfs_buftarg_free(mp
->m_ddev_targp
);
1048 * Release any global resources used by libxfs.
1052 struct libxfs_init
*li
)
1056 libxfs_close_devices(li
);
1058 libxfs_bcache_free();
1059 leaked
= destroy_caches();
1060 rcu_unregister_thread();
1061 if (getenv("LIBXFS_LEAK_CHECK") && leaked
)
1066 libxfs_device_alignment(void)
1068 return platform_align_blockdev();