]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/init.c
xfs: make xfs_buf_get_uncached return an error code
[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
a9468486
DW
262static void
263libxfs_close_devices(
264 struct libxfs_xinit *li)
265{
266 if (li->ddev)
267 libxfs_device_close(li->ddev);
268 if (li->logdev && li->logdev != li->ddev)
269 libxfs_device_close(li->logdev);
270 if (li->rtdev)
271 libxfs_device_close(li->rtdev);
272
273 li->ddev = li->logdev = li->rtdev = 0;
274 li->dfd = li->logfd = li->rtfd = -1;
275}
276
2bd0ea18
NS
277/*
278 * libxfs initialization.
279 * Caller gets a 0 on failure (and we print a message), 1 on success.
280 */
281int
282libxfs_init(libxfs_init_t *a)
283{
284 char *blockfile;
2bd0ea18
NS
285 char *dname;
286 char dpath[25];
287 int fd;
288 char *logname;
289 char logpath[25];
2bd0ea18
NS
290 char *rawfile;
291 char *rtname;
292 char rtpath[25];
293 int rval = 0;
c781939c 294 int flags;
2bd0ea18
NS
295
296 dpath[0] = logpath[0] = rtpath[0] = '\0';
297 dname = a->dname;
298 logname = a->logname;
299 rtname = a->rtname;
2bd0ea18 300 a->dfd = a->logfd = a->rtfd = -1;
74668075 301 a->ddev = a->logdev = a->rtdev = 0;
06ac92fd
DC
302 a->dsize = a->lbsize = a->rtbsize = 0;
303 a->dbsize = a->logBBsize = a->logBBstart = a->rtsize = 0;
2bd0ea18 304
2bd0ea18 305 fd = -1;
b74a1f6a 306 flags = (a->isreadonly | a->isdirect);
c781939c 307
bacd44a5
AE
308 radix_tree_init();
309
2bd0ea18 310 if (a->volname) {
c781939c 311 if(!check_open(a->volname,flags,&rawfile,&blockfile))
2bd0ea18 312 goto done;
2bd0ea18 313 fd = open(rawfile, O_RDONLY);
eb37fca5
NS
314 dname = a->dname = a->volname;
315 a->volname = NULL;
2bd0ea18 316 }
2bd0ea18 317 if (dname) {
2bd0ea18 318 if (a->disfile) {
7eb6693f 319 a->ddev= libxfs_device_open(dname, a->dcreat, flags,
c5907b96 320 a->setblksize);
2bd0ea18 321 a->dfd = libxfs_device_to_fd(a->ddev);
06ac92fd
DC
322 platform_findsizes(dname, a->dfd, &a->dsize,
323 &a->dbsize);
2bd0ea18 324 } else {
f02037ce 325 if (!check_open(dname, flags, &rawfile, &blockfile))
2bd0ea18
NS
326 goto done;
327 a->ddev = libxfs_device_open(rawfile,
7eb6693f 328 a->dcreat, flags, a->setblksize);
2bd0ea18 329 a->dfd = libxfs_device_to_fd(a->ddev);
f02037ce 330 platform_findsizes(rawfile, a->dfd,
06ac92fd 331 &a->dsize, &a->dbsize);
2bd0ea18 332 }
2bd0ea18
NS
333 } else
334 a->dsize = 0;
335 if (logname) {
2bd0ea18
NS
336 if (a->lisfile) {
337 a->logdev = libxfs_device_open(logname,
7eb6693f 338 a->lcreat, flags, a->setblksize);
2bd0ea18 339 a->logfd = libxfs_device_to_fd(a->logdev);
06ac92fd
DC
340 platform_findsizes(dname, a->logfd, &a->logBBsize,
341 &a->lbsize);
2bd0ea18 342 } else {
f02037ce 343 if (!check_open(logname, flags, &rawfile, &blockfile))
2bd0ea18
NS
344 goto done;
345 a->logdev = libxfs_device_open(rawfile,
7eb6693f 346 a->lcreat, flags, a->setblksize);
2bd0ea18 347 a->logfd = libxfs_device_to_fd(a->logdev);
f02037ce 348 platform_findsizes(rawfile, a->logfd,
06ac92fd 349 &a->logBBsize, &a->lbsize);
2bd0ea18 350 }
2bd0ea18
NS
351 } else
352 a->logBBsize = 0;
353 if (rtname) {
2bd0ea18
NS
354 if (a->risfile) {
355 a->rtdev = libxfs_device_open(rtname,
7eb6693f 356 a->rcreat, flags, a->setblksize);
2bd0ea18 357 a->rtfd = libxfs_device_to_fd(a->rtdev);
06ac92fd
DC
358 platform_findsizes(dname, a->rtfd, &a->rtsize,
359 &a->rtbsize);
2bd0ea18 360 } else {
f02037ce 361 if (!check_open(rtname, flags, &rawfile, &blockfile))
2bd0ea18
NS
362 goto done;
363 a->rtdev = libxfs_device_open(rawfile,
7eb6693f 364 a->rcreat, flags, a->setblksize);
2bd0ea18 365 a->rtfd = libxfs_device_to_fd(a->rtdev);
f02037ce 366 platform_findsizes(rawfile, a->rtfd,
06ac92fd 367 &a->rtsize, &a->rtbsize);
2bd0ea18 368 }
2bd0ea18
NS
369 } else
370 a->rtsize = 0;
371 if (a->dsize < 0) {
9440d84d 372 fprintf(stderr, _("%s: can't get size for data subvolume\n"),
2bd0ea18
NS
373 progname);
374 goto done;
375 }
376 if (a->logBBsize < 0) {
9440d84d 377 fprintf(stderr, _("%s: can't get size for log subvolume\n"),
2bd0ea18
NS
378 progname);
379 goto done;
380 }
381 if (a->rtsize < 0) {
9440d84d 382 fprintf(stderr, _("%s: can't get size for realtime subvolume\n"),
2bd0ea18
NS
383 progname);
384 goto done;
385 }
9f38f08d
MV
386 if (!libxfs_bhash_size)
387 libxfs_bhash_size = LIBXFS_BHASHSIZE(sbp);
ba9ecd40
DC
388 libxfs_bcache = cache_init(a->bcache_flags, libxfs_bhash_size,
389 &libxfs_bcache_operations);
d0572de5 390 use_xfs_buf_lock = a->usebuflock;
7a326ce0
ES
391 xfs_dir_startup();
392 init_zones();
2bd0ea18
NS
393 rval = 1;
394done:
395 if (dpath[0])
396 unlink(dpath);
397 if (logpath[0])
398 unlink(logpath);
399 if (rtpath[0])
400 unlink(rtpath);
401 if (fd >= 0)
402 close(fd);
a9468486
DW
403 if (!rval)
404 libxfs_close_devices(a);
405
2bd0ea18
NS
406 return rval;
407}
408
409
b391b7cd
NS
410/*
411 * Initialize realtime fields in the mount structure.
412 */
413static int
414rtmount_init(
39798eb5
NS
415 xfs_mount_t *mp, /* file system mount structure */
416 int flags)
b391b7cd
NS
417{
418 xfs_buf_t *bp; /* buffer for last block of subvolume */
419 xfs_daddr_t d; /* address of last block of subvolume */
420 xfs_sb_t *sbp; /* filesystem superblock copy in mount */
421
422 sbp = &mp->m_sb;
423 if (sbp->sb_rblocks == 0)
424 return 0;
75c8b434 425 if (mp->m_rtdev_targp->dev == 0 && !(flags & LIBXFS_MOUNT_DEBUGGER)) {
9440d84d 426 fprintf(stderr, _("%s: filesystem has a realtime subvolume\n"),
b391b7cd
NS
427 progname);
428 return -1;
429 }
430 mp->m_rsumlevels = sbp->sb_rextslog + 1;
431 mp->m_rsumsize =
432 (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
433 sbp->sb_rbmblocks;
434 mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
435 mp->m_rbmip = mp->m_rsumip = NULL;
39798eb5
NS
436
437 /*
438 * Allow debugger to be run without the realtime device present.
439 */
440 if (flags & LIBXFS_MOUNT_DEBUGGER)
441 return 0;
442
b391b7cd
NS
443 /*
444 * Check that the realtime section is an ok size.
445 */
446 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
447 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
9440d84d
NS
448 fprintf(stderr, _("%s: realtime init - %llu != %llu\n"),
449 progname, (unsigned long long) XFS_BB_TO_FSB(mp, d),
b391b7cd
NS
450 (unsigned long long) mp->m_sb.sb_rblocks);
451 return -1;
452 }
361379e0 453 bp = libxfs_buf_read(mp->m_rtdev,
75c8b434 454 d - XFS_FSB_TO_BB(mp, 1), XFS_FSB_TO_BB(mp, 1), 0, NULL);
b391b7cd 455 if (bp == NULL) {
9440d84d
NS
456 fprintf(stderr, _("%s: realtime size check failed\n"),
457 progname);
b391b7cd
NS
458 return -1;
459 }
e02ba985 460 libxfs_buf_relse(bp);
b391b7cd
NS
461 return 0;
462}
463
56b2de80
DC
464static int
465libxfs_initialize_perag(
466 xfs_mount_t *mp,
467 xfs_agnumber_t agcount,
468 xfs_agnumber_t *maxagi)
469{
470 xfs_agnumber_t index, max_metadata;
471 xfs_agnumber_t first_initialised = 0;
472 xfs_perag_t *pag;
473 xfs_agino_t agino;
474 xfs_ino_t ino;
475 xfs_sb_t *sbp = &mp->m_sb;
476 int error = -ENOMEM;
477
478 /*
479 * Walk the current per-ag tree so we don't try to initialise AGs
480 * that already exist (growfs case). Allocate and insert all the
481 * AGs we don't find ready for initialisation.
482 */
483 for (index = 0; index < agcount; index++) {
484 pag = xfs_perag_get(mp, index);
485 if (pag) {
486 xfs_perag_put(pag);
487 continue;
488 }
489 if (!first_initialised)
490 first_initialised = index;
491
492 pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL);
493 if (!pag)
494 goto out_unwind;
495 pag->pag_agno = index;
496 pag->pag_mount = mp;
497
498 if (radix_tree_insert(&mp->m_perag_tree, index, pag)) {
499 error = -EEXIST;
500 goto out_unwind;
501 }
502 }
503
504 /*
505 * If we mount with the inode64 option, or no inode overflows
506 * the legacy 32-bit address space clear the inode32 option.
507 */
7516da71 508 agino = XFS_AGB_TO_AGINO(mp, sbp->sb_agblocks - 1);
56b2de80
DC
509 ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
510
511 if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) && ino > XFS_MAXINUMBER_32)
512 mp->m_flags |= XFS_MOUNT_32BITINODES;
513 else
514 mp->m_flags &= ~XFS_MOUNT_32BITINODES;
515
516 if (mp->m_flags & XFS_MOUNT_32BITINODES) {
517 /*
518 * Calculate how much should be reserved for inodes to meet
519 * the max inode percentage.
520 */
e7fd2b6f 521 if (M_IGEO(mp)->maxicount) {
14f8b681 522 uint64_t icount;
56b2de80
DC
523
524 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
525 do_div(icount, 100);
526 icount += sbp->sb_agblocks - 1;
527 do_div(icount, sbp->sb_agblocks);
528 max_metadata = icount;
529 } else {
530 max_metadata = agcount;
531 }
532
533 for (index = 0; index < agcount; index++) {
534 ino = XFS_AGINO_TO_INO(mp, index, agino);
535 if (ino > XFS_MAXINUMBER_32) {
536 index++;
537 break;
538 }
539
540 pag = xfs_perag_get(mp, index);
541 pag->pagi_inodeok = 1;
542 if (index < max_metadata)
543 pag->pagf_metadata = 1;
544 xfs_perag_put(pag);
545 }
546 } else {
547 for (index = 0; index < agcount; index++) {
548 pag = xfs_perag_get(mp, index);
549 pag->pagi_inodeok = 1;
550 xfs_perag_put(pag);
551 }
552 }
553
554 if (maxagi)
555 *maxagi = index;
ef5340cd
DW
556
557 mp->m_ag_prealloc_blocks = xfs_prealloc_blocks(mp);
56b2de80
DC
558 return 0;
559
560out_unwind:
561 kmem_free(pag);
562 for (; index > first_initialised; index--) {
563 pag = radix_tree_delete(&mp->m_perag_tree, index);
564 kmem_free(pag);
565 }
566 return error;
567}
568
75c8b434
DC
569static struct xfs_buftarg *
570libxfs_buftarg_alloc(
571 struct xfs_mount *mp,
572 dev_t dev)
573{
574 struct xfs_buftarg *btp;
575
576 btp = malloc(sizeof(*btp));
577 if (!btp) {
578 fprintf(stderr, _("%s: buftarg init failed\n"),
579 progname);
580 exit(1);
581 }
582 btp->bt_mount = mp;
583 btp->dev = dev;
c335b673
DW
584 btp->flags = 0;
585
75c8b434
DC
586 return btp;
587}
588
589void
590libxfs_buftarg_init(
591 struct xfs_mount *mp,
592 dev_t dev,
593 dev_t logdev,
594 dev_t rtdev)
595{
596 if (mp->m_ddev_targp) {
597 /* should already have all buftargs initialised */
598 if (mp->m_ddev_targp->dev != dev ||
599 mp->m_ddev_targp->bt_mount != mp) {
600 fprintf(stderr,
601 _("%s: bad buftarg reinit, ddev\n"),
602 progname);
603 exit(1);
604 }
605 if (!logdev || logdev == dev) {
606 if (mp->m_logdev_targp != mp->m_ddev_targp) {
607 fprintf(stderr,
608 _("%s: bad buftarg reinit, ldev mismatch\n"),
609 progname);
610 exit(1);
611 }
612 } else if (mp->m_logdev_targp->dev != logdev ||
613 mp->m_logdev_targp->bt_mount != mp) {
614 fprintf(stderr,
615 _("%s: bad buftarg reinit, logdev\n"),
616 progname);
617 exit(1);
618 }
619 if (rtdev && (mp->m_rtdev_targp->dev != rtdev ||
620 mp->m_rtdev_targp->bt_mount != mp)) {
621 fprintf(stderr,
622 _("%s: bad buftarg reinit, rtdev\n"),
623 progname);
624 exit(1);
625 }
626 return;
627 }
628
629 mp->m_ddev_targp = libxfs_buftarg_alloc(mp, dev);
630 if (!logdev || logdev == dev)
631 mp->m_logdev_targp = mp->m_ddev_targp;
632 else
633 mp->m_logdev_targp = libxfs_buftarg_alloc(mp, logdev);
634 mp->m_rtdev_targp = libxfs_buftarg_alloc(mp, rtdev);
635}
636
2bd0ea18
NS
637/*
638 * Mount structure initialization, provides a filled-in xfs_mount_t
639 * such that the numerous XFS_* macros can be used. If dev is zero,
640 * no IO will be performed (no size checks, read root inodes).
641 */
d855bce8 642struct xfs_mount *
2bd0ea18 643libxfs_mount(
d855bce8
DW
644 struct xfs_mount *mp,
645 struct xfs_sb *sb,
646 dev_t dev,
647 dev_t logdev,
648 dev_t rtdev,
649 int flags)
2bd0ea18 650{
d855bce8
DW
651 struct xfs_buf *bp;
652 struct xfs_sb *sbp;
653 xfs_daddr_t d;
654 bool debugger = (flags & LIBXFS_MOUNT_DEBUGGER);
655 int error;
2bd0ea18 656
75c8b434
DC
657 libxfs_buftarg_init(mp, dev, logdev, rtdev);
658
f747f7dd 659 mp->m_finobt_nores = true;
6239071d 660 mp->m_flags = (LIBXFS_MOUNT_32BITINODES|LIBXFS_MOUNT_32BITINOOPT);
2bd0ea18 661 mp->m_sb = *sb;
56b2de80 662 INIT_RADIX_TREE(&mp->m_perag_tree, GFP_KERNEL);
2bd0ea18 663 sbp = &(mp->m_sb);
2bd0ea18 664
4896e6c8 665 xfs_sb_mount_common(mp, sb);
2bd0ea18 666
949c0f10
NS
667 /*
668 * Set whether we're using stripe alignment.
669 */
5e656dbb 670 if (xfs_sb_version_hasdalign(&mp->m_sb)) {
949c0f10
NS
671 mp->m_dalign = sbp->sb_unit;
672 mp->m_swidth = sbp->sb_width;
673 }
674
3a05ab22
DW
675 xfs_alloc_compute_maxlevels(mp);
676 xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
677 xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
678 xfs_ialloc_setup_geometry(mp);
679 xfs_rmapbt_compute_maxlevels(mp);
680 xfs_refcountbt_compute_maxlevels(mp);
2bd0ea18
NS
681
682 /*
683 * Check that the data (and log if separate) are an ok size.
684 */
9440d84d 685 d = (xfs_daddr_t) XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
2bd0ea18 686 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
9440d84d 687 fprintf(stderr, _("%s: size check failed\n"), progname);
4ca431fc
NS
688 if (!(flags & LIBXFS_MOUNT_DEBUGGER))
689 return NULL;
2bd0ea18
NS
690 }
691
ff105f75
DC
692 /*
693 * We automatically convert v1 inodes to v2 inodes now, so if
694 * the NLINK bit is not set we can't operate on the filesystem.
695 */
696 if (!(sbp->sb_versionnum & XFS_SB_VERSION_NLINKBIT)) {
697
698 fprintf(stderr, _(
699 "%s: V1 inodes unsupported. Please try an older xfsprogs.\n"),
700 progname);
701 exit(1);
702 }
703
704 /* Check for supported directory formats */
705 if (!(sbp->sb_versionnum & XFS_SB_VERSION_DIRV2BIT)) {
9a048535
DC
706
707 fprintf(stderr, _(
708 "%s: V1 directories unsupported. Please try an older xfsprogs.\n"),
709 progname);
710 exit(1);
5e656dbb 711 }
2bd0ea18 712
ff105f75
DC
713 /* check for unsupported other features */
714 if (!xfs_sb_good_version(sbp)) {
715 fprintf(stderr, _(
716 "%s: Unsupported features detected. Please try a newer xfsprogs.\n"),
717 progname);
718 exit(1);
719 }
720
721 xfs_da_mount(mp);
722
5e656dbb
BN
723 if (xfs_sb_version_hasattr2(&mp->m_sb))
724 mp->m_flags |= LIBXFS_MOUNT_ATTR2;
57c9fccb 725
2bd0ea18 726 /* Initialize the precomputed transaction reservations values */
5e656dbb 727 xfs_trans_init(mp);
2bd0ea18
NS
728
729 if (dev == 0) /* maxtrres, we have no device so leave now */
730 return mp;
731
d855bce8 732 /* device size checks must pass unless we're a debugger. */
361379e0 733 bp = libxfs_buf_read(mp->m_dev, d - XFS_FSS_TO_BB(mp, 1),
d855bce8 734 XFS_FSS_TO_BB(mp, 1), 0, NULL);
9440d84d
NS
735 if (!bp) {
736 fprintf(stderr, _("%s: data size check failed\n"), progname);
d855bce8 737 if (!debugger)
4ca431fc 738 return NULL;
32244196 739 } else
e02ba985 740 libxfs_buf_relse(bp);
2bd0ea18 741
75c8b434
DC
742 if (mp->m_logdev_targp->dev &&
743 mp->m_logdev_targp->dev != mp->m_ddev_targp->dev) {
9440d84d 744 d = (xfs_daddr_t) XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
2bd0ea18 745 if ( (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) ||
361379e0 746 (!(bp = libxfs_buf_read(mp->m_logdev_targp,
9440d84d 747 d - XFS_FSB_TO_BB(mp, 1),
d855bce8 748 XFS_FSB_TO_BB(mp, 1), 0, NULL)))) {
9440d84d 749 fprintf(stderr, _("%s: log size checks failed\n"),
2bd0ea18 750 progname);
d855bce8 751 if (!debugger)
4ca431fc 752 return NULL;
2bd0ea18 753 }
32244196 754 if (bp)
e02ba985 755 libxfs_buf_relse(bp);
2bd0ea18
NS
756 }
757
758 /* Initialize realtime fields in the mount structure */
39798eb5 759 if (rtmount_init(mp, flags)) {
9440d84d
NS
760 fprintf(stderr, _("%s: realtime device init failed\n"),
761 progname);
4ca431fc 762 return NULL;
2bd0ea18
NS
763 }
764
a547152d
ES
765 /*
766 * libxfs_initialize_perag will allocate a perag structure for each ag.
767 * If agcount is corrupted and insanely high, this will OOM the box.
768 * If the agount seems (arbitrarily) high, try to read what would be
769 * the last AG, and if that fails for a relatively high agcount, just
770 * read the first one and let the user know to check the geometry.
771 */
772 if (sbp->sb_agcount > 1000000) {
361379e0 773 bp = libxfs_buf_read(mp->m_dev,
a547152d 774 XFS_AG_DADDR(mp, sbp->sb_agcount - 1, 0), 1,
d855bce8 775 0, NULL);
a547152d
ES
776 if (bp->b_error) {
777 fprintf(stderr, _("%s: read of AG %u failed\n"),
778 progname, sbp->sb_agcount);
d855bce8 779 if (!debugger)
a547152d
ES
780 return NULL;
781 fprintf(stderr, _("%s: limiting reads to AG 0\n"),
782 progname);
783 sbp->sb_agcount = 1;
784 }
e02ba985 785 libxfs_buf_relse(bp);
a547152d
ES
786 }
787
56b2de80
DC
788 error = libxfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi);
789 if (error) {
790 fprintf(stderr, _("%s: perag init failed\n"),
791 progname);
2bd0ea18
NS
792 exit(1);
793 }
794
2bd0ea18
NS
795 return mp;
796}
797
f1b058f9
NS
798void
799libxfs_rtmount_destroy(xfs_mount_t *mp)
800{
801 if (mp->m_rsumip)
31845e4c 802 libxfs_irele(mp->m_rsumip);
f1b058f9 803 if (mp->m_rbmip)
31845e4c 804 libxfs_irele(mp->m_rbmip);
f1b058f9
NS
805 mp->m_rsumip = mp->m_rbmip = NULL;
806}
807
c335b673
DW
808/* Flush a device and report on writes that didn't make it to stable storage. */
809static inline int
810libxfs_flush_buftarg(
811 struct xfs_buftarg *btp,
812 const char *buftarg_descr)
813{
814 int error = 0;
815 int err2;
816
817 /*
818 * Write verifier failures are evidence of a buggy program. Make sure
819 * that this state is always reported to the caller.
820 */
821 if (btp->flags & XFS_BUFTARG_CORRUPT_WRITE) {
822 fprintf(stderr,
823_("%s: Refusing to write a corrupt buffer to the %s!\n"),
824 progname, buftarg_descr);
825 error = -EFSCORRUPTED;
826 }
827
828 if (btp->flags & XFS_BUFTARG_LOST_WRITE) {
829 fprintf(stderr,
830_("%s: Lost a write to the %s!\n"),
831 progname, buftarg_descr);
832 if (!error)
833 error = -EIO;
834 }
835
836 err2 = libxfs_blkdev_issue_flush(btp);
837 if (err2) {
838 fprintf(stderr,
839_("%s: Flushing the %s failed, err=%d!\n"),
840 progname, buftarg_descr, -err2);
841 }
842 if (!error)
843 error = err2;
844
845 return error;
846}
847
848/*
849 * Flush all dirty buffers to stable storage and report on writes that didn't
850 * make it to stable storage.
851 */
852static int
853libxfs_flush_mount(
854 struct xfs_mount *mp)
855{
856 int error = 0;
857 int err2;
858
859 /*
860 * Purge the buffer cache to write all dirty buffers to disk and free
861 * all incore buffers. Buffers that fail write verification will cause
862 * the CORRUPT_WRITE flag to be set in the buftarg. Buffers that
863 * cannot be written will cause the LOST_WRITE flag to be set in the
864 * buftarg.
865 */
866 libxfs_bcache_purge();
867
868 /* Flush all kernel and disk write caches, and report failures. */
869 if (mp->m_ddev_targp) {
870 err2 = libxfs_flush_buftarg(mp->m_ddev_targp, _("data device"));
871 if (!error)
872 error = err2;
873 }
874
875 if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp) {
876 err2 = libxfs_flush_buftarg(mp->m_logdev_targp,
877 _("log device"));
878 if (!error)
879 error = err2;
880 }
881
882 if (mp->m_rtdev_targp) {
883 err2 = libxfs_flush_buftarg(mp->m_rtdev_targp,
884 _("realtime device"));
885 if (!error)
886 error = err2;
887 }
888
889 return error;
890}
891
2bd0ea18 892/*
9440d84d 893 * Release any resource obtained during a mount.
2bd0ea18 894 */
c335b673
DW
895int
896libxfs_umount(
897 struct xfs_mount *mp)
2bd0ea18 898{
56b2de80
DC
899 struct xfs_perag *pag;
900 int agno;
c335b673 901 int error;
56b2de80 902
f1b058f9 903 libxfs_rtmount_destroy(mp);
c335b673
DW
904
905 error = libxfs_flush_mount(mp);
f1b058f9 906
56b2de80
DC
907 for (agno = 0; agno < mp->m_maxagi; agno++) {
908 pag = radix_tree_delete(&mp->m_perag_tree, agno);
909 kmem_free(pag);
fceb0d99 910 }
4334e2e8
ES
911
912 kmem_free(mp->m_attr_geo);
913 kmem_free(mp->m_dir_geo);
914
915 kmem_free(mp->m_rtdev_targp);
916 if (mp->m_logdev_targp != mp->m_ddev_targp)
917 kmem_free(mp->m_logdev_targp);
918 kmem_free(mp->m_ddev_targp);
f8149110 919
c335b673 920 return error;
2bd0ea18 921}
f1b058f9
NS
922
923/*
924 * Release any global resources used by libxfs.
925 */
926void
a9468486
DW
927libxfs_destroy(
928 struct libxfs_xinit *li)
f1b058f9 929{
a9468486
DW
930 int leaked;
931
932 libxfs_close_devices(li);
44488491 933
864028ed
ES
934 /* Free everything from the buffer cache before freeing buffer zone */
935 libxfs_bcache_purge();
936 libxfs_bcache_free();
f1b058f9 937 cache_destroy(libxfs_bcache);
7a326ce0 938 leaked = destroy_zones();
44488491
ES
939 if (getenv("LIBXFS_LEAK_CHECK") && leaked)
940 exit(1);
f1b058f9 941}
9f38f08d 942
b74a1f6a
NS
943int
944libxfs_device_alignment(void)
945{
946 return platform_align_blockdev();
947}
948
9f38f08d 949void
b6281496 950libxfs_report(FILE *fp)
9f38f08d 951{
cb5b3ef4
MV
952 time_t t;
953 char *c;
954
b6281496 955 cache_report(fp, "libxfs_bcache", libxfs_bcache);
cb5b3ef4
MV
956
957 t = time(NULL);
958 c = asctime(localtime(&t));
959 fprintf(fp, "%s", c);
960}