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