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