1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2013 Juniper Networks, Inc. 5 * Copyright (c) 2022-2023 Klara, Inc. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #ifndef _FS_TARFS_TARFS_H_ 30 #define _FS_TARFS_TARFS_H_ 31 32 #ifndef _KERNEL 33 #error Should only be included by kernel 34 #endif 35 36 MALLOC_DECLARE(M_TARFSMNT); 37 MALLOC_DECLARE(M_TARFSNODE); 38 MALLOC_DECLARE(M_TARFSNAME); 39 40 #ifdef SYSCTL_DECL 41 SYSCTL_DECL(_vfs_tarfs); 42 #endif 43 44 struct componentname; 45 struct mount; 46 struct vnode; 47 48 /* 49 * Internal representation of a tarfs file system node. 50 */ 51 struct tarfs_node { 52 TAILQ_ENTRY(tarfs_node) entries; 53 TAILQ_ENTRY(tarfs_node) dirents; 54 55 struct mtx lock; 56 57 struct vnode *vnode; 58 struct tarfs_mount *tmp; 59 __enum_uint8(vtype) type; 60 ino_t ino; 61 off_t offset; 62 size_t size; 63 size_t physize; 64 char *name; 65 size_t namelen; 66 67 /* Node attributes */ 68 uid_t uid; 69 gid_t gid; 70 mode_t mode; 71 unsigned int flags; 72 nlink_t nlink; 73 struct timespec atime; 74 struct timespec mtime; 75 struct timespec ctime; 76 struct timespec birthtime; 77 uint32_t gen; 78 79 /* Block map */ 80 size_t nblk; 81 struct tarfs_blk *blk; 82 83 struct tarfs_node *parent; 84 union { 85 /* VDIR */ 86 struct { 87 TAILQ_HEAD(, tarfs_node) dirhead; 88 off_t lastcookie; 89 struct tarfs_node *lastnode; 90 } dir; 91 92 /* VLNK */ 93 struct { 94 char *name; 95 size_t namelen; 96 } link; 97 98 /* VBLK or VCHR */ 99 dev_t rdev; 100 101 /* VREG */ 102 struct tarfs_node *other; 103 }; 104 }; 105 106 /* 107 * Entry in sparse file block map. 108 */ 109 struct tarfs_blk { 110 off_t i; /* input (physical) offset */ 111 off_t o; /* output (logical) offset */ 112 size_t l; /* length */ 113 }; 114 115 /* 116 * Decompression buffer. 117 */ 118 #define TARFS_ZBUF_SIZE 1048576 119 struct tarfs_zbuf { 120 u_char buf[TARFS_ZBUF_SIZE]; 121 size_t off; /* offset of contents */ 122 size_t len; /* length of contents */ 123 }; 124 125 /* 126 * Internal representation of a tarfs mount point. 127 */ 128 struct tarfs_mount { 129 TAILQ_HEAD(, tarfs_node) allnodes; 130 struct mtx allnode_lock; 131 132 struct tarfs_node *root; 133 struct vnode *vp; 134 struct mount *vfs; 135 ino_t ino; 136 struct unrhdr *ino_unr; 137 size_t iosize; 138 size_t nblocks; 139 size_t nfiles; 140 time_t mtime; /* default mtime for directories */ 141 142 struct tarfs_zio *zio; 143 struct vnode *znode; 144 }; 145 146 struct tarfs_zio { 147 struct tarfs_mount *tmp; 148 149 /* decompression state */ 150 #ifdef ZSTDIO 151 struct tarfs_zstd *zstd; /* decompression state (zstd) */ 152 #endif 153 off_t ipos; /* current input position */ 154 off_t opos; /* current output position */ 155 156 /* index of compression frames */ 157 unsigned int curidx; /* current index position*/ 158 unsigned int nidx; /* number of index entries */ 159 unsigned int szidx; /* index capacity */ 160 struct tarfs_idx { off_t i, o; } *idx; 161 }; 162 163 struct tarfs_fid { 164 u_short len; /* length of data in bytes */ 165 uint32_t gen; 166 ino_t ino; 167 }; 168 169 #define TARFS_NODE_LOCK(tnp) \ 170 mtx_lock(&(tnp)->lock) 171 #define TARFS_NODE_UNLOCK(tnp) \ 172 mtx_unlock(&(tnp)->lock) 173 #define TARFS_ALLNODES_LOCK(tnp) \ 174 mtx_lock(&(tmp)->allnode_lock) 175 #define TARFS_ALLNODES_UNLOCK(tnp) \ 176 mtx_unlock(&(tmp)->allnode_lock) 177 178 /* 179 * Data and metadata within tar files are aligned on 512-byte boundaries, 180 * to match the block size of the magnetic tapes they were originally 181 * intended for. 182 */ 183 #define TARFS_BSHIFT 9 184 #define TARFS_BLOCKSIZE (size_t)(1U << TARFS_BSHIFT) 185 #define TARFS_BLKOFF(l) ((l) % TARFS_BLOCKSIZE) 186 #define TARFS_BLKNUM(l) ((l) >> TARFS_BSHIFT) 187 #define TARFS_SZ2BLKS(sz) (((sz) + TARFS_BLOCKSIZE - 1) / TARFS_BLOCKSIZE) 188 189 /* 190 * Our preferred I/O size. 191 */ 192 extern unsigned int tarfs_ioshift; 193 #define TARFS_IOSHIFT_MIN TARFS_BSHIFT 194 #define TARFS_IOSHIFT_DEFAULT PAGE_SHIFT 195 #define TARFS_IOSHIFT_MAX PAGE_SHIFT 196 197 #define TARFS_ROOTINO ((ino_t)3) 198 #define TARFS_ZIOINO ((ino_t)4) 199 #define TARFS_MININO ((ino_t)65535) 200 201 #define TARFS_COOKIE_DOT 0 202 #define TARFS_COOKIE_DOTDOT 1 203 #define TARFS_COOKIE_EOF OFF_MAX 204 205 #define TARFS_ZIO_NAME ".tar" 206 #define TARFS_ZIO_NAMELEN (sizeof(TARFS_ZIO_NAME) - 1) 207 208 extern struct vop_vector tarfs_vnodeops; 209 210 static inline 211 struct tarfs_mount * 212 MP_TO_TARFS_MOUNT(struct mount *mp) 213 { 214 215 MPASS(mp != NULL && mp->mnt_data != NULL); 216 return (mp->mnt_data); 217 } 218 219 static inline 220 struct tarfs_node * 221 VP_TO_TARFS_NODE(struct vnode *vp) 222 { 223 224 MPASS(vp != NULL && vp->v_data != NULL); 225 return (vp->v_data); 226 } 227 228 int tarfs_alloc_node(struct tarfs_mount *tmp, const char *name, 229 size_t namelen, __enum_uint8(vtype) type, off_t off, size_t sz, 230 time_t mtime, uid_t uid, gid_t gid, mode_t mode, 231 unsigned int flags, const char *linkname, dev_t rdev, 232 struct tarfs_node *parent, struct tarfs_node **node); 233 int tarfs_load_blockmap(struct tarfs_node *tnp, size_t realsize); 234 void tarfs_free_node(struct tarfs_node *tnp); 235 struct tarfs_node * 236 tarfs_lookup_dir(struct tarfs_node *tnp, off_t cookie); 237 struct tarfs_node * 238 tarfs_lookup_node(struct tarfs_node *tnp, struct tarfs_node *f, 239 struct componentname *cnp); 240 int tarfs_read_file(struct tarfs_node *tnp, size_t len, struct uio *uiop); 241 242 int tarfs_io_init(struct tarfs_mount *tmp); 243 int tarfs_io_fini(struct tarfs_mount *tmp); 244 int tarfs_io_read(struct tarfs_mount *tmp, bool raw, 245 struct uio *uiop); 246 ssize_t tarfs_io_read_buf(struct tarfs_mount *tmp, bool raw, 247 void *buf, off_t off, size_t len); 248 unsigned int 249 tarfs_strtofflags(const char *str, char **end); 250 251 #endif /* _FS_TARFS_TARFS_H_ */ 252