3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley
6 * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension
7 * Support code is derived from software contributed to Berkeley
8 * by Atsushi Murai (amurai@spec.co.jp).
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)cd9660_vnops.c 8.19 (Berkeley) 5/27/95
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/namei.h>
43 #include <sys/kernel.h>
48 #include <sys/mount.h>
49 #include <sys/vnode.h>
50 #include <fs/fifofs/fifo.h>
51 #include <sys/malloc.h>
52 #include <sys/dirent.h>
53 #include <sys/unistd.h>
54 #include <sys/filio.h>
57 #include <vm/vnode_pager.h>
60 #include <fs/cd9660/iso.h>
61 #include <fs/cd9660/cd9660_node.h>
62 #include <fs/cd9660/iso_rrip.h>
64 static vop_setattr_t cd9660_setattr;
65 static vop_open_t cd9660_open;
66 static vop_access_t cd9660_access;
67 static vop_getattr_t cd9660_getattr;
68 static vop_ioctl_t cd9660_ioctl;
69 static vop_pathconf_t cd9660_pathconf;
70 static vop_read_t cd9660_read;
72 static int iso_uiodir(struct isoreaddir *idp, struct dirent *dp, off_t off);
73 static int iso_shipdir(struct isoreaddir *idp);
74 static vop_readdir_t cd9660_readdir;
75 static vop_readlink_t cd9660_readlink;
76 static vop_strategy_t cd9660_strategy;
77 static vop_vptofh_t cd9660_vptofh;
80 * Setattr call. Only allowed for block and character special devices.
84 struct vop_setattr_args /* {
85 struct vnodeop_desc *a_desc;
91 struct vnode *vp = ap->a_vp;
92 struct vattr *vap = ap->a_vap;
94 if (vap->va_flags != (u_long)VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
95 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
96 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL)
98 if (vap->va_size != (u_quad_t)VNOVAL) {
119 * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
120 * The mode is shifted to select the owner/group/other fields. The
121 * super user is granted all permissions.
126 struct vop_access_args /* {
129 struct ucred *a_cred;
133 struct vnode *vp = ap->a_vp;
134 struct iso_node *ip = VTOI(vp);
135 accmode_t accmode = ap->a_accmode;
137 if (vp->v_type == VCHR || vp->v_type == VBLK)
141 * Disallow write attempts unless the file is a socket,
142 * fifo, or a block or character device resident on the
145 if (accmode & VWRITE) {
146 switch (vp->v_type) {
157 return (vaccess(vp->v_type, ip->inode.iso_mode, ip->inode.iso_uid,
158 ip->inode.iso_gid, ap->a_accmode, ap->a_cred, NULL));
163 struct vop_open_args /* {
166 struct ucred *a_cred;
171 struct vnode *vp = ap->a_vp;
172 struct iso_node *ip = VTOI(vp);
174 if (vp->v_type == VCHR || vp->v_type == VBLK)
177 vnode_create_vobject(vp, ip->i_size, ap->a_td);
184 struct vop_getattr_args /* {
187 struct ucred *a_cred;
191 struct vnode *vp = ap->a_vp;
192 struct vattr *vap = ap->a_vap;
193 struct iso_node *ip = VTOI(vp);
195 vap->va_fsid = dev2udev(ip->i_mnt->im_dev);
196 vap->va_fileid = ip->i_number;
198 vap->va_mode = ip->inode.iso_mode;
199 vap->va_nlink = ip->inode.iso_links;
200 vap->va_uid = ip->inode.iso_uid;
201 vap->va_gid = ip->inode.iso_gid;
202 vap->va_atime = ip->inode.iso_atime;
203 vap->va_mtime = ip->inode.iso_mtime;
204 vap->va_ctime = ip->inode.iso_ctime;
205 vap->va_rdev = ip->inode.iso_rdev;
207 vap->va_size = (u_quad_t) ip->i_size;
208 if (ip->i_size == 0 && (vap->va_mode & S_IFMT) == S_IFLNK) {
209 struct vop_readlink_args rdlnk;
214 cp = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
216 aiov.iov_len = MAXPATHLEN;
217 auio.uio_iov = &aiov;
220 auio.uio_rw = UIO_READ;
221 auio.uio_segflg = UIO_SYSSPACE;
222 auio.uio_td = curthread;
223 auio.uio_resid = MAXPATHLEN;
225 rdlnk.a_vp = ap->a_vp;
226 rdlnk.a_cred = ap->a_cred;
227 if (cd9660_readlink(&rdlnk) == 0)
228 vap->va_size = MAXPATHLEN - auio.uio_resid;
233 vap->va_blocksize = ip->i_mnt->logical_block_size;
234 vap->va_bytes = (u_quad_t) ip->i_size;
235 vap->va_type = vp->v_type;
241 * Vnode op for ioctl.
245 struct vop_ioctl_args /* {
250 struct ucred *a_cred;
259 vn_lock(vp, LK_SHARED | LK_RETRY);
260 if (vp->v_iflag & VI_DOOMED) {
264 if (vp->v_type == VCHR || vp->v_type == VBLK) {
272 switch (ap->a_command) {
274 *(int *)(ap->a_data) = ip->iso_start;
286 * Vnode op for reading.
290 struct vop_read_args /* {
294 struct ucred *a_cred;
297 struct vnode *vp = ap->a_vp;
298 struct uio *uio = ap->a_uio;
299 struct iso_node *ip = VTOI(vp);
302 daddr_t lbn, rablock;
304 int rasize, error = 0;
308 if (vp->v_type == VCHR || vp->v_type == VBLK)
311 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
313 if (uio->uio_resid == 0)
315 if (uio->uio_offset < 0)
319 lbn = lblkno(imp, uio->uio_offset);
320 on = blkoff(imp, uio->uio_offset);
321 n = MIN(imp->logical_block_size - on, uio->uio_resid);
322 diff = (off_t)ip->i_size - uio->uio_offset;
327 size = blksize(imp, ip, lbn);
329 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
330 if (lblktosize(imp, rablock) < ip->i_size)
331 error = cluster_read(vp, (off_t)ip->i_size,
332 lbn, size, NOCRED, uio->uio_resid,
333 (ap->a_ioflag >> 16), &bp);
335 error = bread(vp, lbn, size, NOCRED, &bp);
338 lblktosize(imp, rablock) < ip->i_size) {
339 rasize = blksize(imp, ip, rablock);
340 error = breadn(vp, lbn, size, &rablock,
341 &rasize, 1, NOCRED, &bp);
343 error = bread(vp, lbn, size, NOCRED, &bp);
345 n = MIN(n, size - bp->b_resid);
351 error = uiomove(bp->b_data + on, (int)n, uio);
353 } while (error == 0 && uio->uio_resid > 0 && n != 0);
358 * Structure for reading directories
361 struct dirent saveent;
362 struct dirent assocent;
363 struct dirent current;
375 iso_uiodir(idp,dp,off)
376 struct isoreaddir *idp;
382 dp->d_name[dp->d_namlen] = 0;
383 dp->d_reclen = GENERIC_DIRSIZ(dp);
385 if (idp->uio->uio_resid < dp->d_reclen) {
391 if (idp->ncookies <= 0) {
396 *idp->cookies++ = off;
400 if ((error = uiomove(dp, dp->d_reclen, idp->uio)) != 0)
408 struct isoreaddir *idp;
415 cl = idp->current.d_namlen;
416 cname = idp->current.d_name;
417 assoc = (cl > 1) && (*cname == ASSOCCHAR);
425 if (!(sl = dp->d_namlen)) {
427 sname = dp->d_name + 1;
428 sl = dp->d_namlen - 1;
432 || bcmp(sname,cname,sl)) {
433 if (idp->assocent.d_namlen) {
434 if ((error = iso_uiodir(idp,&idp->assocent,idp->assocoff)) != 0)
436 idp->assocent.d_namlen = 0;
438 if (idp->saveent.d_namlen) {
439 if ((error = iso_uiodir(idp,&idp->saveent,idp->saveoff)) != 0)
441 idp->saveent.d_namlen = 0;
445 idp->current.d_reclen = GENERIC_DIRSIZ(&idp->current);
447 idp->assocoff = idp->curroff;
448 bcopy(&idp->current,&idp->assocent,idp->current.d_reclen);
450 idp->saveoff = idp->curroff;
451 bcopy(&idp->current,&idp->saveent,idp->current.d_reclen);
457 * Vnode op for readdir
461 struct vop_readdir_args /* {
464 struct ucred *a_cred;
470 struct uio *uio = ap->a_uio;
471 struct isoreaddir *idp;
472 struct vnode *vdp = ap->a_vp;
475 struct buf *bp = NULL;
476 struct iso_directory_record *ep;
477 int entryoffsetinblock;
484 u_long *cookies = NULL;
488 bmask = imp->im_bmask;
490 idp = malloc(sizeof(*idp), M_TEMP, M_WAITOK);
491 idp->saveent.d_namlen = idp->assocent.d_namlen = 0;
494 * Is it worth trying to figure out the type?
496 idp->saveent.d_type = idp->assocent.d_type = idp->current.d_type =
499 if (ap->a_ncookies == NULL) {
503 * Guess the number of cookies needed.
505 ncookies = uio->uio_resid / 16;
506 cookies = malloc(ncookies * sizeof(u_long),
508 idp->cookies = cookies;
509 idp->ncookies = ncookies;
512 idp->curroff = uio->uio_offset;
513 idp->uio_off = uio->uio_offset;
515 if ((entryoffsetinblock = idp->curroff & bmask) &&
516 (error = cd9660_blkatoff(vdp, (off_t)idp->curroff, NULL, &bp))) {
520 endsearch = dp->i_size;
522 while (idp->curroff < endsearch) {
524 * If offset is on a block boundary,
525 * read the next directory block.
526 * Release previous if it exists.
528 if ((idp->curroff & bmask) == 0) {
532 cd9660_blkatoff(vdp, (off_t)idp->curroff, NULL, &bp)) != 0)
534 entryoffsetinblock = 0;
537 * Get pointer to next entry.
539 ep = (struct iso_directory_record *)
540 ((char *)bp->b_data + entryoffsetinblock);
542 reclen = isonum_711(ep->length);
544 /* skip to next block, if any */
546 (idp->curroff & ~bmask) + imp->logical_block_size;
550 if (reclen < ISO_DIRECTORY_RECORD_SIZE) {
552 /* illegal entry, stop */
556 if (entryoffsetinblock + reclen > imp->logical_block_size) {
558 /* illegal directory, so stop looking */
562 idp->current.d_namlen = isonum_711(ep->name_len);
564 if (reclen < ISO_DIRECTORY_RECORD_SIZE + idp->current.d_namlen) {
566 /* illegal entry, stop */
570 if (isonum_711(ep->flags)&2)
571 idp->current.d_fileno = isodirino(ep, imp);
573 idp->current.d_fileno = dbtob(bp->b_blkno) +
576 idp->curroff += reclen;
578 switch (imp->iso_ftype) {
580 cd9660_rrip_getname(ep,idp->current.d_name, &namelen,
581 &idp->current.d_fileno,imp);
582 idp->current.d_namlen = (u_char)namelen;
583 if (idp->current.d_namlen)
584 error = iso_uiodir(idp,&idp->current,idp->curroff);
586 default: /* ISO_FTYPE_DEFAULT || ISO_FTYPE_9660 || ISO_FTYPE_HIGH_SIERRA*/
587 strcpy(idp->current.d_name,"..");
588 if (idp->current.d_namlen == 1 && ep->name[0] == 0) {
589 idp->current.d_namlen = 1;
590 error = iso_uiodir(idp,&idp->current,idp->curroff);
591 } else if (idp->current.d_namlen == 1 && ep->name[0] == 1) {
592 idp->current.d_namlen = 2;
593 error = iso_uiodir(idp,&idp->current,idp->curroff);
595 isofntrans(ep->name,idp->current.d_namlen,
596 idp->current.d_name, &namelen,
597 imp->iso_ftype == ISO_FTYPE_9660,
598 isonum_711(ep->flags)&4,
602 idp->current.d_namlen = (u_char)namelen;
603 if (imp->iso_ftype == ISO_FTYPE_DEFAULT)
604 error = iso_shipdir(idp);
606 error = iso_uiodir(idp,&idp->current,idp->curroff);
612 entryoffsetinblock += reclen;
615 if (!error && imp->iso_ftype == ISO_FTYPE_DEFAULT) {
616 idp->current.d_namlen = 0;
617 error = iso_shipdir(idp);
622 if (ap->a_ncookies != NULL) {
624 free(cookies, M_TEMP);
627 * Work out the number of cookies actually used.
629 *ap->a_ncookies = ncookies - idp->ncookies;
630 *ap->a_cookies = cookies;
637 uio->uio_offset = idp->uio_off;
638 *ap->a_eofflag = idp->eofflag;
646 * Return target name of a symbolic link
647 * Shouldn't we get the parent vnode and read the data from there?
648 * This could eventually result in deadlocks in cd9660_lookup.
649 * But otherwise the block read here is in the block buffer two times.
651 typedef struct iso_directory_record ISODIR;
652 typedef struct iso_node ISONODE;
653 typedef struct iso_mnt ISOMNT;
656 struct vop_readlink_args /* {
659 struct ucred *a_cred;
675 if (imp->iso_ftype != ISO_FTYPE_RRIP)
679 * Get parents directory record block that this inode included.
681 error = bread(imp->im_devvp,
682 (ip->i_number >> imp->im_bshift) <<
683 (imp->im_bshift - DEV_BSHIFT),
684 imp->logical_block_size, NOCRED, &bp);
691 * Setup the directory pointer for this inode
693 dirp = (ISODIR *)(bp->b_data + (ip->i_number & imp->im_bmask));
696 * Just make sure, we have a right one....
697 * 1: Check not cross boundary on block
699 if ((ip->i_number & imp->im_bmask) + isonum_711(dirp->length)
700 > (unsigned)imp->logical_block_size) {
707 * Abuse a namei buffer for now.
709 if (uio->uio_segflg == UIO_SYSSPACE)
710 symname = uio->uio_iov->iov_base;
712 symname = uma_zalloc(namei_zone, M_WAITOK);
715 * Ok, we just gathering a symbolic name in SL record.
717 if (cd9660_rrip_getsymname(dirp, symname, &symlen, imp) == 0) {
718 if (uio->uio_segflg != UIO_SYSSPACE)
719 uma_zfree(namei_zone, symname);
724 * Don't forget before you leave from home ;-)
729 * return with the symbolic name to caller's.
731 if (uio->uio_segflg != UIO_SYSSPACE) {
732 error = uiomove(symname, symlen, uio);
733 uma_zfree(namei_zone, symname);
736 uio->uio_resid -= symlen;
737 uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + symlen;
738 uio->uio_iov->iov_len -= symlen;
743 * Calculate the logical to physical mapping if not done already,
744 * then call the device strategy routine.
748 struct vop_strategy_args /* {
753 struct buf *bp = ap->a_bp;
754 struct vnode *vp = ap->a_vp;
759 if (vp->v_type == VBLK || vp->v_type == VCHR)
760 panic("cd9660_strategy: spec");
761 if (bp->b_blkno == bp->b_lblkno) {
762 bp->b_blkno = (ip->iso_start + bp->b_lblkno) <<
763 (ip->i_mnt->im_bshift - DEV_BSHIFT);
765 bp->b_iooffset = dbtob(bp->b_blkno);
766 bo = ip->i_mnt->im_bo;
772 * Return POSIX pathconf information applicable to cd9660 filesystems.
776 struct vop_pathconf_args /* {
779 register_t *a_retval;
783 switch (ap->a_name) {
788 if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP)
789 *ap->a_retval = NAME_MAX;
794 *ap->a_retval = PATH_MAX;
797 *ap->a_retval = PIPE_BUF;
799 case _PC_CHOWN_RESTRICTED:
812 * Vnode pointer to File handle
816 struct vop_vptofh_args /* {
822 struct iso_node *ip = VTOI(ap->a_vp);
824 ifh.ifid_len = sizeof(struct ifid);
826 ifh.ifid_ino = ip->i_number;
827 ifh.ifid_start = ip->iso_start;
829 * This intentionally uses sizeof(ifh) in order to not copy stack
832 memcpy(ap->a_fhp, &ifh, sizeof(ifh));
835 printf("vptofh: ino %d, start %ld\n",
836 ifh.ifid_ino, ifh.ifid_start);
843 * Global vfs data structures for cd9660
845 struct vop_vector cd9660_vnodeops = {
846 .vop_default = &default_vnodeops,
847 .vop_open = cd9660_open,
848 .vop_access = cd9660_access,
849 .vop_bmap = cd9660_bmap,
850 .vop_cachedlookup = cd9660_lookup,
851 .vop_getattr = cd9660_getattr,
852 .vop_inactive = cd9660_inactive,
853 .vop_ioctl = cd9660_ioctl,
854 .vop_lookup = vfs_cache_lookup,
855 .vop_pathconf = cd9660_pathconf,
856 .vop_read = cd9660_read,
857 .vop_readdir = cd9660_readdir,
858 .vop_readlink = cd9660_readlink,
859 .vop_reclaim = cd9660_reclaim,
860 .vop_setattr = cd9660_setattr,
861 .vop_strategy = cd9660_strategy,
862 .vop_vptofh = cd9660_vptofh,
866 * Special device vnode ops
869 struct vop_vector cd9660_fifoops = {
870 .vop_default = &fifo_specops,
871 .vop_access = cd9660_access,
872 .vop_getattr = cd9660_getattr,
873 .vop_inactive = cd9660_inactive,
874 .vop_reclaim = cd9660_reclaim,
875 .vop_setattr = cd9660_setattr,
876 .vop_vptofh = cd9660_vptofh,