]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/linux.c
Flush out my xfsprogs backlog - bunch of I18N and sector size related
[thirdparty/xfsprogs-dev.git] / libxfs / linux.c
1 /*
2 * Copyright (c) 2000-2002 Silicon Graphics, Inc. All Rights Reserved.
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
34 #include <libxfs.h>
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
42 extern 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
50
51 #define PROC_MOUNTED "/proc/mounts"
52
53 int
54 check_ismounted(char *name, char *block, struct stat64 *s, int verbose)
55 {
56 struct ustat ust;
57 struct stat64 st;
58
59 if (!s) {
60 if (stat64(block, &st) < 0)
61 return 0;
62 if ((st.st_mode & S_IFMT) != S_IFBLK)
63 return 0;
64 s = &st;
65 }
66
67 if (ustat(s->st_rdev, &ust) >= 0) {
68 if (verbose)
69 fprintf(stderr,
70 _("%s: %s contains a mounted filesystem\n"),
71 progname, name);
72 return 1;
73 }
74 return 0;
75 }
76
77 int
78 check_iswritable(char *name, char *block, struct stat64 *s, int fatal)
79 {
80 int sts = 0;
81 FILE *f;
82 struct stat64 mst;
83 struct mntent *mnt;
84 char mounts[MAXPATHLEN];
85
86 strcpy(mounts, access(PROC_MOUNTED, R_OK)? PROC_MOUNTED : MOUNTED);
87 if ((f = setmntent(mounts, "r")) == NULL) {
88 fprintf(stderr, _("%s: %s contains a possibly writable, "
89 "mounted filesystem\n"), progname, name);
90 return fatal;
91 }
92 while ((mnt = getmntent(f)) != NULL) {
93 if (stat64(mnt->mnt_fsname, &mst) < 0)
94 continue;
95 if ((mst.st_mode & S_IFMT) != S_IFBLK)
96 continue;
97 if (mst.st_rdev == s->st_rdev
98 && hasmntopt(mnt, MNTOPT_RO) != NULL)
99 break;
100 }
101 if (mnt != NULL) {
102 fprintf(stderr, _("%s: %s contains a mounted and writable "
103 "filesystem\n"), progname, name);
104 sts = fatal;
105 }
106 endmntent(f);
107 return sts;
108 }
109
110 void
111 set_blocksize(int fd, char *path, int blocksize)
112 {
113 if (ioctl(fd, BLKBSZSET, &blocksize) < 0) {
114 fprintf(stderr, _("%s: warning - cannot set blocksize "
115 "on block device %s: %s\n"),
116 progname, path, strerror(errno));
117 }
118 }
119
120 void
121 flush_device(int fd)
122 {
123 ioctl(fd, BLKFLSBUF, 0);
124 }
125
126 __int64_t
127 findsize(char *path)
128 {
129 int fd;
130 int error;
131 __int64_t ssize;
132 __uint64_t size;
133 struct stat64 st;
134
135 if (stat64(path, &st) < 0) {
136 fprintf(stderr, _("%s: "
137 "cannot stat the device file \"%s\": %s\n"),
138 progname, path, strerror(errno));
139 exit(1);
140 }
141 if ((st.st_mode & S_IFMT) == S_IFREG)
142 return (__int64_t)(st.st_size >> 9);
143
144 if ((fd = open(path, O_RDONLY, 0)) < 0) {
145 fprintf(stderr, _("%s: "
146 "error opening the device special file \"%s\": %s\n"),
147 progname, path, strerror(errno));
148 exit(1);
149 }
150
151 error = ioctl(fd, BLKGETSIZE64, &size);
152 if (error >= 0) {
153 /* BLKGETSIZE64 returns size in bytes not 512-byte blocks */
154 ssize = size >> 9;
155 } else {
156 /* If BLKGETSIZE64 fails, try BLKGETSIZE */
157 unsigned long tmpsize;
158 error = ioctl(fd, BLKGETSIZE, &tmpsize);
159 if (error < 0) {
160 fprintf(stderr, _("%s: can't determine device size\n"),
161 progname);
162 exit(1);
163 }
164 ssize = (__int64_t)tmpsize;
165 }
166
167 close(fd);
168
169 return ssize;
170 }