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