]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/init.c
sync kernel changes (32 bit inodes) with userspace code.
[thirdparty/xfsprogs-dev.git] / libxfs / init.c
CommitLineData
2bd0ea18
NS
1/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
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
14 * or the like. Any license provided herein, whether implied or
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
33#define ustat __kernel_ustat
34#include <libxfs.h>
2bd0ea18
NS
35#include <mntent.h>
36#include <sys/stat.h>
37#undef ustat
38#include <sys/ustat.h>
39#include <sys/ioctl.h>
40#include <sys/mount.h>
41
2bd0ea18
NS
42#define findrawpath(x) x
43#define findblockpath(x) x
44
45char *progname = "libxfs"; /* default, changed by each tool */
46
47/*
48 * dev_map - map open devices to fd.
49 */
50#define MAX_DEVS 10 /* arbitary maximum */
51int nextfakedev = -1; /* device number to give to next fake device */
52static struct dev_to_fd {
53 dev_t dev;
54 int fd;
55} dev_map[MAX_DEVS]={{0}};
56
57static int
fc8202ba 58check_ismounted(char *name, char *block)
2bd0ea18
NS
59{
60 struct ustat ust;
61 struct stat64 st;
62
63 if (stat64(block, &st) < 0)
64 return 0;
65 if ((st.st_mode & S_IFMT) != S_IFBLK)
66 return 0;
67 if (ustat(st.st_rdev, &ust) >= 0) {
fc8202ba
NS
68 fprintf(stderr, "%s: %s contains a mounted filesystem\n",
69 progname, name);
2bd0ea18
NS
70 return 1;
71 }
72 return 0;
73}
74
75/*
76 * Checks whether a given device has a mounted, writable
77 * filesystem, returns 1 if it does & fatal (just warns
78 * if not fatal, but allows us to proceed).
79 *
80 * Useful to tools which will produce uncertain results
81 * if the filesystem is active - repair, check, logprint.
82 */
83static int
84check_isactive(char *name, char *block, int fatal)
85{
fc8202ba 86#define PROC_MOUNTED "/proc/mounts"
2bd0ea18
NS
87 int sts = 0;
88 FILE *f;
89 struct mntent *mnt;
fc8202ba
NS
90 struct stat64 st, mst;
91 struct ustat ust;
92 char mounts[MAXPATHLEN];
2bd0ea18 93
fc8202ba
NS
94 if (stat64(block, &st) < 0)
95 return sts;
96 if ((st.st_mode & S_IFMT) != S_IFBLK)
97 return sts;
98 if (ustat(st.st_rdev, &ust) < 0)
99 return sts;
100
101 strcpy(mounts, access(PROC_MOUNTED, R_OK)? PROC_MOUNTED : MOUNTED);
102 if ((f = setmntent(mounts, "r")) == NULL) {
103 fprintf(stderr, "%s: %s contains a possibly writable, mounted "
2bd0ea18
NS
104 "filesystem\n", progname, name);
105 return fatal;
fc8202ba
NS
106 }
107 while ((mnt = getmntent(f)) != NULL) {
108 if (stat64(mnt->mnt_fsname, &mst) < 0)
109 continue;
110 if ((mst.st_mode & S_IFMT) != S_IFBLK)
111 continue;
112 if (mst.st_rdev == st.st_rdev
113 && hasmntopt(mnt, MNTOPT_RO) != NULL)
114 break;
115 }
116 if (mnt == NULL) {
117 fprintf(stderr, "%s: %s contains a writable, mounted "
2bd0ea18 118 "filesystem\n", progname, name);
fc8202ba 119 sts = fatal;
2bd0ea18 120 }
fc8202ba
NS
121 endmntent(f);
122
2bd0ea18
NS
123 return sts;
124}
125
126static __int64_t
127findsize(char *path)
128{
129 int fd;
130 int error;
557b0af8
NS
131 unsigned long size;
132 struct stat64 st;
2bd0ea18
NS
133
134 /* Test to see if we are dealing with a regular file rather than a
135 * block device, if we are just use the size returned by stat64
136 */
137 if (stat64(path, &st) < 0) {
138 fprintf(stderr, "%s: "
139 "cannot stat the device special file \"%s\": %s\n",
140 progname, path, strerror(errno));
141 exit(1);
142 }
143 if ((st.st_mode & S_IFMT) == S_IFREG) {
144 return (__int64_t)(st.st_size >> 9);
145 }
146
147 if ((fd = open(path, 0)) < 0) {
148 fprintf(stderr, "%s: "
149 "error opening the device special file \"%s\": %s\n",
150 progname, path, strerror(errno));
151 exit(1);
152 }
153 error = ioctl(fd, BLKGETSIZE, &size);
154 if (error < 0) {
155 fprintf(stderr, "%s: can't determine device size\n", progname);
156 exit(1);
157 }
158
159 close(fd);
160
161 return (__int64_t)size;
162}
163
164
165/* libxfs_device_to_fd:
166 * lookup a device number in the device map
167 * return the associated fd
168 */
169int
170libxfs_device_to_fd(dev_t device)
171{
172 int d;
173
174 for (d=0;d<MAX_DEVS;d++)
175 if (dev_map[d].dev == device)
176 return dev_map[d].fd;
177
5b64e00a
NS
178 fprintf(stderr, "%s: device_to_fd: device %lld is not open\n",
179 progname, (long long)device);
2bd0ea18
NS
180 exit(1);
181}
182
183/* libxfs_device_open:
184 * open a device and return its device number
185 */
186dev_t
c5907b96 187libxfs_device_open(char *path, int creat, int readonly, int setblksize)
2bd0ea18
NS
188{
189 int fd;
190 dev_t dev;
191 int d;
192 struct stat statb;
2bd0ea18
NS
193
194 if ((fd = open(path,
195 (readonly ? O_RDONLY : O_RDWR) |
196 (creat ? O_CREAT|O_TRUNC : 0),
197 0666)) < 0) {
198 fprintf(stderr, "%s: cannot open %s: %s\n",
199 progname, path, strerror(errno));
200 exit(1);
201 }
202
203 if (stat(path, &statb)<0) {
204 fprintf(stderr, "%s: cannot stat %s: %s\n",
205 progname, path, strerror(errno));
206 exit(1);
207 }
208
d5dca43b
NS
209#ifdef HAVE_BLKBSZSET
210 /*
211 * Set device blocksize to 512 bytes
212 *
213 * See bug #801063, but we no longer have this ioctl in the kernel -
214 * it will be needed again though when the fs blocksize != pagesize.
215 */
216#ifndef BLKBSZSET
217#define BLKBSZSET _IO(0x12,110) /* set device block size */
218#endif
c5907b96 219 if (!readonly && setblksize && (statb.st_mode & S_IFMT) == S_IFBLK) {
d5dca43b 220 int blocksize = 512; /* bytes */
2075b82a 221 if (ioctl(fd, BLKBSZSET, &blocksize) < 0) {
2bd0ea18
NS
222 fprintf(stderr, "%s: warning - cannot set blocksize on "
223 "block device %s: %s\n",
224 progname, path, strerror(errno));
225 }
226 }
d5dca43b 227#endif
2bd0ea18
NS
228
229 /* get the device number from the stat buf - unless
230 * we're not opening a real device, in which case
231 * choose a new fake device number
232 */
233 dev=(statb.st_rdev)?(statb.st_rdev):(nextfakedev--);
234
235 for (d=0;d<MAX_DEVS;d++)
236 if (dev_map[d].dev == dev) {
5b64e00a
NS
237 fprintf(stderr, "%s: device %lld is already open\n",
238 progname, (long long)dev);
2bd0ea18
NS
239 exit(1);
240 }
241
242 for (d=0;d<MAX_DEVS;d++)
243 if (!dev_map[d].dev) {
244 dev_map[d].dev=dev;
245 dev_map[d].fd=fd;
246
247 return dev;
248 }
249
250 fprintf(stderr, "%s: device_open: too many open devices\n", progname);
251 exit(1);
252}
253
254void
255libxfs_device_close(dev_t dev)
256{
257 int d;
258
259 for (d=0;d<MAX_DEVS;d++)
260 if (dev_map[d].dev == dev) {
261 int fd;
262
263 fd=dev_map[d].dev;
264 dev_map[d].dev=dev_map[d].fd=0;
265
266 fsync(fd);
267 ioctl(fd, BLKFLSBUF, 0);
268 close(fd);
269
270 return;
271 }
272
5b64e00a
NS
273 fprintf(stderr, "%s: device_close: device %lld is not open\n",
274 progname, (long long)dev);
2bd0ea18
NS
275 ASSERT(0);
276 exit(1);
277}
278
279
280/*
281 * libxfs initialization.
282 * Caller gets a 0 on failure (and we print a message), 1 on success.
283 */
284int
285libxfs_init(libxfs_init_t *a)
286{
287 char *blockfile;
288 char curdir[MAXPATHLEN];
289 char *dname;
290 char dpath[25];
291 int fd;
292 char *logname;
293 char logpath[25];
294 int needcd;
295 char *rawfile;
296 char *rtname;
297 char rtpath[25];
298 int rval = 0;
299 int readonly;
300 int inactive;
301 struct stat64 stbuf;
302
303 dpath[0] = logpath[0] = rtpath[0] = '\0';
304 dname = a->dname;
305 logname = a->logname;
306 rtname = a->rtname;
307 a->ddev = a->logdev = a->rtdev = 0;
308 a->dfd = a->logfd = a->rtfd = -1;
309 a->dsize = a->logBBsize = a->logBBstart = a->rtsize = 0;
310
311 (void)getcwd(curdir,MAXPATHLEN);
312 needcd = 0;
313 fd = -1;
314 readonly = (a->isreadonly & LIBXFS_ISREADONLY);
315 inactive = (a->isreadonly & LIBXFS_ISINACTIVE);
316 if (a->volname) {
317 if (stat64(a->volname, &stbuf) < 0) {
318 perror(a->volname);
319 goto done;
320 }
321 if (!(rawfile = findrawpath(a->volname))) {
322 fprintf(stderr, "%s: "
323 "can't find a character device matching %s\n",
324 progname, a->volname);
325 goto done;
326 }
327 if (!(blockfile = findblockpath(a->volname))) {
328 fprintf(stderr, "%s: "
329 "can't find a block device matching %s\n",
330 progname, a->volname);
331 goto done;
332 }
333 if (!readonly && !inactive && check_ismounted(
fc8202ba 334 a->volname, blockfile))
2bd0ea18
NS
335 goto done;
336 if (inactive && check_isactive(
337 a->volname, blockfile, readonly))
338 goto done;
339 needcd = 1;
340 fd = open(rawfile, O_RDONLY);
341#ifdef HAVE_VOLUME_MANAGER
342 xlv_getdev_t getdev;
343 if (ioctl(fd, DIOCGETVOLDEV, &getdev) < 0)
344#else
345 if (1)
346#endif
347 {
348 if (a->notvolok) {
349 dname = a->dname = a->volname;
350 a->volname = NULL;
351 goto voldone;
352 }
353 fprintf(stderr, "%s: "
354 "%s is not a volume device name\n",
355 progname, a->volname);
356 if (a->notvolmsg)
357 fprintf(stderr, a->notvolmsg, a->volname);
358 goto done;
359 }
360#ifdef HAVE_VOLUME_MANAGER
361 if (getdev.data_subvol_dev && dname) {
362 fprintf(stderr, "%s: "
363 "%s has a data subvolume, cannot specify %s\n",
364 progname, a->volname, dname);
365 goto done;
366 }
367 if (getdev.log_subvol_dev && logname) {
368 fprintf(stderr, "%s: "
369 "%s has a log subvolume, cannot specify %s\n",
370 progname, a->volname, logname);
371 goto done;
372 }
373 if (getdev.rt_subvol_dev && rtname) {
374 fprintf(stderr, "%s: %s has a realtime subvolume, "
375 "cannot specify %s\n",
376 progname, a->volname, rtname);
377 goto done;
378 }
379 if (!dname && getdev.data_subvol_dev) {
380 strcpy(dpath, "/tmp/libxfsdXXXXXX");
381 (void)mktemp(dpath);
382 if (mknod(dpath, S_IFCHR | 0600,
383 getdev.data_subvol_dev) < 0) {
384 fprintf(stderr, "%s: mknod failed: %s\n",
385 progname, strerror(errno));
386 goto done;
387 }
388 dname = dpath;
389 }
390 if (!logname && getdev.log_subvol_dev) {
391 strcpy(logpath, "/tmp/libxfslXXXXXX");
392 (void)mktemp(logpath);
393 if (mknod(logpath, S_IFCHR | 0600,
394 getdev.log_subvol_dev) < 0) {
395 fprintf(stderr, "%s: mknod failed: %s\n",
396 progname, strerror(errno));
397 goto done;
398 }
399 logname = logpath;
400 }
401 if (!rtname && getdev.rt_subvol_dev) {
402 strcpy(rtpath, "/tmp/libxfsrXXXXXX");
403 (void)mktemp(rtpath);
404 if (mknod(rtpath, S_IFCHR | 0600,
405 getdev.rt_subvol_dev) < 0) {
406 fprintf(stderr, "%s: mknod failed: %s\n",
407 progname, strerror(errno));
408 goto done;
409 }
410 rtname = rtpath;
411 }
412#endif
413 }
414voldone:
415 if (dname) {
416 if (dname[0] != '/' && needcd)
417 chdir(curdir);
418 if (a->disfile) {
c5907b96
MP
419 a->ddev= libxfs_device_open(dname, a->dcreat, readonly,
420 a->setblksize);
2bd0ea18
NS
421 a->dfd = libxfs_device_to_fd(a->ddev);
422 } else {
423 if (stat64(dname, &stbuf) < 0) {
424 fprintf(stderr, "%s: stat64 failed on %s: %s\n",
425 progname, dname, strerror(errno));
426 goto done;
427 }
428 if (!(rawfile = findrawpath(dname))) {
429 fprintf(stderr, "%s: can't find a char device "
430 "matching %s\n", progname, dname);
431 goto done;
432 }
433 if (!(blockfile = findblockpath(dname))) {
434 fprintf(stderr, "%s: can't find a block device "
435 "matching %s\n", progname, dname);
436 goto done;
437 }
438 if (!readonly && !inactive && check_ismounted(
fc8202ba 439 dname, blockfile))
2bd0ea18
NS
440 goto done;
441 if (inactive && check_isactive(
442 dname, blockfile, readonly))
443 goto done;
444 a->ddev = libxfs_device_open(rawfile,
c5907b96 445 a->dcreat, readonly, a->setblksize);
2bd0ea18
NS
446 a->dfd = libxfs_device_to_fd(a->ddev);
447 a->dsize = findsize(rawfile);
448 }
449 needcd = 1;
450 } else
451 a->dsize = 0;
452 if (logname) {
453 if (logname[0] != '/' && needcd)
454 chdir(curdir);
455 if (a->lisfile) {
456 a->logdev = libxfs_device_open(logname,
c5907b96 457 a->lcreat, readonly, a->setblksize);
2bd0ea18
NS
458 a->logfd = libxfs_device_to_fd(a->logdev);
459 } else {
460 if (stat64(logname, &stbuf) < 0) {
461 fprintf(stderr, "%s: stat64 failed on %s: %s\n",
462 progname, logname, strerror(errno));
463 goto done;
464 }
465 if (!(rawfile = findrawpath(logname))) {
466 fprintf(stderr, "%s: can't find a char device "
467 "matching %s\n", progname, logname);
468 goto done;
469 }
470 if (!(blockfile = findblockpath(logname))) {
471 fprintf(stderr, "%s: can't find a block device "
472 "matching %s\n", progname, logname);
473 goto done;
474 }
475 if (!readonly && !inactive && check_ismounted(
fc8202ba 476 logname, blockfile))
2bd0ea18
NS
477 goto done;
478 else if (inactive && check_isactive(
479 logname, blockfile, readonly))
480 goto done;
481 a->logdev = libxfs_device_open(rawfile,
c5907b96 482 a->lcreat, readonly, a->setblksize);
2bd0ea18
NS
483 a->logfd = libxfs_device_to_fd(a->logdev);
484 a->logBBsize = findsize(rawfile);
485 }
486 needcd = 1;
487 } else
488 a->logBBsize = 0;
489 if (rtname) {
490 if (rtname[0] != '/' && needcd)
491 chdir(curdir);
492 if (a->risfile) {
493 a->rtdev = libxfs_device_open(rtname,
c5907b96 494 a->rcreat, readonly, a->setblksize);
2bd0ea18
NS
495 a->rtfd = libxfs_device_to_fd(a->rtdev);
496 } else {
497 if (stat64(rtname, &stbuf) < 0) {
498 fprintf(stderr, "%s: stat64 failed on %s: %s\n",
499 progname, rtname, strerror(errno));
500 goto done;
501 }
502 if (!(rawfile = findrawpath(rtname))) {
503 fprintf(stderr, "%s: can't find a char device "
504 "matching %s\n", progname, rtname);
505 goto done;
506 }
507 if (!(blockfile = findblockpath(rtname))) {
508 fprintf(stderr, "%s: can't find a block device "
509 "matching %s\n", progname, rtname);
510 goto done;
511 }
512 if (!readonly && !inactive && check_ismounted(
fc8202ba 513 rtname, blockfile))
2bd0ea18
NS
514 goto done;
515 if (inactive && check_isactive(
516 rtname, blockfile, readonly))
517 goto done;
518 a->rtdev = libxfs_device_open(rawfile,
c5907b96 519 a->rcreat, readonly, a->setblksize);
2bd0ea18
NS
520 a->rtfd = libxfs_device_to_fd(a->rtdev);
521 a->rtsize = findsize(rawfile);
522 }
523 needcd = 1;
524 } else
525 a->rtsize = 0;
526 if (a->dsize < 0) {
527 fprintf(stderr, "%s: can't get size for data subvolume\n",
528 progname);
529 goto done;
530 }
531 if (a->logBBsize < 0) {
532 fprintf(stderr, "%s: can't get size for log subvolume\n",
533 progname);
534 goto done;
535 }
536 if (a->rtsize < 0) {
537 fprintf(stderr, "%s: can't get size for realtime subvolume\n",
538 progname);
539 goto done;
540 }
541 if (needcd)
542 chdir(curdir);
543 rval = 1;
544done:
545 if (dpath[0])
546 unlink(dpath);
547 if (logpath[0])
548 unlink(logpath);
549 if (rtpath[0])
550 unlink(rtpath);
551 if (fd >= 0)
552 close(fd);
553 if (!rval && a->ddev)
554 libxfs_device_close(a->ddev);
555 if (!rval && a->logdev)
556 libxfs_device_close(a->logdev);
557 if (!rval && a->rtdev)
558 libxfs_device_close(a->rtdev);
559 return rval;
560}
561
562
563/*
564 * Initialize/destroy all of the zone allocators we use.
565 */
566static void
567manage_zones(int release)
568{
569 extern xfs_zone_t *xfs_ili_zone;
570 extern xfs_zone_t *xfs_inode_zone;
571 extern xfs_zone_t *xfs_ifork_zone;
572 extern xfs_zone_t *xfs_dabuf_zone;
573 extern xfs_zone_t *xfs_buf_item_zone;
574 extern xfs_zone_t *xfs_da_state_zone;
575 extern xfs_zone_t *xfs_btree_cur_zone;
576 extern xfs_zone_t *xfs_bmap_free_item_zone;
577 extern void xfs_dir_startup();
578
579 if (release) { /* free zone allocation */
580 libxfs_free(xfs_inode_zone);
581 libxfs_free(xfs_ifork_zone);
582 libxfs_free(xfs_dabuf_zone);
583 libxfs_free(xfs_buf_item_zone);
584 libxfs_free(xfs_da_state_zone);
585 libxfs_free(xfs_btree_cur_zone);
586 libxfs_free(xfs_bmap_free_item_zone);
587 return;
588 }
589 /* otherwise initialise zone allocation */
590 xfs_inode_zone = libxfs_zone_init(sizeof(xfs_inode_t), "xfs_inode");
591 xfs_ifork_zone = libxfs_zone_init(sizeof(xfs_ifork_t), "xfs_ifork");
592 xfs_dabuf_zone = libxfs_zone_init(sizeof(xfs_dabuf_t), "xfs_dabuf");
593 xfs_ili_zone = libxfs_zone_init(
594 sizeof(xfs_inode_log_item_t), "xfs_inode_log_item");
595 xfs_buf_item_zone = libxfs_zone_init(
596 sizeof(xfs_buf_log_item_t), "xfs_buf_log_item");
597 xfs_da_state_zone = libxfs_zone_init(
598 sizeof(xfs_da_state_t), "xfs_da_state");
599 xfs_btree_cur_zone = libxfs_zone_init(
600 sizeof(xfs_btree_cur_t), "xfs_btree_cur");
601 xfs_bmap_free_item_zone = libxfs_zone_init(
602 sizeof(xfs_bmap_free_item_t), "xfs_bmap_free_item");
603 xfs_dir_startup();
604}
605
606/*
607 * Get the bitmap and summary inodes into the mount structure
608 * at mount time.
609 */
610static int
611rtmount_inodes(xfs_mount_t *mp)
612{
613 int error;
614 xfs_sb_t *sbp;
615
616 sbp = &mp->m_sb;
617 if (sbp->sb_rbmino == NULLFSINO)
618 return 0;
619 error = libxfs_iread(mp, NULL, sbp->sb_rbmino, &mp->m_rbmip, 0);
620 if (error) {
621 fprintf(stderr, "%s: cannot read realtime bitmap inode (%d)\n",
622 progname, error);
623 return error;
624 }
625 ASSERT(mp->m_rbmip != NULL);
626 ASSERT(sbp->sb_rsumino != NULLFSINO);
627 error = libxfs_iread(mp, NULL, sbp->sb_rsumino, &mp->m_rsumip, 0);
628 if (error) {
629 fprintf(stderr, "%s: cannot read realtime summary inode (%d)\n",
630 progname, error);
631 return error;
632 }
633 ASSERT(mp->m_rsumip != NULL);
634 return 0;
635}
636
34317449 637#define XFS_MOUNT_32BITINODES 0x1
2bd0ea18
NS
638/*
639 * Mount structure initialization, provides a filled-in xfs_mount_t
640 * such that the numerous XFS_* macros can be used. If dev is zero,
641 * no IO will be performed (no size checks, read root inodes).
642 */
643xfs_mount_t *
644libxfs_mount(
645 xfs_mount_t *mp,
646 xfs_sb_t *sb,
647 dev_t dev,
648 dev_t logdev,
649 dev_t rtdev,
650 int rrootinos)
651{
652 xfs_daddr_t d;
653 xfs_buf_t *bp;
654 xfs_sb_t *sbp;
655 size_t size;
656 int error;
657
658 mp->m_dev = dev;
659 mp->m_rtdev = rtdev;
660 mp->m_logdev = logdev;
34317449 661 mp->m_flags = XFS_MOUNT_32BITINODES;
2bd0ea18
NS
662 mp->m_sb = *sb;
663 sbp = &(mp->m_sb);
664 manage_zones(0);
665
666 libxfs_mount_common(mp, sb);
667
668 libxfs_alloc_compute_maxlevels(mp);
669 libxfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
670 libxfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
671 libxfs_ialloc_compute_maxlevels(mp);
672
673 if (sbp->sb_imax_pct) {
674 /* Make sure the maximum inode count is a multiple of the
675 * units we allocate inodes in.
676 */
677 mp->m_maxicount = (sbp->sb_dblocks * sbp->sb_imax_pct) / 100;
678 mp->m_maxicount = ((mp->m_maxicount / mp->m_ialloc_blks) *
679 mp->m_ialloc_blks) << sbp->sb_inopblog;
680 } else
681 mp->m_maxicount = 0;
682
683 mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
684
685 /*
686 * Set whether we're using inode alignment.
687 */
688 if (XFS_SB_VERSION_HASALIGN(&mp->m_sb) &&
689 mp->m_sb.sb_inoalignmt >=
690 XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
691 mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
692 else
693 mp->m_inoalign_mask = 0;
694 /*
695 * If we are using stripe alignment, check whether
696 * the stripe unit is a multiple of the inode alignment
697 */
698 if ( mp->m_dalign
699 && mp->m_inoalign_mask && !(mp->m_dalign & mp->m_inoalign_mask))
700 mp->m_sinoalign = mp->m_dalign;
701 else
702 mp->m_sinoalign = 0;
703
704 /*
705 * Check that the data (and log if separate) are an ok size.
706 */
707 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
708 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
709 fprintf(stderr, "%s: size check failed\n", progname);
710 return NULL;
711 }
712
713 /* Initialize the appropriate directory manager */
714 if (XFS_SB_VERSION_HASDIRV2(sbp))
715 libxfs_dir2_mount(mp);
716 else
717 libxfs_dir_mount(mp);
718
719 /* Initialize the precomputed transaction reservations values */
720 libxfs_trans_init(mp);
721
722 if (dev == 0) /* maxtrres, we have no device so leave now */
723 return mp;
724
725 bp = libxfs_readbuf(mp->m_dev, d - 1, 1, 0);
726 if (bp == NULL) {
727 fprintf(stderr, "%s: data size check failed\n", progname);
728 return NULL;
729 }
730 libxfs_putbuf(bp);
731
732 if (mp->m_logdev && mp->m_logdev != mp->m_dev) {
733 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
734 if ( (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) ||
735 (!(bp = libxfs_readbuf(mp->m_logdev, d - 1, 1, 1)))) {
736 fprintf(stderr, "%s: log size checks failed\n",
737 progname);
738 return NULL;
739 }
740 libxfs_putbuf(bp);
741 }
742
743 /* Initialize realtime fields in the mount structure */
744 if (libxfs_rtmount_init(mp)) {
745 fprintf(stderr, "%s: real-time device init failed\n", progname);
746 return NULL;
747 }
748
749 /* Allocate and initialize the per-ag data */
750 size = sbp->sb_agcount * sizeof(xfs_perag_t);
751 if ((mp->m_perag = calloc(size, 1)) == NULL) {
5b64e00a
NS
752 fprintf(stderr, "%s: failed to alloc %ld bytes: %s\n",
753 progname, (long)size, strerror(errno));
2bd0ea18
NS
754 exit(1);
755 }
756
757 /*
758 * mkfs calls mount before the root inode is allocated.
759 */
760 if (rrootinos && sbp->sb_rootino != NULLFSINO) {
761 error = libxfs_iread(mp, NULL, sbp->sb_rootino,
762 &mp->m_rootip, 0);
763 if (error) {
764 fprintf(stderr, "%s: cannot read root inode (%d)\n",
765 progname, error);
766 return NULL;
767 }
768 ASSERT(mp->m_rootip != NULL);
769 }
770 if (rrootinos && rtmount_inodes(mp))
771 return NULL;
772 return mp;
773}
774
775/*
776 * Release any resourse obtained during a mount.
777 */
778void
779libxfs_umount(xfs_mount_t *mp)
780{
781 manage_zones(1);
782 free(mp->m_perag);
783}