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