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