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