]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/fstat/zfs.c
add -n option to suppress clearing the build tree and add -DNO_CLEAN
[FreeBSD/FreeBSD.git] / usr.bin / fstat / zfs.c
1 /*-
2  * Copyright (c) 2007 Ulf Lilleengen
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #include <sys/types.h>
30 #define _KERNEL
31 #include <sys/mount.h>
32 #undef _KERNEL
33 #include <sys/sysctl.h>
34
35 #undef lbolt
36 #undef lbolt64
37 #undef gethrestime_sec
38 #include <sys/zfs_context.h>
39 #include <sys/spa.h>
40 #include <sys/spa_impl.h>
41 #include <sys/dmu.h>
42 #include <sys/zap.h>
43 #include <sys/fs/zfs.h>
44 #include <sys/zfs_znode.h>
45
46 #include <err.h>
47 #include <kvm.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50
51 #define ZFS
52 #undef dprintf
53 #include <fstat.h>
54
55 /* 
56  * Offset calculations that are used to get data from znode without having the
57  * definition.
58  */
59 #define LOCATION_ZID (2 * sizeof(void *))
60 #define LOCATION_ZPHYS(zsize) ((zsize) - (2 * sizeof(void *)))
61
62 int
63 zfs_filestat(struct vnode *vp, struct filestat *fsp)
64 {
65
66         znode_phys_t zphys;
67         struct mount mount, *mountptr;
68         uint64_t *zid;
69         void *znodeptr, *vnodeptr;
70         char *dataptr;
71         void *zphys_addr;
72         size_t len;
73         int size;
74
75         len = sizeof(size);
76         if (sysctlbyname("debug.sizeof.znode", &size, &len, NULL, 0) == -1) {
77                 dprintf(stderr, "error getting sysctl\n");
78                 return (0);
79         }
80         znodeptr = malloc(size);
81         if (znodeptr == NULL) {
82                 dprintf(stderr, "error allocating memory for znode storage\n");
83                 return (0);
84         }
85
86         /* Since we have problems including vnode.h, we'll use the wrappers. */
87         vnodeptr = getvnodedata(vp);
88         if (!KVM_READ(vnodeptr, znodeptr, (size_t)size)) {
89                 dprintf(stderr, "can't read znode at %p for pid %d\n",
90                     (void *)vnodeptr, Pid);
91                 goto bad;
92         }
93
94         /* 
95          * z_id field is stored in the third pointer. We therefore skip the two
96          * first bytes. 
97          *
98          * Pointer to the z_phys structure is the next last pointer. Therefore
99          * go back two bytes from the end.
100          */
101         dataptr = znodeptr;
102         zid = (uint64_t *)(dataptr + LOCATION_ZID);
103         zphys_addr = *(void **)(dataptr + LOCATION_ZPHYS(size));
104
105         if (!KVM_READ(zphys_addr, &zphys, sizeof(zphys))) {
106                 dprintf(stderr, "can't read znode_phys at %p for pid %d\n",
107                     zphys_addr, Pid);
108                 goto bad;
109         }
110
111         /* Get the mount pointer, and read from the address. */
112         mountptr = getvnodemount(vp);
113         if (!KVM_READ(mountptr, &mount, sizeof(mount))) {
114                 dprintf(stderr, "can't read mount at %p for pid %d\n",
115                     (void *)mountptr, Pid);
116                 goto bad;
117         }
118
119         fsp->fsid = (long)mount.mnt_stat.f_fsid.val[0];
120         fsp->fileid = *zid;
121         /*
122          * XXX: Shows up wrong in output, but UFS has this error too. Could
123          * be that we're casting mode-variables from 64-bit to 8-bit or simply
124          * error in the mode-to-string function.
125          */
126         fsp->mode = (mode_t)zphys.zp_mode;
127         fsp->size = (u_long)zphys.zp_size;
128         fsp->rdev = (dev_t)zphys.zp_rdev;
129         free(znodeptr);
130         return (1);
131 bad:
132         free(znodeptr);
133         return (0);
134 }