]>
Commit | Line | Data |
---|---|---|
9440d84d | 1 | /* |
da23017d NS |
2 | * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
9440d84d | 4 | * |
da23017d NS |
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 | |
9440d84d NS |
7 | * published by the Free Software Foundation. |
8 | * | |
da23017d NS |
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. | |
9440d84d | 13 | * |
da23017d NS |
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 | |
9440d84d NS |
17 | */ |
18 | ||
9440d84d NS |
19 | #include <mntent.h> |
20 | #include <sys/stat.h> | |
9440d84d NS |
21 | #include <sys/mount.h> |
22 | #include <sys/ioctl.h> | |
2556c98b | 23 | #include <sys/sysinfo.h> |
9440d84d | 24 | |
9c799827 | 25 | #include "libxfs_priv.h" |
b626fb59 DC |
26 | #include "xfs_fs.h" |
27 | ||
e321f629 | 28 | int platform_has_uuid = 1; |
9440d84d | 29 | extern char *progname; |
b74a1f6a | 30 | static int max_block_alignment; |
9440d84d NS |
31 | |
32 | #ifndef BLKGETSIZE64 | |
7f090a57 | 33 | # define BLKGETSIZE64 _IOR(0x12,114,size_t) |
9440d84d NS |
34 | #endif |
35 | #ifndef BLKBSZSET | |
7f090a57 | 36 | # define BLKBSZSET _IOW(0x12,113,size_t) |
9440d84d | 37 | #endif |
74668075 NS |
38 | #ifndef BLKSSZGET |
39 | # define BLKSSZGET _IO(0x12,104) | |
40 | #endif | |
9440d84d | 41 | |
ac419944 NS |
42 | #ifndef RAMDISK_MAJOR |
43 | #define RAMDISK_MAJOR 1 /* ramdisk major number */ | |
44 | #endif | |
45 | ||
9440d84d NS |
46 | #define PROC_MOUNTED "/proc/mounts" |
47 | ||
48 | int | |
93d9f139 | 49 | platform_check_ismounted(char *name, char *block, struct stat64 *s, int verbose) |
9440d84d | 50 | { |
4e7a824f FJ |
51 | FILE *f; |
52 | struct stat64 st, mst; | |
53 | struct mntent *mnt; | |
54 | char mounts[MAXPATHLEN]; | |
9440d84d NS |
55 | |
56 | if (!s) { | |
57 | if (stat64(block, &st) < 0) | |
58 | return 0; | |
59 | if ((st.st_mode & S_IFMT) != S_IFBLK) | |
60 | return 0; | |
61 | s = &st; | |
62 | } | |
63 | ||
4e7a824f FJ |
64 | strcpy(mounts, (!access(PROC_MOUNTED, R_OK)) ? PROC_MOUNTED : MOUNTED); |
65 | if ((f = setmntent(mounts, "r")) == NULL) { | |
66 | fprintf(stderr, | |
67 | _("%s: %s possibly contains a mounted filesystem\n"), | |
68 | progname, name); | |
69 | return 1; | |
70 | } | |
71 | while ((mnt = getmntent(f)) != NULL) { | |
72 | if (stat64(mnt->mnt_dir, &mst) < 0) | |
73 | continue; | |
74 | if (mst.st_dev != s->st_rdev) | |
75 | continue; | |
76 | ||
9440d84d NS |
77 | if (verbose) |
78 | fprintf(stderr, | |
79 | _("%s: %s contains a mounted filesystem\n"), | |
80 | progname, name); | |
4e7a824f | 81 | break; |
9440d84d | 82 | } |
4e7a824f FJ |
83 | endmntent(f); |
84 | return mnt != NULL; | |
9440d84d NS |
85 | } |
86 | ||
87 | int | |
93d9f139 | 88 | platform_check_iswritable(char *name, char *block, struct stat64 *s, int fatal) |
9440d84d NS |
89 | { |
90 | int sts = 0; | |
91 | FILE *f; | |
92 | struct stat64 mst; | |
93 | struct mntent *mnt; | |
94 | char mounts[MAXPATHLEN]; | |
95 | ||
fc7180ce | 96 | strcpy(mounts, (!access(PROC_MOUNTED, R_OK)) ? PROC_MOUNTED : MOUNTED); |
9440d84d NS |
97 | if ((f = setmntent(mounts, "r")) == NULL) { |
98 | fprintf(stderr, _("%s: %s contains a possibly writable, " | |
99 | "mounted filesystem\n"), progname, name); | |
100 | return fatal; | |
101 | } | |
102 | while ((mnt = getmntent(f)) != NULL) { | |
103 | if (stat64(mnt->mnt_fsname, &mst) < 0) | |
104 | continue; | |
105 | if ((mst.st_mode & S_IFMT) != S_IFBLK) | |
106 | continue; | |
107 | if (mst.st_rdev == s->st_rdev | |
108 | && hasmntopt(mnt, MNTOPT_RO) != NULL) | |
109 | break; | |
110 | } | |
507f4e33 | 111 | if (mnt == NULL) { |
9440d84d NS |
112 | fprintf(stderr, _("%s: %s contains a mounted and writable " |
113 | "filesystem\n"), progname, name); | |
114 | sts = fatal; | |
115 | } | |
116 | endmntent(f); | |
117 | return sts; | |
118 | } | |
119 | ||
edd45774 TS |
120 | int |
121 | platform_set_blocksize(int fd, char *path, dev_t device, int blocksize, int fatal) | |
9440d84d | 122 | { |
edd45774 TS |
123 | int error = 0; |
124 | ||
76956054 | 125 | if (major(device) != RAMDISK_MAJOR) { |
edd45774 TS |
126 | if ((error = ioctl(fd, BLKBSZSET, &blocksize)) < 0) { |
127 | fprintf(stderr, _("%s: %s - cannot set blocksize " | |
fd5eda53 | 128 | "%d on block device %s: %s\n"), |
edd45774 | 129 | progname, fatal ? "error": "warning", |
fd5eda53 | 130 | blocksize, path, strerror(errno)); |
76956054 | 131 | } |
9440d84d | 132 | } |
edd45774 | 133 | return error; |
9440d84d NS |
134 | } |
135 | ||
136 | void | |
ac419944 | 137 | platform_flush_device(int fd, dev_t device) |
9440d84d | 138 | { |
06ac92fd DC |
139 | struct stat64 st; |
140 | if (major(device) == RAMDISK_MAJOR) | |
141 | return; | |
142 | ||
143 | if (fstat64(fd, &st) < 0) | |
144 | return; | |
145 | ||
146 | if (S_ISREG(st.st_mode)) | |
147 | fsync(fd); | |
148 | else | |
ac419944 | 149 | ioctl(fd, BLKFLSBUF, 0); |
9440d84d NS |
150 | } |
151 | ||
f02037ce NS |
152 | void |
153 | platform_findsizes(char *path, int fd, long long *sz, int *bsz) | |
9440d84d | 154 | { |
9440d84d | 155 | struct stat64 st; |
f02037ce NS |
156 | __uint64_t size; |
157 | int error; | |
9440d84d | 158 | |
f02037ce | 159 | if (fstat64(fd, &st) < 0) { |
9440d84d NS |
160 | fprintf(stderr, _("%s: " |
161 | "cannot stat the device file \"%s\": %s\n"), | |
162 | progname, path, strerror(errno)); | |
163 | exit(1); | |
164 | } | |
98dd72d3 | 165 | |
f02037ce | 166 | if ((st.st_mode & S_IFMT) == S_IFREG) { |
98dd72d3 | 167 | struct dioattr da; |
5a7d5937 | 168 | |
f02037ce | 169 | *sz = (long long)(st.st_size >> 9); |
98dd72d3 ES |
170 | |
171 | if (ioctl(fd, XFS_IOC_DIOINFO, &da) < 0) { | |
5a7d5937 ES |
172 | /* |
173 | * fall back to BBSIZE; mkfs might fail if there's a | |
174 | * size mismatch between the image & the host fs... | |
175 | */ | |
176 | *bsz = BBSIZE; | |
177 | } else | |
98dd72d3 | 178 | *bsz = da.d_miniosz; |
5a7d5937 ES |
179 | |
180 | if (*bsz > max_block_alignment) | |
181 | max_block_alignment = *bsz; | |
f02037ce | 182 | return; |
9440d84d NS |
183 | } |
184 | ||
185 | error = ioctl(fd, BLKGETSIZE64, &size); | |
186 | if (error >= 0) { | |
187 | /* BLKGETSIZE64 returns size in bytes not 512-byte blocks */ | |
f02037ce | 188 | *sz = (long long)(size >> 9); |
9440d84d NS |
189 | } else { |
190 | /* If BLKGETSIZE64 fails, try BLKGETSIZE */ | |
191 | unsigned long tmpsize; | |
f02037ce | 192 | |
9440d84d NS |
193 | error = ioctl(fd, BLKGETSIZE, &tmpsize); |
194 | if (error < 0) { | |
195 | fprintf(stderr, _("%s: can't determine device size\n"), | |
196 | progname); | |
197 | exit(1); | |
198 | } | |
f02037ce | 199 | *sz = (long long)tmpsize; |
9440d84d NS |
200 | } |
201 | ||
f02037ce NS |
202 | if (ioctl(fd, BLKSSZGET, bsz) < 0) { |
203 | fprintf(stderr, _("%s: warning - cannot get sector size " | |
204 | "from block device %s: %s\n"), | |
205 | progname, path, strerror(errno)); | |
206 | *bsz = BBSIZE; | |
207 | } | |
b74a1f6a NS |
208 | if (*bsz > max_block_alignment) |
209 | max_block_alignment = *bsz; | |
9440d84d | 210 | } |
cb5b3ef4 | 211 | |
cb5b3ef4 MV |
212 | char * |
213 | platform_findrawpath(char *path) | |
214 | { | |
b74a1f6a NS |
215 | return path; |
216 | } | |
217 | ||
218 | char * | |
219 | platform_findblockpath(char *path) | |
220 | { | |
221 | return path; | |
222 | } | |
223 | ||
224 | int | |
225 | platform_direct_blockdev(void) | |
226 | { | |
227 | return 1; | |
228 | } | |
229 | ||
230 | int | |
231 | platform_align_blockdev(void) | |
232 | { | |
233 | if (!max_block_alignment) | |
76956054 | 234 | return getpagesize(); |
b74a1f6a | 235 | return max_block_alignment; |
cb5b3ef4 | 236 | } |
3b6ac903 MV |
237 | |
238 | int | |
239 | platform_nproc(void) | |
240 | { | |
241 | return sysconf(_SC_NPROCESSORS_ONLN); | |
242 | } | |
2556c98b BN |
243 | |
244 | unsigned long | |
245 | platform_physmem(void) | |
246 | { | |
247 | struct sysinfo si; | |
248 | ||
249 | if (sysinfo(&si) < 0) { | |
250 | fprintf(stderr, _("%s: can't determine memory size\n"), | |
251 | progname); | |
252 | exit(1); | |
253 | } | |
254 | return (si.totalram >> 10) * si.mem_unit; /* kilobytes */ | |
255 | } |