]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/init.c
mkfs: set agblklog when we're verifying minimum log size
[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
f1b058f9
NS
46static void manage_zones(int); /* setup global zones */
47
1d519883
DC
48kmem_zone_t *xfs_inode_zone;
49
2bd0ea18
NS
50/*
51 * dev_map - map open devices to fd.
52 */
53#define MAX_DEVS 10 /* arbitary maximum */
54int nextfakedev = -1; /* device number to give to next fake device */
55static struct dev_to_fd {
9440d84d
NS
56 dev_t dev;
57 int fd;
2bd0ea18
NS
58} dev_map[MAX_DEVS]={{0}};
59
2bd0ea18
NS
60/*
61 * Checks whether a given device has a mounted, writable
62 * filesystem, returns 1 if it does & fatal (just warns
63 * if not fatal, but allows us to proceed).
5000d01d 64 *
2bd0ea18
NS
65 * Useful to tools which will produce uncertain results
66 * if the filesystem is active - repair, check, logprint.
67 */
68static int
69check_isactive(char *name, char *block, int fatal)
70{
f594a0d1 71 struct stat st;
2bd0ea18 72
f594a0d1 73 if (stat(block, &st) < 0)
9440d84d 74 return 0;
fc8202ba 75 if ((st.st_mode & S_IFMT) != S_IFBLK)
9440d84d 76 return 0;
93d9f139 77 if (platform_check_ismounted(name, block, &st, 0) == 0)
9440d84d 78 return 0;
7f510afb
ES
79 if (platform_check_iswritable(name, block, &st))
80 return fatal ? 1 : 0;
81 return 0;
2bd0ea18
NS
82}
83
5000d01d 84/* libxfs_device_to_fd:
2bd0ea18
NS
85 * lookup a device number in the device map
86 * return the associated fd
87 */
88int
89libxfs_device_to_fd(dev_t device)
90{
9440d84d 91 int d;
5000d01d 92
9440d84d 93 for (d = 0; d < MAX_DEVS; d++)
5000d01d 94 if (dev_map[d].dev == device)
2bd0ea18 95 return dev_map[d].fd;
5000d01d 96
9440d84d
NS
97 fprintf(stderr, _("%s: %s: device %lld is not open\n"),
98 progname, __FUNCTION__, (long long)device);
2bd0ea18 99 exit(1);
25d246df 100 /* NOTREACHED */
2bd0ea18
NS
101}
102
103/* libxfs_device_open:
104 * open a device and return its device number
105 */
106dev_t
7eb6693f 107libxfs_device_open(char *path, int creat, int xflags, int setblksize)
2bd0ea18 108{
2bd0ea18 109 dev_t dev;
7eb6693f 110 int fd, d, flags;
b74a1f6a 111 int readonly, dio, excl;
f594a0d1 112 struct stat statb;
7eb6693f
NS
113
114 readonly = (xflags & LIBXFS_ISREADONLY);
115 excl = (xflags & LIBXFS_EXCLUSIVELY) && !creat;
b74a1f6a 116 dio = (xflags & LIBXFS_DIRECT) && !creat && platform_direct_blockdev();
2bd0ea18 117
b74a1f6a 118retry:
7eb6693f
NS
119 flags = (readonly ? O_RDONLY : O_RDWR) | \
120 (creat ? (O_CREAT|O_TRUNC) : 0) | \
b74a1f6a 121 (dio ? O_DIRECT : 0) | \
7eb6693f 122 (excl ? O_EXCL : 0);
b74a1f6a 123
7eb6693f 124 if ((fd = open(path, flags, 0666)) < 0) {
b74a1f6a
NS
125 if (errno == EINVAL && --dio == 0)
126 goto retry;
9440d84d 127 fprintf(stderr, _("%s: cannot open %s: %s\n"),
2bd0ea18
NS
128 progname, path, strerror(errno));
129 exit(1);
130 }
131
f594a0d1 132 if (fstat(fd, &statb) < 0) {
9440d84d 133 fprintf(stderr, _("%s: cannot stat %s: %s\n"),
2bd0ea18
NS
134 progname, path, strerror(errno));
135 exit(1);
136 }
a33a9e62 137
edd45774
TS
138 if (!readonly && setblksize && (statb.st_mode & S_IFMT) == S_IFBLK) {
139 if (setblksize == 1)
140 /* use the default blocksize */
141 (void)platform_set_blocksize(fd, path, statb.st_rdev, XFS_MIN_SECTORSIZE, 0);
142 else {
143 /* given an explicit blocksize to use */
144 if (platform_set_blocksize(fd, path, statb.st_rdev, setblksize, 1))
145 exit(1);
146 }
147 }
2bd0ea18 148
a33a9e62
NS
149 /*
150 * Get the device number from the stat buf - unless
2bd0ea18 151 * we're not opening a real device, in which case
a33a9e62 152 * choose a new fake device number.
2bd0ea18 153 */
32181a02 154 dev = (statb.st_rdev) ? (statb.st_rdev) : (nextfakedev--);
2bd0ea18 155
32181a02 156 for (d = 0; d < MAX_DEVS; d++)
2bd0ea18 157 if (dev_map[d].dev == dev) {
9440d84d 158 fprintf(stderr, _("%s: device %lld is already open\n"),
5b64e00a 159 progname, (long long)dev);
2bd0ea18
NS
160 exit(1);
161 }
162
32181a02 163 for (d = 0; d < MAX_DEVS; d++)
2bd0ea18 164 if (!dev_map[d].dev) {
32181a02
NS
165 dev_map[d].dev = dev;
166 dev_map[d].fd = fd;
5000d01d 167
2bd0ea18
NS
168 return dev;
169 }
170
9440d84d
NS
171 fprintf(stderr, _("%s: %s: too many open devices\n"),
172 progname, __FUNCTION__);
2bd0ea18 173 exit(1);
25d246df 174 /* NOTREACHED */
2bd0ea18
NS
175}
176
177void
178libxfs_device_close(dev_t dev)
179{
5000d01d 180 int d;
2bd0ea18 181
32181a02 182 for (d = 0; d < MAX_DEVS; d++)
2bd0ea18 183 if (dev_map[d].dev == dev) {
32181a02 184 int fd;
5000d01d 185
32181a02
NS
186 fd = dev_map[d].fd;
187 dev_map[d].dev = dev_map[d].fd = 0;
5000d01d 188
2bd0ea18 189 fsync(fd);
ac419944 190 platform_flush_device(fd, dev);
2bd0ea18 191 close(fd);
5000d01d 192
2bd0ea18
NS
193 return;
194 }
195
9440d84d
NS
196 fprintf(stderr, _("%s: %s: device %lld is not open\n"),
197 progname, __FUNCTION__, (long long)dev);
2bd0ea18
NS
198 exit(1);
199}
200
74668075
NS
201static int
202check_open(char *path, int flags, char **rawfile, char **blockfile)
203{
c781939c
RC
204 int readonly = (flags & LIBXFS_ISREADONLY);
205 int inactive = (flags & LIBXFS_ISINACTIVE);
206 int dangerously = (flags & LIBXFS_DANGEROUSLY);
f594a0d1 207 struct stat stbuf;
c781939c 208
f594a0d1 209 if (stat(path, &stbuf) < 0) {
c781939c
RC
210 perror(path);
211 return 0;
212 }
b74a1f6a 213 if (!(*rawfile = platform_findrawpath(path))) {
c781939c
RC
214 fprintf(stderr, _("%s: "
215 "can't find a character device matching %s\n"),
216 progname, path);
217 return 0;
218 }
b74a1f6a 219 if (!(*blockfile = platform_findblockpath(path))) {
c781939c
RC
220 fprintf(stderr, _("%s: "
221 "can't find a block device matching %s\n"),
222 progname, path);
223 return 0;
224 }
225 if (!readonly && !inactive && platform_check_ismounted(path, *blockfile, NULL, 1))
226 return 0;
e08f5594 227
c781939c
RC
228 if (inactive && check_isactive(path, *blockfile, ((readonly|dangerously)?1:0)))
229 return 0;
230
231 return 1;
232}
233
2bd0ea18
NS
234/*
235 * libxfs initialization.
236 * Caller gets a 0 on failure (and we print a message), 1 on success.
237 */
238int
239libxfs_init(libxfs_init_t *a)
240{
241 char *blockfile;
242 char curdir[MAXPATHLEN];
243 char *dname;
244 char dpath[25];
245 int fd;
246 char *logname;
247 char logpath[25];
248 int needcd;
249 char *rawfile;
250 char *rtname;
251 char rtpath[25];
252 int rval = 0;
c781939c 253 int flags;
2bd0ea18
NS
254
255 dpath[0] = logpath[0] = rtpath[0] = '\0';
256 dname = a->dname;
257 logname = a->logname;
258 rtname = a->rtname;
2bd0ea18 259 a->dfd = a->logfd = a->rtfd = -1;
74668075 260 a->ddev = a->logdev = a->rtdev = 0;
06ac92fd
DC
261 a->dsize = a->lbsize = a->rtbsize = 0;
262 a->dbsize = a->logBBsize = a->logBBstart = a->rtsize = 0;
2bd0ea18
NS
263
264 (void)getcwd(curdir,MAXPATHLEN);
265 needcd = 0;
266 fd = -1;
b74a1f6a 267 flags = (a->isreadonly | a->isdirect);
c781939c 268
dc3bce02
DW
269 xfs_extent_free_init_defer_op();
270 xfs_rmap_update_init_defer_op();
cdc9cf25 271 xfs_refcount_update_init_defer_op();
f8f8c8a0 272 xfs_bmap_update_init_defer_op();
dc3bce02 273
bacd44a5
AE
274 radix_tree_init();
275
2bd0ea18 276 if (a->volname) {
c781939c 277 if(!check_open(a->volname,flags,&rawfile,&blockfile))
2bd0ea18
NS
278 goto done;
279 needcd = 1;
280 fd = open(rawfile, O_RDONLY);
eb37fca5
NS
281 dname = a->dname = a->volname;
282 a->volname = NULL;
2bd0ea18 283 }
2bd0ea18
NS
284 if (dname) {
285 if (dname[0] != '/' && needcd)
286 chdir(curdir);
287 if (a->disfile) {
7eb6693f 288 a->ddev= libxfs_device_open(dname, a->dcreat, flags,
c5907b96 289 a->setblksize);
2bd0ea18 290 a->dfd = libxfs_device_to_fd(a->ddev);
06ac92fd
DC
291 platform_findsizes(dname, a->dfd, &a->dsize,
292 &a->dbsize);
2bd0ea18 293 } else {
f02037ce 294 if (!check_open(dname, flags, &rawfile, &blockfile))
2bd0ea18
NS
295 goto done;
296 a->ddev = libxfs_device_open(rawfile,
7eb6693f 297 a->dcreat, flags, a->setblksize);
2bd0ea18 298 a->dfd = libxfs_device_to_fd(a->ddev);
f02037ce 299 platform_findsizes(rawfile, a->dfd,
06ac92fd 300 &a->dsize, &a->dbsize);
2bd0ea18
NS
301 }
302 needcd = 1;
303 } else
304 a->dsize = 0;
305 if (logname) {
306 if (logname[0] != '/' && needcd)
307 chdir(curdir);
308 if (a->lisfile) {
309 a->logdev = libxfs_device_open(logname,
7eb6693f 310 a->lcreat, flags, a->setblksize);
2bd0ea18 311 a->logfd = libxfs_device_to_fd(a->logdev);
06ac92fd
DC
312 platform_findsizes(dname, a->logfd, &a->logBBsize,
313 &a->lbsize);
2bd0ea18 314 } else {
f02037ce 315 if (!check_open(logname, flags, &rawfile, &blockfile))
2bd0ea18
NS
316 goto done;
317 a->logdev = libxfs_device_open(rawfile,
7eb6693f 318 a->lcreat, flags, a->setblksize);
2bd0ea18 319 a->logfd = libxfs_device_to_fd(a->logdev);
f02037ce 320 platform_findsizes(rawfile, a->logfd,
06ac92fd 321 &a->logBBsize, &a->lbsize);
2bd0ea18
NS
322 }
323 needcd = 1;
324 } else
325 a->logBBsize = 0;
326 if (rtname) {
327 if (rtname[0] != '/' && needcd)
328 chdir(curdir);
329 if (a->risfile) {
330 a->rtdev = libxfs_device_open(rtname,
7eb6693f 331 a->rcreat, flags, a->setblksize);
2bd0ea18 332 a->rtfd = libxfs_device_to_fd(a->rtdev);
06ac92fd
DC
333 platform_findsizes(dname, a->rtfd, &a->rtsize,
334 &a->rtbsize);
2bd0ea18 335 } else {
f02037ce 336 if (!check_open(rtname, flags, &rawfile, &blockfile))
2bd0ea18
NS
337 goto done;
338 a->rtdev = libxfs_device_open(rawfile,
7eb6693f 339 a->rcreat, flags, a->setblksize);
2bd0ea18 340 a->rtfd = libxfs_device_to_fd(a->rtdev);
f02037ce 341 platform_findsizes(rawfile, a->rtfd,
06ac92fd 342 &a->rtsize, &a->rtbsize);
2bd0ea18
NS
343 }
344 needcd = 1;
345 } else
346 a->rtsize = 0;
347 if (a->dsize < 0) {
9440d84d 348 fprintf(stderr, _("%s: can't get size for data subvolume\n"),
2bd0ea18
NS
349 progname);
350 goto done;
351 }
352 if (a->logBBsize < 0) {
9440d84d 353 fprintf(stderr, _("%s: can't get size for log subvolume\n"),
2bd0ea18
NS
354 progname);
355 goto done;
356 }
357 if (a->rtsize < 0) {
9440d84d 358 fprintf(stderr, _("%s: can't get size for realtime subvolume\n"),
2bd0ea18
NS
359 progname);
360 goto done;
361 }
362 if (needcd)
363 chdir(curdir);
9f38f08d
MV
364 if (!libxfs_bhash_size)
365 libxfs_bhash_size = LIBXFS_BHASHSIZE(sbp);
ba9ecd40
DC
366 libxfs_bcache = cache_init(a->bcache_flags, libxfs_bhash_size,
367 &libxfs_bcache_operations);
d0572de5 368 use_xfs_buf_lock = a->usebuflock;
f1b058f9 369 manage_zones(0);
2bd0ea18
NS
370 rval = 1;
371done:
372 if (dpath[0])
373 unlink(dpath);
374 if (logpath[0])
375 unlink(logpath);
376 if (rtpath[0])
377 unlink(rtpath);
378 if (fd >= 0)
379 close(fd);
380 if (!rval && a->ddev)
381 libxfs_device_close(a->ddev);
382 if (!rval && a->logdev)
383 libxfs_device_close(a->logdev);
384 if (!rval && a->rtdev)
385 libxfs_device_close(a->rtdev);
386 return rval;
387}
388
389
390/*
391 * Initialize/destroy all of the zone allocators we use.
392 */
393static void
394manage_zones(int release)
395{
5e656dbb
BN
396 extern kmem_zone_t *xfs_buf_zone;
397 extern kmem_zone_t *xfs_ili_zone;
5e656dbb 398 extern kmem_zone_t *xfs_ifork_zone;
5e656dbb
BN
399 extern kmem_zone_t *xfs_buf_item_zone;
400 extern kmem_zone_t *xfs_da_state_zone;
401 extern kmem_zone_t *xfs_btree_cur_zone;
402 extern kmem_zone_t *xfs_bmap_free_item_zone;
56b2de80 403 extern kmem_zone_t *xfs_log_item_desc_zone;
2bd0ea18
NS
404 extern void xfs_dir_startup();
405
406 if (release) { /* free zone allocation */
5e656dbb
BN
407 kmem_free(xfs_buf_zone);
408 kmem_free(xfs_inode_zone);
409 kmem_free(xfs_ifork_zone);
5e656dbb
BN
410 kmem_free(xfs_buf_item_zone);
411 kmem_free(xfs_da_state_zone);
412 kmem_free(xfs_btree_cur_zone);
413 kmem_free(xfs_bmap_free_item_zone);
56b2de80 414 kmem_free(xfs_log_item_desc_zone);
2bd0ea18
NS
415 return;
416 }
417 /* otherwise initialise zone allocation */
5e656dbb 418 xfs_buf_zone = kmem_zone_init(sizeof(xfs_buf_t), "xfs_buffer");
b626fb59 419 xfs_inode_zone = kmem_zone_init(sizeof(struct xfs_inode), "xfs_inode");
5e656dbb 420 xfs_ifork_zone = kmem_zone_init(sizeof(xfs_ifork_t), "xfs_ifork");
5e656dbb 421 xfs_ili_zone = kmem_zone_init(
2bd0ea18 422 sizeof(xfs_inode_log_item_t), "xfs_inode_log_item");
5e656dbb 423 xfs_buf_item_zone = kmem_zone_init(
2bd0ea18 424 sizeof(xfs_buf_log_item_t), "xfs_buf_log_item");
5e656dbb 425 xfs_da_state_zone = kmem_zone_init(
2bd0ea18 426 sizeof(xfs_da_state_t), "xfs_da_state");
5e656dbb 427 xfs_btree_cur_zone = kmem_zone_init(
2bd0ea18 428 sizeof(xfs_btree_cur_t), "xfs_btree_cur");
5e656dbb 429 xfs_bmap_free_item_zone = kmem_zone_init(
6f530e9a
DW
430 sizeof(struct xfs_extent_free_item),
431 "xfs_bmap_free_item");
56b2de80
DC
432 xfs_log_item_desc_zone = kmem_zone_init(
433 sizeof(struct xfs_log_item_desc), "xfs_log_item_desc");
2bd0ea18
NS
434 xfs_dir_startup();
435}
436
b391b7cd
NS
437/*
438 * Initialize realtime fields in the mount structure.
439 */
440static int
441rtmount_init(
39798eb5
NS
442 xfs_mount_t *mp, /* file system mount structure */
443 int flags)
b391b7cd
NS
444{
445 xfs_buf_t *bp; /* buffer for last block of subvolume */
446 xfs_daddr_t d; /* address of last block of subvolume */
447 xfs_sb_t *sbp; /* filesystem superblock copy in mount */
448
449 sbp = &mp->m_sb;
450 if (sbp->sb_rblocks == 0)
451 return 0;
75c8b434 452 if (mp->m_rtdev_targp->dev == 0 && !(flags & LIBXFS_MOUNT_DEBUGGER)) {
9440d84d 453 fprintf(stderr, _("%s: filesystem has a realtime subvolume\n"),
b391b7cd
NS
454 progname);
455 return -1;
456 }
457 mp->m_rsumlevels = sbp->sb_rextslog + 1;
458 mp->m_rsumsize =
459 (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
460 sbp->sb_rbmblocks;
461 mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
462 mp->m_rbmip = mp->m_rsumip = NULL;
39798eb5
NS
463
464 /*
465 * Allow debugger to be run without the realtime device present.
466 */
467 if (flags & LIBXFS_MOUNT_DEBUGGER)
468 return 0;
469
b391b7cd
NS
470 /*
471 * Check that the realtime section is an ok size.
472 */
473 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
474 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
9440d84d
NS
475 fprintf(stderr, _("%s: realtime init - %llu != %llu\n"),
476 progname, (unsigned long long) XFS_BB_TO_FSB(mp, d),
b391b7cd
NS
477 (unsigned long long) mp->m_sb.sb_rblocks);
478 return -1;
479 }
9440d84d 480 bp = libxfs_readbuf(mp->m_rtdev,
75c8b434 481 d - XFS_FSB_TO_BB(mp, 1), XFS_FSB_TO_BB(mp, 1), 0, NULL);
b391b7cd 482 if (bp == NULL) {
9440d84d
NS
483 fprintf(stderr, _("%s: realtime size check failed\n"),
484 progname);
b391b7cd
NS
485 return -1;
486 }
487 libxfs_putbuf(bp);
488 return 0;
489}
490
56b2de80
DC
491static int
492libxfs_initialize_perag(
493 xfs_mount_t *mp,
494 xfs_agnumber_t agcount,
495 xfs_agnumber_t *maxagi)
496{
497 xfs_agnumber_t index, max_metadata;
498 xfs_agnumber_t first_initialised = 0;
499 xfs_perag_t *pag;
500 xfs_agino_t agino;
501 xfs_ino_t ino;
502 xfs_sb_t *sbp = &mp->m_sb;
503 int error = -ENOMEM;
504
505 /*
506 * Walk the current per-ag tree so we don't try to initialise AGs
507 * that already exist (growfs case). Allocate and insert all the
508 * AGs we don't find ready for initialisation.
509 */
510 for (index = 0; index < agcount; index++) {
511 pag = xfs_perag_get(mp, index);
512 if (pag) {
513 xfs_perag_put(pag);
514 continue;
515 }
516 if (!first_initialised)
517 first_initialised = index;
518
519 pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL);
520 if (!pag)
521 goto out_unwind;
522 pag->pag_agno = index;
523 pag->pag_mount = mp;
524
525 if (radix_tree_insert(&mp->m_perag_tree, index, pag)) {
526 error = -EEXIST;
527 goto out_unwind;
528 }
529 }
530
531 /*
532 * If we mount with the inode64 option, or no inode overflows
533 * the legacy 32-bit address space clear the inode32 option.
534 */
535 agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
536 ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
537
538 if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) && ino > XFS_MAXINUMBER_32)
539 mp->m_flags |= XFS_MOUNT_32BITINODES;
540 else
541 mp->m_flags &= ~XFS_MOUNT_32BITINODES;
542
543 if (mp->m_flags & XFS_MOUNT_32BITINODES) {
544 /*
545 * Calculate how much should be reserved for inodes to meet
546 * the max inode percentage.
547 */
548 if (mp->m_maxicount) {
549 __uint64_t icount;
550
551 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
552 do_div(icount, 100);
553 icount += sbp->sb_agblocks - 1;
554 do_div(icount, sbp->sb_agblocks);
555 max_metadata = icount;
556 } else {
557 max_metadata = agcount;
558 }
559
560 for (index = 0; index < agcount; index++) {
561 ino = XFS_AGINO_TO_INO(mp, index, agino);
562 if (ino > XFS_MAXINUMBER_32) {
563 index++;
564 break;
565 }
566
567 pag = xfs_perag_get(mp, index);
568 pag->pagi_inodeok = 1;
569 if (index < max_metadata)
570 pag->pagf_metadata = 1;
571 xfs_perag_put(pag);
572 }
573 } else {
574 for (index = 0; index < agcount; index++) {
575 pag = xfs_perag_get(mp, index);
576 pag->pagi_inodeok = 1;
577 xfs_perag_put(pag);
578 }
579 }
580
581 if (maxagi)
582 *maxagi = index;
ef5340cd
DW
583
584 mp->m_ag_prealloc_blocks = xfs_prealloc_blocks(mp);
56b2de80
DC
585 return 0;
586
587out_unwind:
588 kmem_free(pag);
589 for (; index > first_initialised; index--) {
590 pag = radix_tree_delete(&mp->m_perag_tree, index);
591 kmem_free(pag);
592 }
593 return error;
594}
595
75c8b434
DC
596static struct xfs_buftarg *
597libxfs_buftarg_alloc(
598 struct xfs_mount *mp,
599 dev_t dev)
600{
601 struct xfs_buftarg *btp;
602
603 btp = malloc(sizeof(*btp));
604 if (!btp) {
605 fprintf(stderr, _("%s: buftarg init failed\n"),
606 progname);
607 exit(1);
608 }
609 btp->bt_mount = mp;
610 btp->dev = dev;
611 return btp;
612}
613
614void
615libxfs_buftarg_init(
616 struct xfs_mount *mp,
617 dev_t dev,
618 dev_t logdev,
619 dev_t rtdev)
620{
621 if (mp->m_ddev_targp) {
622 /* should already have all buftargs initialised */
623 if (mp->m_ddev_targp->dev != dev ||
624 mp->m_ddev_targp->bt_mount != mp) {
625 fprintf(stderr,
626 _("%s: bad buftarg reinit, ddev\n"),
627 progname);
628 exit(1);
629 }
630 if (!logdev || logdev == dev) {
631 if (mp->m_logdev_targp != mp->m_ddev_targp) {
632 fprintf(stderr,
633 _("%s: bad buftarg reinit, ldev mismatch\n"),
634 progname);
635 exit(1);
636 }
637 } else if (mp->m_logdev_targp->dev != logdev ||
638 mp->m_logdev_targp->bt_mount != mp) {
639 fprintf(stderr,
640 _("%s: bad buftarg reinit, logdev\n"),
641 progname);
642 exit(1);
643 }
644 if (rtdev && (mp->m_rtdev_targp->dev != rtdev ||
645 mp->m_rtdev_targp->bt_mount != mp)) {
646 fprintf(stderr,
647 _("%s: bad buftarg reinit, rtdev\n"),
648 progname);
649 exit(1);
650 }
651 return;
652 }
653
654 mp->m_ddev_targp = libxfs_buftarg_alloc(mp, dev);
655 if (!logdev || logdev == dev)
656 mp->m_logdev_targp = mp->m_ddev_targp;
657 else
658 mp->m_logdev_targp = libxfs_buftarg_alloc(mp, logdev);
659 mp->m_rtdev_targp = libxfs_buftarg_alloc(mp, rtdev);
660}
661
2bd0ea18
NS
662/*
663 * Mount structure initialization, provides a filled-in xfs_mount_t
664 * such that the numerous XFS_* macros can be used. If dev is zero,
665 * no IO will be performed (no size checks, read root inodes).
666 */
667xfs_mount_t *
668libxfs_mount(
669 xfs_mount_t *mp,
670 xfs_sb_t *sb,
671 dev_t dev,
672 dev_t logdev,
673 dev_t rtdev,
4ca431fc 674 int flags)
2bd0ea18
NS
675{
676 xfs_daddr_t d;
677 xfs_buf_t *bp;
678 xfs_sb_t *sbp;
2bd0ea18
NS
679 int error;
680
75c8b434
DC
681 libxfs_buftarg_init(mp, dev, logdev, rtdev);
682
6239071d 683 mp->m_flags = (LIBXFS_MOUNT_32BITINODES|LIBXFS_MOUNT_32BITINOOPT);
2bd0ea18 684 mp->m_sb = *sb;
56b2de80 685 INIT_RADIX_TREE(&mp->m_perag_tree, GFP_KERNEL);
2bd0ea18 686 sbp = &(mp->m_sb);
2bd0ea18 687
4896e6c8 688 xfs_sb_mount_common(mp, sb);
2bd0ea18 689
5e656dbb
BN
690 xfs_alloc_compute_maxlevels(mp);
691 xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
692 xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
693 xfs_ialloc_compute_maxlevels(mp);
b3a96b46 694 xfs_rmapbt_compute_maxlevels(mp);
e7be6330 695 xfs_refcountbt_compute_maxlevels(mp);
2bd0ea18
NS
696
697 if (sbp->sb_imax_pct) {
698 /* Make sure the maximum inode count is a multiple of the
699 * units we allocate inodes in.
700 */
701 mp->m_maxicount = (sbp->sb_dblocks * sbp->sb_imax_pct) / 100;
702 mp->m_maxicount = ((mp->m_maxicount / mp->m_ialloc_blks) *
703 mp->m_ialloc_blks) << sbp->sb_inopblog;
704 } else
705 mp->m_maxicount = 0;
706
707 mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
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{
896 manage_zones(1);
f1b058f9
NS
897 cache_destroy(libxfs_bcache);
898}
9f38f08d 899
b74a1f6a
NS
900int
901libxfs_device_alignment(void)
902{
903 return platform_align_blockdev();
904}
905
9f38f08d 906void
b6281496 907libxfs_report(FILE *fp)
9f38f08d 908{
cb5b3ef4
MV
909 time_t t;
910 char *c;
911
b6281496 912 cache_report(fp, "libxfs_bcache", libxfs_bcache);
cb5b3ef4
MV
913
914 t = time(NULL);
915 c = asctime(localtime(&t));
916 fprintf(fp, "%s", c);
917}
3b6ac903
MV
918
919int
920libxfs_nproc(void)
921{
922 return platform_nproc();
923}
2556c98b
BN
924
925unsigned long
926libxfs_physmem(void)
927{
928 return platform_physmem();
e08f5594 929}