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