]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/init.c
xfsprogs: Release v6.8.0
[thirdparty/xfsprogs-dev.git] / libxfs / init.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6
7 #include <sys/stat.h>
8 #include "init.h"
9
10 #include "libxfs_priv.h"
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"
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 "libfrog/platform.h"
25
26 #include "xfs_format.h"
27 #include "xfs_da_format.h"
28 #include "xfs_log_format.h"
29 #include "xfs_ondisk.h"
30
31 #include "libxfs.h" /* for now */
32
33 #ifndef HAVE_LIBURCU_ATOMIC64
34 pthread_mutex_t atomic64_lock = PTHREAD_MUTEX_INITIALIZER;
35 #endif
36
37 char *progname = "libxfs"; /* default, changed by each tool */
38
39 struct cache *libxfs_bcache; /* global buffer cache */
40 int libxfs_bhash_size; /* #buckets in bcache */
41
42 int use_xfs_buf_lock; /* global flag: use xfs_buf locks for MT */
43
44 static int nextfakedev = -1; /* device number to give to next fake device */
45
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).
50 *
51 * Useful to tools which will produce uncertain results
52 * if the filesystem is active - repair, check, logprint.
53 */
54 static int
55 check_isactive(char *name, char *block, int fatal)
56 {
57 struct stat st;
58
59 if (stat(block, &st) < 0)
60 return 0;
61 if ((st.st_mode & S_IFMT) != S_IFBLK)
62 return 0;
63 if (platform_check_ismounted(name, block, &st, 0) == 0)
64 return 0;
65 if (platform_check_iswritable(name, block, &st))
66 return fatal ? 1 : 0;
67 return 0;
68 }
69
70 static int
71 check_open(
72 struct libxfs_init *xi,
73 struct libxfs_dev *dev)
74 {
75 struct stat stbuf;
76
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;
85
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
94 static bool
95 libxfs_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 }
122
123 retry:
124 dev->fd = open(dev->name, flags, 0666);
125 if (dev->fd < 0) {
126 if (errno == EINVAL && (flags & O_DIRECT)) {
127 flags &= ~O_DIRECT;
128 goto retry;
129 }
130 fprintf(stderr, _("%s: cannot open %s: %s\n"),
131 progname, dev->name, strerror(errno));
132 exit(1);
133 }
134
135 if (fstat(dev->fd, &statb) < 0) {
136 fprintf(stderr, _("%s: cannot stat %s: %s\n"),
137 progname, dev->name, strerror(errno));
138 exit(1);
139 }
140
141 if (!(xi->flags & LIBXFS_ISREADONLY) &&
142 xi->setblksize &&
143 (statb.st_mode & S_IFMT) == S_IFBLK) {
144 /*
145 * Try to use the given explicit blocksize. Failure to set the
146 * block size is only fatal for direct I/O.
147 */
148 platform_set_blocksize(dev->fd, dev->name, statb.st_rdev,
149 xi->setblksize, flags & O_DIRECT);
150 }
151
152 /*
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.
155 */
156 if (statb.st_rdev)
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;
162 }
163
164 static void
165 libxfs_device_close(
166 struct libxfs_dev *dev)
167 {
168 int ret;
169
170 ret = platform_flush_device(dev->fd, dev->dev);
171 if (ret) {
172 ret = -errno;
173 fprintf(stderr,
174 _("%s: flush of device %s failed, err=%d"),
175 progname, dev->name, ret);
176 }
177 close(dev->fd);
178
179 dev->fd = -1;
180 dev->dev = 0;
181 }
182
183 /*
184 * Initialize/destroy all of the cache allocators we use.
185 */
186 static void
187 init_caches(void)
188 {
189 int error;
190
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(
196 sizeof(struct xfs_inode_log_item),"xfs_inode_log_item");
197 xfs_buf_item_cache = kmem_cache_init(
198 sizeof(struct xfs_buf_log_item), "xfs_buf_log_item");
199 error = xfs_defer_init_item_caches();
200 if (error) {
201 fprintf(stderr, "Could not allocate defer init item caches.\n");
202 abort();
203 }
204 xfs_da_state_cache = kmem_cache_init(
205 sizeof(struct xfs_da_state), "xfs_da_state");
206 error = xfs_btree_init_cur_caches();
207 if (error) {
208 fprintf(stderr, "Could not allocate btree cursor caches.\n");
209 abort();
210 }
211 xfs_extfree_item_cache = kmem_cache_init(
212 sizeof(struct xfs_extent_free_item),
213 "xfs_extfree_item");
214 xfs_trans_cache = kmem_cache_init(
215 sizeof(struct xfs_trans), "xfs_trans");
216 }
217
218 static int
219 destroy_caches(void)
220 {
221 int leaked = 0;
222
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);
229 xfs_defer_destroy_item_caches();
230 xfs_btree_destroy_cur_caches();
231 leaked += kmem_cache_destroy(xfs_extfree_item_cache);
232 leaked += kmem_cache_destroy(xfs_trans_cache);
233
234 return leaked;
235 }
236
237 static void
238 libxfs_close_devices(
239 struct libxfs_init *li)
240 {
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);
247 }
248
249 /*
250 * libxfs initialization.
251 * Caller gets a 0 on failure (and we print a message), 1 on success.
252 */
253 int
254 libxfs_init(struct libxfs_init *a)
255 {
256 xfs_check_ondisk_structs();
257 rcu_init();
258 rcu_register_thread();
259 radix_tree_init();
260
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;
267
268 if (!libxfs_bhash_size)
269 libxfs_bhash_size = LIBXFS_BHASHSIZE(sbp);
270 libxfs_bcache = cache_init(a->bcache_flags, libxfs_bhash_size,
271 &libxfs_bcache_operations);
272 use_xfs_buf_lock = a->flags & LIBXFS_USEBUFLOCK;
273 xfs_dir_startup();
274 init_caches();
275 return 1;
276
277 done:
278 libxfs_close_devices(a);
279 rcu_unregister_thread();
280 return 0;
281 }
282
283
284 /*
285 * Initialize realtime fields in the mount structure.
286 */
287 static int
288 rtmount_init(
289 xfs_mount_t *mp) /* file system mount structure */
290 {
291 struct xfs_buf *bp; /* buffer for last block of subvolume */
292 xfs_daddr_t d; /* address of last block of subvolume */
293 unsigned int rsumblocks;
294 int error;
295
296 if (mp->m_sb.sb_rblocks == 0)
297 return 0;
298
299 if (xfs_has_reflink(mp)) {
300 fprintf(stderr,
301 _("%s: Reflink not compatible with realtime device. Please try a newer xfsprogs.\n"),
302 progname);
303 return -1;
304 }
305
306 if (xfs_has_rmapbt(mp)) {
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
313 if (mp->m_rtdev_targp->bt_bdev == 0 && !xfs_is_debugger(mp)) {
314 fprintf(stderr, _("%s: filesystem has a realtime subvolume\n"),
315 progname);
316 return -1;
317 }
318 mp->m_rsumlevels = mp->m_sb.sb_rextslog + 1;
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);
322 mp->m_rbmip = mp->m_rsumip = NULL;
323
324 /*
325 * Allow debugger to be run without the realtime device present.
326 */
327 if (xfs_is_debugger(mp))
328 return 0;
329
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) {
335 fprintf(stderr, _("%s: realtime init - %llu != %llu\n"),
336 progname, (unsigned long long) XFS_BB_TO_FSB(mp, d),
337 (unsigned long long) mp->m_sb.sb_rblocks);
338 return -1;
339 }
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) {
343 fprintf(stderr, _("%s: realtime size check failed\n"),
344 progname);
345 return -1;
346 }
347 libxfs_buf_relse(bp);
348 return 0;
349 }
350
351 static bool
352 xfs_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
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.
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.
394 */
395 xfs_agnumber_t
396 xfs_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 */
432 if (xfs_has_small_inums(mp) && ino > XFS_MAXINUMBER_32)
433 set_bit(XFS_OPSTATE_INODE32, &mp->m_opstate);
434 else
435 clear_bit(XFS_OPSTATE_INODE32, &mp->m_opstate);
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);
443 if (xfs_set_inode_alloc_perag(pag, ino, max_metadata))
444 maxagi++;
445 xfs_perag_put(pag);
446 }
447
448 return xfs_is_inode32(mp) ? maxagi : agcount;
449 }
450
451 static struct xfs_buftarg *
452 libxfs_buftarg_alloc(
453 struct xfs_mount *mp,
454 struct libxfs_dev *dev,
455 unsigned long write_fails)
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;
466 btp->bt_bdev = dev->dev;
467 btp->bt_bdev_fd = dev->fd;
468 btp->flags = 0;
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);
474
475 return btp;
476 }
477
478 enum libxfs_write_failure_nums {
479 WF_DATA = 0,
480 WF_LOG,
481 WF_RT,
482 WF_MAX_OPTS,
483 };
484
485 static char *wf_opts[] = {
486 [WF_DATA] = "ddev",
487 [WF_LOG] = "logdev",
488 [WF_RT] = "rtdev",
489 [WF_MAX_OPTS] = NULL,
490 };
491
492 void
493 libxfs_buftarg_init(
494 struct xfs_mount *mp,
495 struct libxfs_init *xi)
496 {
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
537 if (mp->m_ddev_targp) {
538 /* should already have all buftargs initialised */
539 if (mp->m_ddev_targp->bt_bdev != xi->data.dev ||
540 mp->m_ddev_targp->bt_mount != mp) {
541 fprintf(stderr,
542 _("%s: bad buftarg reinit, ddev\n"),
543 progname);
544 exit(1);
545 }
546 if (!xi->log.dev || xi->log.dev == xi->data.dev) {
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 }
553 } else if (mp->m_logdev_targp->bt_bdev != xi->log.dev ||
554 mp->m_logdev_targp->bt_mount != mp) {
555 fprintf(stderr,
556 _("%s: bad buftarg reinit, logdev\n"),
557 progname);
558 exit(1);
559 }
560 if (xi->rt.dev &&
561 (mp->m_rtdev_targp->bt_bdev != xi->rt.dev ||
562 mp->m_rtdev_targp->bt_mount != mp)) {
563 fprintf(stderr,
564 _("%s: bad buftarg reinit, rtdev\n"),
565 progname);
566 exit(1);
567 }
568 return;
569 }
570
571 mp->m_ddev_targp = libxfs_buftarg_alloc(mp, &xi->data, dfail);
572 if (!xi->log.dev || xi->log.dev == xi->data.dev)
573 mp->m_logdev_targp = mp->m_ddev_targp;
574 else
575 mp->m_logdev_targp = libxfs_buftarg_alloc(mp, &xi->log, lfail);
576 mp->m_rtdev_targp = libxfs_buftarg_alloc(mp, &xi->rt, rfail);
577 }
578
579 /* Compute maximum possible height for per-AG btree types for this fs. */
580 static inline void
581 xfs_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
591 /* Compute maximum possible height of all btrees. */
592 void
593 libxfs_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
606 /*
607 * precalculate the low space thresholds for dynamic speculative preallocation.
608 */
609 static void
610 xfs_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
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 */
627 struct xfs_mount *
628 libxfs_mount(
629 struct xfs_mount *mp,
630 struct xfs_sb *sb,
631 struct libxfs_init *xi,
632 unsigned int flags)
633 {
634 struct xfs_buf *bp;
635 struct xfs_sb *sbp;
636 xfs_daddr_t d;
637 int error;
638
639 mp->m_features = xfs_sb_version_to_features(sb);
640 if (flags & LIBXFS_MOUNT_DEBUGGER)
641 xfs_set_debugger(mp);
642 if (flags & LIBXFS_MOUNT_REPORT_CORRUPTION)
643 xfs_set_reporting_corruption(mp);
644 libxfs_buftarg_init(mp, xi);
645
646 mp->m_finobt_nores = true;
647 xfs_set_inode32(mp);
648 mp->m_sb = *sb;
649 INIT_RADIX_TREE(&mp->m_perag_tree, GFP_KERNEL);
650 sbp = &mp->m_sb;
651 spin_lock_init(&mp->m_sb_lock);
652 spin_lock_init(&mp->m_agirotor_lock);
653
654 xfs_sb_mount_common(mp, sb);
655
656 /*
657 * Set whether we're using stripe alignment.
658 */
659 if (xfs_has_dalign(mp)) {
660 mp->m_dalign = sbp->sb_unit;
661 mp->m_swidth = sbp->sb_width;
662 }
663
664 libxfs_compute_all_maxlevels(mp);
665
666 /*
667 * Check that the data (and log if separate) are an ok size.
668 */
669 d = (xfs_daddr_t) XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
670 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
671 fprintf(stderr, _("%s: size check failed\n"), progname);
672 if (!xfs_is_debugger(mp))
673 return NULL;
674 }
675
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)) {
690
691 fprintf(stderr, _(
692 "%s: V1 directories unsupported. Please try an older xfsprogs.\n"),
693 progname);
694 exit(1);
695 }
696
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
707 /* Initialize the precomputed transaction reservations values */
708 xfs_trans_init(mp);
709
710 if (xi->data.dev == 0) /* maxtrres, we have no device so leave now */
711 return mp;
712
713 /* device size checks must pass unless we're a debugger. */
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) {
717 fprintf(stderr, _("%s: data size check failed\n"), progname);
718 if (!xfs_is_debugger(mp))
719 return NULL;
720 } else
721 libxfs_buf_relse(bp);
722
723 if (mp->m_logdev_targp->bt_bdev &&
724 mp->m_logdev_targp->bt_bdev != mp->m_ddev_targp->bt_bdev) {
725 d = (xfs_daddr_t) XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
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)) {
730 fprintf(stderr, _("%s: log size checks failed\n"),
731 progname);
732 if (!xfs_is_debugger(mp))
733 return NULL;
734 }
735 if (bp)
736 libxfs_buf_relse(bp);
737 }
738
739 xfs_set_low_space_thresholds(mp);
740
741 /* Initialize realtime fields in the mount structure */
742 if (rtmount_init(mp)) {
743 fprintf(stderr, _("%s: realtime device init failed\n"),
744 progname);
745 return NULL;
746 }
747
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) {
756 error = libxfs_buf_read(mp->m_dev,
757 XFS_AG_DADDR(mp, sbp->sb_agcount - 1, 0), 1,
758 0, &bp, NULL);
759 if (error) {
760 fprintf(stderr, _("%s: read of AG %u failed\n"),
761 progname, sbp->sb_agcount);
762 if (!xfs_is_debugger(mp))
763 return NULL;
764 fprintf(stderr, _("%s: limiting reads to AG 0\n"),
765 progname);
766 sbp->sb_agcount = 1;
767 } else
768 libxfs_buf_relse(bp);
769 }
770
771 error = libxfs_initialize_perag(mp, sbp->sb_agcount, sbp->sb_dblocks,
772 &mp->m_maxagi);
773 if (error) {
774 fprintf(stderr, _("%s: perag init failed\n"),
775 progname);
776 exit(1);
777 }
778 xfs_set_perag_data_loaded(mp);
779
780 return mp;
781 }
782
783 void
784 libxfs_rtmount_destroy(xfs_mount_t *mp)
785 {
786 if (mp->m_rsumip)
787 libxfs_irele(mp->m_rsumip);
788 if (mp->m_rbmip)
789 libxfs_irele(mp->m_rbmip);
790 mp->m_rsumip = mp->m_rbmip = NULL;
791 }
792
793 /* Flush a device and report on writes that didn't make it to stable storage. */
794 static inline int
795 libxfs_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 */
837 int
838 libxfs_flush_mount(
839 struct xfs_mount *mp)
840 {
841 int error = 0;
842 int err2;
843
844 /*
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.
850 */
851 libxfs_bcache_flush();
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
877 /*
878 * Release any resource obtained during a mount.
879 */
880 int
881 libxfs_umount(
882 struct xfs_mount *mp)
883 {
884 int error;
885
886 libxfs_rtmount_destroy(mp);
887
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();
894 error = libxfs_flush_mount(mp);
895
896 /*
897 * Only try to free the per-AG structures if we set them up in the
898 * first place.
899 */
900 if (xfs_is_perag_data_loaded(mp))
901 libxfs_free_perag(mp);
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);
910
911 return error;
912 }
913
914 /*
915 * Release any global resources used by libxfs.
916 */
917 void
918 libxfs_destroy(
919 struct libxfs_init *li)
920 {
921 int leaked;
922
923 libxfs_close_devices(li);
924
925 /* Free everything from the buffer cache before freeing buffer cache */
926 libxfs_bcache_purge();
927 libxfs_bcache_free();
928 cache_destroy(libxfs_bcache);
929 leaked = destroy_caches();
930 rcu_unregister_thread();
931 if (getenv("LIBXFS_LEAK_CHECK") && leaked)
932 exit(1);
933 }
934
935 int
936 libxfs_device_alignment(void)
937 {
938 return platform_align_blockdev();
939 }
940
941 void
942 libxfs_report(FILE *fp)
943 {
944 time_t t;
945 char *c;
946
947 cache_report(fp, "libxfs_bcache", libxfs_bcache);
948
949 t = time(NULL);
950 c = asctime(localtime(&t));
951 fprintf(fp, "%s", c);
952 }