2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2013 Juniper Networks, Inc.
5 * Copyright (c) 2022-2023 Klara, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #ifndef _FS_TARFS_TARFS_H_
30 #define _FS_TARFS_TARFS_H_
33 #error Should only be included by kernel
36 MALLOC_DECLARE(M_TARFSMNT);
37 MALLOC_DECLARE(M_TARFSNODE);
38 MALLOC_DECLARE(M_TARFSNAME);
41 SYSCTL_DECL(_vfs_tarfs);
49 * Internal representation of a tarfs file system node.
52 TAILQ_ENTRY(tarfs_node) entries;
53 TAILQ_ENTRY(tarfs_node) dirents;
58 struct tarfs_mount *tmp;
59 __enum_uint8(vtype) type;
73 struct timespec atime;
74 struct timespec mtime;
75 struct timespec ctime;
76 struct timespec birthtime;
81 struct tarfs_blk *blk;
83 struct tarfs_node *parent;
87 TAILQ_HEAD(, tarfs_node) dirhead;
89 struct tarfs_node *lastnode;
102 struct tarfs_node *other;
107 * Entry in sparse file block map.
110 off_t i; /* input (physical) offset */
111 off_t o; /* output (logical) offset */
112 size_t l; /* length */
116 * Decompression buffer.
118 #define TARFS_ZBUF_SIZE 1048576
120 u_char buf[TARFS_ZBUF_SIZE];
121 size_t off; /* offset of contents */
122 size_t len; /* length of contents */
126 * Internal representation of a tarfs mount point.
129 TAILQ_HEAD(, tarfs_node) allnodes;
130 struct mtx allnode_lock;
132 struct tarfs_node *root;
136 struct unrhdr *ino_unr;
140 time_t mtime; /* default mtime for directories */
142 struct tarfs_zio *zio;
147 struct tarfs_mount *tmp;
149 /* decompression state */
151 struct tarfs_zstd *zstd; /* decompression state (zstd) */
153 off_t ipos; /* current input position */
154 off_t opos; /* current output position */
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;
164 u_short len; /* length of data in bytes */
165 u_short data0; /* force alignment */
170 #define TARFS_NODE_LOCK(tnp) \
171 mtx_lock(&(tnp)->lock)
172 #define TARFS_NODE_UNLOCK(tnp) \
173 mtx_unlock(&(tnp)->lock)
174 #define TARFS_ALLNODES_LOCK(tnp) \
175 mtx_lock(&(tmp)->allnode_lock)
176 #define TARFS_ALLNODES_UNLOCK(tnp) \
177 mtx_unlock(&(tmp)->allnode_lock)
180 * Data and metadata within tar files are aligned on 512-byte boundaries,
181 * to match the block size of the magnetic tapes they were originally
184 #define TARFS_BSHIFT 9
185 #define TARFS_BLOCKSIZE (size_t)(1U << TARFS_BSHIFT)
186 #define TARFS_BLKOFF(l) ((l) % TARFS_BLOCKSIZE)
187 #define TARFS_BLKNUM(l) ((l) >> TARFS_BSHIFT)
188 #define TARFS_SZ2BLKS(sz) (((sz) + TARFS_BLOCKSIZE - 1) / TARFS_BLOCKSIZE)
191 * Our preferred I/O size.
193 extern unsigned int tarfs_ioshift;
194 #define TARFS_IOSHIFT_MIN TARFS_BSHIFT
195 #define TARFS_IOSHIFT_DEFAULT PAGE_SHIFT
196 #define TARFS_IOSHIFT_MAX PAGE_SHIFT
198 #define TARFS_ROOTINO ((ino_t)3)
199 #define TARFS_ZIOINO ((ino_t)4)
200 #define TARFS_MININO ((ino_t)65535)
202 #define TARFS_COOKIE_DOT 0
203 #define TARFS_COOKIE_DOTDOT 1
204 #define TARFS_COOKIE_EOF OFF_MAX
206 #define TARFS_ZIO_NAME ".tar"
207 #define TARFS_ZIO_NAMELEN (sizeof(TARFS_ZIO_NAME) - 1)
209 extern struct vop_vector tarfs_vnodeops;
213 MP_TO_TARFS_MOUNT(struct mount *mp)
216 MPASS(mp != NULL && mp->mnt_data != NULL);
217 return (mp->mnt_data);
222 VP_TO_TARFS_NODE(struct vnode *vp)
225 MPASS(vp != NULL && vp->v_data != NULL);
229 int tarfs_alloc_node(struct tarfs_mount *tmp, const char *name,
230 size_t namelen, __enum_uint8(vtype) type, off_t off, size_t sz,
231 time_t mtime, uid_t uid, gid_t gid, mode_t mode,
232 unsigned int flags, const char *linkname, dev_t rdev,
233 struct tarfs_node *parent, struct tarfs_node **node);
234 int tarfs_load_blockmap(struct tarfs_node *tnp, size_t realsize);
235 void tarfs_free_node(struct tarfs_node *tnp);
237 tarfs_lookup_dir(struct tarfs_node *tnp, off_t cookie);
239 tarfs_lookup_node(struct tarfs_node *tnp, struct tarfs_node *f,
240 struct componentname *cnp);
241 int tarfs_read_file(struct tarfs_node *tnp, size_t len, struct uio *uiop);
243 int tarfs_io_init(struct tarfs_mount *tmp);
244 int tarfs_io_fini(struct tarfs_mount *tmp);
245 int tarfs_io_read(struct tarfs_mount *tmp, bool raw,
247 ssize_t tarfs_io_read_buf(struct tarfs_mount *tmp, bool raw,
248 void *buf, off_t off, size_t len);
250 tarfs_strtofflags(const char *str, char **end);
252 #endif /* _FS_TARFS_TARFS_H_ */