1*0b082c0eSderaadt /* $OpenBSD: kvm_udf.c,v 1.11 2021/09/10 00:02:43 deraadt Exp $ */
2de71b0ceSmillert
3de71b0ceSmillert /*
4de71b0ceSmillert * Copyright (c) 2001, 2002 Scott Long <scottl@freebsd.org>
5de71b0ceSmillert * All rights reserved.
6de71b0ceSmillert *
7de71b0ceSmillert * Redistribution and use in source and binary forms, with or without
8de71b0ceSmillert * modification, are permitted provided that the following conditions
9de71b0ceSmillert * are met:
10de71b0ceSmillert * 1. Redistributions of source code must retain the above copyright
11de71b0ceSmillert * notice, this list of conditions and the following disclaimer.
12de71b0ceSmillert * 2. Redistributions in binary form must reproduce the above copyright
13de71b0ceSmillert * notice, this list of conditions and the following disclaimer in the
14de71b0ceSmillert * documentation and/or other materials provided with the distribution.
15de71b0ceSmillert *
16de71b0ceSmillert * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17de71b0ceSmillert * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18de71b0ceSmillert * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19de71b0ceSmillert * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20de71b0ceSmillert * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21de71b0ceSmillert * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22de71b0ceSmillert * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23de71b0ceSmillert * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24de71b0ceSmillert * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25de71b0ceSmillert * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26de71b0ceSmillert * SUCH DAMAGE.
27de71b0ceSmillert */
28de71b0ceSmillert
29*0b082c0eSderaadt #include <sys/types.h>
30*0b082c0eSderaadt #include <sys/time.h>
31de71b0ceSmillert #include <sys/ucred.h>
32de71b0ceSmillert #define _KERNEL
33de71b0ceSmillert #include <sys/mount.h>
34de71b0ceSmillert #undef _KERNEL
35ae35154bStedu #include <sys/lock.h>
36de71b0ceSmillert #include <sys/vnode.h>
37de71b0ceSmillert #include <sys/sysctl.h>
38544451c3Sderaadt #include <sys/specdev.h>
39de71b0ceSmillert
40961d826bSdlg #include <crypto/siphash.h>
41961d826bSdlg
42de71b0ceSmillert #include <isofs/udf/ecma167-udf.h>
43de71b0ceSmillert #include <isofs/udf/udf.h>
44de71b0ceSmillert
45de71b0ceSmillert #include <stdint.h>
46de71b0ceSmillert #include <limits.h>
47de71b0ceSmillert #include <kvm.h>
48de71b0ceSmillert #include <db.h>
49de71b0ceSmillert
50de71b0ceSmillert #include "kvm_private.h"
5126de9129Sguenther #include "kvm_file.h"
52de71b0ceSmillert
53de71b0ceSmillert /* Convert file entry permission (5 bits per owner/group/user) to a mode_t */
54de71b0ceSmillert static mode_t
udf_permtomode(struct unode * up)55de71b0ceSmillert udf_permtomode(struct unode *up)
56de71b0ceSmillert {
57de71b0ceSmillert uint32_t perm;
58de71b0ceSmillert uint16_t flags;
59de71b0ceSmillert mode_t mode;
60de71b0ceSmillert
61de71b0ceSmillert perm = letoh32(up->u_fentry->perm);
62de71b0ceSmillert flags = letoh16(up->u_fentry->icbtag.flags);
63de71b0ceSmillert
64de71b0ceSmillert mode = perm & UDF_FENTRY_PERM_USER_MASK;
65de71b0ceSmillert mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK) >> 2);
66de71b0ceSmillert mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4);
67de71b0ceSmillert mode |= ((flags & UDF_ICB_TAG_FLAGS_STICKY) << 4);
68de71b0ceSmillert mode |= ((flags & UDF_ICB_TAG_FLAGS_SETGID) << 6);
69de71b0ceSmillert mode |= ((flags & UDF_ICB_TAG_FLAGS_SETUID) << 8);
70de71b0ceSmillert
71de71b0ceSmillert return (mode);
72de71b0ceSmillert }
73de71b0ceSmillert
74de71b0ceSmillert int
_kvm_stat_udf(kvm_t * kd,struct kinfo_file * kf,struct vnode * vp)75cef0bbe1Sguenther _kvm_stat_udf(kvm_t *kd, struct kinfo_file *kf, struct vnode *vp)
76de71b0ceSmillert {
77de71b0ceSmillert struct unode up;
78de71b0ceSmillert struct file_entry fentry;
79de71b0ceSmillert struct umount um;
80de71b0ceSmillert
81de71b0ceSmillert if (KREAD(kd, (u_long)VTOU(vp), &up)) {
82de71b0ceSmillert _kvm_err(kd, kd->program, "can't read unode at %p", VTOU(vp));
83de71b0ceSmillert return (-1);
84de71b0ceSmillert }
85de71b0ceSmillert if (KREAD(kd, (u_long)up.u_fentry, &fentry)) {
86de71b0ceSmillert _kvm_err(kd, kd->program, "can't read file_entry at %p",
87de71b0ceSmillert up.u_fentry);
88de71b0ceSmillert return (-1);
89de71b0ceSmillert }
90de71b0ceSmillert if (KREAD(kd, (u_long)up.u_ump, &um)) {
91de71b0ceSmillert _kvm_err(kd, kd->program, "can't read umount at %p",
92de71b0ceSmillert up.u_ump);
93de71b0ceSmillert return (-1);
94de71b0ceSmillert }
95de71b0ceSmillert kf->va_fsid = up.u_dev;
96de71b0ceSmillert kf->va_fileid = (long)up.u_ino;
97de71b0ceSmillert kf->va_mode = udf_permtomode(&up); /* XXX */
98de71b0ceSmillert kf->va_rdev = 0;
998cdd1cc4Sguenther kf->va_nlink = letoh16(fentry.link_cnt);
100de71b0ceSmillert if (vp->v_type & VDIR) {
101de71b0ceSmillert /*
102de71b0ceSmillert * Directories that are recorded within their ICB will show
103de71b0ceSmillert * as having 0 blocks recorded. Since tradition dictates
104de71b0ceSmillert * that directories consume at least one logical block,
105de71b0ceSmillert * make it appear so.
106de71b0ceSmillert */
107de71b0ceSmillert if (fentry.logblks_rec != 0) {
108de71b0ceSmillert kf->va_size =
109de71b0ceSmillert letoh64(fentry.logblks_rec) * um.um_bsize;
110de71b0ceSmillert } else {
111de71b0ceSmillert kf->va_size = um.um_bsize;
112de71b0ceSmillert }
113de71b0ceSmillert } else {
114de71b0ceSmillert kf->va_size = letoh64(fentry.inf_len);
115de71b0ceSmillert }
116de71b0ceSmillert
117de71b0ceSmillert return (0);
118de71b0ceSmillert }
119