]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libfrog/linux.c
libxfs: return flush failures
[thirdparty/xfsprogs-dev.git] / libfrog / linux.c
CommitLineData
959ef981 1// SPDX-License-Identifier: GPL-2.0
9440d84d 2/*
da23017d
NS
3 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
9440d84d
NS
5 */
6
9440d84d
NS
7#include <mntent.h>
8#include <sys/stat.h>
9440d84d 9#include <sys/ioctl.h>
2556c98b 10#include <sys/sysinfo.h>
9440d84d 11
33c692a2
DW
12#include "platform_defs.h"
13#include "xfs.h"
12812407 14#include "init.h"
b626fb59 15
9440d84d 16extern char *progname;
b74a1f6a 17static int max_block_alignment;
9440d84d
NS
18
19#ifndef BLKGETSIZE64
7f090a57 20# define BLKGETSIZE64 _IOR(0x12,114,size_t)
9440d84d
NS
21#endif
22#ifndef BLKBSZSET
7f090a57 23# define BLKBSZSET _IOW(0x12,113,size_t)
9440d84d 24#endif
74668075
NS
25#ifndef BLKSSZGET
26# define BLKSSZGET _IO(0x12,104)
27#endif
9440d84d 28
ac419944
NS
29#ifndef RAMDISK_MAJOR
30#define RAMDISK_MAJOR 1 /* ramdisk major number */
31#endif
32
9440d84d
NS
33#define PROC_MOUNTED "/proc/mounts"
34
4d457e24
ES
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
42static int
f594a0d1 43platform_check_mount(char *name, char *block, struct stat *s, int flags)
9440d84d 44{
4e7a824f 45 FILE *f;
f594a0d1 46 struct stat st, mst;
4e7a824f
FJ
47 struct mntent *mnt;
48 char mounts[MAXPATHLEN];
9440d84d
NS
49
50 if (!s) {
4d457e24 51 /* If either fails we are not mounted */
f594a0d1 52 if (stat(block, &st) < 0)
9440d84d
NS
53 return 0;
54 if ((st.st_mode & S_IFMT) != S_IFBLK)
55 return 0;
56 s = &st;
57 }
58
4e7a824f
FJ
59 strcpy(mounts, (!access(PROC_MOUNTED, R_OK)) ? PROC_MOUNTED : MOUNTED);
60 if ((f = setmntent(mounts, "r")) == NULL) {
4d457e24 61 /* Unexpected failure, warn unconditionally */
4e7a824f
FJ
62 fprintf(stderr,
63 _("%s: %s possibly contains a mounted filesystem\n"),
64 progname, name);
65 return 1;
66 }
98c4a01c
ES
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 */
4e7a824f 75 while ((mnt = getmntent(f)) != NULL) {
98c4a01c
ES
76 if (mnt->mnt_fsname[0] != '/')
77 continue;
f594a0d1 78 if (stat(mnt->mnt_dir, &mst) < 0)
4e7a824f
FJ
79 continue;
80 if (mst.st_dev != s->st_rdev)
81 continue;
4d457e24
ES
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);
4e7a824f 89
4d457e24
ES
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 {
9440d84d 100 fprintf(stderr,
4d457e24 101_("%s: %s contains a mounted filesystem\n"),
9440d84d 102 progname, name);
4d457e24 103 }
9440d84d 104 }
4d457e24 105 return 1;
9440d84d
NS
106}
107
108int
f594a0d1 109platform_check_ismounted(char *name, char *block, struct stat *s, int verbose)
9440d84d 110{
4d457e24 111 int flags;
9440d84d 112
4d457e24
ES
113 flags = verbose ? CHECK_MOUNT_VERBOSE : 0;
114 return platform_check_mount(name, block, s, flags);
115}
7f510afb 116
4d457e24 117int
f594a0d1 118platform_check_iswritable(char *name, char *block, struct stat *s)
4d457e24
ES
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);
9440d84d
NS
125}
126
edd45774
TS
127int
128platform_set_blocksize(int fd, char *path, dev_t device, int blocksize, int fatal)
9440d84d 129{
edd45774
TS
130 int error = 0;
131
76956054 132 if (major(device) != RAMDISK_MAJOR) {
edd45774
TS
133 if ((error = ioctl(fd, BLKBSZSET, &blocksize)) < 0) {
134 fprintf(stderr, _("%s: %s - cannot set blocksize "
fd5eda53 135 "%d on block device %s: %s\n"),
edd45774 136 progname, fatal ? "error": "warning",
fd5eda53 137 blocksize, path, strerror(errno));
76956054 138 }
9440d84d 139 }
edd45774 140 return error;
9440d84d
NS
141}
142
a688242b
DW
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 */
147int
148platform_flush_device(
149 int fd,
150 dev_t device)
9440d84d 151{
f594a0d1 152 struct stat st;
a688242b
DW
153 int ret;
154
06ac92fd 155 if (major(device) == RAMDISK_MAJOR)
a688242b 156 return 0;
06ac92fd 157
a688242b
DW
158 ret = fstat(fd, &st);
159 if (ret)
160 return ret;
06ac92fd
DC
161
162 if (S_ISREG(st.st_mode))
a688242b
DW
163 return fsync(fd);
164
165 return ioctl(fd, BLKFLSBUF, 0);
9440d84d
NS
166}
167
f02037ce
NS
168void
169platform_findsizes(char *path, int fd, long long *sz, int *bsz)
9440d84d 170{
f594a0d1 171 struct stat st;
14f8b681 172 uint64_t size;
f02037ce 173 int error;
9440d84d 174
f594a0d1 175 if (fstat(fd, &st) < 0) {
9440d84d
NS
176 fprintf(stderr, _("%s: "
177 "cannot stat the device file \"%s\": %s\n"),
178 progname, path, strerror(errno));
179 exit(1);
180 }
98dd72d3 181
f02037ce 182 if ((st.st_mode & S_IFMT) == S_IFREG) {
98dd72d3 183 struct dioattr da;
5a7d5937 184
f02037ce 185 *sz = (long long)(st.st_size >> 9);
98dd72d3
ES
186
187 if (ioctl(fd, XFS_IOC_DIOINFO, &da) < 0) {
5a7d5937
ES
188 /*
189 * fall back to BBSIZE; mkfs might fail if there's a
190 * size mismatch between the image & the host fs...
191 */
192 *bsz = BBSIZE;
193 } else
98dd72d3 194 *bsz = da.d_miniosz;
5a7d5937
ES
195
196 if (*bsz > max_block_alignment)
197 max_block_alignment = *bsz;
f02037ce 198 return;
9440d84d
NS
199 }
200
201 error = ioctl(fd, BLKGETSIZE64, &size);
202 if (error >= 0) {
203 /* BLKGETSIZE64 returns size in bytes not 512-byte blocks */
f02037ce 204 *sz = (long long)(size >> 9);
9440d84d
NS
205 } else {
206 /* If BLKGETSIZE64 fails, try BLKGETSIZE */
207 unsigned long tmpsize;
f02037ce 208
9440d84d
NS
209 error = ioctl(fd, BLKGETSIZE, &tmpsize);
210 if (error < 0) {
211 fprintf(stderr, _("%s: can't determine device size\n"),
212 progname);
213 exit(1);
214 }
f02037ce 215 *sz = (long long)tmpsize;
9440d84d
NS
216 }
217
f02037ce
NS
218 if (ioctl(fd, BLKSSZGET, bsz) < 0) {
219 fprintf(stderr, _("%s: warning - cannot get sector size "
220 "from block device %s: %s\n"),
221 progname, path, strerror(errno));
222 *bsz = BBSIZE;
223 }
b74a1f6a
NS
224 if (*bsz > max_block_alignment)
225 max_block_alignment = *bsz;
9440d84d 226}
cb5b3ef4 227
cb5b3ef4
MV
228char *
229platform_findrawpath(char *path)
230{
b74a1f6a
NS
231 return path;
232}
233
234char *
235platform_findblockpath(char *path)
236{
237 return path;
238}
239
240int
241platform_direct_blockdev(void)
242{
243 return 1;
244}
245
246int
247platform_align_blockdev(void)
248{
249 if (!max_block_alignment)
76956054 250 return getpagesize();
b74a1f6a 251 return max_block_alignment;
cb5b3ef4 252}
3b6ac903 253
d530e589 254/* How many CPUs are online? */
3b6ac903
MV
255int
256platform_nproc(void)
257{
d530e589
DW
258 long nproc = sysconf(_SC_NPROCESSORS_ONLN);
259
260 if (nproc < 1)
261 return 1;
262 if (nproc >= INT_MAX)
263 return INT_MAX;
264 return nproc;
3b6ac903 265}
2556c98b
BN
266
267unsigned long
268platform_physmem(void)
269{
270 struct sysinfo si;
271
272 if (sysinfo(&si) < 0) {
273 fprintf(stderr, _("%s: can't determine memory size\n"),
274 progname);
275 exit(1);
276 }
277 return (si.totalram >> 10) * si.mem_unit; /* kilobytes */
278}