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