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