]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/init.c
xfsprogs: Release v6.8.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"
b658de93 24#include "libfrog/platform.h"
b626fb59 25
d57269fa
CH
26#include "xfs_format.h"
27#include "xfs_da_format.h"
28#include "xfs_log_format.h"
29#include "xfs_ondisk.h"
30
6b803e5a 31#include "libxfs.h" /* for now */
b626fb59 32
7448af58
DW
33#ifndef HAVE_LIBURCU_ATOMIC64
34pthread_mutex_t atomic64_lock = PTHREAD_MUTEX_INITIALIZER;
35#endif
36
2bd0ea18
NS
37char *progname = "libxfs"; /* default, changed by each tool */
38
f1b058f9 39struct cache *libxfs_bcache; /* global buffer cache */
9f38f08d 40int libxfs_bhash_size; /* #buckets in bcache */
f1b058f9 41
167137fe 42int use_xfs_buf_lock; /* global flag: use xfs_buf locks for MT */
d0572de5 43
00ff2b10 44static int nextfakedev = -1; /* device number to give to next fake device */
2bd0ea18 45
2bd0ea18
NS
46/*
47 * Checks whether a given device has a mounted, writable
48 * filesystem, returns 1 if it does & fatal (just warns
49 * if not fatal, but allows us to proceed).
5000d01d 50 *
2bd0ea18
NS
51 * Useful to tools which will produce uncertain results
52 * if the filesystem is active - repair, check, logprint.
53 */
54static int
55check_isactive(char *name, char *block, int fatal)
56{
f594a0d1 57 struct stat st;
2bd0ea18 58
f594a0d1 59 if (stat(block, &st) < 0)
9440d84d 60 return 0;
fc8202ba 61 if ((st.st_mode & S_IFMT) != S_IFBLK)
9440d84d 62 return 0;
93d9f139 63 if (platform_check_ismounted(name, block, &st, 0) == 0)
9440d84d 64 return 0;
7f510afb
ES
65 if (platform_check_iswritable(name, block, &st))
66 return fatal ? 1 : 0;
67 return 0;
2bd0ea18
NS
68}
69
fc83c757
CH
70static int
71check_open(
72 struct libxfs_init *xi,
73 struct libxfs_dev *dev)
2bd0ea18 74{
fc83c757 75 struct stat stbuf;
7eb6693f 76
fc83c757
CH
77 if (stat(dev->name, &stbuf) < 0) {
78 perror(dev->name);
79 return 0;
80 }
81 if (!(xi->flags & LIBXFS_ISREADONLY) &&
82 !(xi->flags & LIBXFS_ISINACTIVE) &&
83 platform_check_ismounted(dev->name, dev->name, NULL, 1))
84 return 0;
2bd0ea18 85
fc83c757
CH
86 if ((xi->flags & LIBXFS_ISINACTIVE) &&
87 check_isactive(dev->name, dev->name, !!(xi->flags &
88 (LIBXFS_ISREADONLY | LIBXFS_DANGEROUSLY))))
89 return 0;
90
91 return 1;
92}
93
94static bool
95libxfs_device_open(
96 struct libxfs_init *xi,
97 struct libxfs_dev *dev)
98{
99 struct stat statb;
100 int flags;
101
102 dev->fd = -1;
103
104 if (!dev->name)
105 return true;
106 if (!dev->isfile && !check_open(xi, dev))
107 return false;
108
109 if (xi->flags & LIBXFS_ISREADONLY)
110 flags = O_RDONLY;
111 else
112 flags = O_RDWR;
113
114 if (dev->create) {
115 flags |= O_CREAT | O_TRUNC;
116 } else {
117 if (xi->flags & LIBXFS_EXCLUSIVELY)
118 flags |= O_EXCL;
119 if ((xi->flags & LIBXFS_DIRECT) && platform_direct_blockdev())
120 flags |= O_DIRECT;
121 }
b74a1f6a 122
fc83c757
CH
123retry:
124 dev->fd = open(dev->name, flags, 0666);
125 if (dev->fd < 0) {
126 if (errno == EINVAL && (flags & O_DIRECT)) {
127 flags &= ~O_DIRECT;
b74a1f6a 128 goto retry;
fc83c757 129 }
9440d84d 130 fprintf(stderr, _("%s: cannot open %s: %s\n"),
fc83c757 131 progname, dev->name, strerror(errno));
2bd0ea18
NS
132 exit(1);
133 }
134
fc83c757 135 if (fstat(dev->fd, &statb) < 0) {
9440d84d 136 fprintf(stderr, _("%s: cannot stat %s: %s\n"),
fc83c757 137 progname, dev->name, strerror(errno));
2bd0ea18
NS
138 exit(1);
139 }
a33a9e62 140
fc83c757
CH
141 if (!(xi->flags & LIBXFS_ISREADONLY) &&
142 xi->setblksize &&
143 (statb.st_mode & S_IFMT) == S_IFBLK) {
a3106f32
CH
144 /*
145 * Try to use the given explicit blocksize. Failure to set the
146 * block size is only fatal for direct I/O.
147 */
fc83c757
CH
148 platform_set_blocksize(dev->fd, dev->name, statb.st_rdev,
149 xi->setblksize, flags & O_DIRECT);
edd45774 150 }
2bd0ea18 151
a33a9e62 152 /*
7b47b1bc
CH
153 * Get the device number from the stat buf - unless we're not opening a
154 * real device, in which case choose a new fake device number.
2bd0ea18 155 */
7b47b1bc 156 if (statb.st_rdev)
fc83c757
CH
157 dev->dev = statb.st_rdev;
158 else
159 dev->dev = nextfakedev--;
160 platform_findsizes(dev->name, dev->fd, &dev->size, &dev->bsize);
161 return true;
2bd0ea18
NS
162}
163
4f112cb1 164static void
fc83c757
CH
165libxfs_device_close(
166 struct libxfs_dev *dev)
2bd0ea18 167{
fc83c757 168 int ret;
2bd0ea18 169
fc83c757 170 ret = platform_flush_device(dev->fd, dev->dev);
7b47b1bc
CH
171 if (ret) {
172 ret = -errno;
173 fprintf(stderr,
fa703790
DW
174 _("%s: flush of device %s failed, err=%d"),
175 progname, dev->name, ret);
7b47b1bc 176 }
fc83c757 177 close(dev->fd);
c781939c 178
fc83c757
CH
179 dev->fd = -1;
180 dev->dev = 0;
c781939c
RC
181}
182
7a326ce0 183/*
2e1394fc 184 * Initialize/destroy all of the cache allocators we use.
7a326ce0
ES
185 */
186static void
2e1394fc 187init_caches(void)
7a326ce0 188{
7d10d094
DW
189 int error;
190
2e1394fc
DW
191 /* initialise cache allocation */
192 xfs_buf_cache = kmem_cache_init(sizeof(struct xfs_buf), "xfs_buffer");
193 xfs_inode_cache = kmem_cache_init(sizeof(struct xfs_inode), "xfs_inode");
194 xfs_ifork_cache = kmem_cache_init(sizeof(struct xfs_ifork), "xfs_ifork");
195 xfs_ili_cache = kmem_cache_init(
7a326ce0 196 sizeof(struct xfs_inode_log_item),"xfs_inode_log_item");
2e1394fc 197 xfs_buf_item_cache = kmem_cache_init(
7a326ce0 198 sizeof(struct xfs_buf_log_item), "xfs_buf_log_item");
1577541c
DW
199 error = xfs_defer_init_item_caches();
200 if (error) {
201 fprintf(stderr, "Could not allocate defer init item caches.\n");
202 abort();
203 }
2e1394fc 204 xfs_da_state_cache = kmem_cache_init(
7a326ce0 205 sizeof(struct xfs_da_state), "xfs_da_state");
7d10d094
DW
206 error = xfs_btree_init_cur_caches();
207 if (error) {
208 fprintf(stderr, "Could not allocate btree cursor caches.\n");
209 abort();
210 }
7d84b02d 211 xfs_extfree_item_cache = kmem_cache_init(
7a326ce0 212 sizeof(struct xfs_extent_free_item),
7d84b02d 213 "xfs_extfree_item");
2e1394fc 214 xfs_trans_cache = kmem_cache_init(
7a326ce0
ES
215 sizeof(struct xfs_trans), "xfs_trans");
216}
217
218static int
2e1394fc 219destroy_caches(void)
7a326ce0
ES
220{
221 int leaked = 0;
222
2e1394fc
DW
223 leaked += kmem_cache_destroy(xfs_buf_cache);
224 leaked += kmem_cache_destroy(xfs_ili_cache);
225 leaked += kmem_cache_destroy(xfs_inode_cache);
226 leaked += kmem_cache_destroy(xfs_ifork_cache);
227 leaked += kmem_cache_destroy(xfs_buf_item_cache);
228 leaked += kmem_cache_destroy(xfs_da_state_cache);
1577541c 229 xfs_defer_destroy_item_caches();
7d10d094 230 xfs_btree_destroy_cur_caches();
7d84b02d 231 leaked += kmem_cache_destroy(xfs_extfree_item_cache);
2e1394fc 232 leaked += kmem_cache_destroy(xfs_trans_cache);
7a326ce0
ES
233
234 return leaked;
235}
236
a9468486
DW
237static void
238libxfs_close_devices(
01dcfd9e 239 struct libxfs_init *li)
a9468486 240{
fc83c757
CH
241 if (li->data.dev)
242 libxfs_device_close(&li->data);
243 if (li->log.dev && li->log.dev != li->data.dev)
244 libxfs_device_close(&li->log);
245 if (li->rt.dev)
246 libxfs_device_close(&li->rt);
a9468486
DW
247}
248
2bd0ea18
NS
249/*
250 * libxfs initialization.
251 * Caller gets a 0 on failure (and we print a message), 1 on success.
252 */
253int
01dcfd9e 254libxfs_init(struct libxfs_init *a)
2bd0ea18 255{
d57269fa 256 xfs_check_ondisk_structs();
e4da1b16
DC
257 rcu_init();
258 rcu_register_thread();
bacd44a5
AE
259 radix_tree_init();
260
fc83c757
CH
261 if (!libxfs_device_open(a, &a->data))
262 goto done;
263 if (!libxfs_device_open(a, &a->log))
264 goto done;
265 if (!libxfs_device_open(a, &a->rt))
266 goto done;
b6e08bf3 267
9f38f08d
MV
268 if (!libxfs_bhash_size)
269 libxfs_bhash_size = LIBXFS_BHASHSIZE(sbp);
ba9ecd40
DC
270 libxfs_bcache = cache_init(a->bcache_flags, libxfs_bhash_size,
271 &libxfs_bcache_operations);
23d88955 272 use_xfs_buf_lock = a->flags & LIBXFS_USEBUFLOCK;
7a326ce0 273 xfs_dir_startup();
2e1394fc 274 init_caches();
732f5b90 275 return 1;
a9468486 276
732f5b90
CH
277done:
278 libxfs_close_devices(a);
279 rcu_unregister_thread();
280 return 0;
2bd0ea18
NS
281}
282
283
b391b7cd
NS
284/*
285 * Initialize realtime fields in the mount structure.
286 */
287static int
288rtmount_init(
2420d095 289 xfs_mount_t *mp) /* file system mount structure */
b391b7cd 290{
31079e67 291 struct xfs_buf *bp; /* buffer for last block of subvolume */
b391b7cd 292 xfs_daddr_t d; /* address of last block of subvolume */
113af235 293 unsigned int rsumblocks;
31079e67 294 int error;
b391b7cd 295
575f24e5 296 if (mp->m_sb.sb_rblocks == 0)
b391b7cd 297 return 0;
4aaeedc4 298
eefdf2ab 299 if (xfs_has_reflink(mp)) {
4aaeedc4
DW
300 fprintf(stderr,
301 _("%s: Reflink not compatible with realtime device. Please try a newer xfsprogs.\n"),
302 progname);
303 return -1;
304 }
305
eefdf2ab 306 if (xfs_has_rmapbt(mp)) {
4aaeedc4
DW
307 fprintf(stderr,
308 _("%s: Reverse mapping btree not compatible with realtime device. Please try a newer xfsprogs.\n"),
309 progname);
310 return -1;
311 }
312
2420d095 313 if (mp->m_rtdev_targp->bt_bdev == 0 && !xfs_is_debugger(mp)) {
9440d84d 314 fprintf(stderr, _("%s: filesystem has a realtime subvolume\n"),
b391b7cd
NS
315 progname);
316 return -1;
317 }
575f24e5 318 mp->m_rsumlevels = mp->m_sb.sb_rextslog + 1;
113af235
DW
319 rsumblocks = xfs_rtsummary_blockcount(mp, mp->m_rsumlevels,
320 mp->m_sb.sb_rbmblocks);
321 mp->m_rsumsize = XFS_FSB_TO_B(mp, rsumblocks);
b391b7cd 322 mp->m_rbmip = mp->m_rsumip = NULL;
39798eb5
NS
323
324 /*
325 * Allow debugger to be run without the realtime device present.
326 */
2420d095 327 if (xfs_is_debugger(mp))
39798eb5
NS
328 return 0;
329
b391b7cd
NS
330 /*
331 * Check that the realtime section is an ok size.
332 */
333 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
334 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
9440d84d
NS
335 fprintf(stderr, _("%s: realtime init - %llu != %llu\n"),
336 progname, (unsigned long long) XFS_BB_TO_FSB(mp, d),
b391b7cd
NS
337 (unsigned long long) mp->m_sb.sb_rblocks);
338 return -1;
339 }
31079e67
DW
340 error = libxfs_buf_read(mp->m_rtdev, d - XFS_FSB_TO_BB(mp, 1),
341 XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
342 if (error) {
9440d84d
NS
343 fprintf(stderr, _("%s: realtime size check failed\n"),
344 progname);
b391b7cd
NS
345 return -1;
346 }
e02ba985 347 libxfs_buf_relse(bp);
b391b7cd
NS
348 return 0;
349}
350
03dc2ef2
DC
351static bool
352xfs_set_inode_alloc_perag(
353 struct xfs_perag *pag,
354 xfs_ino_t ino,
355 xfs_agnumber_t max_metadata)
356{
357 if (!xfs_is_inode32(pag->pag_mount)) {
358 set_bit(XFS_AGSTATE_ALLOWS_INODES, &pag->pag_opstate);
359 clear_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
360 return false;
361 }
362
363 if (ino > XFS_MAXINUMBER_32) {
364 clear_bit(XFS_AGSTATE_ALLOWS_INODES, &pag->pag_opstate);
365 clear_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
366 return false;
367 }
368
369 set_bit(XFS_AGSTATE_ALLOWS_INODES, &pag->pag_opstate);
370 if (pag->pag_agno < max_metadata)
371 set_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
372 else
373 clear_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
374 return true;
375}
376
b9ee1227
DW
377/*
378 * Set parameters for inode allocation heuristics, taking into account
379 * filesystem size and inode32/inode64 mount options; i.e. specifically
380 * whether or not XFS_MOUNT_SMALL_INUMS is set.
381 *
382 * Inode allocation patterns are altered only if inode32 is requested
383 * (XFS_MOUNT_SMALL_INUMS), and the filesystem is sufficiently large.
384 * If altered, XFS_MOUNT_32BITINODES is set as well.
385 *
386 * An agcount independent of that in the mount structure is provided
387 * because in the growfs case, mp->m_sb.sb_agcount is not yet updated
388 * to the potentially higher ag count.
389 *
390 * Returns the maximum AG index which may contain inodes.
ed8f5980
DW
391 *
392 * NOTE: userspace has no concept of "inode32" and so xfs_has_small_inums
393 * is always false, and much of this code is a no-op.
b9ee1227
DW
394 */
395xfs_agnumber_t
396xfs_set_inode_alloc(
397 struct xfs_mount *mp,
398 xfs_agnumber_t agcount)
399{
400 xfs_agnumber_t index;
401 xfs_agnumber_t maxagi = 0;
402 xfs_sb_t *sbp = &mp->m_sb;
403 xfs_agnumber_t max_metadata;
404 xfs_agino_t agino;
405 xfs_ino_t ino;
406
407 /*
408 * Calculate how much should be reserved for inodes to meet
409 * the max inode percentage. Used only for inode32.
410 */
411 if (M_IGEO(mp)->maxicount) {
412 uint64_t icount;
413
414 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
415 do_div(icount, 100);
416 icount += sbp->sb_agblocks - 1;
417 do_div(icount, sbp->sb_agblocks);
418 max_metadata = icount;
419 } else {
420 max_metadata = agcount;
421 }
422
423 /* Get the last possible inode in the filesystem */
424 agino = XFS_AGB_TO_AGINO(mp, sbp->sb_agblocks - 1);
425 ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
426
427 /*
428 * If user asked for no more than 32-bit inodes, and the fs is
429 * sufficiently large, set XFS_MOUNT_32BITINODES if we must alter
430 * the allocator to accommodate the request.
431 */
ed8f5980 432 if (xfs_has_small_inums(mp) && ino > XFS_MAXINUMBER_32)
03dc2ef2 433 set_bit(XFS_OPSTATE_INODE32, &mp->m_opstate);
ed8f5980 434 else
03dc2ef2 435 clear_bit(XFS_OPSTATE_INODE32, &mp->m_opstate);
b9ee1227
DW
436
437 for (index = 0; index < agcount; index++) {
438 struct xfs_perag *pag;
439
440 ino = XFS_AGINO_TO_INO(mp, index, agino);
441
442 pag = xfs_perag_get(mp, index);
03dc2ef2
DC
443 if (xfs_set_inode_alloc_perag(pag, ino, max_metadata))
444 maxagi++;
b9ee1227
DW
445 xfs_perag_put(pag);
446 }
447
0ee9753e 448 return xfs_is_inode32(mp) ? maxagi : agcount;
b9ee1227
DW
449}
450
75c8b434
DC
451static struct xfs_buftarg *
452libxfs_buftarg_alloc(
453 struct xfs_mount *mp,
fc83c757 454 struct libxfs_dev *dev,
704e4cef 455 unsigned long write_fails)
75c8b434
DC
456{
457 struct xfs_buftarg *btp;
458
459 btp = malloc(sizeof(*btp));
460 if (!btp) {
461 fprintf(stderr, _("%s: buftarg init failed\n"),
462 progname);
463 exit(1);
464 }
465 btp->bt_mount = mp;
fc83c757
CH
466 btp->bt_bdev = dev->dev;
467 btp->bt_bdev_fd = dev->fd;
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
75c8b434
DC
475 return btp;
476}
477
704e4cef
DW
478enum libxfs_write_failure_nums {
479 WF_DATA = 0,
480 WF_LOG,
481 WF_RT,
482 WF_MAX_OPTS,
483};
484
485static char *wf_opts[] = {
486 [WF_DATA] = "ddev",
487 [WF_LOG] = "logdev",
488 [WF_RT] = "rtdev",
489 [WF_MAX_OPTS] = NULL,
490};
491
75c8b434
DC
492void
493libxfs_buftarg_init(
494 struct xfs_mount *mp,
ca8cc76e 495 struct libxfs_init *xi)
75c8b434 496{
704e4cef
DW
497 char *p = getenv("LIBXFS_DEBUG_WRITE_CRASH");
498 unsigned long dfail = 0, lfail = 0, rfail = 0;
499
500 /* Simulate utility crash after a certain number of writes. */
501 while (p && *p) {
502 char *val;
503
504 switch (getsubopt(&p, wf_opts, &val)) {
505 case WF_DATA:
506 if (!val) {
507 fprintf(stderr,
508 _("ddev write fail requires a parameter\n"));
509 exit(1);
510 }
511 dfail = strtoul(val, NULL, 0);
512 break;
513 case WF_LOG:
514 if (!val) {
515 fprintf(stderr,
516 _("logdev write fail requires a parameter\n"));
517 exit(1);
518 }
519 lfail = strtoul(val, NULL, 0);
520 break;
521 case WF_RT:
522 if (!val) {
523 fprintf(stderr,
524 _("rtdev write fail requires a parameter\n"));
525 exit(1);
526 }
527 rfail = strtoul(val, NULL, 0);
528 break;
529 default:
530 fprintf(stderr, _("unknown write fail type %s\n"),
531 val);
532 exit(1);
533 break;
534 }
535 }
536
75c8b434
DC
537 if (mp->m_ddev_targp) {
538 /* should already have all buftargs initialised */
fc83c757 539 if (mp->m_ddev_targp->bt_bdev != xi->data.dev ||
75c8b434
DC
540 mp->m_ddev_targp->bt_mount != mp) {
541 fprintf(stderr,
542 _("%s: bad buftarg reinit, ddev\n"),
543 progname);
544 exit(1);
545 }
fc83c757 546 if (!xi->log.dev || xi->log.dev == xi->data.dev) {
75c8b434
DC
547 if (mp->m_logdev_targp != mp->m_ddev_targp) {
548 fprintf(stderr,
549 _("%s: bad buftarg reinit, ldev mismatch\n"),
550 progname);
551 exit(1);
552 }
fc83c757 553 } else if (mp->m_logdev_targp->bt_bdev != xi->log.dev ||
75c8b434
DC
554 mp->m_logdev_targp->bt_mount != mp) {
555 fprintf(stderr,
556 _("%s: bad buftarg reinit, logdev\n"),
557 progname);
558 exit(1);
559 }
fc83c757
CH
560 if (xi->rt.dev &&
561 (mp->m_rtdev_targp->bt_bdev != xi->rt.dev ||
ca8cc76e 562 mp->m_rtdev_targp->bt_mount != mp)) {
75c8b434
DC
563 fprintf(stderr,
564 _("%s: bad buftarg reinit, rtdev\n"),
565 progname);
566 exit(1);
567 }
568 return;
569 }
570
fc83c757
CH
571 mp->m_ddev_targp = libxfs_buftarg_alloc(mp, &xi->data, dfail);
572 if (!xi->log.dev || xi->log.dev == xi->data.dev)
75c8b434
DC
573 mp->m_logdev_targp = mp->m_ddev_targp;
574 else
fc83c757
CH
575 mp->m_logdev_targp = libxfs_buftarg_alloc(mp, &xi->log, lfail);
576 mp->m_rtdev_targp = libxfs_buftarg_alloc(mp, &xi->rt, rfail);
75c8b434
DC
577}
578
6afce48f
DW
579/* Compute maximum possible height for per-AG btree types for this fs. */
580static inline void
581xfs_agbtree_compute_maxlevels(
582 struct xfs_mount *mp)
583{
584 unsigned int levels;
585
586 levels = max(mp->m_alloc_maxlevels, M_IGEO(mp)->inobt_maxlevels);
587 levels = max(levels, mp->m_rmap_maxlevels);
588 mp->m_agbtree_maxlevels = max(levels, mp->m_refc_maxlevels);
589}
590
7aeffc87
DW
591/* Compute maximum possible height of all btrees. */
592void
593libxfs_compute_all_maxlevels(
594 struct xfs_mount *mp)
595{
596 xfs_alloc_compute_maxlevels(mp);
597 xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
598 xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
599 xfs_ialloc_setup_geometry(mp);
600 xfs_rmapbt_compute_maxlevels(mp);
601 xfs_refcountbt_compute_maxlevels(mp);
602
603 xfs_agbtree_compute_maxlevels(mp);
604}
605
33f3aac8
DC
606/*
607 * precalculate the low space thresholds for dynamic speculative preallocation.
608 */
609static void
610xfs_set_low_space_thresholds(
611 struct xfs_mount *mp)
612{
613 uint64_t dblocks = mp->m_sb.sb_dblocks;
614 int i;
615
616 do_div(dblocks, 100);
617
618 for (i = 0; i < XFS_LOWSP_MAX; i++)
619 mp->m_low_space[i] = dblocks * (i + 1);
620}
621
2bd0ea18
NS
622/*
623 * Mount structure initialization, provides a filled-in xfs_mount_t
624 * such that the numerous XFS_* macros can be used. If dev is zero,
625 * no IO will be performed (no size checks, read root inodes).
626 */
d855bce8 627struct xfs_mount *
2bd0ea18 628libxfs_mount(
d855bce8
DW
629 struct xfs_mount *mp,
630 struct xfs_sb *sb,
ddd9942b 631 struct libxfs_init *xi,
ed8f5980 632 unsigned int flags)
2bd0ea18 633{
d855bce8
DW
634 struct xfs_buf *bp;
635 struct xfs_sb *sbp;
636 xfs_daddr_t d;
d855bce8 637 int error;
2bd0ea18 638
3bc1fdd4 639 mp->m_features = xfs_sb_version_to_features(sb);
2420d095
DW
640 if (flags & LIBXFS_MOUNT_DEBUGGER)
641 xfs_set_debugger(mp);
e42c53f3 642 if (flags & LIBXFS_MOUNT_REPORT_CORRUPTION)
2420d095 643 xfs_set_reporting_corruption(mp);
ca8cc76e 644 libxfs_buftarg_init(mp, xi);
75c8b434 645
f747f7dd 646 mp->m_finobt_nores = true;
0ee9753e 647 xfs_set_inode32(mp);
2bd0ea18 648 mp->m_sb = *sb;
56b2de80 649 INIT_RADIX_TREE(&mp->m_perag_tree, GFP_KERNEL);
686bddf9
DC
650 sbp = &mp->m_sb;
651 spin_lock_init(&mp->m_sb_lock);
652 spin_lock_init(&mp->m_agirotor_lock);
2bd0ea18 653
4896e6c8 654 xfs_sb_mount_common(mp, sb);
2bd0ea18 655
949c0f10
NS
656 /*
657 * Set whether we're using stripe alignment.
658 */
2660e653 659 if (xfs_has_dalign(mp)) {
949c0f10
NS
660 mp->m_dalign = sbp->sb_unit;
661 mp->m_swidth = sbp->sb_width;
662 }
663
7aeffc87 664 libxfs_compute_all_maxlevels(mp);
6afce48f 665
2bd0ea18
NS
666 /*
667 * Check that the data (and log if separate) are an ok size.
668 */
9440d84d 669 d = (xfs_daddr_t) XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
2bd0ea18 670 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
9440d84d 671 fprintf(stderr, _("%s: size check failed\n"), progname);
2420d095 672 if (!xfs_is_debugger(mp))
4ca431fc 673 return NULL;
2bd0ea18
NS
674 }
675
ff105f75
DC
676 /*
677 * We automatically convert v1 inodes to v2 inodes now, so if
678 * the NLINK bit is not set we can't operate on the filesystem.
679 */
680 if (!(sbp->sb_versionnum & XFS_SB_VERSION_NLINKBIT)) {
681
682 fprintf(stderr, _(
683 "%s: V1 inodes unsupported. Please try an older xfsprogs.\n"),
684 progname);
685 exit(1);
686 }
687
688 /* Check for supported directory formats */
689 if (!(sbp->sb_versionnum & XFS_SB_VERSION_DIRV2BIT)) {
9a048535
DC
690
691 fprintf(stderr, _(
692 "%s: V1 directories unsupported. Please try an older xfsprogs.\n"),
693 progname);
694 exit(1);
5e656dbb 695 }
2bd0ea18 696
ff105f75
DC
697 /* check for unsupported other features */
698 if (!xfs_sb_good_version(sbp)) {
699 fprintf(stderr, _(
700 "%s: Unsupported features detected. Please try a newer xfsprogs.\n"),
701 progname);
702 exit(1);
703 }
704
705 xfs_da_mount(mp);
706
2bd0ea18 707 /* Initialize the precomputed transaction reservations values */
5e656dbb 708 xfs_trans_init(mp);
2bd0ea18 709
fc83c757 710 if (xi->data.dev == 0) /* maxtrres, we have no device so leave now */
2bd0ea18
NS
711 return mp;
712
d855bce8 713 /* device size checks must pass unless we're a debugger. */
31079e67
DW
714 error = libxfs_buf_read(mp->m_dev, d - XFS_FSS_TO_BB(mp, 1),
715 XFS_FSS_TO_BB(mp, 1), 0, &bp, NULL);
716 if (error) {
9440d84d 717 fprintf(stderr, _("%s: data size check failed\n"), progname);
2420d095 718 if (!xfs_is_debugger(mp))
4ca431fc 719 return NULL;
32244196 720 } else
e02ba985 721 libxfs_buf_relse(bp);
2bd0ea18 722
ab434d12
DC
723 if (mp->m_logdev_targp->bt_bdev &&
724 mp->m_logdev_targp->bt_bdev != mp->m_ddev_targp->bt_bdev) {
9440d84d 725 d = (xfs_daddr_t) XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
31079e67
DW
726 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks ||
727 libxfs_buf_read(mp->m_logdev_targp,
728 d - XFS_FSB_TO_BB(mp, 1), XFS_FSB_TO_BB(mp, 1),
729 0, &bp, NULL)) {
9440d84d 730 fprintf(stderr, _("%s: log size checks failed\n"),
2bd0ea18 731 progname);
2420d095 732 if (!xfs_is_debugger(mp))
4ca431fc 733 return NULL;
2bd0ea18 734 }
32244196 735 if (bp)
e02ba985 736 libxfs_buf_relse(bp);
2bd0ea18
NS
737 }
738
33f3aac8
DC
739 xfs_set_low_space_thresholds(mp);
740
2bd0ea18 741 /* Initialize realtime fields in the mount structure */
2420d095 742 if (rtmount_init(mp)) {
9440d84d
NS
743 fprintf(stderr, _("%s: realtime device init failed\n"),
744 progname);
4ca431fc 745 return NULL;
2bd0ea18
NS
746 }
747
a547152d
ES
748 /*
749 * libxfs_initialize_perag will allocate a perag structure for each ag.
750 * If agcount is corrupted and insanely high, this will OOM the box.
751 * If the agount seems (arbitrarily) high, try to read what would be
752 * the last AG, and if that fails for a relatively high agcount, just
753 * read the first one and let the user know to check the geometry.
754 */
755 if (sbp->sb_agcount > 1000000) {
31079e67 756 error = libxfs_buf_read(mp->m_dev,
a547152d 757 XFS_AG_DADDR(mp, sbp->sb_agcount - 1, 0), 1,
31079e67
DW
758 0, &bp, NULL);
759 if (error) {
a547152d
ES
760 fprintf(stderr, _("%s: read of AG %u failed\n"),
761 progname, sbp->sb_agcount);
2420d095 762 if (!xfs_is_debugger(mp))
a547152d
ES
763 return NULL;
764 fprintf(stderr, _("%s: limiting reads to AG 0\n"),
765 progname);
766 sbp->sb_agcount = 1;
31079e67
DW
767 } else
768 libxfs_buf_relse(bp);
a547152d
ES
769 }
770
83af0d13
DC
771 error = libxfs_initialize_perag(mp, sbp->sb_agcount, sbp->sb_dblocks,
772 &mp->m_maxagi);
56b2de80
DC
773 if (error) {
774 fprintf(stderr, _("%s: perag init failed\n"),
775 progname);
2bd0ea18
NS
776 exit(1);
777 }
2420d095 778 xfs_set_perag_data_loaded(mp);
2bd0ea18 779
2bd0ea18
NS
780 return mp;
781}
782
f1b058f9
NS
783void
784libxfs_rtmount_destroy(xfs_mount_t *mp)
785{
786 if (mp->m_rsumip)
31845e4c 787 libxfs_irele(mp->m_rsumip);
f1b058f9 788 if (mp->m_rbmip)
31845e4c 789 libxfs_irele(mp->m_rbmip);
f1b058f9
NS
790 mp->m_rsumip = mp->m_rbmip = NULL;
791}
792
c335b673
DW
793/* Flush a device and report on writes that didn't make it to stable storage. */
794static inline int
795libxfs_flush_buftarg(
796 struct xfs_buftarg *btp,
797 const char *buftarg_descr)
798{
799 int error = 0;
800 int err2;
801
802 /*
803 * Write verifier failures are evidence of a buggy program. Make sure
804 * that this state is always reported to the caller.
805 */
806 if (btp->flags & XFS_BUFTARG_CORRUPT_WRITE) {
807 fprintf(stderr,
808_("%s: Refusing to write a corrupt buffer to the %s!\n"),
809 progname, buftarg_descr);
810 error = -EFSCORRUPTED;
811 }
812
813 if (btp->flags & XFS_BUFTARG_LOST_WRITE) {
814 fprintf(stderr,
815_("%s: Lost a write to the %s!\n"),
816 progname, buftarg_descr);
817 if (!error)
818 error = -EIO;
819 }
820
821 err2 = libxfs_blkdev_issue_flush(btp);
822 if (err2) {
823 fprintf(stderr,
824_("%s: Flushing the %s failed, err=%d!\n"),
825 progname, buftarg_descr, -err2);
826 }
827 if (!error)
828 error = err2;
829
830 return error;
831}
832
833/*
834 * Flush all dirty buffers to stable storage and report on writes that didn't
835 * make it to stable storage.
836 */
a7348c58 837int
c335b673
DW
838libxfs_flush_mount(
839 struct xfs_mount *mp)
840{
841 int error = 0;
842 int err2;
843
844 /*
a7348c58
DW
845 * Flush the buffer cache to write all dirty buffers to disk. Buffers
846 * that fail write verification will cause the CORRUPT_WRITE flag to be
847 * set in the buftarg. Buffers that cannot be written will cause the
848 * LOST_WRITE flag to be set in the buftarg. Once that's done,
849 * instruct the disks to persist their write caches.
c335b673 850 */
a7348c58 851 libxfs_bcache_flush();
c335b673
DW
852
853 /* Flush all kernel and disk write caches, and report failures. */
854 if (mp->m_ddev_targp) {
855 err2 = libxfs_flush_buftarg(mp->m_ddev_targp, _("data device"));
856 if (!error)
857 error = err2;
858 }
859
860 if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp) {
861 err2 = libxfs_flush_buftarg(mp->m_logdev_targp,
862 _("log device"));
863 if (!error)
864 error = err2;
865 }
866
867 if (mp->m_rtdev_targp) {
868 err2 = libxfs_flush_buftarg(mp->m_rtdev_targp,
869 _("realtime device"));
870 if (!error)
871 error = err2;
872 }
873
874 return error;
875}
876
2bd0ea18 877/*
9440d84d 878 * Release any resource obtained during a mount.
2bd0ea18 879 */
c335b673
DW
880int
881libxfs_umount(
882 struct xfs_mount *mp)
2bd0ea18 883{
c335b673 884 int error;
56b2de80 885
f1b058f9 886 libxfs_rtmount_destroy(mp);
c335b673 887
a7348c58
DW
888 /*
889 * Purge the buffer cache to write all dirty buffers to disk and free
890 * all incore buffers, then pick up the outcome when we tell the disks
891 * to persist their write caches.
892 */
893 libxfs_bcache_purge();
c335b673 894 error = libxfs_flush_mount(mp);
f1b058f9 895
7bf9cd9d
DW
896 /*
897 * Only try to free the per-AG structures if we set them up in the
898 * first place.
899 */
2420d095 900 if (xfs_is_perag_data_loaded(mp))
4bcd30f6 901 libxfs_free_perag(mp);
4334e2e8
ES
902
903 kmem_free(mp->m_attr_geo);
904 kmem_free(mp->m_dir_geo);
905
906 kmem_free(mp->m_rtdev_targp);
907 if (mp->m_logdev_targp != mp->m_ddev_targp)
908 kmem_free(mp->m_logdev_targp);
909 kmem_free(mp->m_ddev_targp);
f8149110 910
c335b673 911 return error;
2bd0ea18 912}
f1b058f9
NS
913
914/*
915 * Release any global resources used by libxfs.
916 */
917void
a9468486 918libxfs_destroy(
01dcfd9e 919 struct libxfs_init *li)
f1b058f9 920{
a9468486
DW
921 int leaked;
922
923 libxfs_close_devices(li);
44488491 924
2e1394fc 925 /* Free everything from the buffer cache before freeing buffer cache */
864028ed
ES
926 libxfs_bcache_purge();
927 libxfs_bcache_free();
f1b058f9 928 cache_destroy(libxfs_bcache);
2e1394fc 929 leaked = destroy_caches();
e4da1b16 930 rcu_unregister_thread();
44488491
ES
931 if (getenv("LIBXFS_LEAK_CHECK") && leaked)
932 exit(1);
f1b058f9 933}
9f38f08d 934
b74a1f6a
NS
935int
936libxfs_device_alignment(void)
937{
938 return platform_align_blockdev();
939}
940
9f38f08d 941void
b6281496 942libxfs_report(FILE *fp)
9f38f08d 943{
cb5b3ef4
MV
944 time_t t;
945 char *c;
946
b6281496 947 cache_report(fp, "libxfs_bcache", libxfs_bcache);
cb5b3ef4
MV
948
949 t = time(NULL);
950 c = asctime(localtime(&t));
951 fprintf(fp, "%s", c);
952}