xref: /netbsd/sys/compat/linux/common/linux_statfs.h (revision 3b97c0bd)
1 /*	$NetBSD: linux_statfs.h,v 1.7 2019/08/23 13:36:45 maxv Exp $	*/
2 
3 /*-
4  * Copyright (c) 1995, 1998, 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Frank van der Linden and Eric Haszlakiewicz; by Jason R. Thorpe
9  * of the Numerical Aerospace Simulation Facility, NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef _LINUX_STATFS_H
34 #define _LINUX_STATFS_H
35 
36 static void __unused bsd_to_linux_statfs(const struct statvfs *,
37     struct linux_statfs *);
38 static void __unused bsd_to_linux_statfs64(const struct statvfs *,
39     struct linux_statfs64 *);
40 
41 /*
42  * Convert NetBSD statvfs structure to Linux statfs structure.
43  * Linux doesn't have f_flag, and we can't set f_frsize due
44  * to glibc statvfs() bug (see below).
45  */
46 static void __unused
bsd_to_linux_statfs(const struct statvfs * bsp,struct linux_statfs * lsp)47 bsd_to_linux_statfs(const struct statvfs *bsp, struct linux_statfs *lsp)
48 {
49 	int i;
50 
51 	memset(lsp, 0, sizeof(*lsp));
52 
53 	for (i = 0; i < linux_fstypes_cnt; i++) {
54 		if (strcmp(bsp->f_fstypename, linux_fstypes[i].mty_bsd) == 0) {
55 			lsp->l_ftype = linux_fstypes[i].mty_linux;
56 			break;
57 		}
58 	}
59 
60 	if (i == linux_fstypes_cnt) {
61 		lsp->l_ftype = LINUX_DEFAULT_SUPER_MAGIC;
62 	}
63 
64 	/*
65 	 * The sizes are expressed in number of blocks. The block
66 	 * size used for the size is f_frsize for POSIX-compliant
67 	 * statvfs. Linux statfs uses f_bsize as the block size
68 	 * (f_frsize used to not be available in Linux struct statfs).
69 	 * However, glibc 2.3.3 statvfs() wrapper fails to adjust the block
70 	 * counts for different f_frsize if f_frsize is provided by the kernel.
71 	 * POSIX conforming apps thus get wrong size if f_frsize
72 	 * is different to f_bsize. Thus, we just pretend we don't
73 	 * support f_frsize.
74 	 */
75 
76 	lsp->l_fbsize = bsp->f_frsize;
77 	lsp->l_ffrsize = 0;			/* compat */
78 	lsp->l_fblocks = bsp->f_blocks;
79 	lsp->l_fbfree = bsp->f_bfree;
80 	lsp->l_fbavail = bsp->f_bavail;
81 	lsp->l_ffiles = bsp->f_files;
82 	lsp->l_fffree = bsp->f_ffree;
83 	/* Linux sets the fsid to 0..., we don't */
84 	lsp->l_ffsid.val[0] = bsp->f_fsidx.__fsid_val[0];
85 	lsp->l_ffsid.val[1] = bsp->f_fsidx.__fsid_val[1];
86 	lsp->l_fnamelen = bsp->f_namemax;
87 }
88 
89 /*
90  * Convert NetBSD statvfs structure to Linux statfs64 structure.
91  * See comments in bsd_to_linux_statfs() for further background.
92  * We can safely pass correct bsize and frsize here, since Linux glibc
93  * statvfs() doesn't use statfs64().
94  */
95 static void __unused
bsd_to_linux_statfs64(const struct statvfs * bsp,struct linux_statfs64 * lsp)96 bsd_to_linux_statfs64(const struct statvfs *bsp, struct linux_statfs64 *lsp)
97 {
98 	int i, div;
99 
100 	memset(lsp, 0, sizeof(*lsp));
101 
102 	for (i = 0; i < linux_fstypes_cnt; i++) {
103 		if (strcmp(bsp->f_fstypename, linux_fstypes[i].mty_bsd) == 0) {
104 			lsp->l_ftype = linux_fstypes[i].mty_linux;
105 			break;
106 		}
107 	}
108 
109 	if (i == linux_fstypes_cnt) {
110 		lsp->l_ftype = LINUX_DEFAULT_SUPER_MAGIC;
111 	}
112 
113 	div = bsp->f_frsize ? (bsp->f_bsize / bsp->f_frsize) : 1;
114 	if (div == 0)
115 		div = 1;
116 	lsp->l_fbsize = bsp->f_bsize;
117 	lsp->l_ffrsize = bsp->f_frsize;
118 	lsp->l_fblocks = bsp->f_blocks / div;
119 	lsp->l_fbfree = bsp->f_bfree / div;
120 	lsp->l_fbavail = bsp->f_bavail / div;
121 	lsp->l_ffiles = bsp->f_files;
122 	lsp->l_fffree = bsp->f_ffree / div;
123 	/* Linux sets the fsid to 0..., we don't */
124 	lsp->l_ffsid.val[0] = bsp->f_fsidx.__fsid_val[0];
125 	lsp->l_ffsid.val[1] = bsp->f_fsidx.__fsid_val[1];
126 	lsp->l_fnamelen = bsp->f_namemax;
127 }
128 
129 #endif /* !_LINUX_STATFS_H */
130