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 <isofs/cd9660/iso.h>
61 #include <isofs/cd9660/cd9660_node.h>
62 #include <isofs/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;
79 * Setattr call. Only allowed for block and character special devices.
83 struct vop_setattr_args /* {
84 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 mode_t mode = ap->a_mode;
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
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_mode, ap->a_cred, NULL));
163 struct vop_open_args /* {
166 struct ucred *a_cred;
171 struct iso_node *ip = VTOI(ap->a_vp);
173 vnode_create_vobject(ap->a_vp, ip->i_size, ap->a_td);
180 struct vop_getattr_args /* {
183 struct ucred *a_cred;
188 struct vnode *vp = ap->a_vp;
189 struct vattr *vap = ap->a_vap;
190 struct iso_node *ip = VTOI(vp);
192 vap->va_fsid = dev2udev(ip->i_mnt->im_dev);
193 vap->va_fileid = ip->i_number;
195 vap->va_mode = ip->inode.iso_mode;
196 vap->va_nlink = ip->inode.iso_links;
197 vap->va_uid = ip->inode.iso_uid;
198 vap->va_gid = ip->inode.iso_gid;
199 vap->va_atime = ip->inode.iso_atime;
200 vap->va_mtime = ip->inode.iso_mtime;
201 vap->va_ctime = ip->inode.iso_ctime;
202 vap->va_rdev = ip->inode.iso_rdev;
204 vap->va_size = (u_quad_t) ip->i_size;
205 if (ip->i_size == 0 && (vap->va_mode & S_IFMT) == S_IFLNK) {
206 struct vop_readlink_args rdlnk;
211 MALLOC(cp, char *, MAXPATHLEN, M_TEMP, M_WAITOK);
213 aiov.iov_len = MAXPATHLEN;
214 auio.uio_iov = &aiov;
217 auio.uio_rw = UIO_READ;
218 auio.uio_segflg = UIO_SYSSPACE;
219 auio.uio_td = ap->a_td;
220 auio.uio_resid = MAXPATHLEN;
222 rdlnk.a_vp = ap->a_vp;
223 rdlnk.a_cred = ap->a_cred;
224 if (cd9660_readlink(&rdlnk) == 0)
225 vap->va_size = MAXPATHLEN - auio.uio_resid;
230 vap->va_blocksize = ip->i_mnt->logical_block_size;
231 vap->va_bytes = (u_quad_t) ip->i_size;
232 vap->va_type = vp->v_type;
238 * Vnode op for ioctl.
242 struct vop_ioctl_args /* {
247 struct ucred *a_cred;
251 struct vnode *vp = ap->a_vp;
252 struct iso_node *ip = VTOI(vp);
254 if (vp->v_type == VCHR || vp->v_type == VBLK)
257 switch (ap->a_command) {
260 *(int *)(ap->a_data) = ip->iso_start;
268 * Vnode op for reading.
272 struct vop_read_args /* {
276 struct ucred *a_cred;
279 struct vnode *vp = ap->a_vp;
280 struct uio *uio = ap->a_uio;
281 struct iso_node *ip = VTOI(vp);
284 daddr_t lbn, rablock;
286 int rasize, error = 0;
290 if (vp->v_type == VCHR || vp->v_type == VBLK)
293 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
295 if (uio->uio_resid == 0)
297 if (uio->uio_offset < 0)
299 ip->i_flag |= IN_ACCESS;
302 lbn = lblkno(imp, uio->uio_offset);
303 on = blkoff(imp, uio->uio_offset);
304 n = min((u_int)(imp->logical_block_size - on),
306 diff = (off_t)ip->i_size - uio->uio_offset;
311 size = blksize(imp, ip, lbn);
313 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
314 if (lblktosize(imp, rablock) < ip->i_size)
315 error = cluster_read(vp, (off_t)ip->i_size,
316 lbn, size, NOCRED, uio->uio_resid,
317 (ap->a_ioflag >> 16), &bp);
319 error = bread(vp, lbn, size, NOCRED, &bp);
322 lblktosize(imp, rablock) < ip->i_size) {
323 rasize = blksize(imp, ip, rablock);
324 error = breadn(vp, lbn, size, &rablock,
325 &rasize, 1, NOCRED, &bp);
327 error = bread(vp, lbn, size, NOCRED, &bp);
329 n = min(n, size - bp->b_resid);
335 error = uiomove(bp->b_data + on, (int)n, uio);
337 } while (error == 0 && uio->uio_resid > 0 && n != 0);
342 * Structure for reading directories
345 struct dirent saveent;
346 struct dirent assocent;
347 struct dirent current;
359 iso_uiodir(idp,dp,off)
360 struct isoreaddir *idp;
366 dp->d_name[dp->d_namlen] = 0;
367 dp->d_reclen = GENERIC_DIRSIZ(dp);
369 if (idp->uio->uio_resid < dp->d_reclen) {
375 if (idp->ncookies <= 0) {
380 *idp->cookies++ = off;
384 if ((error = uiomove(dp, dp->d_reclen, idp->uio)) != 0)
392 struct isoreaddir *idp;
399 cl = idp->current.d_namlen;
400 cname = idp->current.d_name;
401 assoc = (cl > 1) && (*cname == ASSOCCHAR);
409 if (!(sl = dp->d_namlen)) {
411 sname = dp->d_name + 1;
412 sl = dp->d_namlen - 1;
416 || bcmp(sname,cname,sl)) {
417 if (idp->assocent.d_namlen) {
418 if ((error = iso_uiodir(idp,&idp->assocent,idp->assocoff)) != 0)
420 idp->assocent.d_namlen = 0;
422 if (idp->saveent.d_namlen) {
423 if ((error = iso_uiodir(idp,&idp->saveent,idp->saveoff)) != 0)
425 idp->saveent.d_namlen = 0;
429 idp->current.d_reclen = GENERIC_DIRSIZ(&idp->current);
431 idp->assocoff = idp->curroff;
432 bcopy(&idp->current,&idp->assocent,idp->current.d_reclen);
434 idp->saveoff = idp->curroff;
435 bcopy(&idp->current,&idp->saveent,idp->current.d_reclen);
441 * Vnode op for readdir
445 struct vop_readdir_args /* {
448 struct ucred *a_cred;
454 struct uio *uio = ap->a_uio;
455 struct isoreaddir *idp;
456 struct vnode *vdp = ap->a_vp;
459 struct buf *bp = NULL;
460 struct iso_directory_record *ep;
461 int entryoffsetinblock;
468 u_long *cookies = NULL;
472 bmask = imp->im_bmask;
474 MALLOC(idp, struct isoreaddir *, sizeof(*idp), M_TEMP, M_WAITOK);
475 idp->saveent.d_namlen = idp->assocent.d_namlen = 0;
478 * Is it worth trying to figure out the type?
480 idp->saveent.d_type = idp->assocent.d_type = idp->current.d_type =
483 if (ap->a_ncookies == NULL) {
487 * Guess the number of cookies needed.
489 ncookies = uio->uio_resid / 16;
490 MALLOC(cookies, u_long *, ncookies * sizeof(u_long),
492 idp->cookies = cookies;
493 idp->ncookies = ncookies;
496 idp->curroff = uio->uio_offset;
498 if ((entryoffsetinblock = idp->curroff & bmask) &&
499 (error = cd9660_blkatoff(vdp, (off_t)idp->curroff, NULL, &bp))) {
503 endsearch = dp->i_size;
505 while (idp->curroff < endsearch) {
507 * If offset is on a block boundary,
508 * read the next directory block.
509 * Release previous if it exists.
511 if ((idp->curroff & bmask) == 0) {
515 cd9660_blkatoff(vdp, (off_t)idp->curroff, NULL, &bp)) != 0)
517 entryoffsetinblock = 0;
520 * Get pointer to next entry.
522 ep = (struct iso_directory_record *)
523 ((char *)bp->b_data + entryoffsetinblock);
525 reclen = isonum_711(ep->length);
527 /* skip to next block, if any */
529 (idp->curroff & ~bmask) + imp->logical_block_size;
533 if (reclen < ISO_DIRECTORY_RECORD_SIZE) {
535 /* illegal entry, stop */
539 if (entryoffsetinblock + reclen > imp->logical_block_size) {
541 /* illegal directory, so stop looking */
545 idp->current.d_namlen = isonum_711(ep->name_len);
547 if (reclen < ISO_DIRECTORY_RECORD_SIZE + idp->current.d_namlen) {
549 /* illegal entry, stop */
553 if (isonum_711(ep->flags)&2)
554 idp->current.d_fileno = isodirino(ep, imp);
556 idp->current.d_fileno = dbtob(bp->b_blkno) +
559 idp->curroff += reclen;
561 switch (imp->iso_ftype) {
563 cd9660_rrip_getname(ep,idp->current.d_name, &namelen,
564 &idp->current.d_fileno,imp);
565 idp->current.d_namlen = (u_char)namelen;
566 if (idp->current.d_namlen)
567 error = iso_uiodir(idp,&idp->current,idp->curroff);
569 default: /* ISO_FTYPE_DEFAULT || ISO_FTYPE_9660 || ISO_FTYPE_HIGH_SIERRA*/
570 strcpy(idp->current.d_name,"..");
571 if (idp->current.d_namlen == 1 && ep->name[0] == 0) {
572 idp->current.d_namlen = 1;
573 error = iso_uiodir(idp,&idp->current,idp->curroff);
574 } else if (idp->current.d_namlen == 1 && ep->name[0] == 1) {
575 idp->current.d_namlen = 2;
576 error = iso_uiodir(idp,&idp->current,idp->curroff);
578 isofntrans(ep->name,idp->current.d_namlen,
579 idp->current.d_name, &namelen,
580 imp->iso_ftype == ISO_FTYPE_9660,
581 isonum_711(ep->flags)&4,
585 idp->current.d_namlen = (u_char)namelen;
586 if (imp->iso_ftype == ISO_FTYPE_DEFAULT)
587 error = iso_shipdir(idp);
589 error = iso_uiodir(idp,&idp->current,idp->curroff);
595 entryoffsetinblock += reclen;
598 if (!error && imp->iso_ftype == ISO_FTYPE_DEFAULT) {
599 idp->current.d_namlen = 0;
600 error = iso_shipdir(idp);
605 if (ap->a_ncookies != NULL) {
607 free(cookies, M_TEMP);
610 * Work out the number of cookies actually used.
612 *ap->a_ncookies = ncookies - idp->ncookies;
613 *ap->a_cookies = cookies;
620 uio->uio_offset = idp->uio_off;
621 *ap->a_eofflag = idp->eofflag;
629 * Return target name of a symbolic link
630 * Shouldn't we get the parent vnode and read the data from there?
631 * This could eventually result in deadlocks in cd9660_lookup.
632 * But otherwise the block read here is in the block buffer two times.
634 typedef struct iso_directory_record ISODIR;
635 typedef struct iso_node ISONODE;
636 typedef struct iso_mnt ISOMNT;
639 struct vop_readlink_args /* {
642 struct ucred *a_cred;
658 if (imp->iso_ftype != ISO_FTYPE_RRIP)
662 * Get parents directory record block that this inode included.
664 error = bread(imp->im_devvp,
665 (ip->i_number >> imp->im_bshift) <<
666 (imp->im_bshift - DEV_BSHIFT),
667 imp->logical_block_size, NOCRED, &bp);
674 * Setup the directory pointer for this inode
676 dirp = (ISODIR *)(bp->b_data + (ip->i_number & imp->im_bmask));
679 * Just make sure, we have a right one....
680 * 1: Check not cross boundary on block
682 if ((ip->i_number & imp->im_bmask) + isonum_711(dirp->length)
683 > (unsigned)imp->logical_block_size) {
690 * Abuse a namei buffer for now.
692 if (uio->uio_segflg == UIO_SYSSPACE)
693 symname = uio->uio_iov->iov_base;
695 symname = uma_zalloc(namei_zone, M_WAITOK);
698 * Ok, we just gathering a symbolic name in SL record.
700 if (cd9660_rrip_getsymname(dirp, symname, &symlen, imp) == 0) {
701 if (uio->uio_segflg != UIO_SYSSPACE)
702 uma_zfree(namei_zone, symname);
707 * Don't forget before you leave from home ;-)
712 * return with the symbolic name to caller's.
714 if (uio->uio_segflg != UIO_SYSSPACE) {
715 error = uiomove(symname, symlen, uio);
716 uma_zfree(namei_zone, symname);
719 uio->uio_resid -= symlen;
720 uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + symlen;
721 uio->uio_iov->iov_len -= symlen;
726 * Calculate the logical to physical mapping if not done already,
727 * then call the device strategy routine.
731 struct vop_strategy_args /* {
736 struct buf *bp = ap->a_bp;
737 struct vnode *vp = ap->a_vp;
742 if (vp->v_type == VBLK || vp->v_type == VCHR)
743 panic("cd9660_strategy: spec");
744 if (bp->b_blkno == bp->b_lblkno) {
745 bp->b_blkno = (ip->iso_start + bp->b_lblkno) <<
746 (ip->i_mnt->im_bshift - DEV_BSHIFT);
747 if ((long)bp->b_blkno == -1) /* XXX: cut&paste junk ? */
750 if ((long)bp->b_blkno == -1) { /* XXX: cut&paste junk ? */
754 bp->b_iooffset = dbtob(bp->b_blkno);
755 bo = ip->i_mnt->im_bo;
761 * Return POSIX pathconf information applicable to cd9660 filesystems.
765 struct vop_pathconf_args /* {
768 register_t *a_retval;
772 switch (ap->a_name) {
777 if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP)
778 *ap->a_retval = NAME_MAX;
783 *ap->a_retval = PATH_MAX;
786 *ap->a_retval = PIPE_BUF;
788 case _PC_CHOWN_RESTRICTED:
801 * Global vfs data structures for cd9660
803 struct vop_vector cd9660_vnodeops = {
804 .vop_default = &default_vnodeops,
805 .vop_open = cd9660_open,
806 .vop_access = cd9660_access,
807 .vop_bmap = cd9660_bmap,
808 .vop_cachedlookup = cd9660_lookup,
809 .vop_getattr = cd9660_getattr,
810 .vop_inactive = cd9660_inactive,
811 .vop_ioctl = cd9660_ioctl,
812 .vop_lookup = vfs_cache_lookup,
813 .vop_pathconf = cd9660_pathconf,
814 .vop_read = cd9660_read,
815 .vop_readdir = cd9660_readdir,
816 .vop_readlink = cd9660_readlink,
817 .vop_reclaim = cd9660_reclaim,
818 .vop_setattr = cd9660_setattr,
819 .vop_strategy = cd9660_strategy,
823 * Special device vnode ops
826 struct vop_vector cd9660_fifoops = {
827 .vop_default = &fifo_specops,
828 .vop_access = cd9660_access,
829 .vop_getattr = cd9660_getattr,
830 .vop_inactive = cd9660_inactive,
831 .vop_reclaim = cd9660_reclaim,
832 .vop_setattr = cd9660_setattr,