]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/init.c
libfrog: take over platform headers
[thirdparty/xfsprogs-dev.git] / libxfs / init.c
CommitLineData
959ef981 1// SPDX-License-Identifier: GPL-2.0
2bd0ea18 2/*
da23017d
NS
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
2bd0ea18
NS
5 */
6
2bd0ea18 7#include <sys/stat.h>
9440d84d 8#include "init.h"
29e62271 9
9c799827 10#include "libxfs_priv.h"
b626fb59
DC
11#include "xfs_fs.h"
12#include "xfs_shared.h"
13#include "xfs_format.h"
14#include "xfs_log_format.h"
15#include "xfs_trans_resv.h"
16#include "xfs_mount.h"
794a5604 17#include "xfs_defer.h"
b626fb59
DC
18#include "xfs_inode_buf.h"
19#include "xfs_inode_fork.h"
20#include "xfs_inode.h"
21#include "xfs_trans.h"
b3a96b46 22#include "xfs_rmap_btree.h"
e7be6330 23#include "xfs_refcount_btree.h"
b658de93 24#include "libfrog/platform.h"
b626fb59 25
6b803e5a 26#include "libxfs.h" /* for now */
b626fb59 27
2bd0ea18
NS
28char *progname = "libxfs"; /* default, changed by each tool */
29
f1b058f9 30struct cache *libxfs_bcache; /* global buffer cache */
9f38f08d 31int libxfs_bhash_size; /* #buckets in bcache */
f1b058f9 32
d0572de5
BN
33int use_xfs_buf_lock; /* global flag: use xfs_buf_t locks for MT */
34
2bd0ea18
NS
35/*
36 * dev_map - map open devices to fd.
37 */
38#define MAX_DEVS 10 /* arbitary maximum */
00ff2b10 39static int nextfakedev = -1; /* device number to give to next fake device */
2bd0ea18 40static struct dev_to_fd {
9440d84d
NS
41 dev_t dev;
42 int fd;
2bd0ea18
NS
43} dev_map[MAX_DEVS]={{0}};
44
2bd0ea18
NS
45/*
46 * Checks whether a given device has a mounted, writable
47 * filesystem, returns 1 if it does & fatal (just warns
48 * if not fatal, but allows us to proceed).
5000d01d 49 *
2bd0ea18
NS
50 * Useful to tools which will produce uncertain results
51 * if the filesystem is active - repair, check, logprint.
52 */
53static int
54check_isactive(char *name, char *block, int fatal)
55{
f594a0d1 56 struct stat st;
2bd0ea18 57
f594a0d1 58 if (stat(block, &st) < 0)
9440d84d 59 return 0;
fc8202ba 60 if ((st.st_mode & S_IFMT) != S_IFBLK)
9440d84d 61 return 0;
93d9f139 62 if (platform_check_ismounted(name, block, &st, 0) == 0)
9440d84d 63 return 0;
7f510afb
ES
64 if (platform_check_iswritable(name, block, &st))
65 return fatal ? 1 : 0;
66 return 0;
2bd0ea18
NS
67}
68
5000d01d 69/* libxfs_device_to_fd:
2bd0ea18
NS
70 * lookup a device number in the device map
71 * return the associated fd
72 */
73int
74libxfs_device_to_fd(dev_t device)
75{
9440d84d 76 int d;
5000d01d 77
9440d84d 78 for (d = 0; d < MAX_DEVS; d++)
5000d01d 79 if (dev_map[d].dev == device)
2bd0ea18 80 return dev_map[d].fd;
5000d01d 81
9440d84d
NS
82 fprintf(stderr, _("%s: %s: device %lld is not open\n"),
83 progname, __FUNCTION__, (long long)device);
2bd0ea18 84 exit(1);
25d246df 85 /* NOTREACHED */
2bd0ea18
NS
86}
87
88/* libxfs_device_open:
89 * open a device and return its device number
90 */
91dev_t
7eb6693f 92libxfs_device_open(char *path, int creat, int xflags, int setblksize)
2bd0ea18 93{
2bd0ea18 94 dev_t dev;
7eb6693f 95 int fd, d, flags;
b74a1f6a 96 int readonly, dio, excl;
f594a0d1 97 struct stat statb;
7eb6693f
NS
98
99 readonly = (xflags & LIBXFS_ISREADONLY);
100 excl = (xflags & LIBXFS_EXCLUSIVELY) && !creat;
b74a1f6a 101 dio = (xflags & LIBXFS_DIRECT) && !creat && platform_direct_blockdev();
2bd0ea18 102
b74a1f6a 103retry:
7eb6693f
NS
104 flags = (readonly ? O_RDONLY : O_RDWR) | \
105 (creat ? (O_CREAT|O_TRUNC) : 0) | \
b74a1f6a 106 (dio ? O_DIRECT : 0) | \
7eb6693f 107 (excl ? O_EXCL : 0);
b74a1f6a 108
7eb6693f 109 if ((fd = open(path, flags, 0666)) < 0) {
b74a1f6a
NS
110 if (errno == EINVAL && --dio == 0)
111 goto retry;
9440d84d 112 fprintf(stderr, _("%s: cannot open %s: %s\n"),
2bd0ea18
NS
113 progname, path, strerror(errno));
114 exit(1);
115 }
116
f594a0d1 117 if (fstat(fd, &statb) < 0) {
9440d84d 118 fprintf(stderr, _("%s: cannot stat %s: %s\n"),
2bd0ea18
NS
119 progname, path, strerror(errno));
120 exit(1);
121 }
a33a9e62 122
edd45774
TS
123 if (!readonly && setblksize && (statb.st_mode & S_IFMT) == S_IFBLK) {
124 if (setblksize == 1)
125 /* use the default blocksize */
126 (void)platform_set_blocksize(fd, path, statb.st_rdev, XFS_MIN_SECTORSIZE, 0);
127 else {
128 /* given an explicit blocksize to use */
129 if (platform_set_blocksize(fd, path, statb.st_rdev, setblksize, 1))
130 exit(1);
131 }
132 }
2bd0ea18 133
a33a9e62
NS
134 /*
135 * Get the device number from the stat buf - unless
2bd0ea18 136 * we're not opening a real device, in which case
a33a9e62 137 * choose a new fake device number.
2bd0ea18 138 */
32181a02 139 dev = (statb.st_rdev) ? (statb.st_rdev) : (nextfakedev--);
2bd0ea18 140
32181a02 141 for (d = 0; d < MAX_DEVS; d++)
2bd0ea18 142 if (dev_map[d].dev == dev) {
9440d84d 143 fprintf(stderr, _("%s: device %lld is already open\n"),
5b64e00a 144 progname, (long long)dev);
2bd0ea18
NS
145 exit(1);
146 }
147
32181a02 148 for (d = 0; d < MAX_DEVS; d++)
2bd0ea18 149 if (!dev_map[d].dev) {
32181a02
NS
150 dev_map[d].dev = dev;
151 dev_map[d].fd = fd;
5000d01d 152
2bd0ea18
NS
153 return dev;
154 }
155
9440d84d
NS
156 fprintf(stderr, _("%s: %s: too many open devices\n"),
157 progname, __FUNCTION__);
2bd0ea18 158 exit(1);
25d246df 159 /* NOTREACHED */
2bd0ea18
NS
160}
161
162void
163libxfs_device_close(dev_t dev)
164{
5000d01d 165 int d;
2bd0ea18 166
32181a02 167 for (d = 0; d < MAX_DEVS; d++)
2bd0ea18 168 if (dev_map[d].dev == dev) {
32181a02 169 int fd;
5000d01d 170
32181a02
NS
171 fd = dev_map[d].fd;
172 dev_map[d].dev = dev_map[d].fd = 0;
5000d01d 173
2bd0ea18 174 fsync(fd);
ac419944 175 platform_flush_device(fd, dev);
2bd0ea18 176 close(fd);
5000d01d 177
2bd0ea18
NS
178 return;
179 }
180
9440d84d
NS
181 fprintf(stderr, _("%s: %s: device %lld is not open\n"),
182 progname, __FUNCTION__, (long long)dev);
2bd0ea18
NS
183 exit(1);
184}
185
74668075
NS
186static int
187check_open(char *path, int flags, char **rawfile, char **blockfile)
188{
c781939c
RC
189 int readonly = (flags & LIBXFS_ISREADONLY);
190 int inactive = (flags & LIBXFS_ISINACTIVE);
191 int dangerously = (flags & LIBXFS_DANGEROUSLY);
f594a0d1 192 struct stat stbuf;
c781939c 193
f594a0d1 194 if (stat(path, &stbuf) < 0) {
c781939c
RC
195 perror(path);
196 return 0;
197 }
b74a1f6a 198 if (!(*rawfile = platform_findrawpath(path))) {
c781939c
RC
199 fprintf(stderr, _("%s: "
200 "can't find a character device matching %s\n"),
201 progname, path);
202 return 0;
203 }
b74a1f6a 204 if (!(*blockfile = platform_findblockpath(path))) {
c781939c
RC
205 fprintf(stderr, _("%s: "
206 "can't find a block device matching %s\n"),
207 progname, path);
208 return 0;
209 }
210 if (!readonly && !inactive && platform_check_ismounted(path, *blockfile, NULL, 1))
211 return 0;
e08f5594 212
c781939c
RC
213 if (inactive && check_isactive(path, *blockfile, ((readonly|dangerously)?1:0)))
214 return 0;
215
216 return 1;
217}
218
7a326ce0
ES
219/*
220 * Initialize/destroy all of the zone allocators we use.
221 */
222static void
223init_zones(void)
224{
225 /* initialise zone allocation */
226 xfs_buf_zone = kmem_zone_init(sizeof(struct xfs_buf), "xfs_buffer");
227 xfs_inode_zone = kmem_zone_init(sizeof(struct xfs_inode), "xfs_inode");
228 xfs_ifork_zone = kmem_zone_init(sizeof(struct xfs_ifork), "xfs_ifork");
229 xfs_ili_zone = kmem_zone_init(
230 sizeof(struct xfs_inode_log_item),"xfs_inode_log_item");
231 xfs_buf_item_zone = kmem_zone_init(
232 sizeof(struct xfs_buf_log_item), "xfs_buf_log_item");
233 xfs_da_state_zone = kmem_zone_init(
234 sizeof(struct xfs_da_state), "xfs_da_state");
235 xfs_btree_cur_zone = kmem_zone_init(
236 sizeof(struct xfs_btree_cur), "xfs_btree_cur");
237 xfs_bmap_free_item_zone = kmem_zone_init(
238 sizeof(struct xfs_extent_free_item),
239 "xfs_bmap_free_item");
240 xfs_trans_zone = kmem_zone_init(
241 sizeof(struct xfs_trans), "xfs_trans");
242}
243
244static int
245destroy_zones(void)
246{
247 int leaked = 0;
248
249 leaked += kmem_zone_destroy(xfs_buf_zone);
250 leaked += kmem_zone_destroy(xfs_ili_zone);
251 leaked += kmem_zone_destroy(xfs_inode_zone);
252 leaked += kmem_zone_destroy(xfs_ifork_zone);
253 leaked += kmem_zone_destroy(xfs_buf_item_zone);
254 leaked += kmem_zone_destroy(xfs_da_state_zone);
255 leaked += kmem_zone_destroy(xfs_btree_cur_zone);
256 leaked += kmem_zone_destroy(xfs_bmap_free_item_zone);
257 leaked += kmem_zone_destroy(xfs_trans_zone);
258
259 return leaked;
260}
261
2bd0ea18
NS
262/*
263 * libxfs initialization.
264 * Caller gets a 0 on failure (and we print a message), 1 on success.
265 */
266int
267libxfs_init(libxfs_init_t *a)
268{
269 char *blockfile;
2bd0ea18
NS
270 char *dname;
271 char dpath[25];
272 int fd;
273 char *logname;
274 char logpath[25];
2bd0ea18
NS
275 char *rawfile;
276 char *rtname;
277 char rtpath[25];
278 int rval = 0;
c781939c 279 int flags;
2bd0ea18
NS
280
281 dpath[0] = logpath[0] = rtpath[0] = '\0';
282 dname = a->dname;
283 logname = a->logname;
284 rtname = a->rtname;
2bd0ea18 285 a->dfd = a->logfd = a->rtfd = -1;
74668075 286 a->ddev = a->logdev = a->rtdev = 0;
06ac92fd
DC
287 a->dsize = a->lbsize = a->rtbsize = 0;
288 a->dbsize = a->logBBsize = a->logBBstart = a->rtsize = 0;
2bd0ea18 289
2bd0ea18 290 fd = -1;
b74a1f6a 291 flags = (a->isreadonly | a->isdirect);
c781939c 292
bacd44a5
AE
293 radix_tree_init();
294
2bd0ea18 295 if (a->volname) {
c781939c 296 if(!check_open(a->volname,flags,&rawfile,&blockfile))
2bd0ea18 297 goto done;
2bd0ea18 298 fd = open(rawfile, O_RDONLY);
eb37fca5
NS
299 dname = a->dname = a->volname;
300 a->volname = NULL;
2bd0ea18 301 }
2bd0ea18 302 if (dname) {
2bd0ea18 303 if (a->disfile) {
7eb6693f 304 a->ddev= libxfs_device_open(dname, a->dcreat, flags,
c5907b96 305 a->setblksize);
2bd0ea18 306 a->dfd = libxfs_device_to_fd(a->ddev);
06ac92fd
DC
307 platform_findsizes(dname, a->dfd, &a->dsize,
308 &a->dbsize);
2bd0ea18 309 } else {
f02037ce 310 if (!check_open(dname, flags, &rawfile, &blockfile))
2bd0ea18
NS
311 goto done;
312 a->ddev = libxfs_device_open(rawfile,
7eb6693f 313 a->dcreat, flags, a->setblksize);
2bd0ea18 314 a->dfd = libxfs_device_to_fd(a->ddev);
f02037ce 315 platform_findsizes(rawfile, a->dfd,
06ac92fd 316 &a->dsize, &a->dbsize);
2bd0ea18 317 }
2bd0ea18
NS
318 } else
319 a->dsize = 0;
320 if (logname) {
2bd0ea18
NS
321 if (a->lisfile) {
322 a->logdev = libxfs_device_open(logname,
7eb6693f 323 a->lcreat, flags, a->setblksize);
2bd0ea18 324 a->logfd = libxfs_device_to_fd(a->logdev);
06ac92fd
DC
325 platform_findsizes(dname, a->logfd, &a->logBBsize,
326 &a->lbsize);
2bd0ea18 327 } else {
f02037ce 328 if (!check_open(logname, flags, &rawfile, &blockfile))
2bd0ea18
NS
329 goto done;
330 a->logdev = libxfs_device_open(rawfile,
7eb6693f 331 a->lcreat, flags, a->setblksize);
2bd0ea18 332 a->logfd = libxfs_device_to_fd(a->logdev);
f02037ce 333 platform_findsizes(rawfile, a->logfd,
06ac92fd 334 &a->logBBsize, &a->lbsize);
2bd0ea18 335 }
2bd0ea18
NS
336 } else
337 a->logBBsize = 0;
338 if (rtname) {
2bd0ea18
NS
339 if (a->risfile) {
340 a->rtdev = libxfs_device_open(rtname,
7eb6693f 341 a->rcreat, flags, a->setblksize);
2bd0ea18 342 a->rtfd = libxfs_device_to_fd(a->rtdev);
06ac92fd
DC
343 platform_findsizes(dname, a->rtfd, &a->rtsize,
344 &a->rtbsize);
2bd0ea18 345 } else {
f02037ce 346 if (!check_open(rtname, flags, &rawfile, &blockfile))
2bd0ea18
NS
347 goto done;
348 a->rtdev = libxfs_device_open(rawfile,
7eb6693f 349 a->rcreat, flags, a->setblksize);
2bd0ea18 350 a->rtfd = libxfs_device_to_fd(a->rtdev);
f02037ce 351 platform_findsizes(rawfile, a->rtfd,
06ac92fd 352 &a->rtsize, &a->rtbsize);
2bd0ea18 353 }
2bd0ea18
NS
354 } else
355 a->rtsize = 0;
356 if (a->dsize < 0) {
9440d84d 357 fprintf(stderr, _("%s: can't get size for data subvolume\n"),
2bd0ea18
NS
358 progname);
359 goto done;
360 }
361 if (a->logBBsize < 0) {
9440d84d 362 fprintf(stderr, _("%s: can't get size for log subvolume\n"),
2bd0ea18
NS
363 progname);
364 goto done;
365 }
366 if (a->rtsize < 0) {
9440d84d 367 fprintf(stderr, _("%s: can't get size for realtime subvolume\n"),
2bd0ea18
NS
368 progname);
369 goto done;
370 }
9f38f08d
MV
371 if (!libxfs_bhash_size)
372 libxfs_bhash_size = LIBXFS_BHASHSIZE(sbp);
ba9ecd40
DC
373 libxfs_bcache = cache_init(a->bcache_flags, libxfs_bhash_size,
374 &libxfs_bcache_operations);
d0572de5 375 use_xfs_buf_lock = a->usebuflock;
7a326ce0
ES
376 xfs_dir_startup();
377 init_zones();
2bd0ea18
NS
378 rval = 1;
379done:
380 if (dpath[0])
381 unlink(dpath);
382 if (logpath[0])
383 unlink(logpath);
384 if (rtpath[0])
385 unlink(rtpath);
386 if (fd >= 0)
387 close(fd);
388 if (!rval && a->ddev)
389 libxfs_device_close(a->ddev);
390 if (!rval && a->logdev)
391 libxfs_device_close(a->logdev);
392 if (!rval && a->rtdev)
393 libxfs_device_close(a->rtdev);
394 return rval;
395}
396
397
b391b7cd
NS
398/*
399 * Initialize realtime fields in the mount structure.
400 */
401static int
402rtmount_init(
39798eb5
NS
403 xfs_mount_t *mp, /* file system mount structure */
404 int flags)
b391b7cd
NS
405{
406 xfs_buf_t *bp; /* buffer for last block of subvolume */
407 xfs_daddr_t d; /* address of last block of subvolume */
408 xfs_sb_t *sbp; /* filesystem superblock copy in mount */
409
410 sbp = &mp->m_sb;
411 if (sbp->sb_rblocks == 0)
412 return 0;
75c8b434 413 if (mp->m_rtdev_targp->dev == 0 && !(flags & LIBXFS_MOUNT_DEBUGGER)) {
9440d84d 414 fprintf(stderr, _("%s: filesystem has a realtime subvolume\n"),
b391b7cd
NS
415 progname);
416 return -1;
417 }
418 mp->m_rsumlevels = sbp->sb_rextslog + 1;
419 mp->m_rsumsize =
420 (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
421 sbp->sb_rbmblocks;
422 mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
423 mp->m_rbmip = mp->m_rsumip = NULL;
39798eb5
NS
424
425 /*
426 * Allow debugger to be run without the realtime device present.
427 */
428 if (flags & LIBXFS_MOUNT_DEBUGGER)
429 return 0;
430
b391b7cd
NS
431 /*
432 * Check that the realtime section is an ok size.
433 */
434 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
435 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
9440d84d
NS
436 fprintf(stderr, _("%s: realtime init - %llu != %llu\n"),
437 progname, (unsigned long long) XFS_BB_TO_FSB(mp, d),
b391b7cd
NS
438 (unsigned long long) mp->m_sb.sb_rblocks);
439 return -1;
440 }
9440d84d 441 bp = libxfs_readbuf(mp->m_rtdev,
75c8b434 442 d - XFS_FSB_TO_BB(mp, 1), XFS_FSB_TO_BB(mp, 1), 0, NULL);
b391b7cd 443 if (bp == NULL) {
9440d84d
NS
444 fprintf(stderr, _("%s: realtime size check failed\n"),
445 progname);
b391b7cd
NS
446 return -1;
447 }
448 libxfs_putbuf(bp);
449 return 0;
450}
451
56b2de80
DC
452static int
453libxfs_initialize_perag(
454 xfs_mount_t *mp,
455 xfs_agnumber_t agcount,
456 xfs_agnumber_t *maxagi)
457{
458 xfs_agnumber_t index, max_metadata;
459 xfs_agnumber_t first_initialised = 0;
460 xfs_perag_t *pag;
461 xfs_agino_t agino;
462 xfs_ino_t ino;
463 xfs_sb_t *sbp = &mp->m_sb;
464 int error = -ENOMEM;
465
466 /*
467 * Walk the current per-ag tree so we don't try to initialise AGs
468 * that already exist (growfs case). Allocate and insert all the
469 * AGs we don't find ready for initialisation.
470 */
471 for (index = 0; index < agcount; index++) {
472 pag = xfs_perag_get(mp, index);
473 if (pag) {
474 xfs_perag_put(pag);
475 continue;
476 }
477 if (!first_initialised)
478 first_initialised = index;
479
480 pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL);
481 if (!pag)
482 goto out_unwind;
483 pag->pag_agno = index;
484 pag->pag_mount = mp;
485
486 if (radix_tree_insert(&mp->m_perag_tree, index, pag)) {
487 error = -EEXIST;
488 goto out_unwind;
489 }
490 }
491
492 /*
493 * If we mount with the inode64 option, or no inode overflows
494 * the legacy 32-bit address space clear the inode32 option.
495 */
7516da71 496 agino = XFS_AGB_TO_AGINO(mp, sbp->sb_agblocks - 1);
56b2de80
DC
497 ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
498
499 if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) && ino > XFS_MAXINUMBER_32)
500 mp->m_flags |= XFS_MOUNT_32BITINODES;
501 else
502 mp->m_flags &= ~XFS_MOUNT_32BITINODES;
503
504 if (mp->m_flags & XFS_MOUNT_32BITINODES) {
505 /*
506 * Calculate how much should be reserved for inodes to meet
507 * the max inode percentage.
508 */
e7fd2b6f 509 if (M_IGEO(mp)->maxicount) {
14f8b681 510 uint64_t icount;
56b2de80
DC
511
512 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
513 do_div(icount, 100);
514 icount += sbp->sb_agblocks - 1;
515 do_div(icount, sbp->sb_agblocks);
516 max_metadata = icount;
517 } else {
518 max_metadata = agcount;
519 }
520
521 for (index = 0; index < agcount; index++) {
522 ino = XFS_AGINO_TO_INO(mp, index, agino);
523 if (ino > XFS_MAXINUMBER_32) {
524 index++;
525 break;
526 }
527
528 pag = xfs_perag_get(mp, index);
529 pag->pagi_inodeok = 1;
530 if (index < max_metadata)
531 pag->pagf_metadata = 1;
532 xfs_perag_put(pag);
533 }
534 } else {
535 for (index = 0; index < agcount; index++) {
536 pag = xfs_perag_get(mp, index);
537 pag->pagi_inodeok = 1;
538 xfs_perag_put(pag);
539 }
540 }
541
542 if (maxagi)
543 *maxagi = index;
ef5340cd
DW
544
545 mp->m_ag_prealloc_blocks = xfs_prealloc_blocks(mp);
56b2de80
DC
546 return 0;
547
548out_unwind:
549 kmem_free(pag);
550 for (; index > first_initialised; index--) {
551 pag = radix_tree_delete(&mp->m_perag_tree, index);
552 kmem_free(pag);
553 }
554 return error;
555}
556
75c8b434
DC
557static struct xfs_buftarg *
558libxfs_buftarg_alloc(
559 struct xfs_mount *mp,
560 dev_t dev)
561{
562 struct xfs_buftarg *btp;
563
564 btp = malloc(sizeof(*btp));
565 if (!btp) {
566 fprintf(stderr, _("%s: buftarg init failed\n"),
567 progname);
568 exit(1);
569 }
570 btp->bt_mount = mp;
571 btp->dev = dev;
572 return btp;
573}
574
575void
576libxfs_buftarg_init(
577 struct xfs_mount *mp,
578 dev_t dev,
579 dev_t logdev,
580 dev_t rtdev)
581{
582 if (mp->m_ddev_targp) {
583 /* should already have all buftargs initialised */
584 if (mp->m_ddev_targp->dev != dev ||
585 mp->m_ddev_targp->bt_mount != mp) {
586 fprintf(stderr,
587 _("%s: bad buftarg reinit, ddev\n"),
588 progname);
589 exit(1);
590 }
591 if (!logdev || logdev == dev) {
592 if (mp->m_logdev_targp != mp->m_ddev_targp) {
593 fprintf(stderr,
594 _("%s: bad buftarg reinit, ldev mismatch\n"),
595 progname);
596 exit(1);
597 }
598 } else if (mp->m_logdev_targp->dev != logdev ||
599 mp->m_logdev_targp->bt_mount != mp) {
600 fprintf(stderr,
601 _("%s: bad buftarg reinit, logdev\n"),
602 progname);
603 exit(1);
604 }
605 if (rtdev && (mp->m_rtdev_targp->dev != rtdev ||
606 mp->m_rtdev_targp->bt_mount != mp)) {
607 fprintf(stderr,
608 _("%s: bad buftarg reinit, rtdev\n"),
609 progname);
610 exit(1);
611 }
612 return;
613 }
614
615 mp->m_ddev_targp = libxfs_buftarg_alloc(mp, dev);
616 if (!logdev || logdev == dev)
617 mp->m_logdev_targp = mp->m_ddev_targp;
618 else
619 mp->m_logdev_targp = libxfs_buftarg_alloc(mp, logdev);
620 mp->m_rtdev_targp = libxfs_buftarg_alloc(mp, rtdev);
621}
622
2bd0ea18
NS
623/*
624 * Mount structure initialization, provides a filled-in xfs_mount_t
625 * such that the numerous XFS_* macros can be used. If dev is zero,
626 * no IO will be performed (no size checks, read root inodes).
627 */
628xfs_mount_t *
629libxfs_mount(
630 xfs_mount_t *mp,
631 xfs_sb_t *sb,
632 dev_t dev,
633 dev_t logdev,
634 dev_t rtdev,
4ca431fc 635 int flags)
2bd0ea18
NS
636{
637 xfs_daddr_t d;
638 xfs_buf_t *bp;
639 xfs_sb_t *sbp;
2bd0ea18
NS
640 int error;
641
75c8b434
DC
642 libxfs_buftarg_init(mp, dev, logdev, rtdev);
643
f747f7dd 644 mp->m_finobt_nores = true;
6239071d 645 mp->m_flags = (LIBXFS_MOUNT_32BITINODES|LIBXFS_MOUNT_32BITINOOPT);
2bd0ea18 646 mp->m_sb = *sb;
56b2de80 647 INIT_RADIX_TREE(&mp->m_perag_tree, GFP_KERNEL);
2bd0ea18 648 sbp = &(mp->m_sb);
2bd0ea18 649
4896e6c8 650 xfs_sb_mount_common(mp, sb);
2bd0ea18 651
949c0f10
NS
652 /*
653 * Set whether we're using stripe alignment.
654 */
5e656dbb 655 if (xfs_sb_version_hasdalign(&mp->m_sb)) {
949c0f10
NS
656 mp->m_dalign = sbp->sb_unit;
657 mp->m_swidth = sbp->sb_width;
658 }
659
3a05ab22
DW
660 xfs_alloc_compute_maxlevels(mp);
661 xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
662 xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
663 xfs_ialloc_setup_geometry(mp);
664 xfs_rmapbt_compute_maxlevels(mp);
665 xfs_refcountbt_compute_maxlevels(mp);
2bd0ea18
NS
666
667 /*
668 * Check that the data (and log if separate) are an ok size.
669 */
9440d84d 670 d = (xfs_daddr_t) XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
2bd0ea18 671 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
9440d84d 672 fprintf(stderr, _("%s: size check failed\n"), progname);
4ca431fc
NS
673 if (!(flags & LIBXFS_MOUNT_DEBUGGER))
674 return NULL;
2bd0ea18
NS
675 }
676
ff105f75
DC
677 /*
678 * We automatically convert v1 inodes to v2 inodes now, so if
679 * the NLINK bit is not set we can't operate on the filesystem.
680 */
681 if (!(sbp->sb_versionnum & XFS_SB_VERSION_NLINKBIT)) {
682
683 fprintf(stderr, _(
684 "%s: V1 inodes unsupported. Please try an older xfsprogs.\n"),
685 progname);
686 exit(1);
687 }
688
689 /* Check for supported directory formats */
690 if (!(sbp->sb_versionnum & XFS_SB_VERSION_DIRV2BIT)) {
9a048535
DC
691
692 fprintf(stderr, _(
693 "%s: V1 directories unsupported. Please try an older xfsprogs.\n"),
694 progname);
695 exit(1);
5e656dbb 696 }
2bd0ea18 697
ff105f75
DC
698 /* check for unsupported other features */
699 if (!xfs_sb_good_version(sbp)) {
700 fprintf(stderr, _(
701 "%s: Unsupported features detected. Please try a newer xfsprogs.\n"),
702 progname);
703 exit(1);
704 }
705
706 xfs_da_mount(mp);
707
5e656dbb
BN
708 if (xfs_sb_version_hasattr2(&mp->m_sb))
709 mp->m_flags |= LIBXFS_MOUNT_ATTR2;
57c9fccb 710
2bd0ea18 711 /* Initialize the precomputed transaction reservations values */
5e656dbb 712 xfs_trans_init(mp);
2bd0ea18
NS
713
714 if (dev == 0) /* maxtrres, we have no device so leave now */
715 return mp;
716
9440d84d 717 bp = libxfs_readbuf(mp->m_dev,
4ca431fc 718 d - XFS_FSS_TO_BB(mp, 1), XFS_FSS_TO_BB(mp, 1),
75c8b434 719 !(flags & LIBXFS_MOUNT_DEBUGGER), NULL);
9440d84d
NS
720 if (!bp) {
721 fprintf(stderr, _("%s: data size check failed\n"), progname);
4ca431fc
NS
722 if (!(flags & LIBXFS_MOUNT_DEBUGGER))
723 return NULL;
32244196
BN
724 } else
725 libxfs_putbuf(bp);
2bd0ea18 726
75c8b434
DC
727 if (mp->m_logdev_targp->dev &&
728 mp->m_logdev_targp->dev != mp->m_ddev_targp->dev) {
9440d84d 729 d = (xfs_daddr_t) XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
2bd0ea18 730 if ( (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) ||
75c8b434 731 (!(bp = libxfs_readbuf(mp->m_logdev_targp,
9440d84d 732 d - XFS_FSB_TO_BB(mp, 1),
4ca431fc 733 XFS_FSB_TO_BB(mp, 1),
75c8b434 734 !(flags & LIBXFS_MOUNT_DEBUGGER), NULL))) ) {
9440d84d 735 fprintf(stderr, _("%s: log size checks failed\n"),
2bd0ea18 736 progname);
4ca431fc
NS
737 if (!(flags & LIBXFS_MOUNT_DEBUGGER))
738 return NULL;
2bd0ea18 739 }
32244196
BN
740 if (bp)
741 libxfs_putbuf(bp);
2bd0ea18
NS
742 }
743
744 /* Initialize realtime fields in the mount structure */
39798eb5 745 if (rtmount_init(mp, flags)) {
9440d84d
NS
746 fprintf(stderr, _("%s: realtime device init failed\n"),
747 progname);
4ca431fc 748 return NULL;
2bd0ea18
NS
749 }
750
a547152d
ES
751 /*
752 * libxfs_initialize_perag will allocate a perag structure for each ag.
753 * If agcount is corrupted and insanely high, this will OOM the box.
754 * If the agount seems (arbitrarily) high, try to read what would be
755 * the last AG, and if that fails for a relatively high agcount, just
756 * read the first one and let the user know to check the geometry.
757 */
758 if (sbp->sb_agcount > 1000000) {
759 bp = libxfs_readbuf(mp->m_dev,
760 XFS_AG_DADDR(mp, sbp->sb_agcount - 1, 0), 1,
761 !(flags & LIBXFS_MOUNT_DEBUGGER), NULL);
762 if (bp->b_error) {
763 fprintf(stderr, _("%s: read of AG %u failed\n"),
764 progname, sbp->sb_agcount);
765 if (!(flags & LIBXFS_MOUNT_DEBUGGER))
766 return NULL;
767 fprintf(stderr, _("%s: limiting reads to AG 0\n"),
768 progname);
769 sbp->sb_agcount = 1;
770 }
771 libxfs_putbuf(bp);
772 }
773
56b2de80
DC
774 error = libxfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi);
775 if (error) {
776 fprintf(stderr, _("%s: perag init failed\n"),
777 progname);
2bd0ea18
NS
778 exit(1);
779 }
780
2bd0ea18
NS
781 return mp;
782}
783
f1b058f9
NS
784void
785libxfs_rtmount_destroy(xfs_mount_t *mp)
786{
787 if (mp->m_rsumip)
31845e4c 788 libxfs_irele(mp->m_rsumip);
f1b058f9 789 if (mp->m_rbmip)
31845e4c 790 libxfs_irele(mp->m_rbmip);
f1b058f9
NS
791 mp->m_rsumip = mp->m_rbmip = NULL;
792}
793
2bd0ea18 794/*
9440d84d 795 * Release any resource obtained during a mount.
2bd0ea18
NS
796 */
797void
798libxfs_umount(xfs_mount_t *mp)
799{
56b2de80
DC
800 struct xfs_perag *pag;
801 int agno;
802
f1b058f9 803 libxfs_rtmount_destroy(mp);
f1b058f9
NS
804 libxfs_bcache_purge();
805
56b2de80
DC
806 for (agno = 0; agno < mp->m_maxagi; agno++) {
807 pag = radix_tree_delete(&mp->m_perag_tree, agno);
808 kmem_free(pag);
fceb0d99 809 }
4334e2e8
ES
810
811 kmem_free(mp->m_attr_geo);
812 kmem_free(mp->m_dir_geo);
813
814 kmem_free(mp->m_rtdev_targp);
815 if (mp->m_logdev_targp != mp->m_ddev_targp)
816 kmem_free(mp->m_logdev_targp);
817 kmem_free(mp->m_ddev_targp);
f8149110 818
2bd0ea18 819}
f1b058f9
NS
820
821/*
822 * Release any global resources used by libxfs.
823 */
824void
825libxfs_destroy(void)
826{
44488491
ES
827 int leaked;
828
864028ed
ES
829 /* Free everything from the buffer cache before freeing buffer zone */
830 libxfs_bcache_purge();
831 libxfs_bcache_free();
f1b058f9 832 cache_destroy(libxfs_bcache);
7a326ce0 833 leaked = destroy_zones();
44488491
ES
834 if (getenv("LIBXFS_LEAK_CHECK") && leaked)
835 exit(1);
f1b058f9 836}
9f38f08d 837
b74a1f6a
NS
838int
839libxfs_device_alignment(void)
840{
841 return platform_align_blockdev();
842}
843
9f38f08d 844void
b6281496 845libxfs_report(FILE *fp)
9f38f08d 846{
cb5b3ef4
MV
847 time_t t;
848 char *c;
849
b6281496 850 cache_report(fp, "libxfs_bcache", libxfs_bcache);
cb5b3ef4
MV
851
852 t = time(NULL);
853 c = asctime(localtime(&t));
854 fprintf(fp, "%s", c);
855}