]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/init.c
mkfs: use geometry generation / helper functions
[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 int manage_zones(int); /* setup/teardown global zones */
47
48 /*
49 * dev_map - map open devices to fd.
50 */
51 #define MAX_DEVS 10 /* arbitary maximum */
52 int nextfakedev = -1; /* device number to give to next fake device */
53 static struct dev_to_fd {
54 dev_t dev;
55 int fd;
56 } dev_map[MAX_DEVS]={{0}};
57
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).
62 *
63 * Useful to tools which will produce uncertain results
64 * if the filesystem is active - repair, check, logprint.
65 */
66 static int
67 check_isactive(char *name, char *block, int fatal)
68 {
69 struct stat st;
70
71 if (stat(block, &st) < 0)
72 return 0;
73 if ((st.st_mode & S_IFMT) != S_IFBLK)
74 return 0;
75 if (platform_check_ismounted(name, block, &st, 0) == 0)
76 return 0;
77 if (platform_check_iswritable(name, block, &st))
78 return fatal ? 1 : 0;
79 return 0;
80 }
81
82 /* libxfs_device_to_fd:
83 * lookup a device number in the device map
84 * return the associated fd
85 */
86 int
87 libxfs_device_to_fd(dev_t device)
88 {
89 int d;
90
91 for (d = 0; d < MAX_DEVS; d++)
92 if (dev_map[d].dev == device)
93 return dev_map[d].fd;
94
95 fprintf(stderr, _("%s: %s: device %lld is not open\n"),
96 progname, __FUNCTION__, (long long)device);
97 exit(1);
98 /* NOTREACHED */
99 }
100
101 /* libxfs_device_open:
102 * open a device and return its device number
103 */
104 dev_t
105 libxfs_device_open(char *path, int creat, int xflags, int setblksize)
106 {
107 dev_t dev;
108 int fd, d, flags;
109 int readonly, dio, excl;
110 struct stat statb;
111
112 readonly = (xflags & LIBXFS_ISREADONLY);
113 excl = (xflags & LIBXFS_EXCLUSIVELY) && !creat;
114 dio = (xflags & LIBXFS_DIRECT) && !creat && platform_direct_blockdev();
115
116 retry:
117 flags = (readonly ? O_RDONLY : O_RDWR) | \
118 (creat ? (O_CREAT|O_TRUNC) : 0) | \
119 (dio ? O_DIRECT : 0) | \
120 (excl ? O_EXCL : 0);
121
122 if ((fd = open(path, flags, 0666)) < 0) {
123 if (errno == EINVAL && --dio == 0)
124 goto retry;
125 fprintf(stderr, _("%s: cannot open %s: %s\n"),
126 progname, path, strerror(errno));
127 exit(1);
128 }
129
130 if (fstat(fd, &statb) < 0) {
131 fprintf(stderr, _("%s: cannot stat %s: %s\n"),
132 progname, path, strerror(errno));
133 exit(1);
134 }
135
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 }
146
147 /*
148 * Get the device number from the stat buf - unless
149 * we're not opening a real device, in which case
150 * choose a new fake device number.
151 */
152 dev = (statb.st_rdev) ? (statb.st_rdev) : (nextfakedev--);
153
154 for (d = 0; d < MAX_DEVS; d++)
155 if (dev_map[d].dev == dev) {
156 fprintf(stderr, _("%s: device %lld is already open\n"),
157 progname, (long long)dev);
158 exit(1);
159 }
160
161 for (d = 0; d < MAX_DEVS; d++)
162 if (!dev_map[d].dev) {
163 dev_map[d].dev = dev;
164 dev_map[d].fd = fd;
165
166 return dev;
167 }
168
169 fprintf(stderr, _("%s: %s: too many open devices\n"),
170 progname, __FUNCTION__);
171 exit(1);
172 /* NOTREACHED */
173 }
174
175 void
176 libxfs_device_close(dev_t dev)
177 {
178 int d;
179
180 for (d = 0; d < MAX_DEVS; d++)
181 if (dev_map[d].dev == dev) {
182 int fd;
183
184 fd = dev_map[d].fd;
185 dev_map[d].dev = dev_map[d].fd = 0;
186
187 fsync(fd);
188 platform_flush_device(fd, dev);
189 close(fd);
190
191 return;
192 }
193
194 fprintf(stderr, _("%s: %s: device %lld is not open\n"),
195 progname, __FUNCTION__, (long long)dev);
196 exit(1);
197 }
198
199 static int
200 check_open(char *path, int flags, char **rawfile, char **blockfile)
201 {
202 int readonly = (flags & LIBXFS_ISREADONLY);
203 int inactive = (flags & LIBXFS_ISINACTIVE);
204 int dangerously = (flags & LIBXFS_DANGEROUSLY);
205 struct stat stbuf;
206
207 if (stat(path, &stbuf) < 0) {
208 perror(path);
209 return 0;
210 }
211 if (!(*rawfile = platform_findrawpath(path))) {
212 fprintf(stderr, _("%s: "
213 "can't find a character device matching %s\n"),
214 progname, path);
215 return 0;
216 }
217 if (!(*blockfile = platform_findblockpath(path))) {
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;
225
226 if (inactive && check_isactive(path, *blockfile, ((readonly|dangerously)?1:0)))
227 return 0;
228
229 return 1;
230 }
231
232 /*
233 * libxfs initialization.
234 * Caller gets a 0 on failure (and we print a message), 1 on success.
235 */
236 int
237 libxfs_init(libxfs_init_t *a)
238 {
239 char *blockfile;
240 char *dname;
241 char dpath[25];
242 int fd;
243 char *logname;
244 char logpath[25];
245 char *rawfile;
246 char *rtname;
247 char rtpath[25];
248 int rval = 0;
249 int flags;
250
251 dpath[0] = logpath[0] = rtpath[0] = '\0';
252 dname = a->dname;
253 logname = a->logname;
254 rtname = a->rtname;
255 a->dfd = a->logfd = a->rtfd = -1;
256 a->ddev = a->logdev = a->rtdev = 0;
257 a->dsize = a->lbsize = a->rtbsize = 0;
258 a->dbsize = a->logBBsize = a->logBBstart = a->rtsize = 0;
259
260 fd = -1;
261 flags = (a->isreadonly | a->isdirect);
262
263 xfs_extent_free_init_defer_op();
264 xfs_rmap_update_init_defer_op();
265 xfs_refcount_update_init_defer_op();
266 xfs_bmap_update_init_defer_op();
267
268 radix_tree_init();
269
270 if (a->volname) {
271 if(!check_open(a->volname,flags,&rawfile,&blockfile))
272 goto done;
273 fd = open(rawfile, O_RDONLY);
274 dname = a->dname = a->volname;
275 a->volname = NULL;
276 }
277 if (dname) {
278 if (a->disfile) {
279 a->ddev= libxfs_device_open(dname, a->dcreat, flags,
280 a->setblksize);
281 a->dfd = libxfs_device_to_fd(a->ddev);
282 platform_findsizes(dname, a->dfd, &a->dsize,
283 &a->dbsize);
284 } else {
285 if (!check_open(dname, flags, &rawfile, &blockfile))
286 goto done;
287 a->ddev = libxfs_device_open(rawfile,
288 a->dcreat, flags, a->setblksize);
289 a->dfd = libxfs_device_to_fd(a->ddev);
290 platform_findsizes(rawfile, a->dfd,
291 &a->dsize, &a->dbsize);
292 }
293 } else
294 a->dsize = 0;
295 if (logname) {
296 if (a->lisfile) {
297 a->logdev = libxfs_device_open(logname,
298 a->lcreat, flags, a->setblksize);
299 a->logfd = libxfs_device_to_fd(a->logdev);
300 platform_findsizes(dname, a->logfd, &a->logBBsize,
301 &a->lbsize);
302 } else {
303 if (!check_open(logname, flags, &rawfile, &blockfile))
304 goto done;
305 a->logdev = libxfs_device_open(rawfile,
306 a->lcreat, flags, a->setblksize);
307 a->logfd = libxfs_device_to_fd(a->logdev);
308 platform_findsizes(rawfile, a->logfd,
309 &a->logBBsize, &a->lbsize);
310 }
311 } else
312 a->logBBsize = 0;
313 if (rtname) {
314 if (a->risfile) {
315 a->rtdev = libxfs_device_open(rtname,
316 a->rcreat, flags, a->setblksize);
317 a->rtfd = libxfs_device_to_fd(a->rtdev);
318 platform_findsizes(dname, a->rtfd, &a->rtsize,
319 &a->rtbsize);
320 } else {
321 if (!check_open(rtname, flags, &rawfile, &blockfile))
322 goto done;
323 a->rtdev = libxfs_device_open(rawfile,
324 a->rcreat, flags, a->setblksize);
325 a->rtfd = libxfs_device_to_fd(a->rtdev);
326 platform_findsizes(rawfile, a->rtfd,
327 &a->rtsize, &a->rtbsize);
328 }
329 } else
330 a->rtsize = 0;
331 if (a->dsize < 0) {
332 fprintf(stderr, _("%s: can't get size for data subvolume\n"),
333 progname);
334 goto done;
335 }
336 if (a->logBBsize < 0) {
337 fprintf(stderr, _("%s: can't get size for log subvolume\n"),
338 progname);
339 goto done;
340 }
341 if (a->rtsize < 0) {
342 fprintf(stderr, _("%s: can't get size for realtime subvolume\n"),
343 progname);
344 goto done;
345 }
346 if (!libxfs_bhash_size)
347 libxfs_bhash_size = LIBXFS_BHASHSIZE(sbp);
348 libxfs_bcache = cache_init(a->bcache_flags, libxfs_bhash_size,
349 &libxfs_bcache_operations);
350 use_xfs_buf_lock = a->usebuflock;
351 manage_zones(0);
352 rval = 1;
353 done:
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 */
375 static int
376 manage_zones(int release)
377 {
378 extern kmem_zone_t *xfs_buf_zone;
379 extern kmem_zone_t *xfs_ili_zone;
380 extern kmem_zone_t *xfs_inode_zone;
381 extern kmem_zone_t *xfs_ifork_zone;
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;
386 extern kmem_zone_t *xfs_trans_zone;
387 extern kmem_zone_t *xfs_log_item_desc_zone;
388 extern void xfs_dir_startup();
389
390 if (release) { /* free zone allocation */
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;
405 }
406 /* otherwise initialise zone allocation */
407 xfs_buf_zone = kmem_zone_init(sizeof(xfs_buf_t), "xfs_buffer");
408 xfs_inode_zone = kmem_zone_init(sizeof(struct xfs_inode), "xfs_inode");
409 xfs_ifork_zone = kmem_zone_init(sizeof(xfs_ifork_t), "xfs_ifork");
410 xfs_ili_zone = kmem_zone_init(
411 sizeof(xfs_inode_log_item_t), "xfs_inode_log_item");
412 xfs_buf_item_zone = kmem_zone_init(
413 sizeof(xfs_buf_log_item_t), "xfs_buf_log_item");
414 xfs_da_state_zone = kmem_zone_init(
415 sizeof(xfs_da_state_t), "xfs_da_state");
416 xfs_btree_cur_zone = kmem_zone_init(
417 sizeof(xfs_btree_cur_t), "xfs_btree_cur");
418 xfs_bmap_free_item_zone = kmem_zone_init(
419 sizeof(struct xfs_extent_free_item),
420 "xfs_bmap_free_item");
421 xfs_trans_zone = kmem_zone_init(
422 sizeof(struct xfs_trans), "xfs_trans");
423 xfs_log_item_desc_zone = kmem_zone_init(
424 sizeof(struct xfs_log_item_desc), "xfs_log_item_desc");
425 xfs_dir_startup();
426
427 return 0;
428 }
429
430 /*
431 * Initialize realtime fields in the mount structure.
432 */
433 static int
434 rtmount_init(
435 xfs_mount_t *mp, /* file system mount structure */
436 int flags)
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;
445 if (mp->m_rtdev_targp->dev == 0 && !(flags & LIBXFS_MOUNT_DEBUGGER)) {
446 fprintf(stderr, _("%s: filesystem has a realtime subvolume\n"),
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;
456
457 /*
458 * Allow debugger to be run without the realtime device present.
459 */
460 if (flags & LIBXFS_MOUNT_DEBUGGER)
461 return 0;
462
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) {
468 fprintf(stderr, _("%s: realtime init - %llu != %llu\n"),
469 progname, (unsigned long long) XFS_BB_TO_FSB(mp, d),
470 (unsigned long long) mp->m_sb.sb_rblocks);
471 return -1;
472 }
473 bp = libxfs_readbuf(mp->m_rtdev,
474 d - XFS_FSB_TO_BB(mp, 1), XFS_FSB_TO_BB(mp, 1), 0, NULL);
475 if (bp == NULL) {
476 fprintf(stderr, _("%s: realtime size check failed\n"),
477 progname);
478 return -1;
479 }
480 libxfs_putbuf(bp);
481 return 0;
482 }
483
484 static int
485 libxfs_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) {
542 uint64_t icount;
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;
576
577 mp->m_ag_prealloc_blocks = xfs_prealloc_blocks(mp);
578 return 0;
579
580 out_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
589 static struct xfs_buftarg *
590 libxfs_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
607 void
608 libxfs_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
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 */
660 xfs_mount_t *
661 libxfs_mount(
662 xfs_mount_t *mp,
663 xfs_sb_t *sb,
664 dev_t dev,
665 dev_t logdev,
666 dev_t rtdev,
667 int flags)
668 {
669 xfs_daddr_t d;
670 xfs_buf_t *bp;
671 xfs_sb_t *sbp;
672 int error;
673
674 libxfs_buftarg_init(mp, dev, logdev, rtdev);
675
676 mp->m_flags = (LIBXFS_MOUNT_32BITINODES|LIBXFS_MOUNT_32BITINOOPT);
677 mp->m_sb = *sb;
678 INIT_RADIX_TREE(&mp->m_perag_tree, GFP_KERNEL);
679 sbp = &(mp->m_sb);
680
681 xfs_sb_mount_common(mp, sb);
682
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);
687 xfs_rmapbt_compute_maxlevels(mp);
688 xfs_refcountbt_compute_maxlevels(mp);
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;
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 }
708
709 /*
710 * Set whether we're using stripe alignment.
711 */
712 if (xfs_sb_version_hasdalign(&mp->m_sb)) {
713 mp->m_dalign = sbp->sb_unit;
714 mp->m_swidth = sbp->sb_width;
715 }
716
717 /*
718 * Set whether we're using inode alignment.
719 */
720 if (xfs_sb_version_hasalign(&mp->m_sb) &&
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 */
730 if (mp->m_dalign && mp->m_inoalign_mask &&
731 !(mp->m_dalign & mp->m_inoalign_mask))
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 */
739 d = (xfs_daddr_t) XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
740 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
741 fprintf(stderr, _("%s: size check failed\n"), progname);
742 if (!(flags & LIBXFS_MOUNT_DEBUGGER))
743 return NULL;
744 }
745
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)) {
760
761 fprintf(stderr, _(
762 "%s: V1 directories unsupported. Please try an older xfsprogs.\n"),
763 progname);
764 exit(1);
765 }
766
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
777 if (xfs_sb_version_hasattr2(&mp->m_sb))
778 mp->m_flags |= LIBXFS_MOUNT_ATTR2;
779
780 /* Initialize the precomputed transaction reservations values */
781 xfs_trans_init(mp);
782
783 if (dev == 0) /* maxtrres, we have no device so leave now */
784 return mp;
785
786 bp = libxfs_readbuf(mp->m_dev,
787 d - XFS_FSS_TO_BB(mp, 1), XFS_FSS_TO_BB(mp, 1),
788 !(flags & LIBXFS_MOUNT_DEBUGGER), NULL);
789 if (!bp) {
790 fprintf(stderr, _("%s: data size check failed\n"), progname);
791 if (!(flags & LIBXFS_MOUNT_DEBUGGER))
792 return NULL;
793 } else
794 libxfs_putbuf(bp);
795
796 if (mp->m_logdev_targp->dev &&
797 mp->m_logdev_targp->dev != mp->m_ddev_targp->dev) {
798 d = (xfs_daddr_t) XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
799 if ( (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) ||
800 (!(bp = libxfs_readbuf(mp->m_logdev_targp,
801 d - XFS_FSB_TO_BB(mp, 1),
802 XFS_FSB_TO_BB(mp, 1),
803 !(flags & LIBXFS_MOUNT_DEBUGGER), NULL))) ) {
804 fprintf(stderr, _("%s: log size checks failed\n"),
805 progname);
806 if (!(flags & LIBXFS_MOUNT_DEBUGGER))
807 return NULL;
808 }
809 if (bp)
810 libxfs_putbuf(bp);
811 }
812
813 /* Initialize realtime fields in the mount structure */
814 if (rtmount_init(mp, flags)) {
815 fprintf(stderr, _("%s: realtime device init failed\n"),
816 progname);
817 return NULL;
818 }
819
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
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);
847 exit(1);
848 }
849
850 return mp;
851 }
852
853 void
854 libxfs_rtmount_destroy(xfs_mount_t *mp)
855 {
856 if (mp->m_rsumip)
857 IRELE(mp->m_rsumip);
858 if (mp->m_rbmip)
859 IRELE(mp->m_rbmip);
860 mp->m_rsumip = mp->m_rbmip = NULL;
861 }
862
863 /*
864 * Release any resource obtained during a mount.
865 */
866 void
867 libxfs_umount(xfs_mount_t *mp)
868 {
869 struct xfs_perag *pag;
870 int agno;
871
872 libxfs_rtmount_destroy(mp);
873 libxfs_bcache_purge();
874
875 for (agno = 0; agno < mp->m_maxagi; agno++) {
876 pag = radix_tree_delete(&mp->m_perag_tree, agno);
877 kmem_free(pag);
878 }
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);
887
888 }
889
890 /*
891 * Release any global resources used by libxfs.
892 */
893 void
894 libxfs_destroy(void)
895 {
896 int leaked;
897
898 /* Free everything from the buffer cache before freeing buffer zone */
899 libxfs_bcache_purge();
900 libxfs_bcache_free();
901 cache_destroy(libxfs_bcache);
902 leaked = manage_zones(1);
903 if (getenv("LIBXFS_LEAK_CHECK") && leaked)
904 exit(1);
905 }
906
907 int
908 libxfs_device_alignment(void)
909 {
910 return platform_align_blockdev();
911 }
912
913 void
914 libxfs_report(FILE *fp)
915 {
916 time_t t;
917 char *c;
918
919 cache_report(fp, "libxfs_bcache", libxfs_bcache);
920
921 t = time(NULL);
922 c = asctime(localtime(&t));
923 fprintf(fp, "%s", c);
924 }
925
926 int
927 libxfs_nproc(void)
928 {
929 int nr;
930
931 nr = platform_nproc();
932 if (nr < 1)
933 nr = 1;
934 return nr;
935 }
936
937 unsigned long
938 libxfs_physmem(void)
939 {
940 return platform_physmem();
941 }