]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/init.c
libxfs: clean up libxfs_destroy
[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 "libxfs.h" /* for now */
27
28 char *progname = "libxfs"; /* default, changed by each tool */
29
30 struct cache *libxfs_bcache; /* global buffer cache */
31 int libxfs_bhash_size; /* #buckets in bcache */
32
33 int use_xfs_buf_lock; /* global flag: use xfs_buf_t locks for MT */
34
35 /*
36 * dev_map - map open devices to fd.
37 */
38 #define MAX_DEVS 10 /* arbitary maximum */
39 static int nextfakedev = -1; /* device number to give to next fake device */
40 static struct dev_to_fd {
41 dev_t dev;
42 int fd;
43 } dev_map[MAX_DEVS]={{0}};
44
45 /*
46 * Checks whether a given device has a mounted, writable
47 * filesystem, returns 1 if it does & fatal (just warns
48 * if not fatal, but allows us to proceed).
49 *
50 * Useful to tools which will produce uncertain results
51 * if the filesystem is active - repair, check, logprint.
52 */
53 static int
54 check_isactive(char *name, char *block, int fatal)
55 {
56 struct stat st;
57
58 if (stat(block, &st) < 0)
59 return 0;
60 if ((st.st_mode & S_IFMT) != S_IFBLK)
61 return 0;
62 if (platform_check_ismounted(name, block, &st, 0) == 0)
63 return 0;
64 if (platform_check_iswritable(name, block, &st))
65 return fatal ? 1 : 0;
66 return 0;
67 }
68
69 /* libxfs_device_to_fd:
70 * lookup a device number in the device map
71 * return the associated fd
72 */
73 int
74 libxfs_device_to_fd(dev_t device)
75 {
76 int d;
77
78 for (d = 0; d < MAX_DEVS; d++)
79 if (dev_map[d].dev == device)
80 return dev_map[d].fd;
81
82 fprintf(stderr, _("%s: %s: device %lld is not open\n"),
83 progname, __FUNCTION__, (long long)device);
84 exit(1);
85 /* NOTREACHED */
86 }
87
88 /* libxfs_device_open:
89 * open a device and return its device number
90 */
91 dev_t
92 libxfs_device_open(char *path, int creat, int xflags, int setblksize)
93 {
94 dev_t dev;
95 int fd, d, flags;
96 int readonly, dio, excl;
97 struct stat statb;
98
99 readonly = (xflags & LIBXFS_ISREADONLY);
100 excl = (xflags & LIBXFS_EXCLUSIVELY) && !creat;
101 dio = (xflags & LIBXFS_DIRECT) && !creat && platform_direct_blockdev();
102
103 retry:
104 flags = (readonly ? O_RDONLY : O_RDWR) | \
105 (creat ? (O_CREAT|O_TRUNC) : 0) | \
106 (dio ? O_DIRECT : 0) | \
107 (excl ? O_EXCL : 0);
108
109 if ((fd = open(path, flags, 0666)) < 0) {
110 if (errno == EINVAL && --dio == 0)
111 goto retry;
112 fprintf(stderr, _("%s: cannot open %s: %s\n"),
113 progname, path, strerror(errno));
114 exit(1);
115 }
116
117 if (fstat(fd, &statb) < 0) {
118 fprintf(stderr, _("%s: cannot stat %s: %s\n"),
119 progname, path, strerror(errno));
120 exit(1);
121 }
122
123 if (!readonly && setblksize && (statb.st_mode & S_IFMT) == S_IFBLK) {
124 if (setblksize == 1)
125 /* use the default blocksize */
126 (void)platform_set_blocksize(fd, path, statb.st_rdev, XFS_MIN_SECTORSIZE, 0);
127 else {
128 /* given an explicit blocksize to use */
129 if (platform_set_blocksize(fd, path, statb.st_rdev, setblksize, 1))
130 exit(1);
131 }
132 }
133
134 /*
135 * Get the device number from the stat buf - unless
136 * we're not opening a real device, in which case
137 * choose a new fake device number.
138 */
139 dev = (statb.st_rdev) ? (statb.st_rdev) : (nextfakedev--);
140
141 for (d = 0; d < MAX_DEVS; d++)
142 if (dev_map[d].dev == dev) {
143 fprintf(stderr, _("%s: device %lld is already open\n"),
144 progname, (long long)dev);
145 exit(1);
146 }
147
148 for (d = 0; d < MAX_DEVS; d++)
149 if (!dev_map[d].dev) {
150 dev_map[d].dev = dev;
151 dev_map[d].fd = fd;
152
153 return dev;
154 }
155
156 fprintf(stderr, _("%s: %s: too many open devices\n"),
157 progname, __FUNCTION__);
158 exit(1);
159 /* NOTREACHED */
160 }
161
162 void
163 libxfs_device_close(dev_t dev)
164 {
165 int d;
166
167 for (d = 0; d < MAX_DEVS; d++)
168 if (dev_map[d].dev == dev) {
169 int fd;
170
171 fd = dev_map[d].fd;
172 dev_map[d].dev = dev_map[d].fd = 0;
173
174 fsync(fd);
175 platform_flush_device(fd, dev);
176 close(fd);
177
178 return;
179 }
180
181 fprintf(stderr, _("%s: %s: device %lld is not open\n"),
182 progname, __FUNCTION__, (long long)dev);
183 exit(1);
184 }
185
186 static int
187 check_open(char *path, int flags, char **rawfile, char **blockfile)
188 {
189 int readonly = (flags & LIBXFS_ISREADONLY);
190 int inactive = (flags & LIBXFS_ISINACTIVE);
191 int dangerously = (flags & LIBXFS_DANGEROUSLY);
192 struct stat stbuf;
193
194 if (stat(path, &stbuf) < 0) {
195 perror(path);
196 return 0;
197 }
198 if (!(*rawfile = platform_findrawpath(path))) {
199 fprintf(stderr, _("%s: "
200 "can't find a character device matching %s\n"),
201 progname, path);
202 return 0;
203 }
204 if (!(*blockfile = platform_findblockpath(path))) {
205 fprintf(stderr, _("%s: "
206 "can't find a block device matching %s\n"),
207 progname, path);
208 return 0;
209 }
210 if (!readonly && !inactive && platform_check_ismounted(path, *blockfile, NULL, 1))
211 return 0;
212
213 if (inactive && check_isactive(path, *blockfile, ((readonly|dangerously)?1:0)))
214 return 0;
215
216 return 1;
217 }
218
219 /*
220 * Initialize/destroy all of the zone allocators we use.
221 */
222 static void
223 init_zones(void)
224 {
225 /* initialise zone allocation */
226 xfs_buf_zone = kmem_zone_init(sizeof(struct xfs_buf), "xfs_buffer");
227 xfs_inode_zone = kmem_zone_init(sizeof(struct xfs_inode), "xfs_inode");
228 xfs_ifork_zone = kmem_zone_init(sizeof(struct xfs_ifork), "xfs_ifork");
229 xfs_ili_zone = kmem_zone_init(
230 sizeof(struct xfs_inode_log_item),"xfs_inode_log_item");
231 xfs_buf_item_zone = kmem_zone_init(
232 sizeof(struct xfs_buf_log_item), "xfs_buf_log_item");
233 xfs_da_state_zone = kmem_zone_init(
234 sizeof(struct xfs_da_state), "xfs_da_state");
235 xfs_btree_cur_zone = kmem_zone_init(
236 sizeof(struct xfs_btree_cur), "xfs_btree_cur");
237 xfs_bmap_free_item_zone = kmem_zone_init(
238 sizeof(struct xfs_extent_free_item),
239 "xfs_bmap_free_item");
240 xfs_trans_zone = kmem_zone_init(
241 sizeof(struct xfs_trans), "xfs_trans");
242 }
243
244 static int
245 destroy_zones(void)
246 {
247 int leaked = 0;
248
249 leaked += kmem_zone_destroy(xfs_buf_zone);
250 leaked += kmem_zone_destroy(xfs_ili_zone);
251 leaked += kmem_zone_destroy(xfs_inode_zone);
252 leaked += kmem_zone_destroy(xfs_ifork_zone);
253 leaked += kmem_zone_destroy(xfs_buf_item_zone);
254 leaked += kmem_zone_destroy(xfs_da_state_zone);
255 leaked += kmem_zone_destroy(xfs_btree_cur_zone);
256 leaked += kmem_zone_destroy(xfs_bmap_free_item_zone);
257 leaked += kmem_zone_destroy(xfs_trans_zone);
258
259 return leaked;
260 }
261
262 static void
263 libxfs_close_devices(
264 struct libxfs_xinit *li)
265 {
266 if (li->ddev)
267 libxfs_device_close(li->ddev);
268 if (li->logdev && li->logdev != li->ddev)
269 libxfs_device_close(li->logdev);
270 if (li->rtdev)
271 libxfs_device_close(li->rtdev);
272
273 li->ddev = li->logdev = li->rtdev = 0;
274 li->dfd = li->logfd = li->rtfd = -1;
275 }
276
277 /*
278 * libxfs initialization.
279 * Caller gets a 0 on failure (and we print a message), 1 on success.
280 */
281 int
282 libxfs_init(libxfs_init_t *a)
283 {
284 char *blockfile;
285 char *dname;
286 char dpath[25];
287 int fd;
288 char *logname;
289 char logpath[25];
290 char *rawfile;
291 char *rtname;
292 char rtpath[25];
293 int rval = 0;
294 int flags;
295
296 dpath[0] = logpath[0] = rtpath[0] = '\0';
297 dname = a->dname;
298 logname = a->logname;
299 rtname = a->rtname;
300 a->dfd = a->logfd = a->rtfd = -1;
301 a->ddev = a->logdev = a->rtdev = 0;
302 a->dsize = a->lbsize = a->rtbsize = 0;
303 a->dbsize = a->logBBsize = a->logBBstart = a->rtsize = 0;
304
305 fd = -1;
306 flags = (a->isreadonly | a->isdirect);
307
308 radix_tree_init();
309
310 if (a->volname) {
311 if(!check_open(a->volname,flags,&rawfile,&blockfile))
312 goto done;
313 fd = open(rawfile, O_RDONLY);
314 dname = a->dname = a->volname;
315 a->volname = NULL;
316 }
317 if (dname) {
318 if (a->disfile) {
319 a->ddev= libxfs_device_open(dname, a->dcreat, flags,
320 a->setblksize);
321 a->dfd = libxfs_device_to_fd(a->ddev);
322 platform_findsizes(dname, a->dfd, &a->dsize,
323 &a->dbsize);
324 } else {
325 if (!check_open(dname, flags, &rawfile, &blockfile))
326 goto done;
327 a->ddev = libxfs_device_open(rawfile,
328 a->dcreat, flags, a->setblksize);
329 a->dfd = libxfs_device_to_fd(a->ddev);
330 platform_findsizes(rawfile, a->dfd,
331 &a->dsize, &a->dbsize);
332 }
333 } else
334 a->dsize = 0;
335 if (logname) {
336 if (a->lisfile) {
337 a->logdev = libxfs_device_open(logname,
338 a->lcreat, flags, a->setblksize);
339 a->logfd = libxfs_device_to_fd(a->logdev);
340 platform_findsizes(dname, a->logfd, &a->logBBsize,
341 &a->lbsize);
342 } else {
343 if (!check_open(logname, flags, &rawfile, &blockfile))
344 goto done;
345 a->logdev = libxfs_device_open(rawfile,
346 a->lcreat, flags, a->setblksize);
347 a->logfd = libxfs_device_to_fd(a->logdev);
348 platform_findsizes(rawfile, a->logfd,
349 &a->logBBsize, &a->lbsize);
350 }
351 } else
352 a->logBBsize = 0;
353 if (rtname) {
354 if (a->risfile) {
355 a->rtdev = libxfs_device_open(rtname,
356 a->rcreat, flags, a->setblksize);
357 a->rtfd = libxfs_device_to_fd(a->rtdev);
358 platform_findsizes(dname, a->rtfd, &a->rtsize,
359 &a->rtbsize);
360 } else {
361 if (!check_open(rtname, flags, &rawfile, &blockfile))
362 goto done;
363 a->rtdev = libxfs_device_open(rawfile,
364 a->rcreat, flags, a->setblksize);
365 a->rtfd = libxfs_device_to_fd(a->rtdev);
366 platform_findsizes(rawfile, a->rtfd,
367 &a->rtsize, &a->rtbsize);
368 }
369 } else
370 a->rtsize = 0;
371 if (a->dsize < 0) {
372 fprintf(stderr, _("%s: can't get size for data subvolume\n"),
373 progname);
374 goto done;
375 }
376 if (a->logBBsize < 0) {
377 fprintf(stderr, _("%s: can't get size for log subvolume\n"),
378 progname);
379 goto done;
380 }
381 if (a->rtsize < 0) {
382 fprintf(stderr, _("%s: can't get size for realtime subvolume\n"),
383 progname);
384 goto done;
385 }
386 if (!libxfs_bhash_size)
387 libxfs_bhash_size = LIBXFS_BHASHSIZE(sbp);
388 libxfs_bcache = cache_init(a->bcache_flags, libxfs_bhash_size,
389 &libxfs_bcache_operations);
390 use_xfs_buf_lock = a->usebuflock;
391 xfs_dir_startup();
392 init_zones();
393 rval = 1;
394 done:
395 if (dpath[0])
396 unlink(dpath);
397 if (logpath[0])
398 unlink(logpath);
399 if (rtpath[0])
400 unlink(rtpath);
401 if (fd >= 0)
402 close(fd);
403 if (!rval)
404 libxfs_close_devices(a);
405
406 return rval;
407 }
408
409
410 /*
411 * Initialize realtime fields in the mount structure.
412 */
413 static int
414 rtmount_init(
415 xfs_mount_t *mp, /* file system mount structure */
416 int flags)
417 {
418 xfs_buf_t *bp; /* buffer for last block of subvolume */
419 xfs_daddr_t d; /* address of last block of subvolume */
420 xfs_sb_t *sbp; /* filesystem superblock copy in mount */
421
422 sbp = &mp->m_sb;
423 if (sbp->sb_rblocks == 0)
424 return 0;
425 if (mp->m_rtdev_targp->dev == 0 && !(flags & LIBXFS_MOUNT_DEBUGGER)) {
426 fprintf(stderr, _("%s: filesystem has a realtime subvolume\n"),
427 progname);
428 return -1;
429 }
430 mp->m_rsumlevels = sbp->sb_rextslog + 1;
431 mp->m_rsumsize =
432 (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
433 sbp->sb_rbmblocks;
434 mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
435 mp->m_rbmip = mp->m_rsumip = NULL;
436
437 /*
438 * Allow debugger to be run without the realtime device present.
439 */
440 if (flags & LIBXFS_MOUNT_DEBUGGER)
441 return 0;
442
443 /*
444 * Check that the realtime section is an ok size.
445 */
446 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
447 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
448 fprintf(stderr, _("%s: realtime init - %llu != %llu\n"),
449 progname, (unsigned long long) XFS_BB_TO_FSB(mp, d),
450 (unsigned long long) mp->m_sb.sb_rblocks);
451 return -1;
452 }
453 bp = libxfs_readbuf(mp->m_rtdev,
454 d - XFS_FSB_TO_BB(mp, 1), XFS_FSB_TO_BB(mp, 1), 0, NULL);
455 if (bp == NULL) {
456 fprintf(stderr, _("%s: realtime size check failed\n"),
457 progname);
458 return -1;
459 }
460 libxfs_putbuf(bp);
461 return 0;
462 }
463
464 static int
465 libxfs_initialize_perag(
466 xfs_mount_t *mp,
467 xfs_agnumber_t agcount,
468 xfs_agnumber_t *maxagi)
469 {
470 xfs_agnumber_t index, max_metadata;
471 xfs_agnumber_t first_initialised = 0;
472 xfs_perag_t *pag;
473 xfs_agino_t agino;
474 xfs_ino_t ino;
475 xfs_sb_t *sbp = &mp->m_sb;
476 int error = -ENOMEM;
477
478 /*
479 * Walk the current per-ag tree so we don't try to initialise AGs
480 * that already exist (growfs case). Allocate and insert all the
481 * AGs we don't find ready for initialisation.
482 */
483 for (index = 0; index < agcount; index++) {
484 pag = xfs_perag_get(mp, index);
485 if (pag) {
486 xfs_perag_put(pag);
487 continue;
488 }
489 if (!first_initialised)
490 first_initialised = index;
491
492 pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL);
493 if (!pag)
494 goto out_unwind;
495 pag->pag_agno = index;
496 pag->pag_mount = mp;
497
498 if (radix_tree_insert(&mp->m_perag_tree, index, pag)) {
499 error = -EEXIST;
500 goto out_unwind;
501 }
502 }
503
504 /*
505 * If we mount with the inode64 option, or no inode overflows
506 * the legacy 32-bit address space clear the inode32 option.
507 */
508 agino = XFS_AGB_TO_AGINO(mp, sbp->sb_agblocks - 1);
509 ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
510
511 if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) && ino > XFS_MAXINUMBER_32)
512 mp->m_flags |= XFS_MOUNT_32BITINODES;
513 else
514 mp->m_flags &= ~XFS_MOUNT_32BITINODES;
515
516 if (mp->m_flags & XFS_MOUNT_32BITINODES) {
517 /*
518 * Calculate how much should be reserved for inodes to meet
519 * the max inode percentage.
520 */
521 if (M_IGEO(mp)->maxicount) {
522 uint64_t icount;
523
524 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
525 do_div(icount, 100);
526 icount += sbp->sb_agblocks - 1;
527 do_div(icount, sbp->sb_agblocks);
528 max_metadata = icount;
529 } else {
530 max_metadata = agcount;
531 }
532
533 for (index = 0; index < agcount; index++) {
534 ino = XFS_AGINO_TO_INO(mp, index, agino);
535 if (ino > XFS_MAXINUMBER_32) {
536 index++;
537 break;
538 }
539
540 pag = xfs_perag_get(mp, index);
541 pag->pagi_inodeok = 1;
542 if (index < max_metadata)
543 pag->pagf_metadata = 1;
544 xfs_perag_put(pag);
545 }
546 } else {
547 for (index = 0; index < agcount; index++) {
548 pag = xfs_perag_get(mp, index);
549 pag->pagi_inodeok = 1;
550 xfs_perag_put(pag);
551 }
552 }
553
554 if (maxagi)
555 *maxagi = index;
556
557 mp->m_ag_prealloc_blocks = xfs_prealloc_blocks(mp);
558 return 0;
559
560 out_unwind:
561 kmem_free(pag);
562 for (; index > first_initialised; index--) {
563 pag = radix_tree_delete(&mp->m_perag_tree, index);
564 kmem_free(pag);
565 }
566 return error;
567 }
568
569 static struct xfs_buftarg *
570 libxfs_buftarg_alloc(
571 struct xfs_mount *mp,
572 dev_t dev)
573 {
574 struct xfs_buftarg *btp;
575
576 btp = malloc(sizeof(*btp));
577 if (!btp) {
578 fprintf(stderr, _("%s: buftarg init failed\n"),
579 progname);
580 exit(1);
581 }
582 btp->bt_mount = mp;
583 btp->dev = dev;
584 return btp;
585 }
586
587 void
588 libxfs_buftarg_init(
589 struct xfs_mount *mp,
590 dev_t dev,
591 dev_t logdev,
592 dev_t rtdev)
593 {
594 if (mp->m_ddev_targp) {
595 /* should already have all buftargs initialised */
596 if (mp->m_ddev_targp->dev != dev ||
597 mp->m_ddev_targp->bt_mount != mp) {
598 fprintf(stderr,
599 _("%s: bad buftarg reinit, ddev\n"),
600 progname);
601 exit(1);
602 }
603 if (!logdev || logdev == dev) {
604 if (mp->m_logdev_targp != mp->m_ddev_targp) {
605 fprintf(stderr,
606 _("%s: bad buftarg reinit, ldev mismatch\n"),
607 progname);
608 exit(1);
609 }
610 } else if (mp->m_logdev_targp->dev != logdev ||
611 mp->m_logdev_targp->bt_mount != mp) {
612 fprintf(stderr,
613 _("%s: bad buftarg reinit, logdev\n"),
614 progname);
615 exit(1);
616 }
617 if (rtdev && (mp->m_rtdev_targp->dev != rtdev ||
618 mp->m_rtdev_targp->bt_mount != mp)) {
619 fprintf(stderr,
620 _("%s: bad buftarg reinit, rtdev\n"),
621 progname);
622 exit(1);
623 }
624 return;
625 }
626
627 mp->m_ddev_targp = libxfs_buftarg_alloc(mp, dev);
628 if (!logdev || logdev == dev)
629 mp->m_logdev_targp = mp->m_ddev_targp;
630 else
631 mp->m_logdev_targp = libxfs_buftarg_alloc(mp, logdev);
632 mp->m_rtdev_targp = libxfs_buftarg_alloc(mp, rtdev);
633 }
634
635 /*
636 * Mount structure initialization, provides a filled-in xfs_mount_t
637 * such that the numerous XFS_* macros can be used. If dev is zero,
638 * no IO will be performed (no size checks, read root inodes).
639 */
640 xfs_mount_t *
641 libxfs_mount(
642 xfs_mount_t *mp,
643 xfs_sb_t *sb,
644 dev_t dev,
645 dev_t logdev,
646 dev_t rtdev,
647 int flags)
648 {
649 xfs_daddr_t d;
650 xfs_buf_t *bp;
651 xfs_sb_t *sbp;
652 int error;
653
654 libxfs_buftarg_init(mp, dev, logdev, rtdev);
655
656 mp->m_finobt_nores = true;
657 mp->m_flags = (LIBXFS_MOUNT_32BITINODES|LIBXFS_MOUNT_32BITINOOPT);
658 mp->m_sb = *sb;
659 INIT_RADIX_TREE(&mp->m_perag_tree, GFP_KERNEL);
660 sbp = &(mp->m_sb);
661
662 xfs_sb_mount_common(mp, sb);
663
664 /*
665 * Set whether we're using stripe alignment.
666 */
667 if (xfs_sb_version_hasdalign(&mp->m_sb)) {
668 mp->m_dalign = sbp->sb_unit;
669 mp->m_swidth = sbp->sb_width;
670 }
671
672 xfs_alloc_compute_maxlevels(mp);
673 xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
674 xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
675 xfs_ialloc_setup_geometry(mp);
676 xfs_rmapbt_compute_maxlevels(mp);
677 xfs_refcountbt_compute_maxlevels(mp);
678
679 /*
680 * Check that the data (and log if separate) are an ok size.
681 */
682 d = (xfs_daddr_t) XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
683 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
684 fprintf(stderr, _("%s: size check failed\n"), progname);
685 if (!(flags & LIBXFS_MOUNT_DEBUGGER))
686 return NULL;
687 }
688
689 /*
690 * We automatically convert v1 inodes to v2 inodes now, so if
691 * the NLINK bit is not set we can't operate on the filesystem.
692 */
693 if (!(sbp->sb_versionnum & XFS_SB_VERSION_NLINKBIT)) {
694
695 fprintf(stderr, _(
696 "%s: V1 inodes unsupported. Please try an older xfsprogs.\n"),
697 progname);
698 exit(1);
699 }
700
701 /* Check for supported directory formats */
702 if (!(sbp->sb_versionnum & XFS_SB_VERSION_DIRV2BIT)) {
703
704 fprintf(stderr, _(
705 "%s: V1 directories unsupported. Please try an older xfsprogs.\n"),
706 progname);
707 exit(1);
708 }
709
710 /* check for unsupported other features */
711 if (!xfs_sb_good_version(sbp)) {
712 fprintf(stderr, _(
713 "%s: Unsupported features detected. Please try a newer xfsprogs.\n"),
714 progname);
715 exit(1);
716 }
717
718 xfs_da_mount(mp);
719
720 if (xfs_sb_version_hasattr2(&mp->m_sb))
721 mp->m_flags |= LIBXFS_MOUNT_ATTR2;
722
723 /* Initialize the precomputed transaction reservations values */
724 xfs_trans_init(mp);
725
726 if (dev == 0) /* maxtrres, we have no device so leave now */
727 return mp;
728
729 bp = libxfs_readbuf(mp->m_dev,
730 d - XFS_FSS_TO_BB(mp, 1), XFS_FSS_TO_BB(mp, 1),
731 !(flags & LIBXFS_MOUNT_DEBUGGER), NULL);
732 if (!bp) {
733 fprintf(stderr, _("%s: data size check failed\n"), progname);
734 if (!(flags & LIBXFS_MOUNT_DEBUGGER))
735 return NULL;
736 } else
737 libxfs_putbuf(bp);
738
739 if (mp->m_logdev_targp->dev &&
740 mp->m_logdev_targp->dev != mp->m_ddev_targp->dev) {
741 d = (xfs_daddr_t) XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
742 if ( (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) ||
743 (!(bp = libxfs_readbuf(mp->m_logdev_targp,
744 d - XFS_FSB_TO_BB(mp, 1),
745 XFS_FSB_TO_BB(mp, 1),
746 !(flags & LIBXFS_MOUNT_DEBUGGER), NULL))) ) {
747 fprintf(stderr, _("%s: log size checks failed\n"),
748 progname);
749 if (!(flags & LIBXFS_MOUNT_DEBUGGER))
750 return NULL;
751 }
752 if (bp)
753 libxfs_putbuf(bp);
754 }
755
756 /* Initialize realtime fields in the mount structure */
757 if (rtmount_init(mp, flags)) {
758 fprintf(stderr, _("%s: realtime device init failed\n"),
759 progname);
760 return NULL;
761 }
762
763 /*
764 * libxfs_initialize_perag will allocate a perag structure for each ag.
765 * If agcount is corrupted and insanely high, this will OOM the box.
766 * If the agount seems (arbitrarily) high, try to read what would be
767 * the last AG, and if that fails for a relatively high agcount, just
768 * read the first one and let the user know to check the geometry.
769 */
770 if (sbp->sb_agcount > 1000000) {
771 bp = libxfs_readbuf(mp->m_dev,
772 XFS_AG_DADDR(mp, sbp->sb_agcount - 1, 0), 1,
773 !(flags & LIBXFS_MOUNT_DEBUGGER), NULL);
774 if (bp->b_error) {
775 fprintf(stderr, _("%s: read of AG %u failed\n"),
776 progname, sbp->sb_agcount);
777 if (!(flags & LIBXFS_MOUNT_DEBUGGER))
778 return NULL;
779 fprintf(stderr, _("%s: limiting reads to AG 0\n"),
780 progname);
781 sbp->sb_agcount = 1;
782 }
783 libxfs_putbuf(bp);
784 }
785
786 error = libxfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi);
787 if (error) {
788 fprintf(stderr, _("%s: perag init failed\n"),
789 progname);
790 exit(1);
791 }
792
793 return mp;
794 }
795
796 void
797 libxfs_rtmount_destroy(xfs_mount_t *mp)
798 {
799 if (mp->m_rsumip)
800 libxfs_irele(mp->m_rsumip);
801 if (mp->m_rbmip)
802 libxfs_irele(mp->m_rbmip);
803 mp->m_rsumip = mp->m_rbmip = NULL;
804 }
805
806 /*
807 * Release any resource obtained during a mount.
808 */
809 void
810 libxfs_umount(xfs_mount_t *mp)
811 {
812 struct xfs_perag *pag;
813 int agno;
814
815 libxfs_rtmount_destroy(mp);
816 libxfs_bcache_purge();
817
818 for (agno = 0; agno < mp->m_maxagi; agno++) {
819 pag = radix_tree_delete(&mp->m_perag_tree, agno);
820 kmem_free(pag);
821 }
822
823 kmem_free(mp->m_attr_geo);
824 kmem_free(mp->m_dir_geo);
825
826 kmem_free(mp->m_rtdev_targp);
827 if (mp->m_logdev_targp != mp->m_ddev_targp)
828 kmem_free(mp->m_logdev_targp);
829 kmem_free(mp->m_ddev_targp);
830
831 }
832
833 /*
834 * Release any global resources used by libxfs.
835 */
836 void
837 libxfs_destroy(
838 struct libxfs_xinit *li)
839 {
840 int leaked;
841
842 libxfs_close_devices(li);
843
844 /* Free everything from the buffer cache before freeing buffer zone */
845 libxfs_bcache_purge();
846 libxfs_bcache_free();
847 cache_destroy(libxfs_bcache);
848 leaked = destroy_zones();
849 if (getenv("LIBXFS_LEAK_CHECK") && leaked)
850 exit(1);
851 }
852
853 int
854 libxfs_device_alignment(void)
855 {
856 return platform_align_blockdev();
857 }
858
859 void
860 libxfs_report(FILE *fp)
861 {
862 time_t t;
863 char *c;
864
865 cache_report(fp, "libxfs_bcache", libxfs_bcache);
866
867 t = time(NULL);
868 c = asctime(localtime(&t));
869 fprintf(fp, "%s", c);
870 }