2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 2000 Peter Edwards
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by Peter Edwards
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
39 #include <sys/cdefs.h>
42 #include <sys/param.h>
46 #include <sys/vnode.h>
48 #include <netinet/in.h>
51 #include <sys/mount.h>
52 #include <fs/msdosfs/bpb.h>
53 #include <fs/msdosfs/msdosfsmount.h>
56 #include <fs/msdosfs/denode.h>
57 #include <fs/msdosfs/direntry.h>
58 #include <fs/msdosfs/fat.h>
67 * VTODE is defined in denode.h only if _KERNEL is defined, but that leads to
70 #define VTODE(vp) ((struct denode *)getvnodedata(vp))
72 #include "libprocstat.h"
73 #include "common_kvm.h"
76 struct dosmount *next;
77 struct msdosfsmount *kptr; /* Pointer in kernel space */
78 struct msdosfsmount data; /* User space copy of structure */
82 msdosfs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn)
85 static struct dosmount *mounts;
90 if (!kvm_read_all(kd, (unsigned long)VTODE(vp), &denode,
92 warnx("can't read denode at %p", (void *)VTODE(vp));
97 * Find msdosfsmount structure for the vnode's filesystem. Needed
98 * for some filesystem parameters
100 for (mnt = mounts; mnt; mnt = mnt->next)
101 if (mnt->kptr == denode.de_pmp)
105 if ((mnt = malloc(sizeof(struct dosmount))) == NULL) {
109 if (!kvm_read_all(kd, (unsigned long)denode.de_pmp,
110 &mnt->data, sizeof(mnt->data))) {
112 warnx("can't read mount info at %p",
113 (void *)denode.de_pmp);
118 mnt->kptr = denode.de_pmp;
121 vn->vn_fsid = dev2udev(kd, mnt->data.pm_dev);
123 vn->vn_mode |= denode.de_Attributes & ATTR_READONLY ? 0 : 0222;
124 vn->vn_mode &= mnt->data.pm_mask;
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;
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.
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
141 dirsperblk = mnt->data.pm_BytesPerSec / sizeof(struct direntry);
142 if (denode.de_Attributes & ATTR_DIRECTORY) {
143 fileid = cntobn(&mnt->data, denode.de_StartCluster)
145 if (denode.de_StartCluster == MSDOSFSROOT)
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);
154 vn->vn_fileid = fileid;