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