]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/init.c
xfsprogs: Release v6.15.0
[thirdparty/xfsprogs-dev.git] / libxfs / init.c
CommitLineData
959ef981 1// SPDX-License-Identifier: GPL-2.0
2bd0ea18 2/*
da23017d
NS
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
2bd0ea18
NS
5 */
6
2bd0ea18 7#include <sys/stat.h>
9440d84d 8#include "init.h"
29e62271 9
9c799827 10#include "libxfs_priv.h"
b626fb59
DC
11#include "xfs_fs.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"
794a5604 17#include "xfs_defer.h"
b626fb59
DC
18#include "xfs_inode_buf.h"
19#include "xfs_inode_fork.h"
20#include "xfs_inode.h"
21#include "xfs_trans.h"
b3a96b46 22#include "xfs_rmap_btree.h"
e7be6330 23#include "xfs_refcount_btree.h"
c522cfc4 24#include "xfs_metafile.h"
b658de93 25#include "libfrog/platform.h"
8a044052 26#include "libfrog/util.h"
124b388d
DW
27#include "libxfs/xfile.h"
28#include "libxfs/buf_mem.h"
b626fb59 29
d57269fa
CH
30#include "xfs_format.h"
31#include "xfs_da_format.h"
32#include "xfs_log_format.h"
33#include "xfs_ondisk.h"
34
6b803e5a 35#include "libxfs.h" /* for now */
001b79f0 36#include "xfs_rtgroup.h"
b626fb59 37
7448af58
DW
38#ifndef HAVE_LIBURCU_ATOMIC64
39pthread_mutex_t atomic64_lock = PTHREAD_MUTEX_INITIALIZER;
40#endif
41
2bd0ea18
NS
42char *progname = "libxfs"; /* default, changed by each tool */
43
9f38f08d 44int libxfs_bhash_size; /* #buckets in bcache */
f1b058f9 45
167137fe 46int use_xfs_buf_lock; /* global flag: use xfs_buf locks for MT */
d0572de5 47
00ff2b10 48static int nextfakedev = -1; /* device number to give to next fake device */
2bd0ea18 49
8a044052
PR
50unsigned int PAGE_SHIFT;
51
2bd0ea18
NS
52/*
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).
5000d01d 56 *
2bd0ea18
NS
57 * Useful to tools which will produce uncertain results
58 * if the filesystem is active - repair, check, logprint.
59 */
60static int
61check_isactive(char *name, char *block, int fatal)
62{
f594a0d1 63 struct stat st;
2bd0ea18 64
f594a0d1 65 if (stat(block, &st) < 0)
9440d84d 66 return 0;
fc8202ba 67 if ((st.st_mode & S_IFMT) != S_IFBLK)
9440d84d 68 return 0;
93d9f139 69 if (platform_check_ismounted(name, block, &st, 0) == 0)
9440d84d 70 return 0;
7f510afb
ES
71 if (platform_check_iswritable(name, block, &st))
72 return fatal ? 1 : 0;
73 return 0;
2bd0ea18
NS
74}
75
fc83c757
CH
76static int
77check_open(
78 struct libxfs_init *xi,
79 struct libxfs_dev *dev)
2bd0ea18 80{
fc83c757 81 struct stat stbuf;
7eb6693f 82
fc83c757
CH
83 if (stat(dev->name, &stbuf) < 0) {
84 perror(dev->name);
85 return 0;
86 }
87 if (!(xi->flags & LIBXFS_ISREADONLY) &&
88 !(xi->flags & LIBXFS_ISINACTIVE) &&
89 platform_check_ismounted(dev->name, dev->name, NULL, 1))
90 return 0;
2bd0ea18 91
fc83c757
CH
92 if ((xi->flags & LIBXFS_ISINACTIVE) &&
93 check_isactive(dev->name, dev->name, !!(xi->flags &
94 (LIBXFS_ISREADONLY | LIBXFS_DANGEROUSLY))))
95 return 0;
96
97 return 1;
98}
99
100static bool
101libxfs_device_open(
102 struct libxfs_init *xi,
103 struct libxfs_dev *dev)
104{
105 struct stat statb;
106 int flags;
107
108 dev->fd = -1;
109
110 if (!dev->name)
111 return true;
112 if (!dev->isfile && !check_open(xi, dev))
113 return false;
114
115 if (xi->flags & LIBXFS_ISREADONLY)
116 flags = O_RDONLY;
117 else
118 flags = O_RDWR;
119
120 if (dev->create) {
121 flags |= O_CREAT | O_TRUNC;
122 } else {
123 if (xi->flags & LIBXFS_EXCLUSIVELY)
124 flags |= O_EXCL;
125 if ((xi->flags & LIBXFS_DIRECT) && platform_direct_blockdev())
126 flags |= O_DIRECT;
127 }
b74a1f6a 128
fc83c757
CH
129retry:
130 dev->fd = open(dev->name, flags, 0666);
131 if (dev->fd < 0) {
132 if (errno == EINVAL && (flags & O_DIRECT)) {
133 flags &= ~O_DIRECT;
b74a1f6a 134 goto retry;
fc83c757 135 }
9440d84d 136 fprintf(stderr, _("%s: cannot open %s: %s\n"),
fc83c757 137 progname, dev->name, strerror(errno));
2bd0ea18
NS
138 exit(1);
139 }
140
fc83c757 141 if (fstat(dev->fd, &statb) < 0) {
9440d84d 142 fprintf(stderr, _("%s: cannot stat %s: %s\n"),
fc83c757 143 progname, dev->name, strerror(errno));
2bd0ea18
NS
144 exit(1);
145 }
a33a9e62 146
fc83c757
CH
147 if (!(xi->flags & LIBXFS_ISREADONLY) &&
148 xi->setblksize &&
149 (statb.st_mode & S_IFMT) == S_IFBLK) {
a3106f32
CH
150 /*
151 * Try to use the given explicit blocksize. Failure to set the
152 * block size is only fatal for direct I/O.
153 */
fc83c757
CH
154 platform_set_blocksize(dev->fd, dev->name, statb.st_rdev,
155 xi->setblksize, flags & O_DIRECT);
edd45774 156 }
2bd0ea18 157
a33a9e62 158 /*
7b47b1bc
CH
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.
2bd0ea18 161 */
7b47b1bc 162 if (statb.st_rdev)
fc83c757
CH
163 dev->dev = statb.st_rdev;
164 else
165 dev->dev = nextfakedev--;
166 platform_findsizes(dev->name, dev->fd, &dev->size, &dev->bsize);
167 return true;
2bd0ea18
NS
168}
169
4f112cb1 170static void
fc83c757
CH
171libxfs_device_close(
172 struct libxfs_dev *dev)
2bd0ea18 173{
fc83c757 174 int ret;
2bd0ea18 175
fc83c757 176 ret = platform_flush_device(dev->fd, dev->dev);
7b47b1bc
CH
177 if (ret) {
178 ret = -errno;
179 fprintf(stderr,
fa703790
DW
180 _("%s: flush of device %s failed, err=%d"),
181 progname, dev->name, ret);
7b47b1bc 182 }
fc83c757 183 close(dev->fd);
c781939c 184
fc83c757
CH
185 dev->fd = -1;
186 dev->dev = 0;
c781939c
RC
187}
188
7a326ce0 189/*
2e1394fc 190 * Initialize/destroy all of the cache allocators we use.
7a326ce0
ES
191 */
192static void
2e1394fc 193init_caches(void)
7a326ce0 194{
7d10d094
DW
195 int error;
196
2e1394fc
DW
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(
7a326ce0 202 sizeof(struct xfs_inode_log_item),"xfs_inode_log_item");
2e1394fc 203 xfs_buf_item_cache = kmem_cache_init(
7a326ce0 204 sizeof(struct xfs_buf_log_item), "xfs_buf_log_item");
1577541c
DW
205 error = xfs_defer_init_item_caches();
206 if (error) {
207 fprintf(stderr, "Could not allocate defer init item caches.\n");
208 abort();
209 }
2e1394fc 210 xfs_da_state_cache = kmem_cache_init(
7a326ce0 211 sizeof(struct xfs_da_state), "xfs_da_state");
7d10d094
DW
212 error = xfs_btree_init_cur_caches();
213 if (error) {
214 fprintf(stderr, "Could not allocate btree cursor caches.\n");
215 abort();
216 }
7d84b02d 217 xfs_extfree_item_cache = kmem_cache_init(
7a326ce0 218 sizeof(struct xfs_extent_free_item),
7d84b02d 219 "xfs_extfree_item");
2e1394fc 220 xfs_trans_cache = kmem_cache_init(
7a326ce0 221 sizeof(struct xfs_trans), "xfs_trans");
4bba3a07
AH
222 xfs_parent_args_cache = kmem_cache_init(
223 sizeof(struct xfs_parent_args), "xfs_parent_args");
7a326ce0
ES
224}
225
226static int
2e1394fc 227destroy_caches(void)
7a326ce0
ES
228{
229 int leaked = 0;
230
2e1394fc
DW
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);
1577541c 237 xfs_defer_destroy_item_caches();
7d10d094 238 xfs_btree_destroy_cur_caches();
7d84b02d 239 leaked += kmem_cache_destroy(xfs_extfree_item_cache);
2e1394fc 240 leaked += kmem_cache_destroy(xfs_trans_cache);
4bba3a07 241 leaked += kmem_cache_destroy(xfs_parent_args_cache);
7a326ce0
ES
242
243 return leaked;
244}
245
a9468486
DW
246static void
247libxfs_close_devices(
01dcfd9e 248 struct libxfs_init *li)
a9468486 249{
fc83c757
CH
250 if (li->data.dev)
251 libxfs_device_close(&li->data);
252 if (li->log.dev && li->log.dev != li->data.dev)
253 libxfs_device_close(&li->log);
2e5a737a 254 if (li->rt.dev && li->rt.dev != li->data.dev)
fc83c757 255 libxfs_device_close(&li->rt);
a9468486
DW
256}
257
2bd0ea18
NS
258/*
259 * libxfs initialization.
260 * Caller gets a 0 on failure (and we print a message), 1 on success.
261 */
262int
01dcfd9e 263libxfs_init(struct libxfs_init *a)
2bd0ea18 264{
8a044052
PR
265 if (!PAGE_SHIFT)
266 PAGE_SHIFT = log2_roundup(PAGE_SIZE);
d57269fa 267 xfs_check_ondisk_structs();
124b388d 268 xmbuf_libinit();
e4da1b16
DC
269 rcu_init();
270 rcu_register_thread();
bacd44a5
AE
271 radix_tree_init();
272
fc83c757
CH
273 if (!libxfs_device_open(a, &a->data))
274 goto done;
275 if (!libxfs_device_open(a, &a->log))
276 goto done;
277 if (!libxfs_device_open(a, &a->rt))
278 goto done;
b6e08bf3 279
9f38f08d
MV
280 if (!libxfs_bhash_size)
281 libxfs_bhash_size = LIBXFS_BHASHSIZE(sbp);
23d88955 282 use_xfs_buf_lock = a->flags & LIBXFS_USEBUFLOCK;
7a326ce0 283 xfs_dir_startup();
2e1394fc 284 init_caches();
732f5b90 285 return 1;
a9468486 286
732f5b90
CH
287done:
288 libxfs_close_devices(a);
289 rcu_unregister_thread();
290 return 0;
2bd0ea18
NS
291}
292
293
b391b7cd
NS
294/*
295 * Initialize realtime fields in the mount structure.
296 */
297static int
298rtmount_init(
2420d095 299 xfs_mount_t *mp) /* file system mount structure */
b391b7cd 300{
31079e67 301 struct xfs_buf *bp; /* buffer for last block of subvolume */
b391b7cd 302 xfs_daddr_t d; /* address of last block of subvolume */
31079e67 303 int error;
b391b7cd 304
575f24e5 305 if (mp->m_sb.sb_rblocks == 0)
b391b7cd 306 return 0;
4aaeedc4 307
17408f88 308 if (xfs_has_reflink(mp) && mp->m_sb.sb_rextsize > 1) {
4aaeedc4 309 fprintf(stderr,
17408f88 310 _("%s: Reflink not compatible with realtime extent size > 1. Please try a newer xfsprogs.\n"),
4aaeedc4
DW
311 progname);
312 return -1;
313 }
314
2420d095 315 if (mp->m_rtdev_targp->bt_bdev == 0 && !xfs_is_debugger(mp)) {
9440d84d 316 fprintf(stderr, _("%s: filesystem has a realtime subvolume\n"),
b391b7cd
NS
317 progname);
318 return -1;
319 }
3241cd2c 320 mp->m_rsumblocks = xfs_rtsummary_blockcount(mp, &mp->m_rsumlevels);
39798eb5
NS
321
322 /*
323 * Allow debugger to be run without the realtime device present.
324 */
2420d095 325 if (xfs_is_debugger(mp))
39798eb5
NS
326 return 0;
327
b391b7cd
NS
328 /*
329 * Check that the realtime section is an ok size.
330 */
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) {
9440d84d
NS
333 fprintf(stderr, _("%s: realtime init - %llu != %llu\n"),
334 progname, (unsigned long long) XFS_BB_TO_FSB(mp, d),
b391b7cd
NS
335 (unsigned long long) mp->m_sb.sb_rblocks);
336 return -1;
337 }
31079e67
DW
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);
340 if (error) {
9440d84d
NS
341 fprintf(stderr, _("%s: realtime size check failed\n"),
342 progname);
b391b7cd
NS
343 return -1;
344 }
e02ba985 345 libxfs_buf_relse(bp);
b391b7cd
NS
346 return 0;
347}
348
03dc2ef2
DC
349static bool
350xfs_set_inode_alloc_perag(
351 struct xfs_perag *pag,
352 xfs_ino_t ino,
353 xfs_agnumber_t max_metadata)
354{
6a271c73 355 if (!xfs_is_inode32(pag_mount(pag))) {
03dc2ef2
DC
356 set_bit(XFS_AGSTATE_ALLOWS_INODES, &pag->pag_opstate);
357 clear_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
358 return false;
359 }
360
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);
364 return false;
365 }
366
367 set_bit(XFS_AGSTATE_ALLOWS_INODES, &pag->pag_opstate);
6a271c73 368 if (pag_agno(pag) < max_metadata)
03dc2ef2
DC
369 set_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
370 else
371 clear_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
372 return true;
373}
374
b9ee1227
DW
375/*
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.
379 *
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.
383 *
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.
387 *
388 * Returns the maximum AG index which may contain inodes.
ed8f5980
DW
389 *
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.
b9ee1227
DW
392 */
393xfs_agnumber_t
394xfs_set_inode_alloc(
395 struct xfs_mount *mp,
396 xfs_agnumber_t agcount)
397{
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;
402 xfs_agino_t agino;
403 xfs_ino_t ino;
404
405 /*
406 * Calculate how much should be reserved for inodes to meet
407 * the max inode percentage. Used only for inode32.
408 */
409 if (M_IGEO(mp)->maxicount) {
410 uint64_t icount;
411
412 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
413 do_div(icount, 100);
414 icount += sbp->sb_agblocks - 1;
415 do_div(icount, sbp->sb_agblocks);
416 max_metadata = icount;
417 } else {
418 max_metadata = agcount;
419 }
420
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);
424
425 /*
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.
429 */
ed8f5980 430 if (xfs_has_small_inums(mp) && ino > XFS_MAXINUMBER_32)
03dc2ef2 431 set_bit(XFS_OPSTATE_INODE32, &mp->m_opstate);
ed8f5980 432 else
03dc2ef2 433 clear_bit(XFS_OPSTATE_INODE32, &mp->m_opstate);
b9ee1227
DW
434
435 for (index = 0; index < agcount; index++) {
436 struct xfs_perag *pag;
437
438 ino = XFS_AGINO_TO_INO(mp, index, agino);
439
440 pag = xfs_perag_get(mp, index);
03dc2ef2
DC
441 if (xfs_set_inode_alloc_perag(pag, ino, max_metadata))
442 maxagi++;
b9ee1227
DW
443 xfs_perag_put(pag);
444 }
445
0ee9753e 446 return xfs_is_inode32(mp) ? maxagi : agcount;
b9ee1227
DW
447}
448
75c8b434
DC
449static struct xfs_buftarg *
450libxfs_buftarg_alloc(
451 struct xfs_mount *mp,
27d81845 452 struct libxfs_init *xi,
fc83c757 453 struct libxfs_dev *dev,
704e4cef 454 unsigned long write_fails)
75c8b434
DC
455{
456 struct xfs_buftarg *btp;
457
458 btp = malloc(sizeof(*btp));
459 if (!btp) {
460 fprintf(stderr, _("%s: buftarg init failed\n"),
461 progname);
462 exit(1);
463 }
464 btp->bt_mount = mp;
fc83c757
CH
465 btp->bt_bdev = dev->dev;
466 btp->bt_bdev_fd = dev->fd;
124b388d 467 btp->bt_xfile = NULL;
c335b673 468 btp->flags = 0;
704e4cef
DW
469 if (write_fails) {
470 btp->writes_left = write_fails;
471 btp->flags |= XFS_BUFTARG_INJECT_WRITE_FAIL;
472 }
473 pthread_mutex_init(&btp->lock, NULL);
c335b673 474
27d81845
DW
475 btp->bcache = cache_init(xi->bcache_flags, libxfs_bhash_size,
476 &libxfs_bcache_operations);
477
75c8b434
DC
478 return btp;
479}
480
704e4cef
DW
481enum libxfs_write_failure_nums {
482 WF_DATA = 0,
483 WF_LOG,
484 WF_RT,
485 WF_MAX_OPTS,
486};
487
488static char *wf_opts[] = {
489 [WF_DATA] = "ddev",
490 [WF_LOG] = "logdev",
491 [WF_RT] = "rtdev",
492 [WF_MAX_OPTS] = NULL,
493};
494
75c8b434
DC
495void
496libxfs_buftarg_init(
497 struct xfs_mount *mp,
ca8cc76e 498 struct libxfs_init *xi)
75c8b434 499{
704e4cef
DW
500 char *p = getenv("LIBXFS_DEBUG_WRITE_CRASH");
501 unsigned long dfail = 0, lfail = 0, rfail = 0;
502
503 /* Simulate utility crash after a certain number of writes. */
504 while (p && *p) {
505 char *val;
506
507 switch (getsubopt(&p, wf_opts, &val)) {
508 case WF_DATA:
509 if (!val) {
510 fprintf(stderr,
511 _("ddev write fail requires a parameter\n"));
512 exit(1);
513 }
514 dfail = strtoul(val, NULL, 0);
515 break;
516 case WF_LOG:
517 if (!val) {
518 fprintf(stderr,
519 _("logdev write fail requires a parameter\n"));
520 exit(1);
521 }
522 lfail = strtoul(val, NULL, 0);
523 break;
524 case WF_RT:
525 if (!val) {
526 fprintf(stderr,
527 _("rtdev write fail requires a parameter\n"));
528 exit(1);
529 }
530 rfail = strtoul(val, NULL, 0);
531 break;
532 default:
533 fprintf(stderr, _("unknown write fail type %s\n"),
534 val);
535 exit(1);
536 break;
537 }
538 }
539
75c8b434
DC
540 if (mp->m_ddev_targp) {
541 /* should already have all buftargs initialised */
fc83c757 542 if (mp->m_ddev_targp->bt_bdev != xi->data.dev ||
75c8b434
DC
543 mp->m_ddev_targp->bt_mount != mp) {
544 fprintf(stderr,
545 _("%s: bad buftarg reinit, ddev\n"),
546 progname);
547 exit(1);
548 }
fc83c757 549 if (!xi->log.dev || xi->log.dev == xi->data.dev) {
75c8b434
DC
550 if (mp->m_logdev_targp != mp->m_ddev_targp) {
551 fprintf(stderr,
552 _("%s: bad buftarg reinit, ldev mismatch\n"),
553 progname);
554 exit(1);
555 }
fc83c757 556 } else if (mp->m_logdev_targp->bt_bdev != xi->log.dev ||
75c8b434
DC
557 mp->m_logdev_targp->bt_mount != mp) {
558 fprintf(stderr,
559 _("%s: bad buftarg reinit, logdev\n"),
560 progname);
561 exit(1);
562 }
9840f7e0 563 if ((xi->rt.dev || xi->rt.dev == xi->data.dev) &&
fc83c757 564 (mp->m_rtdev_targp->bt_bdev != xi->rt.dev ||
ca8cc76e 565 mp->m_rtdev_targp->bt_mount != mp)) {
75c8b434
DC
566 fprintf(stderr,
567 _("%s: bad buftarg reinit, rtdev\n"),
568 progname);
569 exit(1);
570 }
571 return;
572 }
573
27d81845 574 mp->m_ddev_targp = libxfs_buftarg_alloc(mp, xi, &xi->data, dfail);
fc83c757 575 if (!xi->log.dev || xi->log.dev == xi->data.dev)
75c8b434
DC
576 mp->m_logdev_targp = mp->m_ddev_targp;
577 else
27d81845
DW
578 mp->m_logdev_targp = libxfs_buftarg_alloc(mp, xi, &xi->log,
579 lfail);
9840f7e0
CH
580 if (!xi->rt.dev || xi->rt.dev == xi->data.dev)
581 mp->m_rtdev_targp = mp->m_ddev_targp;
582 else
583 mp->m_rtdev_targp = libxfs_buftarg_alloc(mp, xi, &xi->rt,
584 rfail);
75c8b434
DC
585}
586
6afce48f
DW
587/* Compute maximum possible height for per-AG btree types for this fs. */
588static inline void
589xfs_agbtree_compute_maxlevels(
590 struct xfs_mount *mp)
591{
592 unsigned int levels;
593
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);
597}
598
c522cfc4
DW
599/* Compute maximum possible height for realtime btree types for this fs. */
600static inline void
601xfs_rtbtree_compute_maxlevels(
602 struct xfs_mount *mp)
603{
310df80d
DW
604 mp->m_rtbtree_maxlevels = max(mp->m_rtrmap_maxlevels,
605 mp->m_rtrefc_maxlevels);
c522cfc4
DW
606}
607
7aeffc87
DW
608/* Compute maximum possible height of all btrees. */
609void
610libxfs_compute_all_maxlevels(
611 struct xfs_mount *mp)
612{
30b4d5d6
DW
613 struct xfs_ino_geometry *igeo = M_IGEO(mp);
614
7aeffc87
DW
615 xfs_alloc_compute_maxlevels(mp);
616 xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
617 xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
30b4d5d6 618 igeo->attr_fork_offset = xfs_bmap_compute_attr_offset(mp);
7aeffc87
DW
619 xfs_ialloc_setup_geometry(mp);
620 xfs_rmapbt_compute_maxlevels(mp);
c522cfc4 621 xfs_rtrmapbt_compute_maxlevels(mp);
7aeffc87 622 xfs_refcountbt_compute_maxlevels(mp);
310df80d 623 xfs_rtrefcountbt_compute_maxlevels(mp);
7aeffc87
DW
624
625 xfs_agbtree_compute_maxlevels(mp);
c522cfc4 626 xfs_rtbtree_compute_maxlevels(mp);
7aeffc87
DW
627}
628
15402626
DW
629/* Mount the metadata files under the metadata directory tree. */
630STATIC void
631libxfs_mount_setup_metadir(
632 struct xfs_mount *mp)
633{
634 int error;
635
636 /* Ignore filesystems that are under construction. */
637 if (mp->m_sb.sb_inprogress)
638 return;
639
640 error = -libxfs_metafile_iget(mp, mp->m_sb.sb_metadirino,
641 XFS_METAFILE_DIR, &mp->m_metadirip);
642 if (error) {
643 fprintf(stderr,
644 _("%s: Failed to load metadir root directory, error %d\n"),
645 progname, error);
646 return;
647 }
648}
649
33f3aac8
DC
650/*
651 * precalculate the low space thresholds for dynamic speculative preallocation.
652 */
653static void
654xfs_set_low_space_thresholds(
655 struct xfs_mount *mp)
656{
657 uint64_t dblocks = mp->m_sb.sb_dblocks;
658 int i;
659
660 do_div(dblocks, 100);
661
662 for (i = 0; i < XFS_LOWSP_MAX; i++)
663 mp->m_low_space[i] = dblocks * (i + 1);
664}
665
8ea12bc8
DW
666/*
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.
671 */
672static inline bool
673check_many_rtgroups(
674 struct xfs_mount *mp,
675 struct xfs_sb *sbp)
676{
677 struct xfs_buf *bp;
678 xfs_daddr_t d;
679 int error;
680
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))
685 return false;
686
687 sbp->sb_rgcount = 0;
688 return true;
689 }
690
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,
693 &bp, NULL);
694 if (!error) {
695 libxfs_buf_relse(bp);
696 return true;
697 }
698
699 fprintf(stderr, _("%s: read of rtgroup %u failed\n"), progname,
700 sbp->sb_rgcount - 1);
701 if (!xfs_is_debugger(mp))
702 return false;
703
704 fprintf(stderr, _("%s: limiting reads to rtgroup 0\n"), progname);
705 sbp->sb_rgcount = 1;
706 return true;
707}
708
2bd0ea18
NS
709/*
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).
713 */
d855bce8 714struct xfs_mount *
2bd0ea18 715libxfs_mount(
d855bce8
DW
716 struct xfs_mount *mp,
717 struct xfs_sb *sb,
ddd9942b 718 struct libxfs_init *xi,
ed8f5980 719 unsigned int flags)
2bd0ea18 720{
d855bce8
DW
721 struct xfs_buf *bp;
722 struct xfs_sb *sbp;
723 xfs_daddr_t d;
6a271c73 724 int i;
d855bce8 725 int error;
2bd0ea18 726
3bc1fdd4 727 mp->m_features = xfs_sb_version_to_features(sb);
2420d095
DW
728 if (flags & LIBXFS_MOUNT_DEBUGGER)
729 xfs_set_debugger(mp);
e42c53f3 730 if (flags & LIBXFS_MOUNT_REPORT_CORRUPTION)
2420d095 731 xfs_set_reporting_corruption(mp);
ca8cc76e 732 libxfs_buftarg_init(mp, xi);
75c8b434 733
e9add503
DW
734 if (xi->data.name)
735 mp->m_fsname = strdup(xi->data.name);
736 else
737 mp->m_fsname = NULL;
738
f747f7dd 739 mp->m_finobt_nores = true;
0ee9753e 740 xfs_set_inode32(mp);
2bd0ea18 741 mp->m_sb = *sb;
6a271c73
CH
742 for (i = 0; i < XG_TYPE_MAX; i++)
743 xa_init(&mp->m_groups[i].xa);
686bddf9
DC
744 sbp = &mp->m_sb;
745 spin_lock_init(&mp->m_sb_lock);
746 spin_lock_init(&mp->m_agirotor_lock);
2bd0ea18 747
4896e6c8 748 xfs_sb_mount_common(mp, sb);
2bd0ea18 749
949c0f10
NS
750 /*
751 * Set whether we're using stripe alignment.
752 */
2660e653 753 if (xfs_has_dalign(mp)) {
949c0f10
NS
754 mp->m_dalign = sbp->sb_unit;
755 mp->m_swidth = sbp->sb_width;
756 }
757
7aeffc87 758 libxfs_compute_all_maxlevels(mp);
6afce48f 759
2bd0ea18
NS
760 /*
761 * Check that the data (and log if separate) are an ok size.
762 */
9440d84d 763 d = (xfs_daddr_t) XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
2bd0ea18 764 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
9440d84d 765 fprintf(stderr, _("%s: size check failed\n"), progname);
2420d095 766 if (!xfs_is_debugger(mp))
4ca431fc 767 return NULL;
2bd0ea18
NS
768 }
769
ff105f75
DC
770 /*
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.
773 */
774 if (!(sbp->sb_versionnum & XFS_SB_VERSION_NLINKBIT)) {
775
776 fprintf(stderr, _(
777 "%s: V1 inodes unsupported. Please try an older xfsprogs.\n"),
778 progname);
779 exit(1);
780 }
781
782 /* Check for supported directory formats */
783 if (!(sbp->sb_versionnum & XFS_SB_VERSION_DIRV2BIT)) {
9a048535
DC
784
785 fprintf(stderr, _(
786 "%s: V1 directories unsupported. Please try an older xfsprogs.\n"),
787 progname);
788 exit(1);
5e656dbb 789 }
2bd0ea18 790
ff105f75
DC
791 /* check for unsupported other features */
792 if (!xfs_sb_good_version(sbp)) {
793 fprintf(stderr, _(
794 "%s: Unsupported features detected. Please try a newer xfsprogs.\n"),
795 progname);
796 exit(1);
797 }
798
799 xfs_da_mount(mp);
800
2bd0ea18 801 /* Initialize the precomputed transaction reservations values */
5e656dbb 802 xfs_trans_init(mp);
2bd0ea18 803
fc83c757 804 if (xi->data.dev == 0) /* maxtrres, we have no device so leave now */
2bd0ea18
NS
805 return mp;
806
d855bce8 807 /* device size checks must pass unless we're a debugger. */
31079e67
DW
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);
810 if (error) {
9440d84d 811 fprintf(stderr, _("%s: data size check failed\n"), progname);
2420d095 812 if (!xfs_is_debugger(mp))
73cdf003 813 goto out_da;
32244196 814 } else
e02ba985 815 libxfs_buf_relse(bp);
2bd0ea18 816
ab434d12
DC
817 if (mp->m_logdev_targp->bt_bdev &&
818 mp->m_logdev_targp->bt_bdev != mp->m_ddev_targp->bt_bdev) {
9440d84d 819 d = (xfs_daddr_t) XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
31079e67
DW
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),
823 0, &bp, NULL)) {
9440d84d 824 fprintf(stderr, _("%s: log size checks failed\n"),
2bd0ea18 825 progname);
2420d095 826 if (!xfs_is_debugger(mp))
73cdf003 827 goto out_da;
2bd0ea18 828 }
32244196 829 if (bp)
e02ba985 830 libxfs_buf_relse(bp);
2bd0ea18
NS
831 }
832
33f3aac8
DC
833 xfs_set_low_space_thresholds(mp);
834
2bd0ea18 835 /* Initialize realtime fields in the mount structure */
2420d095 836 if (rtmount_init(mp)) {
9440d84d 837 fprintf(stderr, _("%s: realtime device init failed\n"),
73cdf003
DW
838 progname);
839 goto out_da;
2bd0ea18
NS
840 }
841
a547152d
ES
842 /*
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.
848 */
849 if (sbp->sb_agcount > 1000000) {
31079e67 850 error = libxfs_buf_read(mp->m_dev,
a547152d 851 XFS_AG_DADDR(mp, sbp->sb_agcount - 1, 0), 1,
31079e67
DW
852 0, &bp, NULL);
853 if (error) {
a547152d
ES
854 fprintf(stderr, _("%s: read of AG %u failed\n"),
855 progname, sbp->sb_agcount);
2420d095 856 if (!xfs_is_debugger(mp))
73cdf003 857 goto out_da;
a547152d
ES
858 fprintf(stderr, _("%s: limiting reads to AG 0\n"),
859 progname);
860 sbp->sb_agcount = 1;
31079e67
DW
861 } else
862 libxfs_buf_relse(bp);
a547152d
ES
863 }
864
8ea12bc8
DW
865 if (sbp->sb_rgcount > 1000000 && !check_many_rtgroups(mp, sbp))
866 goto out_da;
867
d64d607e
CH
868 error = libxfs_initialize_perag(mp, 0, sbp->sb_agcount,
869 sbp->sb_dblocks, &mp->m_maxagi);
56b2de80
DC
870 if (error) {
871 fprintf(stderr, _("%s: perag init failed\n"),
872 progname);
2bd0ea18
NS
873 exit(1);
874 }
2420d095 875 xfs_set_perag_data_loaded(mp);
2bd0ea18 876
15402626
DW
877 if (xfs_has_metadir(mp))
878 libxfs_mount_setup_metadir(mp);
879
001b79f0
DW
880 error = libxfs_initialize_rtgroups(mp, 0, sbp->sb_rgcount,
881 sbp->sb_rextents);
882 if (error) {
883 fprintf(stderr, _("%s: rtgroup init failed\n"),
884 progname);
885 exit(1);
886 }
887
001b79f0
DW
888 xfs_set_rtgroup_data_loaded(mp);
889
2bd0ea18 890 return mp;
73cdf003
DW
891out_da:
892 xfs_da_unmount(mp);
893 return NULL;
2bd0ea18
NS
894}
895
f1b058f9 896void
15c6dada
DW
897libxfs_rtmount_destroy(
898 struct xfs_mount *mp)
f1b058f9 899{
15c6dada
DW
900 struct xfs_rtgroup *rtg = NULL;
901 unsigned int i;
902
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]);
55ed4049 906 kvfree(rtg->rtg_rsum_cache);
15c6dada
DW
907 }
908 libxfs_rtginode_irele(&mp->m_rtdirip);
f1b058f9
NS
909}
910
c335b673
DW
911/* Flush a device and report on writes that didn't make it to stable storage. */
912static inline int
913libxfs_flush_buftarg(
914 struct xfs_buftarg *btp,
915 const char *buftarg_descr)
916{
917 int error = 0;
918 int err2;
919
920 /*
921 * Write verifier failures are evidence of a buggy program. Make sure
922 * that this state is always reported to the caller.
923 */
924 if (btp->flags & XFS_BUFTARG_CORRUPT_WRITE) {
925 fprintf(stderr,
926_("%s: Refusing to write a corrupt buffer to the %s!\n"),
927 progname, buftarg_descr);
928 error = -EFSCORRUPTED;
929 }
930
931 if (btp->flags & XFS_BUFTARG_LOST_WRITE) {
932 fprintf(stderr,
933_("%s: Lost a write to the %s!\n"),
934 progname, buftarg_descr);
935 if (!error)
936 error = -EIO;
937 }
938
939 err2 = libxfs_blkdev_issue_flush(btp);
940 if (err2) {
941 fprintf(stderr,
942_("%s: Flushing the %s failed, err=%d!\n"),
943 progname, buftarg_descr, -err2);
944 }
945 if (!error)
946 error = err2;
947
948 return error;
949}
950
951/*
952 * Flush all dirty buffers to stable storage and report on writes that didn't
953 * make it to stable storage.
954 */
a7348c58 955int
c335b673
DW
956libxfs_flush_mount(
957 struct xfs_mount *mp)
958{
959 int error = 0;
960 int err2;
961
962 /*
a7348c58
DW
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.
c335b673 968 */
27d81845 969 libxfs_bcache_flush(mp);
c335b673
DW
970
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"));
974 if (!error)
975 error = err2;
976 }
977
978 if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp) {
979 err2 = libxfs_flush_buftarg(mp->m_logdev_targp,
980 _("log device"));
981 if (!error)
982 error = err2;
983 }
984
9840f7e0 985 if (mp->m_rtdev_targp && mp->m_rtdev_targp != mp->m_ddev_targp) {
c335b673
DW
986 err2 = libxfs_flush_buftarg(mp->m_rtdev_targp,
987 _("realtime device"));
988 if (!error)
989 error = err2;
990 }
991
992 return error;
993}
994
27d81845
DW
995static void
996libxfs_buftarg_free(
997 struct xfs_buftarg *btp)
998{
999 cache_destroy(btp->bcache);
bb785467 1000 kfree(btp);
27d81845
DW
1001}
1002
2bd0ea18 1003/*
9440d84d 1004 * Release any resource obtained during a mount.
2bd0ea18 1005 */
c335b673
DW
1006int
1007libxfs_umount(
1008 struct xfs_mount *mp)
2bd0ea18 1009{
c335b673 1010 int error;
56b2de80 1011
f1b058f9 1012 libxfs_rtmount_destroy(mp);
15402626
DW
1013 if (mp->m_metadirip)
1014 libxfs_irele(mp->m_metadirip);
c335b673 1015
a7348c58
DW
1016 /*
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.
1020 */
27d81845 1021 libxfs_bcache_purge(mp);
c335b673 1022 error = libxfs_flush_mount(mp);
f1b058f9 1023
7bf9cd9d
DW
1024 /*
1025 * Only try to free the per-AG structures if we set them up in the
1026 * first place.
1027 */
001b79f0
DW
1028 if (xfs_is_rtgroup_data_loaded(mp))
1029 libxfs_free_rtgroups(mp, 0, mp->m_sb.sb_rgcount);
2420d095 1030 if (xfs_is_perag_data_loaded(mp))
d64d607e 1031 libxfs_free_perag_range(mp, 0, mp->m_sb.sb_agcount);
4334e2e8 1032
73cdf003 1033 xfs_da_unmount(mp);
4334e2e8 1034
e9add503
DW
1035 free(mp->m_fsname);
1036 mp->m_fsname = NULL;
1037
9840f7e0
CH
1038 if (mp->m_rtdev_targp != mp->m_ddev_targp)
1039 libxfs_buftarg_free(mp->m_rtdev_targp);
4334e2e8 1040 if (mp->m_logdev_targp != mp->m_ddev_targp)
27d81845
DW
1041 libxfs_buftarg_free(mp->m_logdev_targp);
1042 libxfs_buftarg_free(mp->m_ddev_targp);
f8149110 1043
c335b673 1044 return error;
2bd0ea18 1045}
f1b058f9
NS
1046
1047/*
1048 * Release any global resources used by libxfs.
1049 */
1050void
a9468486 1051libxfs_destroy(
01dcfd9e 1052 struct libxfs_init *li)
f1b058f9 1053{
a9468486
DW
1054 int leaked;
1055
1056 libxfs_close_devices(li);
44488491 1057
864028ed 1058 libxfs_bcache_free();
2e1394fc 1059 leaked = destroy_caches();
e4da1b16 1060 rcu_unregister_thread();
44488491
ES
1061 if (getenv("LIBXFS_LEAK_CHECK") && leaked)
1062 exit(1);
f1b058f9 1063}
9f38f08d 1064
b74a1f6a
NS
1065int
1066libxfs_device_alignment(void)
1067{
1068 return platform_align_blockdev();
1069}