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 #include "opt_tarfs.h"
31 #include <sys/param.h>
32 #include <sys/systm.h>
35 #include <sys/dirent.h>
36 #include <sys/fcntl.h>
37 #include <sys/limits.h>
38 #include <sys/mount.h>
39 #include <sys/namei.h>
41 #include <sys/vnode.h>
43 #include <fs/tarfs/tarfs.h>
44 #include <fs/tarfs/tarfs_dbg.h>
47 tarfs_open(struct vop_open_args *ap)
49 struct tarfs_node *tnp;
53 MPASS(VOP_ISLOCKED(vp));
54 tnp = VP_TO_TARFS_NODE(vp);
56 TARFS_DPF(VNODE, "%s(%p=%s, %o)\n", __func__,
57 tnp, tnp->name, ap->a_mode);
59 if (vp->v_type != VREG && vp->v_type != VDIR)
62 vnode_create_vobject(vp, tnp->size, ap->a_td);
67 tarfs_close(struct vop_close_args *ap)
70 struct tarfs_node *tnp;
75 MPASS(VOP_ISLOCKED(vp));
76 tnp = VP_TO_TARFS_NODE(vp);
78 TARFS_DPF(VNODE, "%s(%p=%s)\n", __func__,
87 tarfs_access(struct vop_access_args *ap)
89 struct tarfs_node *tnp;
96 accmode = ap->a_accmode;
99 MPASS(VOP_ISLOCKED(vp));
100 tnp = VP_TO_TARFS_NODE(vp);
102 TARFS_DPF(VNODE, "%s(%p=%s, %o)\n", __func__,
103 tnp, tnp->name, accmode);
105 switch (vp->v_type) {
109 if ((accmode & VWRITE) != 0)
120 if ((accmode & VWRITE) != 0)
123 error = vaccess(vp->v_type, tnp->mode, tnp->uid,
124 tnp->gid, accmode, cred);
129 tarfs_getattr(struct vop_getattr_args *ap)
131 struct tarfs_node *tnp;
137 tnp = VP_TO_TARFS_NODE(vp);
139 TARFS_DPF(VNODE, "%s(%p=%s)\n", __func__,
142 vap->va_type = vp->v_type;
143 vap->va_mode = tnp->mode;
144 vap->va_nlink = tnp->nlink;
145 vap->va_gid = tnp->gid;
146 vap->va_uid = tnp->uid;
147 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
148 vap->va_fileid = tnp->ino;
149 vap->va_size = tnp->size;
150 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
151 vap->va_atime = tnp->atime;
152 vap->va_ctime = tnp->ctime;
153 vap->va_mtime = tnp->mtime;
154 vap->va_birthtime = tnp->birthtime;
155 vap->va_gen = tnp->gen;
156 vap->va_flags = tnp->flags;
157 vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
159 vap->va_bytes = round_page(tnp->physize);
166 tarfs_lookup(struct vop_cachedlookup_args *ap)
168 struct tarfs_node *dirnode, *parent, *tnp;
169 struct componentname *cnp;
170 struct vnode *dvp, **vpp;
181 dirnode = VP_TO_TARFS_NODE(dvp);
182 parent = dirnode->parent;
185 TARFS_DPF(LOOKUP, "%s(%p=%s, %.*s)\n", __func__,
186 dirnode, dirnode->name,
187 (int)cnp->cn_namelen, cnp->cn_nameptr);
189 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, curthread);
193 if (cnp->cn_flags & ISDOTDOT) {
194 /* Do not allow .. on the root node */
195 if (parent == NULL || parent == dirnode)
198 /* Allocate a new vnode on the matching entry */
199 error = vn_vget_ino(dvp, parent->ino, cnp->cn_lkflags,
203 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
207 } else if (dirnode == dirnode->tmp->root &&
208 (vp = dirnode->tmp->znode) != NULL &&
209 cnp->cn_namelen == TARFS_ZIO_NAMELEN &&
210 memcmp(cnp->cn_nameptr, TARFS_ZIO_NAME, TARFS_ZIO_NAMELEN) == 0) {
211 error = vn_lock(vp, cnp->cn_lkflags);
219 tnp = tarfs_lookup_node(dirnode, NULL, cnp);
221 TARFS_DPF(LOOKUP, "%s(%p=%s, %.*s): file not found\n", __func__,
222 dirnode, dirnode->name,
223 (int)cnp->cn_namelen, cnp->cn_nameptr);
227 if ((cnp->cn_flags & ISLASTCN) == 0 &&
228 (tnp->type != VDIR && tnp->type != VLNK))
231 error = vn_vget_ino(dvp, tnp->ino, cnp->cn_lkflags, vpp);
238 tnp = VP_TO_TARFS_NODE(*vpp);
239 TARFS_DPF(LOOKUP, "%s: found vnode %p, tarfs_node %p\n", __func__,
241 #endif /* TARFS_DEBUG */
243 /* Store the result the the cache if MAKEENTRY is specified in flags */
244 if ((cnp->cn_flags & MAKEENTRY) != 0 && cnp->cn_nameiop != CREATE)
245 cache_enter(dvp, *vpp, cnp);
251 tarfs_readdir(struct vop_readdir_args *ap)
254 struct tarfs_node *current, *tnp;
266 eofflag = ap->a_eofflag;
267 cookies = ap->a_cookies;
268 ncookies = ap->a_ncookies;
270 if (vp->v_type != VDIR)
273 tnp = VP_TO_TARFS_NODE(vp);
274 off = uio->uio_offset;
278 TARFS_DPF(VNODE, "%s(%p=%s, %zu, %zd)\n", __func__,
279 tnp, tnp->name, uio->uio_offset, uio->uio_resid);
281 if (uio->uio_offset == TARFS_COOKIE_EOF) {
282 TARFS_DPF(VNODE, "%s: EOF\n", __func__);
286 if (uio->uio_offset == TARFS_COOKIE_DOT) {
287 TARFS_DPF(VNODE, "%s: Generating . entry\n", __func__);
289 cde.d_fileno = tnp->ino;
293 cde.d_name[1] = '\0';
294 cde.d_reclen = GENERIC_DIRSIZ(&cde);
295 if (cde.d_reclen > uio->uio_resid)
297 error = uiomove(&cde, cde.d_reclen, uio);
301 uio->uio_offset = TARFS_COOKIE_DOTDOT;
305 if (uio->uio_offset == TARFS_COOKIE_DOTDOT) {
306 TARFS_DPF(VNODE, "%s: Generating .. entry\n", __func__);
308 MPASS(tnp->parent != NULL);
309 TARFS_NODE_LOCK(tnp->parent);
310 cde.d_fileno = tnp->parent->ino;
311 TARFS_NODE_UNLOCK(tnp->parent);
316 cde.d_name[2] = '\0';
317 cde.d_reclen = GENERIC_DIRSIZ(&cde);
318 if (cde.d_reclen > uio->uio_resid)
320 error = uiomove(&cde, cde.d_reclen, uio);
323 /* next is first child */
324 current = TAILQ_FIRST(&tnp->dir.dirhead);
327 uio->uio_offset = current->ino;
328 TARFS_DPF(VNODE, "%s: [%u] setting current node to %p=%s\n",
329 __func__, ndirents, current, current->name);
333 /* resuming previous call */
334 if (current == NULL) {
335 current = tarfs_lookup_dir(tnp, uio->uio_offset);
336 if (current == NULL) {
340 uio->uio_offset = current->ino;
341 TARFS_DPF(VNODE, "%s: [%u] setting current node to %p=%s\n",
342 __func__, ndirents, current, current->name);
346 cde.d_fileno = current->ino;
347 switch (current->type) {
358 cde.d_type = DT_FIFO;
367 panic("%s: tarfs_node %p, type %d\n", __func__,
368 current, current->type);
370 cde.d_namlen = current->namelen;
371 MPASS(tnp->namelen < sizeof(cde.d_name));
372 (void)memcpy(cde.d_name, current->name, current->namelen);
373 cde.d_name[current->namelen] = '\0';
374 cde.d_reclen = GENERIC_DIRSIZ(&cde);
375 if (cde.d_reclen > uio->uio_resid)
377 error = uiomove(&cde, cde.d_reclen, uio);
382 current = TAILQ_NEXT(current, dirents);
385 uio->uio_offset = current->ino;
386 TARFS_DPF(VNODE, "%s: [%u] setting current node to %p=%s\n",
387 __func__, ndirents, current, current->name);
390 if (cde.d_reclen > uio->uio_resid) {
391 TARFS_DPF(VNODE, "%s: out of space, returning\n",
393 error = (ndirents == 0) ? EINVAL : 0;
396 TARFS_DPF(VNODE, "%s: %u entries written\n", __func__, ndirents);
397 TARFS_DPF(VNODE, "%s: saving cache information\n", __func__);
398 if (current == NULL) {
399 uio->uio_offset = TARFS_COOKIE_EOF;
400 tnp->dir.lastcookie = 0;
401 tnp->dir.lastnode = NULL;
403 tnp->dir.lastcookie = current->ino;
404 tnp->dir.lastnode = current;
407 if (eofflag != NULL) {
408 TARFS_DPF(VNODE, "%s: Setting EOF flag\n", __func__);
409 *eofflag = (error == 0 && current == NULL);
413 if (error == 0 && cookies != NULL && ncookies != NULL) {
414 TARFS_DPF(VNODE, "%s: Updating NFS cookies\n", __func__);
416 *cookies = malloc(ndirents * sizeof(off_t), M_TEMP, M_WAITOK);
417 *ncookies = ndirents;
418 for (idx = 0; idx < ndirents; idx++) {
419 if (off == TARFS_COOKIE_DOT)
420 off = TARFS_COOKIE_DOTDOT;
422 if (off == TARFS_COOKIE_DOTDOT) {
423 current = TAILQ_FIRST(&tnp->dir.dirhead);
424 } else if (current != NULL) {
425 current = TAILQ_NEXT(current, dirents);
427 current = tarfs_lookup_dir(tnp, off);
428 current = TAILQ_NEXT(current, dirents);
431 off = TARFS_COOKIE_EOF;
436 TARFS_DPF(VNODE, "%s: [%u] offset %zu\n", __func__,
438 (*cookies)[idx] = off;
440 MPASS(uio->uio_offset == off);
447 tarfs_read(struct vop_read_args *ap)
449 struct tarfs_node *tnp;
459 if (vp->v_type == VCHR || vp->v_type == VBLK)
462 if (vp->v_type != VREG)
465 if (uiop->uio_offset < 0)
468 tnp = VP_TO_TARFS_NODE(vp);
471 TARFS_DPF(VNODE, "%s(%p=%s, %zu, %zd)\n", __func__,
472 tnp, tnp->name, uiop->uio_offset, uiop->uio_resid);
474 while ((resid = uiop->uio_resid) > 0) {
475 if (tnp->size <= uiop->uio_offset)
477 len = MIN(tnp->size - uiop->uio_offset, resid);
481 error = tarfs_read_file(tnp, len, uiop);
482 if (error != 0 || resid == uiop->uio_resid)
490 tarfs_readlink(struct vop_readlink_args *ap)
492 struct tarfs_node *tnp;
500 MPASS(uiop->uio_offset == 0);
501 MPASS(vp->v_type == VLNK);
503 tnp = VP_TO_TARFS_NODE(vp);
505 TARFS_DPF(VNODE, "%s(%p=%s)\n", __func__,
508 error = uiomove(tnp->link.name,
509 MIN(tnp->size, uiop->uio_resid), uiop);
515 tarfs_reclaim(struct vop_reclaim_args *ap)
517 struct tarfs_node *tnp;
521 tnp = VP_TO_TARFS_NODE(vp);
524 vnode_destroy_vobject(vp);
527 TARFS_NODE_LOCK(tnp);
530 TARFS_NODE_UNLOCK(tnp);
536 tarfs_print(struct vop_print_args *ap)
538 struct tarfs_node *tnp;
542 tnp = VP_TO_TARFS_NODE(vp);
544 printf("tag tarfs, tarfs_node %p, links %lu\n",
545 tnp, (unsigned long)tnp->nlink);
546 printf("\tmode 0%o, owner %d, group %d, size %zd\n",
547 tnp->mode, tnp->uid, tnp->gid,
550 if (vp->v_type == VFIFO)
559 tarfs_strategy(struct vop_strategy_args *ap)
563 struct tarfs_node *tnp;
569 tnp = VP_TO_TARFS_NODE(ap->a_vp);
571 MPASS(bp->b_iocmd == BIO_READ);
572 MPASS(bp->b_iooffset >= 0);
573 MPASS(bp->b_bcount > 0);
574 MPASS(bp->b_bufsize >= bp->b_bcount);
575 TARFS_DPF(VNODE, "%s(%p=%s, %zu, %ld/%ld)\n", __func__, tnp,
576 tnp->name, (size_t)bp->b_iooffset, bp->b_bcount, bp->b_bufsize);
577 iov.iov_base = bp->b_data;
578 iov.iov_len = bp->b_bcount;
579 off = bp->b_iooffset;
582 if (off > tnp->size) {
583 /* XXX read beyond EOF - figure out correct handling */
587 if (off + len > tnp->size) {
588 /* clip to file length */
589 len = tnp->size - off;
593 auio.uio_offset = off;
594 auio.uio_resid = len;
595 auio.uio_segflg = UIO_SYSSPACE;
596 auio.uio_rw = UIO_READ;
597 auio.uio_td = curthread;
598 error = tarfs_read_file(tnp, len, &auio);
599 bp->b_resid -= len - auio.uio_resid;
602 bp->b_ioflags |= BIO_ERROR;
605 bp->b_flags |= B_DONE;
610 tarfs_vptofh(struct vop_vptofh_args *ap)
612 struct tarfs_fid *tfp;
613 struct tarfs_node *tnp;
615 tfp = (struct tarfs_fid *)ap->a_fhp;
616 tnp = VP_TO_TARFS_NODE(ap->a_vp);
618 tfp->len = sizeof(struct tarfs_fid);
625 struct vop_vector tarfs_vnodeops = {
626 .vop_default = &default_vnodeops,
628 .vop_access = tarfs_access,
629 .vop_cachedlookup = tarfs_lookup,
630 .vop_close = tarfs_close,
631 .vop_getattr = tarfs_getattr,
632 .vop_lookup = vfs_cache_lookup,
633 .vop_open = tarfs_open,
634 .vop_print = tarfs_print,
635 .vop_read = tarfs_read,
636 .vop_readdir = tarfs_readdir,
637 .vop_readlink = tarfs_readlink,
638 .vop_reclaim = tarfs_reclaim,
639 .vop_strategy = tarfs_strategy,
640 .vop_vptofh = tarfs_vptofh,
642 VFS_VOP_VECTOR_REGISTER(tarfs_vnodeops);