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
38 #include <sys/ustat.h>
39 #include <sys/ioctl.h>
40 #include <sys/mount.h>
42 #define findrawpath(x) x
43 #define findblockpath(x) x
45 char *progname
= "libxfs"; /* default, changed by each tool */
48 * dev_map - map open devices to fd.
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
{
55 } dev_map
[MAX_DEVS
]={{0}};
58 check_ismounted(char *name
, char *block
)
63 if (stat64(block
, &st
) < 0)
65 if ((st
.st_mode
& S_IFMT
) != S_IFBLK
)
67 if (ustat(st
.st_rdev
, &ust
) >= 0) {
68 fprintf(stderr
, "%s: %s contains a mounted filesystem\n",
76 * Checks whether a given device has a mounted, writable
77 * filesystem, returns 1 if it does & fatal (just warns
78 * if not fatal, but allows us to proceed).
80 * Useful to tools which will produce uncertain results
81 * if the filesystem is active - repair, check, logprint.
84 check_isactive(char *name
, char *block
, int fatal
)
86 #define PROC_MOUNTED "/proc/mounts"
90 struct stat64 st
, mst
;
92 char mounts
[MAXPATHLEN
];
94 if (stat64(block
, &st
) < 0)
96 if ((st
.st_mode
& S_IFMT
) != S_IFBLK
)
98 if (ustat(st
.st_rdev
, &ust
) < 0)
101 strcpy(mounts
, access(PROC_MOUNTED
, R_OK
)? PROC_MOUNTED
: MOUNTED
);
102 if ((f
= setmntent(mounts
, "r")) == NULL
) {
103 fprintf(stderr
, "%s: %s contains a possibly writable, mounted "
104 "filesystem\n", progname
, name
);
107 while ((mnt
= getmntent(f
)) != NULL
) {
108 if (stat64(mnt
->mnt_fsname
, &mst
) < 0)
110 if ((mst
.st_mode
& S_IFMT
) != S_IFBLK
)
112 if (mst
.st_rdev
== st
.st_rdev
113 && hasmntopt(mnt
, MNTOPT_RO
) != NULL
)
117 fprintf(stderr
, "%s: %s contains a writable, mounted "
118 "filesystem\n", progname
, name
);
134 /* Test to see if we are dealing with a regular file rather than a
135 * block device, if we are just use the size returned by stat64
137 if (stat64(path
, &st
) < 0) {
138 fprintf(stderr
, "%s: "
139 "cannot stat the device special file \"%s\": %s\n",
140 progname
, path
, strerror(errno
));
143 if ((st
.st_mode
& S_IFMT
) == S_IFREG
) {
144 return (__int64_t
)(st
.st_size
>> 9);
147 if ((fd
= open(path
, 0)) < 0) {
148 fprintf(stderr
, "%s: "
149 "error opening the device special file \"%s\": %s\n",
150 progname
, path
, strerror(errno
));
153 error
= ioctl(fd
, BLKGETSIZE
, &size
);
155 fprintf(stderr
, "%s: can't determine device size\n", progname
);
161 return (__int64_t
)size
;
165 /* libxfs_device_to_fd:
166 * lookup a device number in the device map
167 * return the associated fd
170 libxfs_device_to_fd(dev_t device
)
174 for (d
=0;d
<MAX_DEVS
;d
++)
175 if (dev_map
[d
].dev
== device
)
176 return dev_map
[d
].fd
;
178 fprintf(stderr
, "%s: device_to_fd: device %lld is not open\n",
179 progname
, (long long)device
);
183 /* libxfs_device_open:
184 * open a device and return its device number
187 libxfs_device_open(char *path
, int creat
, int readonly
, int setblksize
)
195 (readonly
? O_RDONLY
: O_RDWR
) |
196 (creat
? O_CREAT
|O_TRUNC
: 0),
198 fprintf(stderr
, "%s: cannot open %s: %s\n",
199 progname
, path
, strerror(errno
));
203 if (stat(path
, &statb
)<0) {
204 fprintf(stderr
, "%s: cannot stat %s: %s\n",
205 progname
, path
, strerror(errno
));
209 #ifdef HAVE_BLKBSZSET
211 * Set device blocksize to 512 bytes
213 * See bug #801063, but we no longer have this ioctl in the kernel -
214 * it will be needed again though when the fs blocksize != pagesize.
217 #define BLKBSZSET _IO(0x12,110) /* set device block size */
219 if (!readonly
&& setblksize
&& (statb
.st_mode
& S_IFMT
) == S_IFBLK
) {
220 int blocksize
= 512; /* bytes */
221 if (ioctl(fd
, BLKBSZSET
, &blocksize
) < 0) {
222 fprintf(stderr
, "%s: warning - cannot set blocksize on "
223 "block device %s: %s\n",
224 progname
, path
, strerror(errno
));
229 /* get the device number from the stat buf - unless
230 * we're not opening a real device, in which case
231 * choose a new fake device number
233 dev
=(statb
.st_rdev
)?(statb
.st_rdev
):(nextfakedev
--);
235 for (d
=0;d
<MAX_DEVS
;d
++)
236 if (dev_map
[d
].dev
== dev
) {
237 fprintf(stderr
, "%s: device %lld is already open\n",
238 progname
, (long long)dev
);
242 for (d
=0;d
<MAX_DEVS
;d
++)
243 if (!dev_map
[d
].dev
) {
250 fprintf(stderr
, "%s: device_open: too many open devices\n", progname
);
255 libxfs_device_close(dev_t dev
)
259 for (d
=0;d
<MAX_DEVS
;d
++)
260 if (dev_map
[d
].dev
== dev
) {
264 dev_map
[d
].dev
=dev_map
[d
].fd
=0;
267 ioctl(fd
, BLKFLSBUF
, 0);
273 fprintf(stderr
, "%s: device_close: device %lld is not open\n",
274 progname
, (long long)dev
);
281 * libxfs initialization.
282 * Caller gets a 0 on failure (and we print a message), 1 on success.
285 libxfs_init(libxfs_init_t
*a
)
288 char curdir
[MAXPATHLEN
];
303 dpath
[0] = logpath
[0] = rtpath
[0] = '\0';
305 logname
= a
->logname
;
307 a
->ddev
= a
->logdev
= a
->rtdev
= 0;
308 a
->dfd
= a
->logfd
= a
->rtfd
= -1;
309 a
->dsize
= a
->logBBsize
= a
->logBBstart
= a
->rtsize
= 0;
311 (void)getcwd(curdir
,MAXPATHLEN
);
314 readonly
= (a
->isreadonly
& LIBXFS_ISREADONLY
);
315 inactive
= (a
->isreadonly
& LIBXFS_ISINACTIVE
);
317 if (stat64(a
->volname
, &stbuf
) < 0) {
321 if (!(rawfile
= findrawpath(a
->volname
))) {
322 fprintf(stderr
, "%s: "
323 "can't find a character device matching %s\n",
324 progname
, a
->volname
);
327 if (!(blockfile
= findblockpath(a
->volname
))) {
328 fprintf(stderr
, "%s: "
329 "can't find a block device matching %s\n",
330 progname
, a
->volname
);
333 if (!readonly
&& !inactive
&& check_ismounted(
334 a
->volname
, blockfile
))
336 if (inactive
&& check_isactive(
337 a
->volname
, blockfile
, readonly
))
340 fd
= open(rawfile
, O_RDONLY
);
341 #ifdef HAVE_VOLUME_MANAGER
343 if (ioctl(fd
, DIOCGETVOLDEV
, &getdev
) < 0)
349 dname
= a
->dname
= a
->volname
;
353 fprintf(stderr
, "%s: "
354 "%s is not a volume device name\n",
355 progname
, a
->volname
);
357 fprintf(stderr
, a
->notvolmsg
, a
->volname
);
360 #ifdef HAVE_VOLUME_MANAGER
361 if (getdev
.data_subvol_dev
&& dname
) {
362 fprintf(stderr
, "%s: "
363 "%s has a data subvolume, cannot specify %s\n",
364 progname
, a
->volname
, dname
);
367 if (getdev
.log_subvol_dev
&& logname
) {
368 fprintf(stderr
, "%s: "
369 "%s has a log subvolume, cannot specify %s\n",
370 progname
, a
->volname
, logname
);
373 if (getdev
.rt_subvol_dev
&& rtname
) {
374 fprintf(stderr
, "%s: %s has a realtime subvolume, "
375 "cannot specify %s\n",
376 progname
, a
->volname
, rtname
);
379 if (!dname
&& getdev
.data_subvol_dev
) {
380 strcpy(dpath
, "/tmp/libxfsdXXXXXX");
382 if (mknod(dpath
, S_IFCHR
| 0600,
383 getdev
.data_subvol_dev
) < 0) {
384 fprintf(stderr
, "%s: mknod failed: %s\n",
385 progname
, strerror(errno
));
390 if (!logname
&& getdev
.log_subvol_dev
) {
391 strcpy(logpath
, "/tmp/libxfslXXXXXX");
392 (void)mktemp(logpath
);
393 if (mknod(logpath
, S_IFCHR
| 0600,
394 getdev
.log_subvol_dev
) < 0) {
395 fprintf(stderr
, "%s: mknod failed: %s\n",
396 progname
, strerror(errno
));
401 if (!rtname
&& getdev
.rt_subvol_dev
) {
402 strcpy(rtpath
, "/tmp/libxfsrXXXXXX");
403 (void)mktemp(rtpath
);
404 if (mknod(rtpath
, S_IFCHR
| 0600,
405 getdev
.rt_subvol_dev
) < 0) {
406 fprintf(stderr
, "%s: mknod failed: %s\n",
407 progname
, strerror(errno
));
416 if (dname
[0] != '/' && needcd
)
419 a
->ddev
= libxfs_device_open(dname
, a
->dcreat
, readonly
,
421 a
->dfd
= libxfs_device_to_fd(a
->ddev
);
423 if (stat64(dname
, &stbuf
) < 0) {
424 fprintf(stderr
, "%s: stat64 failed on %s: %s\n",
425 progname
, dname
, strerror(errno
));
428 if (!(rawfile
= findrawpath(dname
))) {
429 fprintf(stderr
, "%s: can't find a char device "
430 "matching %s\n", progname
, dname
);
433 if (!(blockfile
= findblockpath(dname
))) {
434 fprintf(stderr
, "%s: can't find a block device "
435 "matching %s\n", progname
, dname
);
438 if (!readonly
&& !inactive
&& check_ismounted(
441 if (inactive
&& check_isactive(
442 dname
, blockfile
, readonly
))
444 a
->ddev
= libxfs_device_open(rawfile
,
445 a
->dcreat
, readonly
, a
->setblksize
);
446 a
->dfd
= libxfs_device_to_fd(a
->ddev
);
447 a
->dsize
= findsize(rawfile
);
453 if (logname
[0] != '/' && needcd
)
456 a
->logdev
= libxfs_device_open(logname
,
457 a
->lcreat
, readonly
, a
->setblksize
);
458 a
->logfd
= libxfs_device_to_fd(a
->logdev
);
460 if (stat64(logname
, &stbuf
) < 0) {
461 fprintf(stderr
, "%s: stat64 failed on %s: %s\n",
462 progname
, logname
, strerror(errno
));
465 if (!(rawfile
= findrawpath(logname
))) {
466 fprintf(stderr
, "%s: can't find a char device "
467 "matching %s\n", progname
, logname
);
470 if (!(blockfile
= findblockpath(logname
))) {
471 fprintf(stderr
, "%s: can't find a block device "
472 "matching %s\n", progname
, logname
);
475 if (!readonly
&& !inactive
&& check_ismounted(
478 else if (inactive
&& check_isactive(
479 logname
, blockfile
, readonly
))
481 a
->logdev
= libxfs_device_open(rawfile
,
482 a
->lcreat
, readonly
, a
->setblksize
);
483 a
->logfd
= libxfs_device_to_fd(a
->logdev
);
484 a
->logBBsize
= findsize(rawfile
);
490 if (rtname
[0] != '/' && needcd
)
493 a
->rtdev
= libxfs_device_open(rtname
,
494 a
->rcreat
, readonly
, a
->setblksize
);
495 a
->rtfd
= libxfs_device_to_fd(a
->rtdev
);
497 if (stat64(rtname
, &stbuf
) < 0) {
498 fprintf(stderr
, "%s: stat64 failed on %s: %s\n",
499 progname
, rtname
, strerror(errno
));
502 if (!(rawfile
= findrawpath(rtname
))) {
503 fprintf(stderr
, "%s: can't find a char device "
504 "matching %s\n", progname
, rtname
);
507 if (!(blockfile
= findblockpath(rtname
))) {
508 fprintf(stderr
, "%s: can't find a block device "
509 "matching %s\n", progname
, rtname
);
512 if (!readonly
&& !inactive
&& check_ismounted(
515 if (inactive
&& check_isactive(
516 rtname
, blockfile
, readonly
))
518 a
->rtdev
= libxfs_device_open(rawfile
,
519 a
->rcreat
, readonly
, a
->setblksize
);
520 a
->rtfd
= libxfs_device_to_fd(a
->rtdev
);
521 a
->rtsize
= findsize(rawfile
);
527 fprintf(stderr
, "%s: can't get size for data subvolume\n",
531 if (a
->logBBsize
< 0) {
532 fprintf(stderr
, "%s: can't get size for log subvolume\n",
537 fprintf(stderr
, "%s: can't get size for realtime subvolume\n",
553 if (!rval
&& a
->ddev
)
554 libxfs_device_close(a
->ddev
);
555 if (!rval
&& a
->logdev
)
556 libxfs_device_close(a
->logdev
);
557 if (!rval
&& a
->rtdev
)
558 libxfs_device_close(a
->rtdev
);
564 * Initialize/destroy all of the zone allocators we use.
567 manage_zones(int release
)
569 extern xfs_zone_t
*xfs_ili_zone
;
570 extern xfs_zone_t
*xfs_inode_zone
;
571 extern xfs_zone_t
*xfs_ifork_zone
;
572 extern xfs_zone_t
*xfs_dabuf_zone
;
573 extern xfs_zone_t
*xfs_buf_item_zone
;
574 extern xfs_zone_t
*xfs_da_state_zone
;
575 extern xfs_zone_t
*xfs_btree_cur_zone
;
576 extern xfs_zone_t
*xfs_bmap_free_item_zone
;
577 extern void xfs_dir_startup();
579 if (release
) { /* free zone allocation */
580 libxfs_free(xfs_inode_zone
);
581 libxfs_free(xfs_ifork_zone
);
582 libxfs_free(xfs_dabuf_zone
);
583 libxfs_free(xfs_buf_item_zone
);
584 libxfs_free(xfs_da_state_zone
);
585 libxfs_free(xfs_btree_cur_zone
);
586 libxfs_free(xfs_bmap_free_item_zone
);
589 /* otherwise initialise zone allocation */
590 xfs_inode_zone
= libxfs_zone_init(sizeof(xfs_inode_t
), "xfs_inode");
591 xfs_ifork_zone
= libxfs_zone_init(sizeof(xfs_ifork_t
), "xfs_ifork");
592 xfs_dabuf_zone
= libxfs_zone_init(sizeof(xfs_dabuf_t
), "xfs_dabuf");
593 xfs_ili_zone
= libxfs_zone_init(
594 sizeof(xfs_inode_log_item_t
), "xfs_inode_log_item");
595 xfs_buf_item_zone
= libxfs_zone_init(
596 sizeof(xfs_buf_log_item_t
), "xfs_buf_log_item");
597 xfs_da_state_zone
= libxfs_zone_init(
598 sizeof(xfs_da_state_t
), "xfs_da_state");
599 xfs_btree_cur_zone
= libxfs_zone_init(
600 sizeof(xfs_btree_cur_t
), "xfs_btree_cur");
601 xfs_bmap_free_item_zone
= libxfs_zone_init(
602 sizeof(xfs_bmap_free_item_t
), "xfs_bmap_free_item");
607 * Get the bitmap and summary inodes into the mount structure
611 rtmount_inodes(xfs_mount_t
*mp
)
617 if (sbp
->sb_rbmino
== NULLFSINO
)
619 error
= libxfs_iread(mp
, NULL
, sbp
->sb_rbmino
, &mp
->m_rbmip
, 0);
621 fprintf(stderr
, "%s: cannot read realtime bitmap inode (%d)\n",
625 ASSERT(mp
->m_rbmip
!= NULL
);
626 ASSERT(sbp
->sb_rsumino
!= NULLFSINO
);
627 error
= libxfs_iread(mp
, NULL
, sbp
->sb_rsumino
, &mp
->m_rsumip
, 0);
629 fprintf(stderr
, "%s: cannot read realtime summary inode (%d)\n",
633 ASSERT(mp
->m_rsumip
!= NULL
);
637 #define XFS_MOUNT_32BITINODES 0x1
639 * Mount structure initialization, provides a filled-in xfs_mount_t
640 * such that the numerous XFS_* macros can be used. If dev is zero,
641 * no IO will be performed (no size checks, read root inodes).
660 mp
->m_logdev
= logdev
;
661 mp
->m_flags
= XFS_MOUNT_32BITINODES
;
666 libxfs_mount_common(mp
, sb
);
668 libxfs_alloc_compute_maxlevels(mp
);
669 libxfs_bmap_compute_maxlevels(mp
, XFS_DATA_FORK
);
670 libxfs_bmap_compute_maxlevels(mp
, XFS_ATTR_FORK
);
671 libxfs_ialloc_compute_maxlevels(mp
);
673 if (sbp
->sb_imax_pct
) {
674 /* Make sure the maximum inode count is a multiple of the
675 * units we allocate inodes in.
677 mp
->m_maxicount
= (sbp
->sb_dblocks
* sbp
->sb_imax_pct
) / 100;
678 mp
->m_maxicount
= ((mp
->m_maxicount
/ mp
->m_ialloc_blks
) *
679 mp
->m_ialloc_blks
) << sbp
->sb_inopblog
;
683 mp
->m_inode_cluster_size
= XFS_INODE_BIG_CLUSTER_SIZE
;
686 * Set whether we're using inode alignment.
688 if (XFS_SB_VERSION_HASALIGN(&mp
->m_sb
) &&
689 mp
->m_sb
.sb_inoalignmt
>=
690 XFS_B_TO_FSBT(mp
, mp
->m_inode_cluster_size
))
691 mp
->m_inoalign_mask
= mp
->m_sb
.sb_inoalignmt
- 1;
693 mp
->m_inoalign_mask
= 0;
695 * If we are using stripe alignment, check whether
696 * the stripe unit is a multiple of the inode alignment
699 && mp
->m_inoalign_mask
&& !(mp
->m_dalign
& mp
->m_inoalign_mask
))
700 mp
->m_sinoalign
= mp
->m_dalign
;
705 * Check that the data (and log if separate) are an ok size.
707 d
= (xfs_daddr_t
)XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_dblocks
);
708 if (XFS_BB_TO_FSB(mp
, d
) != mp
->m_sb
.sb_dblocks
) {
709 fprintf(stderr
, "%s: size check failed\n", progname
);
713 /* Initialize the appropriate directory manager */
714 if (XFS_SB_VERSION_HASDIRV2(sbp
))
715 libxfs_dir2_mount(mp
);
717 libxfs_dir_mount(mp
);
719 /* Initialize the precomputed transaction reservations values */
720 libxfs_trans_init(mp
);
722 if (dev
== 0) /* maxtrres, we have no device so leave now */
725 bp
= libxfs_readbuf(mp
->m_dev
, d
- 1, 1, 0);
727 fprintf(stderr
, "%s: data size check failed\n", progname
);
732 if (mp
->m_logdev
&& mp
->m_logdev
!= mp
->m_dev
) {
733 d
= (xfs_daddr_t
)XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_logblocks
);
734 if ( (XFS_BB_TO_FSB(mp
, d
) != mp
->m_sb
.sb_logblocks
) ||
735 (!(bp
= libxfs_readbuf(mp
->m_logdev
, d
- 1, 1, 1)))) {
736 fprintf(stderr
, "%s: log size checks failed\n",
743 /* Initialize realtime fields in the mount structure */
744 if (libxfs_rtmount_init(mp
)) {
745 fprintf(stderr
, "%s: real-time device init failed\n", progname
);
749 /* Allocate and initialize the per-ag data */
750 size
= sbp
->sb_agcount
* sizeof(xfs_perag_t
);
751 if ((mp
->m_perag
= calloc(size
, 1)) == NULL
) {
752 fprintf(stderr
, "%s: failed to alloc %ld bytes: %s\n",
753 progname
, (long)size
, strerror(errno
));
758 * mkfs calls mount before the root inode is allocated.
760 if (rrootinos
&& sbp
->sb_rootino
!= NULLFSINO
) {
761 error
= libxfs_iread(mp
, NULL
, sbp
->sb_rootino
,
764 fprintf(stderr
, "%s: cannot read root inode (%d)\n",
768 ASSERT(mp
->m_rootip
!= NULL
);
770 if (rrootinos
&& rtmount_inodes(mp
))
776 * Release any resourse obtained during a mount.
779 libxfs_umount(xfs_mount_t
*mp
)