]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/init.c
Fix up variable argument handling around vfprintf's.
[thirdparty/xfsprogs-dev.git] / libxfs / init.c
CommitLineData
2bd0ea18 1/*
da23017d
NS
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
2bd0ea18 4 *
da23017d
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
2bd0ea18
NS
7 * published by the Free Software Foundation.
8 *
da23017d
NS
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
2bd0ea18 13 *
da23017d
NS
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2bd0ea18
NS
17 */
18
1d7e80ee 19#include <xfs/libxfs.h>
2bd0ea18 20#include <sys/stat.h>
9440d84d 21#include "init.h"
29e62271 22
2bd0ea18
NS
23char *progname = "libxfs"; /* default, changed by each tool */
24
f1b058f9 25struct cache *libxfs_icache; /* global inode cache */
9f38f08d 26int libxfs_ihash_size; /* #buckets in icache */
f1b058f9
NS
27
28struct cache *libxfs_bcache; /* global buffer cache */
9f38f08d 29int libxfs_bhash_size; /* #buckets in bcache */
f1b058f9
NS
30
31static void manage_zones(int); /* setup global zones */
32
2bd0ea18
NS
33/*
34 * dev_map - map open devices to fd.
35 */
36#define MAX_DEVS 10 /* arbitary maximum */
37int nextfakedev = -1; /* device number to give to next fake device */
38static struct dev_to_fd {
9440d84d
NS
39 dev_t dev;
40 int fd;
2bd0ea18
NS
41} dev_map[MAX_DEVS]={{0}};
42
2bd0ea18
NS
43/*
44 * Checks whether a given device has a mounted, writable
45 * filesystem, returns 1 if it does & fatal (just warns
46 * if not fatal, but allows us to proceed).
5000d01d 47 *
2bd0ea18
NS
48 * Useful to tools which will produce uncertain results
49 * if the filesystem is active - repair, check, logprint.
50 */
51static int
52check_isactive(char *name, char *block, int fatal)
53{
9440d84d 54 struct stat64 st;
2bd0ea18 55
fc8202ba 56 if (stat64(block, &st) < 0)
9440d84d 57 return 0;
fc8202ba 58 if ((st.st_mode & S_IFMT) != S_IFBLK)
9440d84d 59 return 0;
93d9f139 60 if (platform_check_ismounted(name, block, &st, 0) == 0)
9440d84d 61 return 0;
93d9f139 62 return platform_check_iswritable(name, block, &st, fatal);
2bd0ea18
NS
63}
64
5000d01d 65/* libxfs_device_to_fd:
2bd0ea18
NS
66 * lookup a device number in the device map
67 * return the associated fd
68 */
69int
70libxfs_device_to_fd(dev_t device)
71{
9440d84d 72 int d;
5000d01d 73
9440d84d 74 for (d = 0; d < MAX_DEVS; d++)
5000d01d 75 if (dev_map[d].dev == device)
2bd0ea18 76 return dev_map[d].fd;
5000d01d 77
9440d84d
NS
78 fprintf(stderr, _("%s: %s: device %lld is not open\n"),
79 progname, __FUNCTION__, (long long)device);
2bd0ea18 80 exit(1);
25d246df 81 /* NOTREACHED */
2bd0ea18
NS
82}
83
84/* libxfs_device_open:
85 * open a device and return its device number
86 */
87dev_t
7eb6693f 88libxfs_device_open(char *path, int creat, int xflags, int setblksize)
2bd0ea18 89{
2bd0ea18 90 dev_t dev;
7eb6693f 91 int fd, d, flags;
b74a1f6a 92 int readonly, dio, excl;
7eb6693f
NS
93 struct stat64 statb;
94
95 readonly = (xflags & LIBXFS_ISREADONLY);
96 excl = (xflags & LIBXFS_EXCLUSIVELY) && !creat;
b74a1f6a 97 dio = (xflags & LIBXFS_DIRECT) && !creat && platform_direct_blockdev();
2bd0ea18 98
b74a1f6a 99retry:
7eb6693f
NS
100 flags = (readonly ? O_RDONLY : O_RDWR) | \
101 (creat ? (O_CREAT|O_TRUNC) : 0) | \
b74a1f6a 102 (dio ? O_DIRECT : 0) | \
7eb6693f 103 (excl ? O_EXCL : 0);
b74a1f6a 104
7eb6693f 105 if ((fd = open(path, flags, 0666)) < 0) {
b74a1f6a
NS
106 if (errno == EINVAL && --dio == 0)
107 goto retry;
9440d84d 108 fprintf(stderr, _("%s: cannot open %s: %s\n"),
2bd0ea18
NS
109 progname, path, strerror(errno));
110 exit(1);
111 }
112
7eb6693f 113 if (fstat64(fd, &statb) < 0) {
9440d84d 114 fprintf(stderr, _("%s: cannot stat %s: %s\n"),
2bd0ea18
NS
115 progname, path, strerror(errno));
116 exit(1);
117 }
a33a9e62 118
76956054
NS
119 if (!readonly && setblksize && (statb.st_mode & S_IFMT) == S_IFBLK)
120 platform_set_blocksize(fd, path, statb.st_rdev, 512);
2bd0ea18 121
a33a9e62
NS
122 /*
123 * Get the device number from the stat buf - unless
2bd0ea18 124 * we're not opening a real device, in which case
a33a9e62 125 * choose a new fake device number.
2bd0ea18 126 */
32181a02 127 dev = (statb.st_rdev) ? (statb.st_rdev) : (nextfakedev--);
2bd0ea18 128
32181a02 129 for (d = 0; d < MAX_DEVS; d++)
2bd0ea18 130 if (dev_map[d].dev == dev) {
9440d84d 131 fprintf(stderr, _("%s: device %lld is already open\n"),
5b64e00a 132 progname, (long long)dev);
2bd0ea18
NS
133 exit(1);
134 }
135
32181a02 136 for (d = 0; d < MAX_DEVS; d++)
2bd0ea18 137 if (!dev_map[d].dev) {
32181a02
NS
138 dev_map[d].dev = dev;
139 dev_map[d].fd = fd;
5000d01d 140
2bd0ea18
NS
141 return dev;
142 }
143
9440d84d
NS
144 fprintf(stderr, _("%s: %s: too many open devices\n"),
145 progname, __FUNCTION__);
2bd0ea18 146 exit(1);
25d246df 147 /* NOTREACHED */
2bd0ea18
NS
148}
149
150void
151libxfs_device_close(dev_t dev)
152{
5000d01d 153 int d;
2bd0ea18 154
32181a02 155 for (d = 0; d < MAX_DEVS; d++)
2bd0ea18 156 if (dev_map[d].dev == dev) {
32181a02 157 int fd;
5000d01d 158
32181a02
NS
159 fd = dev_map[d].fd;
160 dev_map[d].dev = dev_map[d].fd = 0;
5000d01d 161
2bd0ea18 162 fsync(fd);
ac419944 163 platform_flush_device(fd, dev);
2bd0ea18 164 close(fd);
5000d01d 165
2bd0ea18
NS
166 return;
167 }
168
9440d84d
NS
169 fprintf(stderr, _("%s: %s: device %lld is not open\n"),
170 progname, __FUNCTION__, (long long)dev);
2bd0ea18
NS
171 exit(1);
172}
173
74668075
NS
174static int
175check_open(char *path, int flags, char **rawfile, char **blockfile)
176{
c781939c
RC
177 int readonly = (flags & LIBXFS_ISREADONLY);
178 int inactive = (flags & LIBXFS_ISINACTIVE);
179 int dangerously = (flags & LIBXFS_DANGEROUSLY);
180 struct stat64 stbuf;
181
182 if (stat64(path, &stbuf) < 0) {
183 perror(path);
184 return 0;
185 }
b74a1f6a 186 if (!(*rawfile = platform_findrawpath(path))) {
c781939c
RC
187 fprintf(stderr, _("%s: "
188 "can't find a character device matching %s\n"),
189 progname, path);
190 return 0;
191 }
b74a1f6a 192 if (!(*blockfile = platform_findblockpath(path))) {
c781939c
RC
193 fprintf(stderr, _("%s: "
194 "can't find a block device matching %s\n"),
195 progname, path);
196 return 0;
197 }
198 if (!readonly && !inactive && platform_check_ismounted(path, *blockfile, NULL, 1))
199 return 0;
200
201 if (inactive && check_isactive(path, *blockfile, ((readonly|dangerously)?1:0)))
202 return 0;
203
204 return 1;
205}
206
2bd0ea18
NS
207/*
208 * libxfs initialization.
209 * Caller gets a 0 on failure (and we print a message), 1 on success.
210 */
211int
212libxfs_init(libxfs_init_t *a)
213{
214 char *blockfile;
215 char curdir[MAXPATHLEN];
216 char *dname;
217 char dpath[25];
218 int fd;
219 char *logname;
220 char logpath[25];
221 int needcd;
222 char *rawfile;
223 char *rtname;
224 char rtpath[25];
225 int rval = 0;
c781939c 226 int flags;
2bd0ea18
NS
227
228 dpath[0] = logpath[0] = rtpath[0] = '\0';
229 dname = a->dname;
230 logname = a->logname;
231 rtname = a->rtname;
2bd0ea18 232 a->dfd = a->logfd = a->rtfd = -1;
74668075
NS
233 a->ddev = a->logdev = a->rtdev = 0;
234 a->dbsize = a->lbsize = a->rtbsize = 0;
2bd0ea18
NS
235 a->dsize = a->logBBsize = a->logBBstart = a->rtsize = 0;
236
237 (void)getcwd(curdir,MAXPATHLEN);
238 needcd = 0;
239 fd = -1;
b74a1f6a 240 flags = (a->isreadonly | a->isdirect);
c781939c 241
2bd0ea18 242 if (a->volname) {
c781939c 243 if(!check_open(a->volname,flags,&rawfile,&blockfile))
2bd0ea18
NS
244 goto done;
245 needcd = 1;
246 fd = open(rawfile, O_RDONLY);
eb37fca5
NS
247 dname = a->dname = a->volname;
248 a->volname = NULL;
2bd0ea18 249 }
2bd0ea18
NS
250 if (dname) {
251 if (dname[0] != '/' && needcd)
252 chdir(curdir);
253 if (a->disfile) {
7eb6693f 254 a->ddev= libxfs_device_open(dname, a->dcreat, flags,
c5907b96 255 a->setblksize);
2bd0ea18
NS
256 a->dfd = libxfs_device_to_fd(a->ddev);
257 } else {
f02037ce 258 if (!check_open(dname, flags, &rawfile, &blockfile))
2bd0ea18
NS
259 goto done;
260 a->ddev = libxfs_device_open(rawfile,
7eb6693f 261 a->dcreat, flags, a->setblksize);
2bd0ea18 262 a->dfd = libxfs_device_to_fd(a->ddev);
f02037ce
NS
263 platform_findsizes(rawfile, a->dfd,
264 &a->dsize, &a->dbsize);
2bd0ea18
NS
265 }
266 needcd = 1;
267 } else
268 a->dsize = 0;
269 if (logname) {
270 if (logname[0] != '/' && needcd)
271 chdir(curdir);
272 if (a->lisfile) {
273 a->logdev = libxfs_device_open(logname,
7eb6693f 274 a->lcreat, flags, a->setblksize);
2bd0ea18
NS
275 a->logfd = libxfs_device_to_fd(a->logdev);
276 } else {
f02037ce 277 if (!check_open(logname, flags, &rawfile, &blockfile))
2bd0ea18
NS
278 goto done;
279 a->logdev = libxfs_device_open(rawfile,
7eb6693f 280 a->lcreat, flags, a->setblksize);
2bd0ea18 281 a->logfd = libxfs_device_to_fd(a->logdev);
f02037ce
NS
282 platform_findsizes(rawfile, a->logfd,
283 &a->logBBsize, &a->lbsize);
2bd0ea18
NS
284 }
285 needcd = 1;
286 } else
287 a->logBBsize = 0;
288 if (rtname) {
289 if (rtname[0] != '/' && needcd)
290 chdir(curdir);
291 if (a->risfile) {
292 a->rtdev = libxfs_device_open(rtname,
7eb6693f 293 a->rcreat, flags, a->setblksize);
2bd0ea18
NS
294 a->rtfd = libxfs_device_to_fd(a->rtdev);
295 } else {
f02037ce 296 if (!check_open(rtname, flags, &rawfile, &blockfile))
2bd0ea18
NS
297 goto done;
298 a->rtdev = libxfs_device_open(rawfile,
7eb6693f 299 a->rcreat, flags, a->setblksize);
2bd0ea18 300 a->rtfd = libxfs_device_to_fd(a->rtdev);
f02037ce
NS
301 platform_findsizes(rawfile, a->rtfd,
302 &a->rtsize, &a->rtbsize);
2bd0ea18
NS
303 }
304 needcd = 1;
305 } else
306 a->rtsize = 0;
307 if (a->dsize < 0) {
9440d84d 308 fprintf(stderr, _("%s: can't get size for data subvolume\n"),
2bd0ea18
NS
309 progname);
310 goto done;
311 }
312 if (a->logBBsize < 0) {
9440d84d 313 fprintf(stderr, _("%s: can't get size for log subvolume\n"),
2bd0ea18
NS
314 progname);
315 goto done;
316 }
317 if (a->rtsize < 0) {
9440d84d 318 fprintf(stderr, _("%s: can't get size for realtime subvolume\n"),
2bd0ea18
NS
319 progname);
320 goto done;
321 }
322 if (needcd)
323 chdir(curdir);
9f38f08d
MV
324 if (!libxfs_ihash_size)
325 libxfs_ihash_size = LIBXFS_IHASHSIZE(sbp);
326 libxfs_icache = cache_init(libxfs_ihash_size, &libxfs_icache_operations);
327 if (!libxfs_bhash_size)
328 libxfs_bhash_size = LIBXFS_BHASHSIZE(sbp);
329 libxfs_bcache = cache_init(libxfs_bhash_size, &libxfs_bcache_operations);
f1b058f9 330 manage_zones(0);
2bd0ea18
NS
331 rval = 1;
332done:
333 if (dpath[0])
334 unlink(dpath);
335 if (logpath[0])
336 unlink(logpath);
337 if (rtpath[0])
338 unlink(rtpath);
339 if (fd >= 0)
340 close(fd);
341 if (!rval && a->ddev)
342 libxfs_device_close(a->ddev);
343 if (!rval && a->logdev)
344 libxfs_device_close(a->logdev);
345 if (!rval && a->rtdev)
346 libxfs_device_close(a->rtdev);
347 return rval;
348}
349
350
351/*
352 * Initialize/destroy all of the zone allocators we use.
353 */
354static void
355manage_zones(int release)
356{
f1b058f9 357 extern xfs_zone_t *xfs_buf_zone;
2bd0ea18
NS
358 extern xfs_zone_t *xfs_ili_zone;
359 extern xfs_zone_t *xfs_inode_zone;
360 extern xfs_zone_t *xfs_ifork_zone;
361 extern xfs_zone_t *xfs_dabuf_zone;
362 extern xfs_zone_t *xfs_buf_item_zone;
363 extern xfs_zone_t *xfs_da_state_zone;
364 extern xfs_zone_t *xfs_btree_cur_zone;
365 extern xfs_zone_t *xfs_bmap_free_item_zone;
366 extern void xfs_dir_startup();
367
368 if (release) { /* free zone allocation */
f1b058f9 369 libxfs_free(xfs_buf_zone);
2bd0ea18
NS
370 libxfs_free(xfs_inode_zone);
371 libxfs_free(xfs_ifork_zone);
372 libxfs_free(xfs_dabuf_zone);
373 libxfs_free(xfs_buf_item_zone);
374 libxfs_free(xfs_da_state_zone);
375 libxfs_free(xfs_btree_cur_zone);
376 libxfs_free(xfs_bmap_free_item_zone);
377 return;
378 }
379 /* otherwise initialise zone allocation */
f1b058f9 380 xfs_buf_zone = libxfs_zone_init(sizeof(xfs_buf_t), "xfs_buffer");
2bd0ea18
NS
381 xfs_inode_zone = libxfs_zone_init(sizeof(xfs_inode_t), "xfs_inode");
382 xfs_ifork_zone = libxfs_zone_init(sizeof(xfs_ifork_t), "xfs_ifork");
383 xfs_dabuf_zone = libxfs_zone_init(sizeof(xfs_dabuf_t), "xfs_dabuf");
384 xfs_ili_zone = libxfs_zone_init(
385 sizeof(xfs_inode_log_item_t), "xfs_inode_log_item");
386 xfs_buf_item_zone = libxfs_zone_init(
387 sizeof(xfs_buf_log_item_t), "xfs_buf_log_item");
388 xfs_da_state_zone = libxfs_zone_init(
389 sizeof(xfs_da_state_t), "xfs_da_state");
390 xfs_btree_cur_zone = libxfs_zone_init(
391 sizeof(xfs_btree_cur_t), "xfs_btree_cur");
392 xfs_bmap_free_item_zone = libxfs_zone_init(
393 sizeof(xfs_bmap_free_item_t), "xfs_bmap_free_item");
394 xfs_dir_startup();
395}
396
397/*
398 * Get the bitmap and summary inodes into the mount structure
399 * at mount time.
400 */
401static int
402rtmount_inodes(xfs_mount_t *mp)
403{
404 int error;
405 xfs_sb_t *sbp;
406
407 sbp = &mp->m_sb;
408 if (sbp->sb_rbmino == NULLFSINO)
409 return 0;
f1b058f9 410 error = libxfs_iget(mp, NULL, sbp->sb_rbmino, 0, &mp->m_rbmip, 0);
2bd0ea18 411 if (error) {
9440d84d
NS
412 fprintf(stderr,
413 _("%s: cannot read realtime bitmap inode (%d)\n"),
2bd0ea18
NS
414 progname, error);
415 return error;
416 }
417 ASSERT(mp->m_rbmip != NULL);
418 ASSERT(sbp->sb_rsumino != NULLFSINO);
f1b058f9 419 error = libxfs_iget(mp, NULL, sbp->sb_rsumino, 0, &mp->m_rsumip, 0);
2bd0ea18 420 if (error) {
f1b058f9 421 libxfs_iput(mp->m_rbmip, 0);
9440d84d
NS
422 fprintf(stderr,
423 _("%s: cannot read realtime summary inode (%d)\n"),
2bd0ea18
NS
424 progname, error);
425 return error;
426 }
427 ASSERT(mp->m_rsumip != NULL);
428 return 0;
429}
430
b391b7cd
NS
431/*
432 * Initialize realtime fields in the mount structure.
433 */
434static int
435rtmount_init(
39798eb5
NS
436 xfs_mount_t *mp, /* file system mount structure */
437 int flags)
b391b7cd
NS
438{
439 xfs_buf_t *bp; /* buffer for last block of subvolume */
440 xfs_daddr_t d; /* address of last block of subvolume */
441 xfs_sb_t *sbp; /* filesystem superblock copy in mount */
442
443 sbp = &mp->m_sb;
444 if (sbp->sb_rblocks == 0)
445 return 0;
39798eb5 446 if (mp->m_rtdev == 0 && !(flags & LIBXFS_MOUNT_DEBUGGER)) {
9440d84d 447 fprintf(stderr, _("%s: filesystem has a realtime subvolume\n"),
b391b7cd
NS
448 progname);
449 return -1;
450 }
451 mp->m_rsumlevels = sbp->sb_rextslog + 1;
452 mp->m_rsumsize =
453 (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
454 sbp->sb_rbmblocks;
455 mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
456 mp->m_rbmip = mp->m_rsumip = NULL;
39798eb5
NS
457
458 /*
459 * Allow debugger to be run without the realtime device present.
460 */
461 if (flags & LIBXFS_MOUNT_DEBUGGER)
462 return 0;
463
b391b7cd
NS
464 /*
465 * Check that the realtime section is an ok size.
466 */
467 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
468 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
9440d84d
NS
469 fprintf(stderr, _("%s: realtime init - %llu != %llu\n"),
470 progname, (unsigned long long) XFS_BB_TO_FSB(mp, d),
b391b7cd
NS
471 (unsigned long long) mp->m_sb.sb_rblocks);
472 return -1;
473 }
9440d84d
NS
474 bp = libxfs_readbuf(mp->m_rtdev,
475 d - XFS_FSB_TO_BB(mp, 1), XFS_FSB_TO_BB(mp, 1), 0);
b391b7cd 476 if (bp == NULL) {
9440d84d
NS
477 fprintf(stderr, _("%s: realtime size check failed\n"),
478 progname);
b391b7cd
NS
479 return -1;
480 }
481 libxfs_putbuf(bp);
482 return 0;
483}
484
2bd0ea18
NS
485/*
486 * Mount structure initialization, provides a filled-in xfs_mount_t
487 * such that the numerous XFS_* macros can be used. If dev is zero,
488 * no IO will be performed (no size checks, read root inodes).
489 */
490xfs_mount_t *
491libxfs_mount(
492 xfs_mount_t *mp,
493 xfs_sb_t *sb,
494 dev_t dev,
495 dev_t logdev,
496 dev_t rtdev,
4ca431fc 497 int flags)
2bd0ea18
NS
498{
499 xfs_daddr_t d;
500 xfs_buf_t *bp;
501 xfs_sb_t *sbp;
502 size_t size;
503 int error;
504
505 mp->m_dev = dev;
506 mp->m_rtdev = rtdev;
507 mp->m_logdev = logdev;
6239071d 508 mp->m_flags = (LIBXFS_MOUNT_32BITINODES|LIBXFS_MOUNT_32BITINOOPT);
2bd0ea18
NS
509 mp->m_sb = *sb;
510 sbp = &(mp->m_sb);
2bd0ea18
NS
511
512 libxfs_mount_common(mp, sb);
513
514 libxfs_alloc_compute_maxlevels(mp);
515 libxfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
516 libxfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
517 libxfs_ialloc_compute_maxlevels(mp);
518
519 if (sbp->sb_imax_pct) {
520 /* Make sure the maximum inode count is a multiple of the
521 * units we allocate inodes in.
522 */
523 mp->m_maxicount = (sbp->sb_dblocks * sbp->sb_imax_pct) / 100;
524 mp->m_maxicount = ((mp->m_maxicount / mp->m_ialloc_blks) *
525 mp->m_ialloc_blks) << sbp->sb_inopblog;
526 } else
527 mp->m_maxicount = 0;
528
529 mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
530
949c0f10
NS
531 /*
532 * Set whether we're using stripe alignment.
533 */
534 if (XFS_SB_VERSION_HASDALIGN(&mp->m_sb)) {
535 mp->m_dalign = sbp->sb_unit;
536 mp->m_swidth = sbp->sb_width;
537 }
538
2bd0ea18
NS
539 /*
540 * Set whether we're using inode alignment.
541 */
542 if (XFS_SB_VERSION_HASALIGN(&mp->m_sb) &&
543 mp->m_sb.sb_inoalignmt >=
544 XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
545 mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
546 else
547 mp->m_inoalign_mask = 0;
548 /*
549 * If we are using stripe alignment, check whether
550 * the stripe unit is a multiple of the inode alignment
551 */
552 if ( mp->m_dalign
553 && mp->m_inoalign_mask && !(mp->m_dalign & mp->m_inoalign_mask))
554 mp->m_sinoalign = mp->m_dalign;
555 else
556 mp->m_sinoalign = 0;
557
558 /*
559 * Check that the data (and log if separate) are an ok size.
560 */
9440d84d 561 d = (xfs_daddr_t) XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
2bd0ea18 562 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
9440d84d 563 fprintf(stderr, _("%s: size check failed\n"), progname);
4ca431fc
NS
564 if (!(flags & LIBXFS_MOUNT_DEBUGGER))
565 return NULL;
2bd0ea18
NS
566 }
567
568 /* Initialize the appropriate directory manager */
569 if (XFS_SB_VERSION_HASDIRV2(sbp))
570 libxfs_dir2_mount(mp);
571 else
572 libxfs_dir_mount(mp);
573
57c9fccb
NS
574 /* Initialize cached values for the attribute manager */
575 mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100;
576
2bd0ea18
NS
577 /* Initialize the precomputed transaction reservations values */
578 libxfs_trans_init(mp);
579
580 if (dev == 0) /* maxtrres, we have no device so leave now */
581 return mp;
582
9440d84d 583 bp = libxfs_readbuf(mp->m_dev,
4ca431fc
NS
584 d - XFS_FSS_TO_BB(mp, 1), XFS_FSS_TO_BB(mp, 1),
585 !(flags & LIBXFS_MOUNT_DEBUGGER));
9440d84d
NS
586 if (!bp) {
587 fprintf(stderr, _("%s: data size check failed\n"), progname);
4ca431fc
NS
588 if (!(flags & LIBXFS_MOUNT_DEBUGGER))
589 return NULL;
2bd0ea18
NS
590 }
591 libxfs_putbuf(bp);
592
593 if (mp->m_logdev && mp->m_logdev != mp->m_dev) {
9440d84d 594 d = (xfs_daddr_t) XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
2bd0ea18 595 if ( (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) ||
9440d84d
NS
596 (!(bp = libxfs_readbuf(mp->m_logdev,
597 d - XFS_FSB_TO_BB(mp, 1),
4ca431fc
NS
598 XFS_FSB_TO_BB(mp, 1),
599 !(flags & LIBXFS_MOUNT_DEBUGGER)))) ) {
9440d84d 600 fprintf(stderr, _("%s: log size checks failed\n"),
2bd0ea18 601 progname);
4ca431fc
NS
602 if (!(flags & LIBXFS_MOUNT_DEBUGGER))
603 return NULL;
2bd0ea18
NS
604 }
605 libxfs_putbuf(bp);
606 }
607
608 /* Initialize realtime fields in the mount structure */
39798eb5 609 if (rtmount_init(mp, flags)) {
9440d84d
NS
610 fprintf(stderr, _("%s: realtime device init failed\n"),
611 progname);
4ca431fc 612 return NULL;
2bd0ea18
NS
613 }
614
615 /* Allocate and initialize the per-ag data */
616 size = sbp->sb_agcount * sizeof(xfs_perag_t);
8ebd0722 617 if (size && (mp->m_perag = calloc(size, 1)) == NULL) {
9440d84d 618 fprintf(stderr, _("%s: failed to alloc %ld bytes: %s\n"),
5b64e00a 619 progname, (long)size, strerror(errno));
2bd0ea18
NS
620 exit(1);
621 }
622
062998e3 623 mp->m_maxagi = libxfs_initialize_perag(mp, sbp->sb_agcount);
a33a9e62 624
2bd0ea18
NS
625 /*
626 * mkfs calls mount before the root inode is allocated.
627 */
4ca431fc 628 if ((flags & LIBXFS_MOUNT_ROOTINOS) && sbp->sb_rootino != NULLFSINO) {
f1b058f9 629 error = libxfs_iget(mp, NULL, sbp->sb_rootino, 0,
2bd0ea18
NS
630 &mp->m_rootip, 0);
631 if (error) {
9440d84d 632 fprintf(stderr, _("%s: cannot read root inode (%d)\n"),
2bd0ea18 633 progname, error);
4ca431fc
NS
634 if (!(flags & LIBXFS_MOUNT_DEBUGGER))
635 return NULL;
2bd0ea18
NS
636 }
637 ASSERT(mp->m_rootip != NULL);
638 }
f1b058f9
NS
639 if ((flags & LIBXFS_MOUNT_ROOTINOS) && rtmount_inodes(mp)) {
640 libxfs_iput(mp->m_rootip, 0);
74668075 641 return NULL;
f1b058f9 642 }
2bd0ea18
NS
643 return mp;
644}
645
f1b058f9
NS
646void
647libxfs_rtmount_destroy(xfs_mount_t *mp)
648{
649 if (mp->m_rsumip)
650 libxfs_iput(mp->m_rsumip, 0);
651 if (mp->m_rbmip)
652 libxfs_iput(mp->m_rbmip, 0);
653 mp->m_rsumip = mp->m_rbmip = NULL;
654}
655
2bd0ea18 656/*
9440d84d 657 * Release any resource obtained during a mount.
2bd0ea18
NS
658 */
659void
660libxfs_umount(xfs_mount_t *mp)
661{
f1b058f9
NS
662 libxfs_rtmount_destroy(mp);
663 libxfs_icache_purge();
664 libxfs_bcache_purge();
665
fceb0d99
ES
666 if (mp->m_perag) {
667 int agno;
668 for (agno = 0; agno < mp->m_maxagi; agno++) {
669 if (mp->m_perag[agno].pagb_list)
670 free(mp->m_perag[agno].pagb_list);
671 }
672 free(mp->m_perag);
673 }
2bd0ea18 674}
f1b058f9
NS
675
676/*
677 * Release any global resources used by libxfs.
678 */
679void
680libxfs_destroy(void)
681{
682 manage_zones(1);
683 cache_destroy(libxfs_icache);
684 cache_destroy(libxfs_bcache);
685}
9f38f08d 686
b74a1f6a
NS
687int
688libxfs_device_alignment(void)
689{
690 return platform_align_blockdev();
691}
692
9f38f08d 693void
b6281496 694libxfs_report(FILE *fp)
9f38f08d 695{
cb5b3ef4
MV
696 time_t t;
697 char *c;
698
b6281496
MV
699 cache_report(fp, "libxfs_icache", libxfs_icache);
700 cache_report(fp, "libxfs_bcache", libxfs_bcache);
cb5b3ef4
MV
701
702 t = time(NULL);
703 c = asctime(localtime(&t));
704 fprintf(fp, "%s", c);
705}
3b6ac903
MV
706
707int
708libxfs_nproc(void)
709{
710 return platform_nproc();
711}