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