xref: /freebsd/lib/libprocstat/msdosfs.c (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
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 #include <stdbool.h>
41 
42 #include <sys/param.h>
43 #include <sys/buf.h>
44 #include <sys/time.h>
45 #include <sys/stat.h>
46 #include <sys/vnode.h>
47 
48 #include <netinet/in.h>
49 
50 #define	_KERNEL
51 #include <sys/mount.h>
52 #include <fs/msdosfs/bpb.h>
53 #include <fs/msdosfs/msdosfsmount.h>
54 #undef _KERNEL
55 
56 #include <fs/msdosfs/denode.h>
57 #include <fs/msdosfs/direntry.h>
58 #include <fs/msdosfs/fat.h>
59 
60 #include <err.h>
61 #include <kvm.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 
65 /*
66  * XXX -
67  * VTODE is defined in denode.h only if _KERNEL is defined, but that leads to
68  * header explosion
69  */
70 #define VTODE(vp) ((struct denode *)getvnodedata(vp))
71 
72 #include "libprocstat.h"
73 #include "common_kvm.h"
74 
75 struct dosmount {
76 	struct dosmount *next;
77 	struct msdosfsmount *kptr;	/* Pointer in kernel space */
78 	struct msdosfsmount data;	/* User space copy of structure */
79 };
80 
81 int
82 msdosfs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn)
83 {
84 	struct denode denode;
85 	static struct dosmount *mounts;
86 	struct dosmount *mnt;
87 	u_long dirsperblk;
88 	int fileid;
89 
90 	if (!kvm_read_all(kd, (unsigned long)VTODE(vp), &denode,
91 	    sizeof(denode))) {
92 		warnx("can't read denode at %p", (void *)VTODE(vp));
93 		return (1);
94 	}
95 
96 	/*
97 	 * Find msdosfsmount structure for the vnode's filesystem. Needed
98 	 * for some filesystem parameters
99 	 */
100 	for (mnt = mounts; mnt; mnt = mnt->next)
101 		if (mnt->kptr == denode.de_pmp)
102 			break;
103 
104 	if (!mnt) {
105 		if ((mnt = malloc(sizeof(struct dosmount))) == NULL) {
106 			warn("malloc()");
107 			return (1);
108 		}
109 		if (!kvm_read_all(kd, (unsigned long)denode.de_pmp,
110 		    &mnt->data, sizeof(mnt->data))) {
111 			free(mnt);
112 			    warnx("can't read mount info at %p",
113 			    (void *)denode.de_pmp);
114 			return (1);
115 		}
116 		mnt->next = mounts;
117 		mounts = mnt;
118 		mnt->kptr = denode.de_pmp;
119 	}
120 
121 	vn->vn_fsid = dev2udev(kd, mnt->data.pm_dev);
122 	vn->vn_mode = 0555;
123 	vn->vn_mode |= denode.de_Attributes & ATTR_READONLY ? 0 : 0222;
124 	vn->vn_mode &= mnt->data.pm_mask;
125 
126 	/* Distinguish directories and files. No "special" files in FAT. */
127 	vn->vn_mode |= denode.de_Attributes & ATTR_DIRECTORY ? S_IFDIR : S_IFREG;
128 	vn->vn_size = denode.de_FileSize;
129 
130 	/*
131 	 * XXX -
132 	 * Culled from msdosfs_vnops.c. There appears to be a problem
133 	 * here, in that a directory has the same inode number as the first
134 	 * file in the directory. stat(2) suffers from this problem also, so
135 	 * I won't try to fix it here.
136 	 *
137 	 * The following computation of the fileid must be the same as that
138 	 * used in msdosfs_readdir() to compute d_fileno. If not, pwd
139 	 * doesn't work.
140 	 */
141 	dirsperblk = mnt->data.pm_BytesPerSec / sizeof(struct direntry);
142 	if (denode.de_Attributes & ATTR_DIRECTORY) {
143 		fileid = cntobn(&mnt->data, denode.de_StartCluster)
144 		    * dirsperblk;
145 		if (denode.de_StartCluster == MSDOSFSROOT)
146 			fileid = 1;
147 	} else {
148 		fileid = cntobn(&mnt->data, denode.de_dirclust) * dirsperblk;
149 		if (denode.de_dirclust == MSDOSFSROOT)
150 			fileid = roottobn(&mnt->data, 0) * dirsperblk;
151 		fileid += denode.de_diroffset / sizeof(struct direntry);
152 	}
153 
154 	vn->vn_fileid = fileid;
155 	return (0);
156 }
157