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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)cd9660_vnops.c 8.19 (Berkeley) 5/27/95
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/namei.h>
47 #include <sys/kernel.h>
51 #include <sys/mount.h>
52 #include <sys/vnode.h>
53 #include <fs/fifofs/fifo.h>
54 #include <sys/malloc.h>
55 #include <sys/dirent.h>
56 #include <sys/unistd.h>
57 #include <sys/filio.h>
60 #include <vm/vnode_pager.h>
63 #include <isofs/cd9660/iso.h>
64 #include <isofs/cd9660/cd9660_node.h>
65 #include <isofs/cd9660/iso_rrip.h>
67 static int cd9660_setattr(struct vop_setattr_args *);
68 static int cd9660_access(struct vop_access_args *);
69 static int cd9660_getattr(struct vop_getattr_args *);
70 static int cd9660_ioctl(struct vop_ioctl_args *);
71 static int cd9660_pathconf(struct vop_pathconf_args *);
72 static int cd9660_read(struct vop_read_args *);
74 static int iso_uiodir(struct isoreaddir *idp, struct dirent *dp, off_t off);
75 static int iso_shipdir(struct isoreaddir *idp);
76 static int cd9660_readdir(struct vop_readdir_args *);
77 static int cd9660_readlink(struct vop_readlink_args *ap);
78 static int cd9660_strategy(struct vop_strategy_args *);
81 * Setattr call. Only allowed for block and character special devices.
85 struct vop_setattr_args /* {
86 struct vnodeop_desc *a_desc;
93 struct vnode *vp = ap->a_vp;
94 struct vattr *vap = ap->a_vap;
96 if (vap->va_flags != (u_long)VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
97 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
98 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL)
100 if (vap->va_size != (u_quad_t)VNOVAL) {
101 switch (vp->v_type) {
120 * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
121 * The mode is shifted to select the owner/group/other fields. The
122 * super user is granted all permissions.
127 struct vop_access_args /* {
130 struct ucred *a_cred;
134 struct vnode *vp = ap->a_vp;
135 struct iso_node *ip = VTOI(vp);
136 mode_t mode = ap->a_mode;
139 * Disallow write attempts unless the file is a socket,
140 * fifo, or a block or character device resident on the
144 switch (vp->v_type) {
155 return (vaccess(vp->v_type, ip->inode.iso_mode, ip->inode.iso_uid,
156 ip->inode.iso_gid, ap->a_mode, ap->a_cred, NULL));
161 struct vop_getattr_args /* {
164 struct ucred *a_cred;
169 struct vnode *vp = ap->a_vp;
170 struct vattr *vap = ap->a_vap;
171 struct iso_node *ip = VTOI(vp);
173 vap->va_fsid = dev2udev(ip->i_dev);
176 * Don't use ip->i_ino for this since it is wrong for hard links.
177 * ip->i_ino should be the same as ip->iso_start (or not exist),
178 * but this currently doesn't work since we abuse it to look up
179 * parent directories from inodes.
181 vap->va_fileid = ip->iso_start;
183 vap->va_mode = ip->inode.iso_mode;
184 vap->va_nlink = ip->inode.iso_links;
185 vap->va_uid = ip->inode.iso_uid;
186 vap->va_gid = ip->inode.iso_gid;
187 vap->va_atime = ip->inode.iso_atime;
188 vap->va_mtime = ip->inode.iso_mtime;
189 vap->va_ctime = ip->inode.iso_ctime;
190 vap->va_rdev = ip->inode.iso_rdev;
192 vap->va_size = (u_quad_t) ip->i_size;
193 if (ip->i_size == 0 && (vap->va_mode & S_IFMT) == S_IFLNK) {
194 struct vop_readlink_args rdlnk;
199 MALLOC(cp, char *, MAXPATHLEN, M_TEMP, M_WAITOK);
201 aiov.iov_len = MAXPATHLEN;
202 auio.uio_iov = &aiov;
205 auio.uio_rw = UIO_READ;
206 auio.uio_segflg = UIO_SYSSPACE;
207 auio.uio_td = ap->a_td;
208 auio.uio_resid = MAXPATHLEN;
210 rdlnk.a_vp = ap->a_vp;
211 rdlnk.a_cred = ap->a_cred;
212 if (cd9660_readlink(&rdlnk) == 0)
213 vap->va_size = MAXPATHLEN - auio.uio_resid;
218 vap->va_blocksize = ip->i_mnt->logical_block_size;
219 vap->va_bytes = (u_quad_t) ip->i_size;
220 vap->va_type = vp->v_type;
226 * Vnode op for ioctl.
230 struct vop_ioctl_args /* {
235 struct ucred *a_cred;
239 struct vnode *vp = ap->a_vp;
240 struct iso_node *ip = VTOI(vp);
242 switch (ap->a_command) {
245 *(int *)(ap->a_data) = ip->iso_start;
253 * Vnode op for reading.
257 struct vop_read_args /* {
261 struct ucred *a_cred;
264 struct vnode *vp = ap->a_vp;
265 struct uio *uio = ap->a_uio;
266 struct iso_node *ip = VTOI(vp);
269 daddr_t lbn, rablock;
271 int rasize, error = 0;
275 seqcount = ap->a_ioflag >> 16;
277 if (uio->uio_resid == 0)
279 if (uio->uio_offset < 0)
281 ip->i_flag |= IN_ACCESS;
284 lbn = lblkno(imp, uio->uio_offset);
285 on = blkoff(imp, uio->uio_offset);
286 n = min((u_int)(imp->logical_block_size - on),
288 diff = (off_t)ip->i_size - uio->uio_offset;
293 size = blksize(imp, ip, lbn);
295 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
296 if (lblktosize(imp, rablock) < ip->i_size)
297 error = cluster_read(vp, (off_t)ip->i_size,
298 lbn, size, NOCRED, uio->uio_resid,
299 (ap->a_ioflag >> 16), &bp);
301 error = bread(vp, lbn, size, NOCRED, &bp);
304 lblktosize(imp, rablock) < ip->i_size) {
305 rasize = blksize(imp, ip, rablock);
306 error = breadn(vp, lbn, size, &rablock,
307 &rasize, 1, NOCRED, &bp);
309 error = bread(vp, lbn, size, NOCRED, &bp);
311 n = min(n, size - bp->b_resid);
317 error = uiomove(bp->b_data + on, (int)n, uio);
319 } while (error == 0 && uio->uio_resid > 0 && n != 0);
324 * Structure for reading directories
327 struct dirent saveent;
328 struct dirent assocent;
329 struct dirent current;
341 iso_uiodir(idp,dp,off)
342 struct isoreaddir *idp;
348 dp->d_name[dp->d_namlen] = 0;
349 dp->d_reclen = GENERIC_DIRSIZ(dp);
351 if (idp->uio->uio_resid < dp->d_reclen) {
357 if (idp->ncookies <= 0) {
362 *idp->cookies++ = off;
366 if ((error = uiomove(dp, dp->d_reclen, idp->uio)) != 0)
374 struct isoreaddir *idp;
381 cl = idp->current.d_namlen;
382 cname = idp->current.d_name;
383 assoc = (cl > 1) && (*cname == ASSOCCHAR);
391 if (!(sl = dp->d_namlen)) {
393 sname = dp->d_name + 1;
394 sl = dp->d_namlen - 1;
398 || bcmp(sname,cname,sl)) {
399 if (idp->assocent.d_namlen) {
400 if ((error = iso_uiodir(idp,&idp->assocent,idp->assocoff)) != 0)
402 idp->assocent.d_namlen = 0;
404 if (idp->saveent.d_namlen) {
405 if ((error = iso_uiodir(idp,&idp->saveent,idp->saveoff)) != 0)
407 idp->saveent.d_namlen = 0;
411 idp->current.d_reclen = GENERIC_DIRSIZ(&idp->current);
413 idp->assocoff = idp->curroff;
414 bcopy(&idp->current,&idp->assocent,idp->current.d_reclen);
416 idp->saveoff = idp->curroff;
417 bcopy(&idp->current,&idp->saveent,idp->current.d_reclen);
423 * Vnode op for readdir
427 struct vop_readdir_args /* {
430 struct ucred *a_cred;
436 struct uio *uio = ap->a_uio;
437 struct isoreaddir *idp;
438 struct vnode *vdp = ap->a_vp;
441 struct buf *bp = NULL;
442 struct iso_directory_record *ep;
443 int entryoffsetinblock;
450 u_long *cookies = NULL;
454 bmask = imp->im_bmask;
456 MALLOC(idp, struct isoreaddir *, sizeof(*idp), M_TEMP, M_WAITOK);
457 idp->saveent.d_namlen = idp->assocent.d_namlen = 0;
460 * Is it worth trying to figure out the type?
462 idp->saveent.d_type = idp->assocent.d_type = idp->current.d_type =
465 if (ap->a_ncookies == NULL) {
469 * Guess the number of cookies needed.
471 ncookies = uio->uio_resid / 16;
472 MALLOC(cookies, u_long *, ncookies * sizeof(u_int), M_TEMP,
474 idp->cookies = cookies;
475 idp->ncookies = ncookies;
478 idp->curroff = uio->uio_offset;
480 if ((entryoffsetinblock = idp->curroff & bmask) &&
481 (error = cd9660_blkatoff(vdp, (off_t)idp->curroff, NULL, &bp))) {
485 endsearch = dp->i_size;
487 while (idp->curroff < endsearch) {
489 * If offset is on a block boundary,
490 * read the next directory block.
491 * Release previous if it exists.
493 if ((idp->curroff & bmask) == 0) {
497 cd9660_blkatoff(vdp, (off_t)idp->curroff, NULL, &bp)) != 0)
499 entryoffsetinblock = 0;
502 * Get pointer to next entry.
504 ep = (struct iso_directory_record *)
505 ((char *)bp->b_data + entryoffsetinblock);
507 reclen = isonum_711(ep->length);
509 /* skip to next block, if any */
511 (idp->curroff & ~bmask) + imp->logical_block_size;
515 if (reclen < ISO_DIRECTORY_RECORD_SIZE) {
517 /* illegal entry, stop */
521 if (entryoffsetinblock + reclen > imp->logical_block_size) {
523 /* illegal directory, so stop looking */
527 idp->current.d_namlen = isonum_711(ep->name_len);
529 if (reclen < ISO_DIRECTORY_RECORD_SIZE + idp->current.d_namlen) {
531 /* illegal entry, stop */
536 * The "inode number" is iso_start, not i_ino, as in
539 idp->current.d_fileno = isonum_711(ep->ext_attr_length) +
540 isonum_733(ep->extent);
542 idp->curroff += reclen;
544 switch (imp->iso_ftype) {
546 cd9660_rrip_getname(ep,idp->current.d_name, &namelen,
547 &idp->current.d_fileno,imp);
548 idp->current.d_namlen = (u_char)namelen;
549 if (idp->current.d_namlen)
550 error = iso_uiodir(idp,&idp->current,idp->curroff);
552 default: /* ISO_FTYPE_DEFAULT || ISO_FTYPE_9660 || ISO_FTYPE_HIGH_SIERRA*/
553 strcpy(idp->current.d_name,"..");
554 if (idp->current.d_namlen == 1 && ep->name[0] == 0) {
555 idp->current.d_namlen = 1;
556 error = iso_uiodir(idp,&idp->current,idp->curroff);
557 } else if (idp->current.d_namlen == 1 && ep->name[0] == 1) {
558 idp->current.d_namlen = 2;
559 error = iso_uiodir(idp,&idp->current,idp->curroff);
561 isofntrans(ep->name,idp->current.d_namlen,
562 idp->current.d_name, &namelen,
563 imp->iso_ftype == ISO_FTYPE_9660,
564 isonum_711(ep->flags)&4,
566 idp->current.d_namlen = (u_char)namelen;
567 if (imp->iso_ftype == ISO_FTYPE_DEFAULT)
568 error = iso_shipdir(idp);
570 error = iso_uiodir(idp,&idp->current,idp->curroff);
576 entryoffsetinblock += reclen;
579 if (!error && imp->iso_ftype == ISO_FTYPE_DEFAULT) {
580 idp->current.d_namlen = 0;
581 error = iso_shipdir(idp);
586 if (ap->a_ncookies != NULL) {
588 free(cookies, M_TEMP);
591 * Work out the number of cookies actually used.
593 *ap->a_ncookies = ncookies - idp->ncookies;
594 *ap->a_cookies = cookies;
601 uio->uio_offset = idp->uio_off;
602 *ap->a_eofflag = idp->eofflag;
610 * Return target name of a symbolic link
611 * Shouldn't we get the parent vnode and read the data from there?
612 * This could eventually result in deadlocks in cd9660_lookup.
613 * But otherwise the block read here is in the block buffer two times.
615 typedef struct iso_directory_record ISODIR;
616 typedef struct iso_node ISONODE;
617 typedef struct iso_mnt ISOMNT;
620 struct vop_readlink_args /* {
623 struct ucred *a_cred;
639 if (imp->iso_ftype != ISO_FTYPE_RRIP)
643 * Get parents directory record block that this inode included.
645 error = bread(imp->im_devvp,
646 (ip->i_number >> imp->im_bshift) <<
647 (imp->im_bshift - DEV_BSHIFT),
648 imp->logical_block_size, NOCRED, &bp);
655 * Setup the directory pointer for this inode
657 dirp = (ISODIR *)(bp->b_data + (ip->i_number & imp->im_bmask));
660 * Just make sure, we have a right one....
661 * 1: Check not cross boundary on block
663 if ((ip->i_number & imp->im_bmask) + isonum_711(dirp->length)
664 > (unsigned)imp->logical_block_size) {
671 * Abuse a namei buffer for now.
673 if (uio->uio_segflg == UIO_SYSSPACE)
674 symname = uio->uio_iov->iov_base;
676 symname = uma_zalloc(namei_zone, M_WAITOK);
679 * Ok, we just gathering a symbolic name in SL record.
681 if (cd9660_rrip_getsymname(dirp, symname, &symlen, imp) == 0) {
682 if (uio->uio_segflg != UIO_SYSSPACE)
683 uma_zfree(namei_zone, symname);
688 * Don't forget before you leave from home ;-)
693 * return with the symbolic name to caller's.
695 if (uio->uio_segflg != UIO_SYSSPACE) {
696 error = uiomove(symname, symlen, uio);
697 uma_zfree(namei_zone, symname);
700 uio->uio_resid -= symlen;
701 uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + symlen;
702 uio->uio_iov->iov_len -= symlen;
707 * Calculate the logical to physical mapping if not done already,
708 * then call the device strategy routine.
712 struct vop_strategy_args /* {
717 struct buf *bp = ap->a_bp;
718 struct vnode *vp = bp->b_vp;
722 if (vp->v_type == VBLK || vp->v_type == VCHR)
723 panic("cd9660_strategy: spec");
724 if (bp->b_blkno == bp->b_lblkno) {
725 bp->b_blkno = (ip->iso_start + bp->b_lblkno) <<
726 (ip->i_mnt->im_bshift - DEV_BSHIFT);
727 if ((long)bp->b_blkno == -1) /* XXX: cut&paste junk ? */
730 if ((long)bp->b_blkno == -1) { /* XXX: cut&paste junk ? */
735 bp->b_dev = vp->v_rdev;
736 VOP_SPECSTRATEGY(vp, bp);
741 * Return POSIX pathconf information applicable to cd9660 filesystems.
745 struct vop_pathconf_args /* {
748 register_t *a_retval;
752 switch (ap->a_name) {
757 if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP)
758 *ap->a_retval = NAME_MAX;
763 *ap->a_retval = PATH_MAX;
766 *ap->a_retval = PIPE_BUF;
768 case _PC_CHOWN_RESTRICTED:
781 * Global vfs data structures for cd9660
783 vop_t **cd9660_vnodeop_p;
784 static struct vnodeopv_entry_desc cd9660_vnodeop_entries[] = {
785 { &vop_default_desc, (vop_t *) vop_defaultop },
786 { &vop_access_desc, (vop_t *) cd9660_access },
787 { &vop_bmap_desc, (vop_t *) cd9660_bmap },
788 { &vop_cachedlookup_desc, (vop_t *) cd9660_lookup },
789 { &vop_getattr_desc, (vop_t *) cd9660_getattr },
790 { &vop_inactive_desc, (vop_t *) cd9660_inactive },
791 { &vop_ioctl_desc, (vop_t *) cd9660_ioctl },
792 { &vop_lookup_desc, (vop_t *) vfs_cache_lookup },
793 { &vop_pathconf_desc, (vop_t *) cd9660_pathconf },
794 { &vop_read_desc, (vop_t *) cd9660_read },
795 { &vop_readdir_desc, (vop_t *) cd9660_readdir },
796 { &vop_readlink_desc, (vop_t *) cd9660_readlink },
797 { &vop_reclaim_desc, (vop_t *) cd9660_reclaim },
798 { &vop_setattr_desc, (vop_t *) cd9660_setattr },
799 { &vop_strategy_desc, (vop_t *) cd9660_strategy },
802 static struct vnodeopv_desc cd9660_vnodeop_opv_desc =
803 { &cd9660_vnodeop_p, cd9660_vnodeop_entries };
804 VNODEOP_SET(cd9660_vnodeop_opv_desc);
807 * Special device vnode ops
809 vop_t **cd9660_specop_p;
810 static struct vnodeopv_entry_desc cd9660_specop_entries[] = {
811 { &vop_default_desc, (vop_t *) spec_vnoperate },
812 { &vop_access_desc, (vop_t *) cd9660_access },
813 { &vop_getattr_desc, (vop_t *) cd9660_getattr },
814 { &vop_inactive_desc, (vop_t *) cd9660_inactive },
815 { &vop_reclaim_desc, (vop_t *) cd9660_reclaim },
816 { &vop_setattr_desc, (vop_t *) cd9660_setattr },
819 static struct vnodeopv_desc cd9660_specop_opv_desc =
820 { &cd9660_specop_p, cd9660_specop_entries };
821 VNODEOP_SET(cd9660_specop_opv_desc);
823 vop_t **cd9660_fifoop_p;
824 static struct vnodeopv_entry_desc cd9660_fifoop_entries[] = {
825 { &vop_default_desc, (vop_t *) fifo_vnoperate },
826 { &vop_access_desc, (vop_t *) cd9660_access },
827 { &vop_getattr_desc, (vop_t *) cd9660_getattr },
828 { &vop_inactive_desc, (vop_t *) cd9660_inactive },
829 { &vop_reclaim_desc, (vop_t *) cd9660_reclaim },
830 { &vop_setattr_desc, (vop_t *) cd9660_setattr },
833 static struct vnodeopv_desc cd9660_fifoop_opv_desc =
834 { &cd9660_fifoop_p, cd9660_fifoop_entries };
836 VNODEOP_SET(cd9660_fifoop_opv_desc);