]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libfrog/linux.c
mkfs: use cvtnum from libfrog
[thirdparty/xfsprogs-dev.git] / libfrog / linux.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6
7 #include <mntent.h>
8 #include <sys/stat.h>
9 #include <sys/ioctl.h>
10 #include <sys/sysinfo.h>
11
12 #include "platform_defs.h"
13 #include "xfs.h"
14 #include "init.h"
15
16 extern char *progname;
17 static int max_block_alignment;
18
19 #ifndef BLKGETSIZE64
20 # define BLKGETSIZE64 _IOR(0x12,114,size_t)
21 #endif
22 #ifndef BLKBSZSET
23 # define BLKBSZSET _IOW(0x12,113,size_t)
24 #endif
25 #ifndef BLKSSZGET
26 # define BLKSSZGET _IO(0x12,104)
27 #endif
28
29 #ifndef RAMDISK_MAJOR
30 #define RAMDISK_MAJOR 1 /* ramdisk major number */
31 #endif
32
33 #define PROC_MOUNTED "/proc/mounts"
34
35 /*
36 * Check if the filesystem is mounted. Be verbose if asked, and
37 * optionally restrict check to /writable/ mounts (i.e. RO is OK)
38 */
39 #define CHECK_MOUNT_VERBOSE 0x1
40 #define CHECK_MOUNT_WRITABLE 0x2
41
42 static int
43 platform_check_mount(char *name, char *block, struct stat *s, int flags)
44 {
45 FILE *f;
46 struct stat st, mst;
47 struct mntent *mnt;
48 char mounts[MAXPATHLEN];
49
50 if (!s) {
51 /* If either fails we are not mounted */
52 if (stat(block, &st) < 0)
53 return 0;
54 if ((st.st_mode & S_IFMT) != S_IFBLK)
55 return 0;
56 s = &st;
57 }
58
59 strcpy(mounts, (!access(PROC_MOUNTED, R_OK)) ? PROC_MOUNTED : MOUNTED);
60 if ((f = setmntent(mounts, "r")) == NULL) {
61 /* Unexpected failure, warn unconditionally */
62 fprintf(stderr,
63 _("%s: %s possibly contains a mounted filesystem\n"),
64 progname, name);
65 return 1;
66 }
67 /*
68 * This whole business is to work out if our block device is mounted
69 * after we lost ustat(2), see:
70 * 4e7a824 libxfs/linux.c: Replace use of ustat by stat
71 * We don't really want to stat every single mounted directory,
72 * as that may include tmpfs, cgroups, procfs or - worst - hung nfs
73 * servers. So first, a simple check: does the "dev" start with "/" ?
74 */
75 while ((mnt = getmntent(f)) != NULL) {
76 if (mnt->mnt_fsname[0] != '/')
77 continue;
78 if (stat(mnt->mnt_dir, &mst) < 0)
79 continue;
80 if (mst.st_dev != s->st_rdev)
81 continue;
82 /* Found our device, is RO OK? */
83 if ((flags & CHECK_MOUNT_WRITABLE) && hasmntopt(mnt, MNTOPT_RO))
84 continue;
85 else
86 break;
87 }
88 endmntent(f);
89
90 /* No mounts contained the condition we were looking for */
91 if (mnt == NULL)
92 return 0;
93
94 if (flags & CHECK_MOUNT_VERBOSE) {
95 if (flags & CHECK_MOUNT_WRITABLE) {
96 fprintf(stderr,
97 _("%s: %s contains a mounted and writable filesystem\n"),
98 progname, name);
99 } else {
100 fprintf(stderr,
101 _("%s: %s contains a mounted filesystem\n"),
102 progname, name);
103 }
104 }
105 return 1;
106 }
107
108 int
109 platform_check_ismounted(char *name, char *block, struct stat *s, int verbose)
110 {
111 int flags;
112
113 flags = verbose ? CHECK_MOUNT_VERBOSE : 0;
114 return platform_check_mount(name, block, s, flags);
115 }
116
117 int
118 platform_check_iswritable(char *name, char *block, struct stat *s)
119 {
120 int flags;
121
122 /* Writable checks are always verbose */
123 flags = CHECK_MOUNT_WRITABLE | CHECK_MOUNT_VERBOSE;
124 return platform_check_mount(name, block, s, flags);
125 }
126
127 int
128 platform_set_blocksize(int fd, char *path, dev_t device, int blocksize, int fatal)
129 {
130 int error = 0;
131
132 if (major(device) != RAMDISK_MAJOR) {
133 if ((error = ioctl(fd, BLKBSZSET, &blocksize)) < 0) {
134 fprintf(stderr, _("%s: %s - cannot set blocksize "
135 "%d on block device %s: %s\n"),
136 progname, fatal ? "error": "warning",
137 blocksize, path, strerror(errno));
138 }
139 }
140 return error;
141 }
142
143 /*
144 * Flush dirty pagecache and disk write cache to stable media. Returns 0 for
145 * success or -1 (with errno set) for failure.
146 */
147 int
148 platform_flush_device(
149 int fd,
150 dev_t device)
151 {
152 struct stat st;
153 int ret;
154
155 if (major(device) == RAMDISK_MAJOR)
156 return 0;
157
158 ret = fsync(fd);
159 if (ret)
160 return ret;
161
162 ret = fstat(fd, &st);
163 if (ret)
164 return ret;
165
166 if (S_ISBLK(st.st_mode))
167 return ioctl(fd, BLKFLSBUF, 0);
168
169 return 0;
170 }
171
172 void
173 platform_findsizes(char *path, int fd, long long *sz, int *bsz)
174 {
175 struct stat st;
176 uint64_t size;
177 int error;
178
179 if (fstat(fd, &st) < 0) {
180 fprintf(stderr, _("%s: "
181 "cannot stat the device file \"%s\": %s\n"),
182 progname, path, strerror(errno));
183 exit(1);
184 }
185
186 if ((st.st_mode & S_IFMT) == S_IFREG) {
187 struct dioattr da;
188
189 *sz = (long long)(st.st_size >> 9);
190
191 if (ioctl(fd, XFS_IOC_DIOINFO, &da) < 0) {
192 /*
193 * fall back to BBSIZE; mkfs might fail if there's a
194 * size mismatch between the image & the host fs...
195 */
196 *bsz = BBSIZE;
197 } else
198 *bsz = da.d_miniosz;
199
200 if (*bsz > max_block_alignment)
201 max_block_alignment = *bsz;
202 return;
203 }
204
205 error = ioctl(fd, BLKGETSIZE64, &size);
206 if (error >= 0) {
207 /* BLKGETSIZE64 returns size in bytes not 512-byte blocks */
208 *sz = (long long)(size >> 9);
209 } else {
210 /* If BLKGETSIZE64 fails, try BLKGETSIZE */
211 unsigned long tmpsize;
212
213 error = ioctl(fd, BLKGETSIZE, &tmpsize);
214 if (error < 0) {
215 fprintf(stderr, _("%s: can't determine device size\n"),
216 progname);
217 exit(1);
218 }
219 *sz = (long long)tmpsize;
220 }
221
222 if (ioctl(fd, BLKSSZGET, bsz) < 0) {
223 fprintf(stderr, _("%s: warning - cannot get sector size "
224 "from block device %s: %s\n"),
225 progname, path, strerror(errno));
226 *bsz = BBSIZE;
227 }
228 if (*bsz > max_block_alignment)
229 max_block_alignment = *bsz;
230 }
231
232 char *
233 platform_findrawpath(char *path)
234 {
235 return path;
236 }
237
238 char *
239 platform_findblockpath(char *path)
240 {
241 return path;
242 }
243
244 int
245 platform_direct_blockdev(void)
246 {
247 return 1;
248 }
249
250 int
251 platform_align_blockdev(void)
252 {
253 if (!max_block_alignment)
254 return getpagesize();
255 return max_block_alignment;
256 }
257
258 /* How many CPUs are online? */
259 int
260 platform_nproc(void)
261 {
262 long nproc = sysconf(_SC_NPROCESSORS_ONLN);
263
264 if (nproc < 1)
265 return 1;
266 if (nproc >= INT_MAX)
267 return INT_MAX;
268 return nproc;
269 }
270
271 unsigned long
272 platform_physmem(void)
273 {
274 struct sysinfo si;
275
276 if (sysinfo(&si) < 0) {
277 fprintf(stderr, _("%s: can't determine memory size\n"),
278 progname);
279 exit(1);
280 }
281 return (si.totalram >> 10) * si.mem_unit; /* kilobytes */
282 }