2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
33 #define ustat __kernel_ustat
40 #include <sys/ustat.h>
41 #include <sys/ioctl.h>
42 #include <sys/mount.h>
45 #define BLKBSZSET _IO(0x12,110) /* set device block size */
48 #define findrawpath(x) x
49 #define findblockpath(x) x
51 char *progname
= "libxfs"; /* default, changed by each tool */
54 * dev_map - map open devices to fd.
56 #define MAX_DEVS 10 /* arbitary maximum */
57 int nextfakedev
= -1; /* device number to give to next fake device */
58 static struct dev_to_fd
{
61 } dev_map
[MAX_DEVS
]={{0}};
64 check_ismounted(char *name
, char *block
, int verbose
)
69 if (stat64(block
, &st
) < 0)
71 if ((st
.st_mode
& S_IFMT
) != S_IFBLK
)
73 if (ustat(st
.st_rdev
, &ust
) >= 0) {
76 "%s: %s contains a mounted filesystem\n",
84 * Checks whether a given device has a mounted, writable
85 * filesystem, returns 1 if it does & fatal (just warns
86 * if not fatal, but allows us to proceed).
88 * Useful to tools which will produce uncertain results
89 * if the filesystem is active - repair, check, logprint.
92 check_isactive(char *name
, char *block
, int fatal
)
98 if (check_ismounted(name
, block
, 0)) {
99 if ((f
= setmntent(MOUNTED
, "r")) == NULL
) {
101 "%s: %s contains a possibly writable, mounted "
102 "filesystem\n", progname
, name
);
105 while ((mnt
= getmntent(f
)) != NULL
) {
106 if (hasmntopt(mnt
, MNTOPT_RO
) != NULL
)
111 "%s: %s contains a writable mounted "
112 "filesystem\n", progname
, name
);
128 /* Test to see if we are dealing with a regular file rather than a
129 * block device, if we are just use the size returned by stat64
131 if (stat64(path
, &st
) < 0) {
132 fprintf(stderr
, "%s: "
133 "cannot stat the device special file \"%s\": %s\n",
134 progname
, path
, strerror(errno
));
137 if ((st
.st_mode
& S_IFMT
) == S_IFREG
) {
138 return (__int64_t
)(st
.st_size
>> 9);
141 if ((fd
= open(path
, 0)) < 0) {
142 fprintf(stderr
, "%s: "
143 "error opening the device special file \"%s\": %s\n",
144 progname
, path
, strerror(errno
));
147 error
= ioctl(fd
, BLKGETSIZE
, &size
);
149 fprintf(stderr
, "%s: can't determine device size\n", progname
);
155 return (__int64_t
)size
;
159 /* libxfs_device_to_fd:
160 * lookup a device number in the device map
161 * return the associated fd
164 libxfs_device_to_fd(dev_t device
)
168 for (d
=0;d
<MAX_DEVS
;d
++)
169 if (dev_map
[d
].dev
== device
)
170 return dev_map
[d
].fd
;
172 fprintf(stderr
, "%s: device_to_fd: device %Ld is not open\n",
177 /* libxfs_device_open:
178 * open a device and return its device number
181 libxfs_device_open(char *path
, int creat
, int readonly
, int setblksize
)
187 int blocksize
= 512; /* bytes */
190 (readonly
? O_RDONLY
: O_RDWR
) |
191 (creat
? O_CREAT
|O_TRUNC
: 0),
193 fprintf(stderr
, "%s: cannot open %s: %s\n",
194 progname
, path
, strerror(errno
));
198 if (stat(path
, &statb
)<0) {
199 fprintf(stderr
, "%s: cannot stat %s: %s\n",
200 progname
, path
, strerror(errno
));
204 /* Set device blocksize to 512 bytes */
205 if (!readonly
&& setblksize
&& (statb
.st_mode
& S_IFMT
) == S_IFBLK
) {
206 if (ioctl(fd
, BLKBSZSET
, &blocksize
) < 0) {
207 fprintf(stderr
, "%s: warning - cannot set blocksize on "
208 "block device %s: %s\n",
209 progname
, path
, strerror(errno
));
213 /* get the device number from the stat buf - unless
214 * we're not opening a real device, in which case
215 * choose a new fake device number
217 dev
=(statb
.st_rdev
)?(statb
.st_rdev
):(nextfakedev
--);
219 for (d
=0;d
<MAX_DEVS
;d
++)
220 if (dev_map
[d
].dev
== dev
) {
221 fprintf(stderr
, "%s: device %Ld is already open\n",
226 for (d
=0;d
<MAX_DEVS
;d
++)
227 if (!dev_map
[d
].dev
) {
234 fprintf(stderr
, "%s: device_open: too many open devices\n", progname
);
239 libxfs_device_close(dev_t dev
)
243 for (d
=0;d
<MAX_DEVS
;d
++)
244 if (dev_map
[d
].dev
== dev
) {
248 dev_map
[d
].dev
=dev_map
[d
].fd
=0;
251 ioctl(fd
, BLKFLSBUF
, 0);
257 fprintf(stderr
, "%s: device_close: device %Ld is not open\n",
265 * libxfs initialization.
266 * Caller gets a 0 on failure (and we print a message), 1 on success.
269 libxfs_init(libxfs_init_t
*a
)
272 char curdir
[MAXPATHLEN
];
287 dpath
[0] = logpath
[0] = rtpath
[0] = '\0';
289 logname
= a
->logname
;
291 a
->ddev
= a
->logdev
= a
->rtdev
= 0;
292 a
->dfd
= a
->logfd
= a
->rtfd
= -1;
293 a
->dsize
= a
->logBBsize
= a
->logBBstart
= a
->rtsize
= 0;
295 (void)getcwd(curdir
,MAXPATHLEN
);
298 readonly
= (a
->isreadonly
& LIBXFS_ISREADONLY
);
299 inactive
= (a
->isreadonly
& LIBXFS_ISINACTIVE
);
301 if (stat64(a
->volname
, &stbuf
) < 0) {
305 if (!(rawfile
= findrawpath(a
->volname
))) {
306 fprintf(stderr
, "%s: "
307 "can't find a character device matching %s\n",
308 progname
, a
->volname
);
311 if (!(blockfile
= findblockpath(a
->volname
))) {
312 fprintf(stderr
, "%s: "
313 "can't find a block device matching %s\n",
314 progname
, a
->volname
);
317 if (!readonly
&& !inactive
&& check_ismounted(
318 a
->volname
, blockfile
, 1))
320 if (inactive
&& check_isactive(
321 a
->volname
, blockfile
, readonly
))
324 fd
= open(rawfile
, O_RDONLY
);
325 #ifdef HAVE_VOLUME_MANAGER
327 if (ioctl(fd
, DIOCGETVOLDEV
, &getdev
) < 0)
333 dname
= a
->dname
= a
->volname
;
337 fprintf(stderr
, "%s: "
338 "%s is not a volume device name\n",
339 progname
, a
->volname
);
341 fprintf(stderr
, a
->notvolmsg
, a
->volname
);
344 #ifdef HAVE_VOLUME_MANAGER
345 if (getdev
.data_subvol_dev
&& dname
) {
346 fprintf(stderr
, "%s: "
347 "%s has a data subvolume, cannot specify %s\n",
348 progname
, a
->volname
, dname
);
351 if (getdev
.log_subvol_dev
&& logname
) {
352 fprintf(stderr
, "%s: "
353 "%s has a log subvolume, cannot specify %s\n",
354 progname
, a
->volname
, logname
);
357 if (getdev
.rt_subvol_dev
&& rtname
) {
358 fprintf(stderr
, "%s: %s has a realtime subvolume, "
359 "cannot specify %s\n",
360 progname
, a
->volname
, rtname
);
363 if (!dname
&& getdev
.data_subvol_dev
) {
364 strcpy(dpath
, "/tmp/libxfsdXXXXXX");
366 if (mknod(dpath
, S_IFCHR
| 0600,
367 getdev
.data_subvol_dev
) < 0) {
368 fprintf(stderr
, "%s: mknod failed: %s\n",
369 progname
, strerror(errno
));
374 if (!logname
&& getdev
.log_subvol_dev
) {
375 strcpy(logpath
, "/tmp/libxfslXXXXXX");
376 (void)mktemp(logpath
);
377 if (mknod(logpath
, S_IFCHR
| 0600,
378 getdev
.log_subvol_dev
) < 0) {
379 fprintf(stderr
, "%s: mknod failed: %s\n",
380 progname
, strerror(errno
));
385 if (!rtname
&& getdev
.rt_subvol_dev
) {
386 strcpy(rtpath
, "/tmp/libxfsrXXXXXX");
387 (void)mktemp(rtpath
);
388 if (mknod(rtpath
, S_IFCHR
| 0600,
389 getdev
.rt_subvol_dev
) < 0) {
390 fprintf(stderr
, "%s: mknod failed: %s\n",
391 progname
, strerror(errno
));
400 if (dname
[0] != '/' && needcd
)
403 a
->ddev
= libxfs_device_open(dname
, a
->dcreat
, readonly
,
405 a
->dfd
= libxfs_device_to_fd(a
->ddev
);
407 if (stat64(dname
, &stbuf
) < 0) {
408 fprintf(stderr
, "%s: stat64 failed on %s: %s\n",
409 progname
, dname
, strerror(errno
));
412 if (!(rawfile
= findrawpath(dname
))) {
413 fprintf(stderr
, "%s: can't find a char device "
414 "matching %s\n", progname
, dname
);
417 if (!(blockfile
= findblockpath(dname
))) {
418 fprintf(stderr
, "%s: can't find a block device "
419 "matching %s\n", progname
, dname
);
422 if (!readonly
&& !inactive
&& check_ismounted(
423 dname
, blockfile
, 1))
425 if (inactive
&& check_isactive(
426 dname
, blockfile
, readonly
))
428 a
->ddev
= libxfs_device_open(rawfile
,
429 a
->dcreat
, readonly
, a
->setblksize
);
430 a
->dfd
= libxfs_device_to_fd(a
->ddev
);
431 a
->dsize
= findsize(rawfile
);
437 if (logname
[0] != '/' && needcd
)
440 a
->logdev
= libxfs_device_open(logname
,
441 a
->lcreat
, readonly
, a
->setblksize
);
442 a
->logfd
= libxfs_device_to_fd(a
->logdev
);
444 if (stat64(logname
, &stbuf
) < 0) {
445 fprintf(stderr
, "%s: stat64 failed on %s: %s\n",
446 progname
, logname
, strerror(errno
));
449 if (!(rawfile
= findrawpath(logname
))) {
450 fprintf(stderr
, "%s: can't find a char device "
451 "matching %s\n", progname
, logname
);
454 if (!(blockfile
= findblockpath(logname
))) {
455 fprintf(stderr
, "%s: can't find a block device "
456 "matching %s\n", progname
, logname
);
459 if (!readonly
&& !inactive
&& check_ismounted(
460 logname
, blockfile
, 1))
462 else if (inactive
&& check_isactive(
463 logname
, blockfile
, readonly
))
465 a
->logdev
= libxfs_device_open(rawfile
,
466 a
->lcreat
, readonly
, a
->setblksize
);
467 a
->logfd
= libxfs_device_to_fd(a
->logdev
);
468 a
->logBBsize
= findsize(rawfile
);
474 if (rtname
[0] != '/' && needcd
)
477 a
->rtdev
= libxfs_device_open(rtname
,
478 a
->rcreat
, readonly
, a
->setblksize
);
479 a
->rtfd
= libxfs_device_to_fd(a
->rtdev
);
481 if (stat64(rtname
, &stbuf
) < 0) {
482 fprintf(stderr
, "%s: stat64 failed on %s: %s\n",
483 progname
, rtname
, strerror(errno
));
486 if (!(rawfile
= findrawpath(rtname
))) {
487 fprintf(stderr
, "%s: can't find a char device "
488 "matching %s\n", progname
, rtname
);
491 if (!(blockfile
= findblockpath(rtname
))) {
492 fprintf(stderr
, "%s: can't find a block device "
493 "matching %s\n", progname
, rtname
);
496 if (!readonly
&& !inactive
&& check_ismounted(
497 rtname
, blockfile
, 1))
499 if (inactive
&& check_isactive(
500 rtname
, blockfile
, readonly
))
502 a
->rtdev
= libxfs_device_open(rawfile
,
503 a
->rcreat
, readonly
, a
->setblksize
);
504 a
->rtfd
= libxfs_device_to_fd(a
->rtdev
);
505 a
->rtsize
= findsize(rawfile
);
511 fprintf(stderr
, "%s: can't get size for data subvolume\n",
515 if (a
->logBBsize
< 0) {
516 fprintf(stderr
, "%s: can't get size for log subvolume\n",
521 fprintf(stderr
, "%s: can't get size for realtime subvolume\n",
537 if (!rval
&& a
->ddev
)
538 libxfs_device_close(a
->ddev
);
539 if (!rval
&& a
->logdev
)
540 libxfs_device_close(a
->logdev
);
541 if (!rval
&& a
->rtdev
)
542 libxfs_device_close(a
->rtdev
);
548 * Initialize/destroy all of the zone allocators we use.
551 manage_zones(int release
)
553 extern xfs_zone_t
*xfs_ili_zone
;
554 extern xfs_zone_t
*xfs_inode_zone
;
555 extern xfs_zone_t
*xfs_ifork_zone
;
556 extern xfs_zone_t
*xfs_dabuf_zone
;
557 extern xfs_zone_t
*xfs_buf_item_zone
;
558 extern xfs_zone_t
*xfs_da_state_zone
;
559 extern xfs_zone_t
*xfs_btree_cur_zone
;
560 extern xfs_zone_t
*xfs_bmap_free_item_zone
;
561 extern void xfs_dir_startup();
563 if (release
) { /* free zone allocation */
564 libxfs_free(xfs_inode_zone
);
565 libxfs_free(xfs_ifork_zone
);
566 libxfs_free(xfs_dabuf_zone
);
567 libxfs_free(xfs_buf_item_zone
);
568 libxfs_free(xfs_da_state_zone
);
569 libxfs_free(xfs_btree_cur_zone
);
570 libxfs_free(xfs_bmap_free_item_zone
);
573 /* otherwise initialise zone allocation */
574 xfs_inode_zone
= libxfs_zone_init(sizeof(xfs_inode_t
), "xfs_inode");
575 xfs_ifork_zone
= libxfs_zone_init(sizeof(xfs_ifork_t
), "xfs_ifork");
576 xfs_dabuf_zone
= libxfs_zone_init(sizeof(xfs_dabuf_t
), "xfs_dabuf");
577 xfs_ili_zone
= libxfs_zone_init(
578 sizeof(xfs_inode_log_item_t
), "xfs_inode_log_item");
579 xfs_buf_item_zone
= libxfs_zone_init(
580 sizeof(xfs_buf_log_item_t
), "xfs_buf_log_item");
581 xfs_da_state_zone
= libxfs_zone_init(
582 sizeof(xfs_da_state_t
), "xfs_da_state");
583 xfs_btree_cur_zone
= libxfs_zone_init(
584 sizeof(xfs_btree_cur_t
), "xfs_btree_cur");
585 xfs_bmap_free_item_zone
= libxfs_zone_init(
586 sizeof(xfs_bmap_free_item_t
), "xfs_bmap_free_item");
591 * Get the bitmap and summary inodes into the mount structure
595 rtmount_inodes(xfs_mount_t
*mp
)
601 if (sbp
->sb_rbmino
== NULLFSINO
)
603 error
= libxfs_iread(mp
, NULL
, sbp
->sb_rbmino
, &mp
->m_rbmip
, 0);
605 fprintf(stderr
, "%s: cannot read realtime bitmap inode (%d)\n",
609 ASSERT(mp
->m_rbmip
!= NULL
);
610 ASSERT(sbp
->sb_rsumino
!= NULLFSINO
);
611 error
= libxfs_iread(mp
, NULL
, sbp
->sb_rsumino
, &mp
->m_rsumip
, 0);
613 fprintf(stderr
, "%s: cannot read realtime summary inode (%d)\n",
617 ASSERT(mp
->m_rsumip
!= NULL
);
622 * Mount structure initialization, provides a filled-in xfs_mount_t
623 * such that the numerous XFS_* macros can be used. If dev is zero,
624 * no IO will be performed (no size checks, read root inodes).
643 mp
->m_logdev
= logdev
;
648 libxfs_mount_common(mp
, sb
);
650 libxfs_alloc_compute_maxlevels(mp
);
651 libxfs_bmap_compute_maxlevels(mp
, XFS_DATA_FORK
);
652 libxfs_bmap_compute_maxlevels(mp
, XFS_ATTR_FORK
);
653 libxfs_ialloc_compute_maxlevels(mp
);
655 if (sbp
->sb_imax_pct
) {
656 /* Make sure the maximum inode count is a multiple of the
657 * units we allocate inodes in.
659 mp
->m_maxicount
= (sbp
->sb_dblocks
* sbp
->sb_imax_pct
) / 100;
660 mp
->m_maxicount
= ((mp
->m_maxicount
/ mp
->m_ialloc_blks
) *
661 mp
->m_ialloc_blks
) << sbp
->sb_inopblog
;
665 mp
->m_inode_cluster_size
= XFS_INODE_BIG_CLUSTER_SIZE
;
668 * Set whether we're using inode alignment.
670 if (XFS_SB_VERSION_HASALIGN(&mp
->m_sb
) &&
671 mp
->m_sb
.sb_inoalignmt
>=
672 XFS_B_TO_FSBT(mp
, mp
->m_inode_cluster_size
))
673 mp
->m_inoalign_mask
= mp
->m_sb
.sb_inoalignmt
- 1;
675 mp
->m_inoalign_mask
= 0;
677 * If we are using stripe alignment, check whether
678 * the stripe unit is a multiple of the inode alignment
681 && mp
->m_inoalign_mask
&& !(mp
->m_dalign
& mp
->m_inoalign_mask
))
682 mp
->m_sinoalign
= mp
->m_dalign
;
687 * Check that the data (and log if separate) are an ok size.
689 d
= (xfs_daddr_t
)XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_dblocks
);
690 if (XFS_BB_TO_FSB(mp
, d
) != mp
->m_sb
.sb_dblocks
) {
691 fprintf(stderr
, "%s: size check failed\n", progname
);
695 /* Initialize the appropriate directory manager */
696 if (XFS_SB_VERSION_HASDIRV2(sbp
))
697 libxfs_dir2_mount(mp
);
699 libxfs_dir_mount(mp
);
701 /* Initialize the precomputed transaction reservations values */
702 libxfs_trans_init(mp
);
704 if (dev
== 0) /* maxtrres, we have no device so leave now */
707 bp
= libxfs_readbuf(mp
->m_dev
, d
- 1, 1, 0);
709 fprintf(stderr
, "%s: data size check failed\n", progname
);
714 if (mp
->m_logdev
&& mp
->m_logdev
!= mp
->m_dev
) {
715 d
= (xfs_daddr_t
)XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_logblocks
);
716 if ( (XFS_BB_TO_FSB(mp
, d
) != mp
->m_sb
.sb_logblocks
) ||
717 (!(bp
= libxfs_readbuf(mp
->m_logdev
, d
- 1, 1, 1)))) {
718 fprintf(stderr
, "%s: log size checks failed\n",
725 /* Initialize realtime fields in the mount structure */
726 if (libxfs_rtmount_init(mp
)) {
727 fprintf(stderr
, "%s: real-time device init failed\n", progname
);
731 /* Allocate and initialize the per-ag data */
732 size
= sbp
->sb_agcount
* sizeof(xfs_perag_t
);
733 if ((mp
->m_perag
= calloc(size
, 1)) == NULL
) {
734 fprintf(stderr
, "%s: failed to alloc %d bytes: %s\n",
735 progname
, size
, strerror(errno
));
740 * mkfs calls mount before the root inode is allocated.
742 if (rrootinos
&& sbp
->sb_rootino
!= NULLFSINO
) {
743 error
= libxfs_iread(mp
, NULL
, sbp
->sb_rootino
,
746 fprintf(stderr
, "%s: cannot read root inode (%d)\n",
750 ASSERT(mp
->m_rootip
!= NULL
);
752 if (rrootinos
&& rtmount_inodes(mp
))
758 * Release any resourse obtained during a mount.
761 libxfs_umount(xfs_mount_t
*mp
)