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