xref: /freebsd/lib/libprocstat/msdosfs.c (revision 81ad6265)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 2000 Peter Edwards
5  * Copyright (c) 1988, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by Peter Edwards
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include <stdbool.h>
43 
44 #include <sys/param.h>
45 #include <sys/buf.h>
46 #include <sys/time.h>
47 #include <sys/stat.h>
48 #include <sys/vnode.h>
49 
50 #include <netinet/in.h>
51 
52 #define	_KERNEL
53 #include <sys/mount.h>
54 #include <fs/msdosfs/bpb.h>
55 #include <fs/msdosfs/msdosfsmount.h>
56 #undef _KERNEL
57 
58 #include <fs/msdosfs/denode.h>
59 #include <fs/msdosfs/direntry.h>
60 #include <fs/msdosfs/fat.h>
61 
62 #include <err.h>
63 #include <kvm.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 
67 /*
68  * XXX -
69  * VTODE is defined in denode.h only if _KERNEL is defined, but that leads to
70  * header explosion
71  */
72 #define VTODE(vp) ((struct denode *)getvnodedata(vp))
73 
74 #include "libprocstat.h"
75 #include "common_kvm.h"
76 
77 struct dosmount {
78 	struct dosmount *next;
79 	struct msdosfsmount *kptr;	/* Pointer in kernel space */
80 	struct msdosfsmount data;	/* User space copy of structure */
81 };
82 
83 int
84 msdosfs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn)
85 {
86 	struct denode denode;
87 	static struct dosmount *mounts;
88 	struct dosmount *mnt;
89 	u_long dirsperblk;
90 	int fileid;
91 
92 	if (!kvm_read_all(kd, (unsigned long)VTODE(vp), &denode,
93 	    sizeof(denode))) {
94 		warnx("can't read denode at %p", (void *)VTODE(vp));
95 		return (1);
96 	}
97 
98 	/*
99 	 * Find msdosfsmount structure for the vnode's filesystem. Needed
100 	 * for some filesystem parameters
101 	 */
102 	for (mnt = mounts; mnt; mnt = mnt->next)
103 		if (mnt->kptr == denode.de_pmp)
104 			break;
105 
106 	if (!mnt) {
107 		if ((mnt = malloc(sizeof(struct dosmount))) == NULL) {
108 			warn("malloc()");
109 			return (1);
110 		}
111 		if (!kvm_read_all(kd, (unsigned long)denode.de_pmp,
112 		    &mnt->data, sizeof(mnt->data))) {
113 			free(mnt);
114 			    warnx("can't read mount info at %p",
115 			    (void *)denode.de_pmp);
116 			return (1);
117 		}
118 		mnt->next = mounts;
119 		mounts = mnt;
120 		mnt->kptr = denode.de_pmp;
121 	}
122 
123 	vn->vn_fsid = dev2udev(kd, mnt->data.pm_dev);
124 	vn->vn_mode = 0555;
125 	vn->vn_mode |= denode.de_Attributes & ATTR_READONLY ? 0 : 0222;
126 	vn->vn_mode &= mnt->data.pm_mask;
127 
128 	/* Distinguish directories and files. No "special" files in FAT. */
129 	vn->vn_mode |= denode.de_Attributes & ATTR_DIRECTORY ? S_IFDIR : S_IFREG;
130 	vn->vn_size = denode.de_FileSize;
131 
132 	/*
133 	 * XXX -
134 	 * Culled from msdosfs_vnops.c. There appears to be a problem
135 	 * here, in that a directory has the same inode number as the first
136 	 * file in the directory. stat(2) suffers from this problem also, so
137 	 * I won't try to fix it here.
138 	 *
139 	 * The following computation of the fileid must be the same as that
140 	 * used in msdosfs_readdir() to compute d_fileno. If not, pwd
141 	 * doesn't work.
142 	 */
143 	dirsperblk = mnt->data.pm_BytesPerSec / sizeof(struct direntry);
144 	if (denode.de_Attributes & ATTR_DIRECTORY) {
145 		fileid = cntobn(&mnt->data, denode.de_StartCluster)
146 		    * dirsperblk;
147 		if (denode.de_StartCluster == MSDOSFSROOT)
148 			fileid = 1;
149 	} else {
150 		fileid = cntobn(&mnt->data, denode.de_dirclust) * dirsperblk;
151 		if (denode.de_dirclust == MSDOSFSROOT)
152 			fileid = roottobn(&mnt->data, 0) * dirsperblk;
153 		fileid += denode.de_diroffset / sizeof(struct direntry);
154 	}
155 
156 	vn->vn_fileid = fileid;
157 	return (0);
158 }
159