]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/linux.c
Fix up variable argument handling around vfprintf's.
[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
1d7e80ee 20#include <xfs/libxfs.h>
9440d84d
NS
21#include <mntent.h>
22#include <sys/stat.h>
cb5b3ef4 23#include <aio.h>
9440d84d
NS
24#undef ustat
25#include <sys/ustat.h>
26#include <sys/mount.h>
27#include <sys/ioctl.h>
28
29extern char *progname;
b74a1f6a 30static 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
48int
93d9f139 49platform_check_ismounted(char *name, char *block, struct stat64 *s, int verbose)
9440d84d
NS
50{
51 struct ustat ust;
52 struct stat64 st;
53
54 if (!s) {
55 if (stat64(block, &st) < 0)
56 return 0;
57 if ((st.st_mode & S_IFMT) != S_IFBLK)
58 return 0;
59 s = &st;
60 }
61
62 if (ustat(s->st_rdev, &ust) >= 0) {
63 if (verbose)
64 fprintf(stderr,
65 _("%s: %s contains a mounted filesystem\n"),
66 progname, name);
67 return 1;
68 }
69 return 0;
70}
71
72int
93d9f139 73platform_check_iswritable(char *name, char *block, struct stat64 *s, int fatal)
9440d84d
NS
74{
75 int sts = 0;
76 FILE *f;
77 struct stat64 mst;
78 struct mntent *mnt;
79 char mounts[MAXPATHLEN];
80
fc7180ce 81 strcpy(mounts, (!access(PROC_MOUNTED, R_OK)) ? PROC_MOUNTED : MOUNTED);
9440d84d
NS
82 if ((f = setmntent(mounts, "r")) == NULL) {
83 fprintf(stderr, _("%s: %s contains a possibly writable, "
84 "mounted filesystem\n"), progname, name);
85 return fatal;
86 }
87 while ((mnt = getmntent(f)) != NULL) {
88 if (stat64(mnt->mnt_fsname, &mst) < 0)
89 continue;
90 if ((mst.st_mode & S_IFMT) != S_IFBLK)
91 continue;
92 if (mst.st_rdev == s->st_rdev
93 && hasmntopt(mnt, MNTOPT_RO) != NULL)
94 break;
95 }
507f4e33 96 if (mnt == NULL) {
9440d84d
NS
97 fprintf(stderr, _("%s: %s contains a mounted and writable "
98 "filesystem\n"), progname, name);
99 sts = fatal;
100 }
101 endmntent(f);
102 return sts;
103}
104
105void
76956054 106platform_set_blocksize(int fd, char *path, dev_t device, int blocksize)
9440d84d 107{
76956054
NS
108 if (major(device) != RAMDISK_MAJOR) {
109 if (ioctl(fd, BLKBSZSET, &blocksize) < 0) {
110 fprintf(stderr, _("%s: warning - cannot set blocksize "
111 "on block device %s: %s\n"),
112 progname, path, strerror(errno));
113 }
9440d84d
NS
114 }
115}
116
117void
ac419944 118platform_flush_device(int fd, dev_t device)
9440d84d 119{
ac419944
NS
120 if (major(device) != RAMDISK_MAJOR)
121 ioctl(fd, BLKFLSBUF, 0);
9440d84d
NS
122}
123
f02037ce
NS
124void
125platform_findsizes(char *path, int fd, long long *sz, int *bsz)
9440d84d 126{
9440d84d 127 struct stat64 st;
f02037ce
NS
128 __uint64_t size;
129 int error;
9440d84d 130
f02037ce 131 if (fstat64(fd, &st) < 0) {
9440d84d
NS
132 fprintf(stderr, _("%s: "
133 "cannot stat the device file \"%s\": %s\n"),
134 progname, path, strerror(errno));
135 exit(1);
136 }
f02037ce
NS
137 if ((st.st_mode & S_IFMT) == S_IFREG) {
138 *sz = (long long)(st.st_size >> 9);
139 *bsz = BBSIZE;
b74a1f6a
NS
140 if (BBSIZE > max_block_alignment)
141 max_block_alignment = BBSIZE;
f02037ce 142 return;
9440d84d
NS
143 }
144
145 error = ioctl(fd, BLKGETSIZE64, &size);
146 if (error >= 0) {
147 /* BLKGETSIZE64 returns size in bytes not 512-byte blocks */
f02037ce 148 *sz = (long long)(size >> 9);
9440d84d
NS
149 } else {
150 /* If BLKGETSIZE64 fails, try BLKGETSIZE */
151 unsigned long tmpsize;
f02037ce 152
9440d84d
NS
153 error = ioctl(fd, BLKGETSIZE, &tmpsize);
154 if (error < 0) {
155 fprintf(stderr, _("%s: can't determine device size\n"),
156 progname);
157 exit(1);
158 }
f02037ce 159 *sz = (long long)tmpsize;
9440d84d
NS
160 }
161
f02037ce
NS
162 if (ioctl(fd, BLKSSZGET, bsz) < 0) {
163 fprintf(stderr, _("%s: warning - cannot get sector size "
164 "from block device %s: %s\n"),
165 progname, path, strerror(errno));
166 *bsz = BBSIZE;
167 }
b74a1f6a
NS
168 if (*bsz > max_block_alignment)
169 max_block_alignment = *bsz;
9440d84d 170}
cb5b3ef4
MV
171
172int
173platform_aio_init(int aio_count)
174{
175 struct aioinit lcl_aio_init;
176
177 memset(&lcl_aio_init, 0, sizeof(lcl_aio_init));
178 lcl_aio_init.aio_threads = aio_count;
179 lcl_aio_init.aio_numusers = aio_count;
180
181 aio_init(&lcl_aio_init);
182 return (1); /* aio/lio_listio available */
183}
184
185char *
186platform_findrawpath(char *path)
187{
b74a1f6a
NS
188 return path;
189}
190
191char *
192platform_findblockpath(char *path)
193{
194 return path;
195}
196
197int
198platform_direct_blockdev(void)
199{
200 return 1;
201}
202
203int
204platform_align_blockdev(void)
205{
206 if (!max_block_alignment)
76956054 207 return getpagesize();
b74a1f6a 208 return max_block_alignment;
cb5b3ef4 209}
3b6ac903
MV
210
211int
212platform_nproc(void)
213{
214 return sysconf(_SC_NPROCESSORS_ONLN);
215}