]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/linux.c
Extract device sector size at mkfs time and issue warnings if the requested filesyste...
[thirdparty/xfsprogs-dev.git] / libxfs / linux.c
CommitLineData
9440d84d 1/*
93d9f139 2 * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved.
9440d84d
NS
3 *
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.
7 *
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.
11 *
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.
18 *
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.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
32
33#define ustat __kernel_ustat
1d7e80ee 34#include <xfs/libxfs.h>
9440d84d
NS
35#include <mntent.h>
36#include <sys/stat.h>
37#undef ustat
38#include <sys/ustat.h>
39#include <sys/mount.h>
40#include <sys/ioctl.h>
41
42extern char *progname;
43
44#ifndef BLKGETSIZE64
45# define BLKGETSIZE64 _IOR(0x12,114,sizeof(__uint64_t))
46#endif
47#ifndef BLKBSZSET
48# define BLKBSZSET _IOW(0x12,113,sizeof(int))
49#endif
74668075
NS
50#ifndef BLKSSZGET
51# define BLKSSZGET _IO(0x12,104)
52#endif
9440d84d
NS
53
54#define PROC_MOUNTED "/proc/mounts"
55
56int
93d9f139 57platform_check_ismounted(char *name, char *block, struct stat64 *s, int verbose)
9440d84d
NS
58{
59 struct ustat ust;
60 struct stat64 st;
61
62 if (!s) {
63 if (stat64(block, &st) < 0)
64 return 0;
65 if ((st.st_mode & S_IFMT) != S_IFBLK)
66 return 0;
67 s = &st;
68 }
69
70 if (ustat(s->st_rdev, &ust) >= 0) {
71 if (verbose)
72 fprintf(stderr,
73 _("%s: %s contains a mounted filesystem\n"),
74 progname, name);
75 return 1;
76 }
77 return 0;
78}
79
80int
93d9f139 81platform_check_iswritable(char *name, char *block, struct stat64 *s, int fatal)
9440d84d
NS
82{
83 int sts = 0;
84 FILE *f;
85 struct stat64 mst;
86 struct mntent *mnt;
87 char mounts[MAXPATHLEN];
88
89 strcpy(mounts, access(PROC_MOUNTED, R_OK)? PROC_MOUNTED : MOUNTED);
90 if ((f = setmntent(mounts, "r")) == NULL) {
91 fprintf(stderr, _("%s: %s contains a possibly writable, "
92 "mounted filesystem\n"), progname, name);
93 return fatal;
94 }
95 while ((mnt = getmntent(f)) != NULL) {
96 if (stat64(mnt->mnt_fsname, &mst) < 0)
97 continue;
98 if ((mst.st_mode & S_IFMT) != S_IFBLK)
99 continue;
100 if (mst.st_rdev == s->st_rdev
101 && hasmntopt(mnt, MNTOPT_RO) != NULL)
102 break;
103 }
507f4e33 104 if (mnt == NULL) {
9440d84d
NS
105 fprintf(stderr, _("%s: %s contains a mounted and writable "
106 "filesystem\n"), progname, name);
107 sts = fatal;
108 }
109 endmntent(f);
110 return sts;
111}
112
113void
93d9f139 114platform_set_blocksize(int fd, char *path, int blocksize)
9440d84d
NS
115{
116 if (ioctl(fd, BLKBSZSET, &blocksize) < 0) {
117 fprintf(stderr, _("%s: warning - cannot set blocksize "
118 "on block device %s: %s\n"),
119 progname, path, strerror(errno));
120 }
121}
122
74668075
NS
123int
124platform_get_blocksize(int fd, char *path)
125{
126 int blocksize;
127
128 if (ioctl(fd, BLKSSZGET, &blocksize) < 0) {
129 fprintf(stderr, _("%s: warning - cannot get sector size "
130 "from block device %s: %s\n"),
131 progname, path, strerror(errno));
132 blocksize = BBSIZE;
133 }
134 return blocksize;
135}
136
9440d84d 137void
93d9f139 138platform_flush_device(int fd)
9440d84d
NS
139{
140 ioctl(fd, BLKFLSBUF, 0);
141}
142
143__int64_t
93d9f139 144platform_findsize(char *path)
9440d84d
NS
145{
146 int fd;
147 int error;
148 __int64_t ssize;
149 __uint64_t size;
150 struct stat64 st;
151
152 if (stat64(path, &st) < 0) {
153 fprintf(stderr, _("%s: "
154 "cannot stat the device file \"%s\": %s\n"),
155 progname, path, strerror(errno));
156 exit(1);
157 }
158 if ((st.st_mode & S_IFMT) == S_IFREG)
159 return (__int64_t)(st.st_size >> 9);
160
161 if ((fd = open(path, O_RDONLY, 0)) < 0) {
162 fprintf(stderr, _("%s: "
163 "error opening the device special file \"%s\": %s\n"),
164 progname, path, strerror(errno));
165 exit(1);
166 }
167
168 error = ioctl(fd, BLKGETSIZE64, &size);
169 if (error >= 0) {
170 /* BLKGETSIZE64 returns size in bytes not 512-byte blocks */
171 ssize = size >> 9;
172 } else {
173 /* If BLKGETSIZE64 fails, try BLKGETSIZE */
174 unsigned long tmpsize;
175 error = ioctl(fd, BLKGETSIZE, &tmpsize);
176 if (error < 0) {
177 fprintf(stderr, _("%s: can't determine device size\n"),
178 progname);
179 exit(1);
180 }
181 ssize = (__int64_t)tmpsize;
182 }
183
184 close(fd);
9440d84d
NS
185 return ssize;
186}