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 <sys/malloc.h>
51 #include <sys/dirent.h>
52 #include <sys/unistd.h>
53 #include <sys/filio.h>
56 #include <vm/vnode_pager.h>
59 #include <fs/cd9660/iso.h>
60 #include <fs/cd9660/cd9660_node.h>
61 #include <fs/cd9660/iso_rrip.h>
63 static vop_setattr_t cd9660_setattr;
64 static vop_open_t cd9660_open;
65 static vop_access_t cd9660_access;
66 static vop_getattr_t cd9660_getattr;
67 static vop_ioctl_t cd9660_ioctl;
68 static vop_pathconf_t cd9660_pathconf;
69 static vop_read_t cd9660_read;
71 static int iso_uiodir(struct isoreaddir *idp, struct dirent *dp, off_t off);
72 static int iso_shipdir(struct isoreaddir *idp);
73 static vop_readdir_t cd9660_readdir;
74 static vop_readlink_t cd9660_readlink;
75 static vop_strategy_t cd9660_strategy;
76 static vop_vptofh_t cd9660_vptofh;
79 * Setattr call. Only allowed for block and character special devices.
83 struct vop_setattr_args /* {
84 struct vnodeop_desc *a_desc;
90 struct vnode *vp = ap->a_vp;
91 struct vattr *vap = ap->a_vap;
93 if (vap->va_flags != (u_long)VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
94 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
95 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL)
97 if (vap->va_size != (u_quad_t)VNOVAL) {
118 * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
119 * The mode is shifted to select the owner/group/other fields. The
120 * super user is granted all permissions.
125 struct vop_access_args /* {
128 struct ucred *a_cred;
132 struct vnode *vp = ap->a_vp;
133 struct iso_node *ip = VTOI(vp);
134 accmode_t accmode = ap->a_accmode;
136 if (vp->v_type == VCHR || vp->v_type == VBLK)
140 * Disallow write attempts unless the file is a socket,
141 * fifo, or a block or character device resident on the
144 if (accmode & VWRITE) {
145 switch (vp->v_type) {
156 return (vaccess(vp->v_type, ip->inode.iso_mode, ip->inode.iso_uid,
157 ip->inode.iso_gid, ap->a_accmode, ap->a_cred, NULL));
162 struct vop_open_args /* {
165 struct ucred *a_cred;
170 struct vnode *vp = ap->a_vp;
171 struct iso_node *ip = VTOI(vp);
173 if (vp->v_type == VCHR || vp->v_type == VBLK)
176 vnode_create_vobject(vp, ip->i_size, ap->a_td);
183 struct vop_getattr_args /* {
186 struct ucred *a_cred;
190 struct vnode *vp = ap->a_vp;
191 struct vattr *vap = ap->a_vap;
192 struct iso_node *ip = VTOI(vp);
194 vap->va_fsid = dev2udev(ip->i_mnt->im_dev);
195 vap->va_fileid = ip->i_number;
197 vap->va_mode = ip->inode.iso_mode;
198 vap->va_nlink = ip->inode.iso_links;
199 vap->va_uid = ip->inode.iso_uid;
200 vap->va_gid = ip->inode.iso_gid;
201 vap->va_atime = ip->inode.iso_atime;
202 vap->va_mtime = ip->inode.iso_mtime;
203 vap->va_ctime = ip->inode.iso_ctime;
204 vap->va_rdev = ip->inode.iso_rdev;
206 vap->va_size = (u_quad_t) ip->i_size;
207 if (ip->i_size == 0 && (vap->va_mode & S_IFMT) == S_IFLNK) {
208 struct vop_readlink_args rdlnk;
213 cp = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
215 aiov.iov_len = MAXPATHLEN;
216 auio.uio_iov = &aiov;
219 auio.uio_rw = UIO_READ;
220 auio.uio_segflg = UIO_SYSSPACE;
221 auio.uio_td = curthread;
222 auio.uio_resid = MAXPATHLEN;
224 rdlnk.a_vp = ap->a_vp;
225 rdlnk.a_cred = ap->a_cred;
226 if (cd9660_readlink(&rdlnk) == 0)
227 vap->va_size = MAXPATHLEN - auio.uio_resid;
232 vap->va_blocksize = ip->i_mnt->logical_block_size;
233 vap->va_bytes = (u_quad_t) ip->i_size;
234 vap->va_type = vp->v_type;
240 * Vnode op for ioctl.
244 struct vop_ioctl_args /* {
249 struct ucred *a_cred;
258 vn_lock(vp, LK_SHARED | LK_RETRY);
259 if (vp->v_iflag & VI_DOOMED) {
263 if (vp->v_type == VCHR || vp->v_type == VBLK) {
271 switch (ap->a_command) {
273 *(int *)(ap->a_data) = ip->iso_start;
285 * Vnode op for reading.
289 struct vop_read_args /* {
293 struct ucred *a_cred;
296 struct vnode *vp = ap->a_vp;
297 struct uio *uio = ap->a_uio;
298 struct iso_node *ip = VTOI(vp);
301 daddr_t lbn, rablock;
303 int rasize, error = 0;
307 if (vp->v_type == VCHR || vp->v_type == VBLK)
310 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
312 if (uio->uio_resid == 0)
314 if (uio->uio_offset < 0)
318 lbn = lblkno(imp, uio->uio_offset);
319 on = blkoff(imp, uio->uio_offset);
320 n = MIN(imp->logical_block_size - on, uio->uio_resid);
321 diff = (off_t)ip->i_size - uio->uio_offset;
326 size = blksize(imp, ip, lbn);
328 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
329 if (lblktosize(imp, rablock) < ip->i_size)
330 error = cluster_read(vp, (off_t)ip->i_size,
331 lbn, size, NOCRED, uio->uio_resid,
332 (ap->a_ioflag >> 16), 0, &bp);
334 error = bread(vp, lbn, size, NOCRED, &bp);
337 lblktosize(imp, rablock) < ip->i_size) {
338 rasize = blksize(imp, ip, rablock);
339 error = breadn(vp, lbn, size, &rablock,
340 &rasize, 1, NOCRED, &bp);
342 error = bread(vp, lbn, size, NOCRED, &bp);
344 n = MIN(n, size - bp->b_resid);
350 error = uiomove(bp->b_data + on, (int)n, uio);
352 } while (error == 0 && uio->uio_resid > 0 && n != 0);
357 * Structure for reading directories
360 struct dirent saveent;
361 struct dirent assocent;
362 struct dirent current;
374 iso_uiodir(idp,dp,off)
375 struct isoreaddir *idp;
381 dp->d_name[dp->d_namlen] = 0;
382 dp->d_reclen = GENERIC_DIRSIZ(dp);
384 if (idp->uio->uio_resid < dp->d_reclen) {
390 if (idp->ncookies <= 0) {
395 *idp->cookies++ = off;
399 if ((error = uiomove(dp, dp->d_reclen, idp->uio)) != 0)
407 struct isoreaddir *idp;
414 cl = idp->current.d_namlen;
415 cname = idp->current.d_name;
416 assoc = (cl > 1) && (*cname == ASSOCCHAR);
424 if (!(sl = dp->d_namlen)) {
426 sname = dp->d_name + 1;
427 sl = dp->d_namlen - 1;
431 || bcmp(sname,cname,sl)) {
432 if (idp->assocent.d_namlen) {
433 if ((error = iso_uiodir(idp,&idp->assocent,idp->assocoff)) != 0)
435 idp->assocent.d_namlen = 0;
437 if (idp->saveent.d_namlen) {
438 if ((error = iso_uiodir(idp,&idp->saveent,idp->saveoff)) != 0)
440 idp->saveent.d_namlen = 0;
444 idp->current.d_reclen = GENERIC_DIRSIZ(&idp->current);
446 idp->assocoff = idp->curroff;
447 bcopy(&idp->current,&idp->assocent,idp->current.d_reclen);
449 idp->saveoff = idp->curroff;
450 bcopy(&idp->current,&idp->saveent,idp->current.d_reclen);
456 * Vnode op for readdir
460 struct vop_readdir_args /* {
463 struct ucred *a_cred;
469 struct uio *uio = ap->a_uio;
470 struct isoreaddir *idp;
471 struct vnode *vdp = ap->a_vp;
474 struct buf *bp = NULL;
475 struct iso_directory_record *ep;
476 int entryoffsetinblock;
483 u_long *cookies = NULL;
487 bmask = imp->im_bmask;
489 idp = malloc(sizeof(*idp), M_TEMP, M_WAITOK);
490 idp->saveent.d_namlen = idp->assocent.d_namlen = 0;
493 * Is it worth trying to figure out the type?
495 idp->saveent.d_type = idp->assocent.d_type = idp->current.d_type =
498 if (ap->a_ncookies == NULL) {
502 * Guess the number of cookies needed.
504 ncookies = uio->uio_resid / 16;
505 cookies = malloc(ncookies * sizeof(u_long),
507 idp->cookies = cookies;
508 idp->ncookies = ncookies;
511 idp->curroff = uio->uio_offset;
512 idp->uio_off = uio->uio_offset;
514 if ((entryoffsetinblock = idp->curroff & bmask) &&
515 (error = cd9660_blkatoff(vdp, (off_t)idp->curroff, NULL, &bp))) {
519 endsearch = dp->i_size;
521 while (idp->curroff < endsearch) {
523 * If offset is on a block boundary,
524 * read the next directory block.
525 * Release previous if it exists.
527 if ((idp->curroff & bmask) == 0) {
531 cd9660_blkatoff(vdp, (off_t)idp->curroff, NULL, &bp)) != 0)
533 entryoffsetinblock = 0;
536 * Get pointer to next entry.
538 ep = (struct iso_directory_record *)
539 ((char *)bp->b_data + entryoffsetinblock);
541 reclen = isonum_711(ep->length);
543 /* skip to next block, if any */
545 (idp->curroff & ~bmask) + imp->logical_block_size;
549 if (reclen < ISO_DIRECTORY_RECORD_SIZE) {
551 /* illegal entry, stop */
555 if (entryoffsetinblock + reclen > imp->logical_block_size) {
557 /* illegal directory, so stop looking */
561 idp->current.d_namlen = isonum_711(ep->name_len);
563 if (reclen < ISO_DIRECTORY_RECORD_SIZE + idp->current.d_namlen) {
565 /* illegal entry, stop */
569 if (isonum_711(ep->flags)&2)
570 idp->current.d_fileno = isodirino(ep, imp);
572 idp->current.d_fileno = dbtob(bp->b_blkno) +
575 idp->curroff += reclen;
577 switch (imp->iso_ftype) {
579 cd9660_rrip_getname(ep,idp->current.d_name, &namelen,
580 &idp->current.d_fileno,imp);
581 idp->current.d_namlen = (u_char)namelen;
582 if (idp->current.d_namlen)
583 error = iso_uiodir(idp,&idp->current,idp->curroff);
585 default: /* ISO_FTYPE_DEFAULT || ISO_FTYPE_9660 || ISO_FTYPE_HIGH_SIERRA*/
586 strcpy(idp->current.d_name,"..");
587 if (idp->current.d_namlen == 1 && ep->name[0] == 0) {
588 idp->current.d_namlen = 1;
589 error = iso_uiodir(idp,&idp->current,idp->curroff);
590 } else if (idp->current.d_namlen == 1 && ep->name[0] == 1) {
591 idp->current.d_namlen = 2;
592 error = iso_uiodir(idp,&idp->current,idp->curroff);
594 isofntrans(ep->name,idp->current.d_namlen,
595 idp->current.d_name, &namelen,
596 imp->iso_ftype == ISO_FTYPE_9660,
597 isonum_711(ep->flags)&4,
601 idp->current.d_namlen = (u_char)namelen;
602 if (imp->iso_ftype == ISO_FTYPE_DEFAULT)
603 error = iso_shipdir(idp);
605 error = iso_uiodir(idp,&idp->current,idp->curroff);
611 entryoffsetinblock += reclen;
614 if (!error && imp->iso_ftype == ISO_FTYPE_DEFAULT) {
615 idp->current.d_namlen = 0;
616 error = iso_shipdir(idp);
621 if (ap->a_ncookies != NULL) {
623 free(cookies, M_TEMP);
626 * Work out the number of cookies actually used.
628 *ap->a_ncookies = ncookies - idp->ncookies;
629 *ap->a_cookies = cookies;
636 uio->uio_offset = idp->uio_off;
637 *ap->a_eofflag = idp->eofflag;
645 * Return target name of a symbolic link
646 * Shouldn't we get the parent vnode and read the data from there?
647 * This could eventually result in deadlocks in cd9660_lookup.
648 * But otherwise the block read here is in the block buffer two times.
650 typedef struct iso_directory_record ISODIR;
651 typedef struct iso_node ISONODE;
652 typedef struct iso_mnt ISOMNT;
655 struct vop_readlink_args /* {
658 struct ucred *a_cred;
674 if (imp->iso_ftype != ISO_FTYPE_RRIP)
678 * Get parents directory record block that this inode included.
680 error = bread(imp->im_devvp,
681 (ip->i_number >> imp->im_bshift) <<
682 (imp->im_bshift - DEV_BSHIFT),
683 imp->logical_block_size, NOCRED, &bp);
690 * Setup the directory pointer for this inode
692 dirp = (ISODIR *)(bp->b_data + (ip->i_number & imp->im_bmask));
695 * Just make sure, we have a right one....
696 * 1: Check not cross boundary on block
698 if ((ip->i_number & imp->im_bmask) + isonum_711(dirp->length)
699 > (unsigned)imp->logical_block_size) {
706 * Abuse a namei buffer for now.
708 if (uio->uio_segflg == UIO_SYSSPACE)
709 symname = uio->uio_iov->iov_base;
711 symname = uma_zalloc(namei_zone, M_WAITOK);
714 * Ok, we just gathering a symbolic name in SL record.
716 if (cd9660_rrip_getsymname(dirp, symname, &symlen, imp) == 0) {
717 if (uio->uio_segflg != UIO_SYSSPACE)
718 uma_zfree(namei_zone, symname);
723 * Don't forget before you leave from home ;-)
728 * return with the symbolic name to caller's.
730 if (uio->uio_segflg != UIO_SYSSPACE) {
731 error = uiomove(symname, symlen, uio);
732 uma_zfree(namei_zone, symname);
735 uio->uio_resid -= symlen;
736 uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + symlen;
737 uio->uio_iov->iov_len -= symlen;
742 * Calculate the logical to physical mapping if not done already,
743 * then call the device strategy routine.
747 struct vop_strategy_args /* {
752 struct buf *bp = ap->a_bp;
753 struct vnode *vp = ap->a_vp;
758 if (vp->v_type == VBLK || vp->v_type == VCHR)
759 panic("cd9660_strategy: spec");
760 if (bp->b_blkno == bp->b_lblkno) {
761 bp->b_blkno = (ip->iso_start + bp->b_lblkno) <<
762 (ip->i_mnt->im_bshift - DEV_BSHIFT);
764 bp->b_iooffset = dbtob(bp->b_blkno);
765 bo = ip->i_mnt->im_bo;
771 * Return POSIX pathconf information applicable to cd9660 filesystems.
775 struct vop_pathconf_args /* {
778 register_t *a_retval;
782 switch (ap->a_name) {
787 if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP)
788 *ap->a_retval = NAME_MAX;
793 *ap->a_retval = PATH_MAX;
796 *ap->a_retval = PIPE_BUF;
798 case _PC_CHOWN_RESTRICTED:
811 * Vnode pointer to File handle
815 struct vop_vptofh_args /* {
821 struct iso_node *ip = VTOI(ap->a_vp);
823 ifh.ifid_len = sizeof(struct ifid);
825 ifh.ifid_ino = ip->i_number;
826 ifh.ifid_start = ip->iso_start;
828 * This intentionally uses sizeof(ifh) in order to not copy stack
831 memcpy(ap->a_fhp, &ifh, sizeof(ifh));
834 printf("vptofh: ino %d, start %ld\n",
835 ifh.ifid_ino, ifh.ifid_start);
842 * Global vfs data structures for cd9660
844 struct vop_vector cd9660_vnodeops = {
845 .vop_default = &default_vnodeops,
846 .vop_open = cd9660_open,
847 .vop_access = cd9660_access,
848 .vop_bmap = cd9660_bmap,
849 .vop_cachedlookup = cd9660_lookup,
850 .vop_getattr = cd9660_getattr,
851 .vop_inactive = cd9660_inactive,
852 .vop_ioctl = cd9660_ioctl,
853 .vop_lookup = vfs_cache_lookup,
854 .vop_pathconf = cd9660_pathconf,
855 .vop_read = cd9660_read,
856 .vop_readdir = cd9660_readdir,
857 .vop_readlink = cd9660_readlink,
858 .vop_reclaim = cd9660_reclaim,
859 .vop_setattr = cd9660_setattr,
860 .vop_strategy = cd9660_strategy,
861 .vop_vptofh = cd9660_vptofh,
865 * Special device vnode ops
868 struct vop_vector cd9660_fifoops = {
869 .vop_default = &fifo_specops,
870 .vop_access = cd9660_access,
871 .vop_getattr = cd9660_getattr,
872 .vop_inactive = cd9660_inactive,
873 .vop_reclaim = cd9660_reclaim,
874 .vop_setattr = cd9660_setattr,
875 .vop_vptofh = cd9660_vptofh,