]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/init.c
Correct display of imaxpct in mkfs output
[thirdparty/xfsprogs-dev.git] / libxfs / init.c
CommitLineData
2bd0ea18 1/*
0d3e0b37 2 * Copyright (c) 2000-2002 Silicon Graphics, Inc. All Rights Reserved.
2bd0ea18
NS
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
5000d01d 14 * or the like. Any license provided herein, whether implied or
2bd0ea18
NS
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
32
2bd0ea18 33#include <libxfs.h>
2bd0ea18 34#include <sys/stat.h>
9440d84d 35#include "init.h"
29e62271 36
2bd0ea18
NS
37#define findrawpath(x) x
38#define findblockpath(x) x
39
40char *progname = "libxfs"; /* default, changed by each tool */
41
42/*
43 * dev_map - map open devices to fd.
44 */
45#define MAX_DEVS 10 /* arbitary maximum */
46int nextfakedev = -1; /* device number to give to next fake device */
47static struct dev_to_fd {
9440d84d
NS
48 dev_t dev;
49 int fd;
2bd0ea18
NS
50} dev_map[MAX_DEVS]={{0}};
51
2bd0ea18
NS
52/*
53 * Checks whether a given device has a mounted, writable
54 * filesystem, returns 1 if it does & fatal (just warns
55 * if not fatal, but allows us to proceed).
5000d01d 56 *
2bd0ea18
NS
57 * Useful to tools which will produce uncertain results
58 * if the filesystem is active - repair, check, logprint.
59 */
60static int
61check_isactive(char *name, char *block, int fatal)
62{
9440d84d 63 struct stat64 st;
2bd0ea18 64
fc8202ba 65 if (stat64(block, &st) < 0)
9440d84d 66 return 0;
fc8202ba 67 if ((st.st_mode & S_IFMT) != S_IFBLK)
9440d84d 68 return 0;
507f4e33 69 if (check_ismounted(name, block, &st, 0) == 0)
9440d84d
NS
70 return 0;
71 return check_iswritable(name, block, &st, fatal);
2bd0ea18
NS
72}
73
5000d01d 74/* libxfs_device_to_fd:
2bd0ea18
NS
75 * lookup a device number in the device map
76 * return the associated fd
77 */
78int
79libxfs_device_to_fd(dev_t device)
80{
9440d84d 81 int d;
5000d01d 82
9440d84d 83 for (d = 0; d < MAX_DEVS; d++)
5000d01d 84 if (dev_map[d].dev == device)
2bd0ea18 85 return dev_map[d].fd;
5000d01d 86
9440d84d
NS
87 fprintf(stderr, _("%s: %s: device %lld is not open\n"),
88 progname, __FUNCTION__, (long long)device);
2bd0ea18
NS
89 exit(1);
90}
91
92/* libxfs_device_open:
93 * open a device and return its device number
94 */
95dev_t
c5907b96 96libxfs_device_open(char *path, int creat, int readonly, int setblksize)
2bd0ea18
NS
97{
98 int fd;
99 dev_t dev;
100 int d;
5000d01d 101 struct stat statb;
2bd0ea18
NS
102
103 if ((fd = open(path,
104 (readonly ? O_RDONLY : O_RDWR) |
105 (creat ? O_CREAT|O_TRUNC : 0),
106 0666)) < 0) {
9440d84d 107 fprintf(stderr, _("%s: cannot open %s: %s\n"),
2bd0ea18
NS
108 progname, path, strerror(errno));
109 exit(1);
110 }
111
112 if (stat(path, &statb)<0) {
9440d84d 113 fprintf(stderr, _("%s: cannot stat %s: %s\n"),
2bd0ea18
NS
114 progname, path, strerror(errno));
115 exit(1);
116 }
a33a9e62 117
c5907b96 118 if (!readonly && setblksize && (statb.st_mode & S_IFMT) == S_IFBLK) {
9440d84d 119 set_blocksize(fd, path, 512);
2bd0ea18
NS
120 }
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
NS
146 exit(1);
147}
148
149void
150libxfs_device_close(dev_t dev)
151{
5000d01d 152 int d;
2bd0ea18 153
32181a02 154 for (d = 0; d < MAX_DEVS; d++)
2bd0ea18 155 if (dev_map[d].dev == dev) {
32181a02 156 int fd;
5000d01d 157
32181a02
NS
158 fd = dev_map[d].fd;
159 dev_map[d].dev = dev_map[d].fd = 0;
5000d01d 160
2bd0ea18 161 fsync(fd);
9440d84d 162 flush_device(fd);
2bd0ea18 163 close(fd);
5000d01d 164
2bd0ea18
NS
165 return;
166 }
167
9440d84d
NS
168 fprintf(stderr, _("%s: %s: device %lld is not open\n"),
169 progname, __FUNCTION__, (long long)dev);
2bd0ea18
NS
170 exit(1);
171}
172
173
174/*
175 * libxfs initialization.
176 * Caller gets a 0 on failure (and we print a message), 1 on success.
177 */
178int
179libxfs_init(libxfs_init_t *a)
180{
181 char *blockfile;
182 char curdir[MAXPATHLEN];
183 char *dname;
184 char dpath[25];
185 int fd;
186 char *logname;
187 char logpath[25];
188 int needcd;
189 char *rawfile;
190 char *rtname;
191 char rtpath[25];
192 int rval = 0;
193 int readonly;
194 int inactive;
195 struct stat64 stbuf;
196
197 dpath[0] = logpath[0] = rtpath[0] = '\0';
198 dname = a->dname;
199 logname = a->logname;
200 rtname = a->rtname;
201 a->ddev = a->logdev = a->rtdev = 0;
202 a->dfd = a->logfd = a->rtfd = -1;
203 a->dsize = a->logBBsize = a->logBBstart = a->rtsize = 0;
204
205 (void)getcwd(curdir,MAXPATHLEN);
206 needcd = 0;
207 fd = -1;
208 readonly = (a->isreadonly & LIBXFS_ISREADONLY);
209 inactive = (a->isreadonly & LIBXFS_ISINACTIVE);
210 if (a->volname) {
211 if (stat64(a->volname, &stbuf) < 0) {
212 perror(a->volname);
213 goto done;
214 }
215 if (!(rawfile = findrawpath(a->volname))) {
9440d84d
NS
216 fprintf(stderr, _("%s: "
217 "can't find a character device matching %s\n"),
2bd0ea18
NS
218 progname, a->volname);
219 goto done;
220 }
221 if (!(blockfile = findblockpath(a->volname))) {
9440d84d
NS
222 fprintf(stderr, _("%s: "
223 "can't find a block device matching %s\n"),
2bd0ea18
NS
224 progname, a->volname);
225 goto done;
226 }
227 if (!readonly && !inactive && check_ismounted(
9440d84d 228 a->volname, blockfile, NULL, 1))
2bd0ea18
NS
229 goto done;
230 if (inactive && check_isactive(
231 a->volname, blockfile, readonly))
232 goto done;
233 needcd = 1;
234 fd = open(rawfile, O_RDONLY);
235#ifdef HAVE_VOLUME_MANAGER
236 xlv_getdev_t getdev;
237 if (ioctl(fd, DIOCGETVOLDEV, &getdev) < 0)
238#else
239 if (1)
240#endif
241 {
242 if (a->notvolok) {
243 dname = a->dname = a->volname;
244 a->volname = NULL;
245 goto voldone;
246 }
9440d84d
NS
247 fprintf(stderr, _("%s: "
248 "%s is not a volume device name\n"),
2bd0ea18
NS
249 progname, a->volname);
250 if (a->notvolmsg)
251 fprintf(stderr, a->notvolmsg, a->volname);
252 goto done;
253 }
254#ifdef HAVE_VOLUME_MANAGER
255 if (getdev.data_subvol_dev && dname) {
9440d84d
NS
256 fprintf(stderr, _("%s: "
257 "%s has a data subvolume, cannot specify %s\n"),
2bd0ea18
NS
258 progname, a->volname, dname);
259 goto done;
260 }
261 if (getdev.log_subvol_dev && logname) {
9440d84d
NS
262 fprintf(stderr, _("%s: "
263 "%s has a log subvolume, cannot specify %s\n"),
2bd0ea18
NS
264 progname, a->volname, logname);
265 goto done;
266 }
267 if (getdev.rt_subvol_dev && rtname) {
9440d84d
NS
268 fprintf(stderr, _("%s: %s has a realtime subvolume, "
269 "cannot specify %s\n"),
2bd0ea18
NS
270 progname, a->volname, rtname);
271 goto done;
272 }
273 if (!dname && getdev.data_subvol_dev) {
274 strcpy(dpath, "/tmp/libxfsdXXXXXX");
275 (void)mktemp(dpath);
276 if (mknod(dpath, S_IFCHR | 0600,
277 getdev.data_subvol_dev) < 0) {
9440d84d 278 fprintf(stderr, _("%s: mknod failed: %s\n"),
2bd0ea18
NS
279 progname, strerror(errno));
280 goto done;
281 }
282 dname = dpath;
283 }
284 if (!logname && getdev.log_subvol_dev) {
285 strcpy(logpath, "/tmp/libxfslXXXXXX");
286 (void)mktemp(logpath);
287 if (mknod(logpath, S_IFCHR | 0600,
288 getdev.log_subvol_dev) < 0) {
9440d84d 289 fprintf(stderr, _("%s: mknod failed: %s\n"),
2bd0ea18
NS
290 progname, strerror(errno));
291 goto done;
292 }
293 logname = logpath;
294 }
295 if (!rtname && getdev.rt_subvol_dev) {
296 strcpy(rtpath, "/tmp/libxfsrXXXXXX");
297 (void)mktemp(rtpath);
298 if (mknod(rtpath, S_IFCHR | 0600,
299 getdev.rt_subvol_dev) < 0) {
9440d84d 300 fprintf(stderr, _("%s: mknod failed: %s\n"),
2bd0ea18
NS
301 progname, strerror(errno));
302 goto done;
303 }
304 rtname = rtpath;
305 }
306#endif
307 }
308voldone:
309 if (dname) {
310 if (dname[0] != '/' && needcd)
311 chdir(curdir);
312 if (a->disfile) {
5000d01d 313 a->ddev= libxfs_device_open(dname, a->dcreat, readonly,
c5907b96 314 a->setblksize);
2bd0ea18
NS
315 a->dfd = libxfs_device_to_fd(a->ddev);
316 } else {
317 if (stat64(dname, &stbuf) < 0) {
9440d84d
NS
318 fprintf(stderr,
319 _("%s: stat64 failed on %s: %s\n"),
2bd0ea18
NS
320 progname, dname, strerror(errno));
321 goto done;
322 }
323 if (!(rawfile = findrawpath(dname))) {
9440d84d
NS
324 fprintf(stderr,
325 _("%s: can't find a char device "
326 "matching %s\n"), progname, dname);
2bd0ea18
NS
327 goto done;
328 }
329 if (!(blockfile = findblockpath(dname))) {
9440d84d
NS
330 fprintf(stderr,
331 _("%s: can't find a block device "
332 "matching %s\n"), progname, dname);
2bd0ea18
NS
333 goto done;
334 }
335 if (!readonly && !inactive && check_ismounted(
9440d84d 336 dname, blockfile, NULL, 1))
2bd0ea18
NS
337 goto done;
338 if (inactive && check_isactive(
339 dname, blockfile, readonly))
340 goto done;
341 a->ddev = libxfs_device_open(rawfile,
c5907b96 342 a->dcreat, readonly, a->setblksize);
2bd0ea18
NS
343 a->dfd = libxfs_device_to_fd(a->ddev);
344 a->dsize = findsize(rawfile);
345 }
346 needcd = 1;
347 } else
348 a->dsize = 0;
349 if (logname) {
350 if (logname[0] != '/' && needcd)
351 chdir(curdir);
352 if (a->lisfile) {
353 a->logdev = libxfs_device_open(logname,
c5907b96 354 a->lcreat, readonly, a->setblksize);
2bd0ea18
NS
355 a->logfd = libxfs_device_to_fd(a->logdev);
356 } else {
357 if (stat64(logname, &stbuf) < 0) {
9440d84d
NS
358 fprintf(stderr,
359 _("%s: stat64 failed on %s: %s\n"),
2bd0ea18
NS
360 progname, logname, strerror(errno));
361 goto done;
362 }
363 if (!(rawfile = findrawpath(logname))) {
9440d84d
NS
364 fprintf(stderr,
365 _("%s: can't find a char device "
366 "matching %s\n"), progname, logname);
2bd0ea18
NS
367 goto done;
368 }
369 if (!(blockfile = findblockpath(logname))) {
9440d84d
NS
370 fprintf(stderr,
371 _("%s: can't find a block device "
372 "matching %s\n"), progname, logname);
2bd0ea18
NS
373 goto done;
374 }
375 if (!readonly && !inactive && check_ismounted(
9440d84d 376 logname, blockfile, NULL, 1))
2bd0ea18
NS
377 goto done;
378 else if (inactive && check_isactive(
379 logname, blockfile, readonly))
380 goto done;
381 a->logdev = libxfs_device_open(rawfile,
c5907b96 382 a->lcreat, readonly, a->setblksize);
2bd0ea18
NS
383 a->logfd = libxfs_device_to_fd(a->logdev);
384 a->logBBsize = findsize(rawfile);
385 }
386 needcd = 1;
387 } else
388 a->logBBsize = 0;
389 if (rtname) {
390 if (rtname[0] != '/' && needcd)
391 chdir(curdir);
392 if (a->risfile) {
393 a->rtdev = libxfs_device_open(rtname,
c5907b96 394 a->rcreat, readonly, a->setblksize);
2bd0ea18
NS
395 a->rtfd = libxfs_device_to_fd(a->rtdev);
396 } else {
397 if (stat64(rtname, &stbuf) < 0) {
9440d84d
NS
398 fprintf(stderr,
399 _("%s: stat64 failed on %s: %s\n"),
2bd0ea18
NS
400 progname, rtname, strerror(errno));
401 goto done;
402 }
403 if (!(rawfile = findrawpath(rtname))) {
9440d84d
NS
404 fprintf(stderr,
405 _("%s: can't find a char device "
406 "matching %s\n"), progname, rtname);
2bd0ea18
NS
407 goto done;
408 }
409 if (!(blockfile = findblockpath(rtname))) {
9440d84d
NS
410 fprintf(stderr,
411 _("%s: can't find a block device "
412 "matching %s\n"), progname, rtname);
2bd0ea18
NS
413 goto done;
414 }
415 if (!readonly && !inactive && check_ismounted(
9440d84d 416 rtname, blockfile, NULL, 1))
2bd0ea18
NS
417 goto done;
418 if (inactive && check_isactive(
419 rtname, blockfile, readonly))
420 goto done;
421 a->rtdev = libxfs_device_open(rawfile,
c5907b96 422 a->rcreat, readonly, a->setblksize);
2bd0ea18
NS
423 a->rtfd = libxfs_device_to_fd(a->rtdev);
424 a->rtsize = findsize(rawfile);
425 }
426 needcd = 1;
427 } else
428 a->rtsize = 0;
429 if (a->dsize < 0) {
9440d84d 430 fprintf(stderr, _("%s: can't get size for data subvolume\n"),
2bd0ea18
NS
431 progname);
432 goto done;
433 }
434 if (a->logBBsize < 0) {
9440d84d 435 fprintf(stderr, _("%s: can't get size for log subvolume\n"),
2bd0ea18
NS
436 progname);
437 goto done;
438 }
439 if (a->rtsize < 0) {
9440d84d 440 fprintf(stderr, _("%s: can't get size for realtime subvolume\n"),
2bd0ea18
NS
441 progname);
442 goto done;
443 }
444 if (needcd)
445 chdir(curdir);
446 rval = 1;
447done:
448 if (dpath[0])
449 unlink(dpath);
450 if (logpath[0])
451 unlink(logpath);
452 if (rtpath[0])
453 unlink(rtpath);
454 if (fd >= 0)
455 close(fd);
456 if (!rval && a->ddev)
457 libxfs_device_close(a->ddev);
458 if (!rval && a->logdev)
459 libxfs_device_close(a->logdev);
460 if (!rval && a->rtdev)
461 libxfs_device_close(a->rtdev);
462 return rval;
463}
464
465
466/*
467 * Initialize/destroy all of the zone allocators we use.
468 */
469static void
470manage_zones(int release)
471{
472 extern xfs_zone_t *xfs_ili_zone;
473 extern xfs_zone_t *xfs_inode_zone;
474 extern xfs_zone_t *xfs_ifork_zone;
475 extern xfs_zone_t *xfs_dabuf_zone;
476 extern xfs_zone_t *xfs_buf_item_zone;
477 extern xfs_zone_t *xfs_da_state_zone;
478 extern xfs_zone_t *xfs_btree_cur_zone;
479 extern xfs_zone_t *xfs_bmap_free_item_zone;
480 extern void xfs_dir_startup();
481
482 if (release) { /* free zone allocation */
483 libxfs_free(xfs_inode_zone);
484 libxfs_free(xfs_ifork_zone);
485 libxfs_free(xfs_dabuf_zone);
486 libxfs_free(xfs_buf_item_zone);
487 libxfs_free(xfs_da_state_zone);
488 libxfs_free(xfs_btree_cur_zone);
489 libxfs_free(xfs_bmap_free_item_zone);
490 return;
491 }
492 /* otherwise initialise zone allocation */
493 xfs_inode_zone = libxfs_zone_init(sizeof(xfs_inode_t), "xfs_inode");
494 xfs_ifork_zone = libxfs_zone_init(sizeof(xfs_ifork_t), "xfs_ifork");
495 xfs_dabuf_zone = libxfs_zone_init(sizeof(xfs_dabuf_t), "xfs_dabuf");
496 xfs_ili_zone = libxfs_zone_init(
497 sizeof(xfs_inode_log_item_t), "xfs_inode_log_item");
498 xfs_buf_item_zone = libxfs_zone_init(
499 sizeof(xfs_buf_log_item_t), "xfs_buf_log_item");
500 xfs_da_state_zone = libxfs_zone_init(
501 sizeof(xfs_da_state_t), "xfs_da_state");
502 xfs_btree_cur_zone = libxfs_zone_init(
503 sizeof(xfs_btree_cur_t), "xfs_btree_cur");
504 xfs_bmap_free_item_zone = libxfs_zone_init(
505 sizeof(xfs_bmap_free_item_t), "xfs_bmap_free_item");
506 xfs_dir_startup();
507}
508
509/*
510 * Get the bitmap and summary inodes into the mount structure
511 * at mount time.
512 */
513static int
514rtmount_inodes(xfs_mount_t *mp)
515{
516 int error;
517 xfs_sb_t *sbp;
518
519 sbp = &mp->m_sb;
520 if (sbp->sb_rbmino == NULLFSINO)
521 return 0;
522 error = libxfs_iread(mp, NULL, sbp->sb_rbmino, &mp->m_rbmip, 0);
523 if (error) {
9440d84d
NS
524 fprintf(stderr,
525 _("%s: cannot read realtime bitmap inode (%d)\n"),
2bd0ea18
NS
526 progname, error);
527 return error;
528 }
529 ASSERT(mp->m_rbmip != NULL);
530 ASSERT(sbp->sb_rsumino != NULLFSINO);
531 error = libxfs_iread(mp, NULL, sbp->sb_rsumino, &mp->m_rsumip, 0);
532 if (error) {
9440d84d
NS
533 fprintf(stderr,
534 _("%s: cannot read realtime summary inode (%d)\n"),
2bd0ea18
NS
535 progname, error);
536 return error;
537 }
538 ASSERT(mp->m_rsumip != NULL);
539 return 0;
540}
541
b391b7cd
NS
542/*
543 * Initialize realtime fields in the mount structure.
544 */
545static int
546rtmount_init(
547 xfs_mount_t *mp) /* file system mount structure */
548{
549 xfs_buf_t *bp; /* buffer for last block of subvolume */
550 xfs_daddr_t d; /* address of last block of subvolume */
551 xfs_sb_t *sbp; /* filesystem superblock copy in mount */
552
553 sbp = &mp->m_sb;
554 if (sbp->sb_rblocks == 0)
555 return 0;
38c82117 556 if (mp->m_rtdev == 0) {
9440d84d 557 fprintf(stderr, _("%s: filesystem has a realtime subvolume\n"),
b391b7cd
NS
558 progname);
559 return -1;
560 }
561 mp->m_rsumlevels = sbp->sb_rextslog + 1;
562 mp->m_rsumsize =
563 (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
564 sbp->sb_rbmblocks;
565 mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
566 mp->m_rbmip = mp->m_rsumip = NULL;
567 /*
568 * Check that the realtime section is an ok size.
569 */
570 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
571 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
9440d84d
NS
572 fprintf(stderr, _("%s: realtime init - %llu != %llu\n"),
573 progname, (unsigned long long) XFS_BB_TO_FSB(mp, d),
b391b7cd
NS
574 (unsigned long long) mp->m_sb.sb_rblocks);
575 return -1;
576 }
9440d84d
NS
577 bp = libxfs_readbuf(mp->m_rtdev,
578 d - XFS_FSB_TO_BB(mp, 1), XFS_FSB_TO_BB(mp, 1), 0);
b391b7cd 579 if (bp == NULL) {
9440d84d
NS
580 fprintf(stderr, _("%s: realtime size check failed\n"),
581 progname);
b391b7cd
NS
582 return -1;
583 }
584 libxfs_putbuf(bp);
585 return 0;
586}
587
34317449 588#define XFS_MOUNT_32BITINODES 0x1
2bd0ea18
NS
589/*
590 * Mount structure initialization, provides a filled-in xfs_mount_t
591 * such that the numerous XFS_* macros can be used. If dev is zero,
592 * no IO will be performed (no size checks, read root inodes).
593 */
594xfs_mount_t *
595libxfs_mount(
596 xfs_mount_t *mp,
597 xfs_sb_t *sb,
598 dev_t dev,
599 dev_t logdev,
600 dev_t rtdev,
4ca431fc 601 int flags)
2bd0ea18
NS
602{
603 xfs_daddr_t d;
604 xfs_buf_t *bp;
605 xfs_sb_t *sbp;
606 size_t size;
607 int error;
608
609 mp->m_dev = dev;
610 mp->m_rtdev = rtdev;
611 mp->m_logdev = logdev;
34317449 612 mp->m_flags = XFS_MOUNT_32BITINODES;
2bd0ea18
NS
613 mp->m_sb = *sb;
614 sbp = &(mp->m_sb);
615 manage_zones(0);
616
617 libxfs_mount_common(mp, sb);
618
619 libxfs_alloc_compute_maxlevels(mp);
620 libxfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
621 libxfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
622 libxfs_ialloc_compute_maxlevels(mp);
623
624 if (sbp->sb_imax_pct) {
625 /* Make sure the maximum inode count is a multiple of the
626 * units we allocate inodes in.
627 */
628 mp->m_maxicount = (sbp->sb_dblocks * sbp->sb_imax_pct) / 100;
629 mp->m_maxicount = ((mp->m_maxicount / mp->m_ialloc_blks) *
630 mp->m_ialloc_blks) << sbp->sb_inopblog;
631 } else
632 mp->m_maxicount = 0;
633
634 mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
635
636 /*
637 * Set whether we're using inode alignment.
638 */
639 if (XFS_SB_VERSION_HASALIGN(&mp->m_sb) &&
640 mp->m_sb.sb_inoalignmt >=
641 XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
642 mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
643 else
644 mp->m_inoalign_mask = 0;
645 /*
646 * If we are using stripe alignment, check whether
647 * the stripe unit is a multiple of the inode alignment
648 */
649 if ( mp->m_dalign
650 && mp->m_inoalign_mask && !(mp->m_dalign & mp->m_inoalign_mask))
651 mp->m_sinoalign = mp->m_dalign;
652 else
653 mp->m_sinoalign = 0;
654
655 /*
656 * Check that the data (and log if separate) are an ok size.
657 */
9440d84d 658 d = (xfs_daddr_t) XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
2bd0ea18 659 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
9440d84d 660 fprintf(stderr, _("%s: size check failed\n"), progname);
4ca431fc
NS
661 if (!(flags & LIBXFS_MOUNT_DEBUGGER))
662 return NULL;
2bd0ea18
NS
663 }
664
665 /* Initialize the appropriate directory manager */
666 if (XFS_SB_VERSION_HASDIRV2(sbp))
667 libxfs_dir2_mount(mp);
668 else
669 libxfs_dir_mount(mp);
670
671 /* Initialize the precomputed transaction reservations values */
672 libxfs_trans_init(mp);
673
674 if (dev == 0) /* maxtrres, we have no device so leave now */
675 return mp;
676
9440d84d 677 bp = libxfs_readbuf(mp->m_dev,
4ca431fc
NS
678 d - XFS_FSS_TO_BB(mp, 1), XFS_FSS_TO_BB(mp, 1),
679 !(flags & LIBXFS_MOUNT_DEBUGGER));
9440d84d
NS
680 if (!bp) {
681 fprintf(stderr, _("%s: data size check failed\n"), progname);
4ca431fc
NS
682 if (!(flags & LIBXFS_MOUNT_DEBUGGER))
683 return NULL;
2bd0ea18
NS
684 }
685 libxfs_putbuf(bp);
686
687 if (mp->m_logdev && mp->m_logdev != mp->m_dev) {
9440d84d 688 d = (xfs_daddr_t) XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
2bd0ea18 689 if ( (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) ||
9440d84d
NS
690 (!(bp = libxfs_readbuf(mp->m_logdev,
691 d - XFS_FSB_TO_BB(mp, 1),
4ca431fc
NS
692 XFS_FSB_TO_BB(mp, 1),
693 !(flags & LIBXFS_MOUNT_DEBUGGER)))) ) {
9440d84d 694 fprintf(stderr, _("%s: log size checks failed\n"),
2bd0ea18 695 progname);
4ca431fc
NS
696 if (!(flags & LIBXFS_MOUNT_DEBUGGER))
697 return NULL;
2bd0ea18
NS
698 }
699 libxfs_putbuf(bp);
700 }
701
702 /* Initialize realtime fields in the mount structure */
b391b7cd 703 if (rtmount_init(mp)) {
9440d84d
NS
704 fprintf(stderr, _("%s: realtime device init failed\n"),
705 progname);
4ca431fc
NS
706 if (!(flags & LIBXFS_MOUNT_DEBUGGER))
707 return NULL;
2bd0ea18
NS
708 }
709
710 /* Allocate and initialize the per-ag data */
711 size = sbp->sb_agcount * sizeof(xfs_perag_t);
712 if ((mp->m_perag = calloc(size, 1)) == NULL) {
9440d84d 713 fprintf(stderr, _("%s: failed to alloc %ld bytes: %s\n"),
5b64e00a 714 progname, (long)size, strerror(errno));
2bd0ea18
NS
715 exit(1);
716 }
717
a33a9e62
NS
718 libxfs_initialize_perag(mp, sbp->sb_agcount);
719
2bd0ea18
NS
720 /*
721 * mkfs calls mount before the root inode is allocated.
722 */
4ca431fc 723 if ((flags & LIBXFS_MOUNT_ROOTINOS) && sbp->sb_rootino != NULLFSINO) {
2bd0ea18
NS
724 error = libxfs_iread(mp, NULL, sbp->sb_rootino,
725 &mp->m_rootip, 0);
726 if (error) {
9440d84d 727 fprintf(stderr, _("%s: cannot read root inode (%d)\n"),
2bd0ea18 728 progname, error);
4ca431fc
NS
729 if (!(flags & LIBXFS_MOUNT_DEBUGGER))
730 return NULL;
2bd0ea18
NS
731 }
732 ASSERT(mp->m_rootip != NULL);
733 }
4ca431fc
NS
734 if ((flags & LIBXFS_MOUNT_ROOTINOS) && rtmount_inodes(mp))
735 if (!(flags & LIBXFS_MOUNT_DEBUGGER))
736 return NULL;
2bd0ea18
NS
737 return mp;
738}
739
740/*
9440d84d 741 * Release any resource obtained during a mount.
2bd0ea18
NS
742 */
743void
744libxfs_umount(xfs_mount_t *mp)
745{
746 manage_zones(1);
747 free(mp->m_perag);
748}