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