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
39 * $FreeBSD: src/sys/isofs/cd9660/cd9660_vnops.c,v 1.62 1999/12/15 23:01:51 eivind Exp $
40 * $DragonFly: src/sys/vfs/isofs/cd9660/cd9660_vnops.c,v 1.2 2003/06/17 04:28:41 dillon Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/namei.h>
46 #include <sys/kernel.h>
49 #include <sys/mount.h>
50 #include <sys/vnode.h>
51 #include <miscfs/fifofs/fifo.h>
52 #include <sys/malloc.h>
53 #include <sys/dirent.h>
54 #include <sys/unistd.h>
55 #include <sys/filio.h>
58 #include <vm/vm_zone.h>
59 #include <vm/vnode_pager.h>
61 #include <isofs/cd9660/iso.h>
62 #include <isofs/cd9660/cd9660_node.h>
63 #include <isofs/cd9660/iso_rrip.h>
65 static int cd9660_setattr __P((struct vop_setattr_args *));
66 static int cd9660_access __P((struct vop_access_args *));
67 static int cd9660_getattr __P((struct vop_getattr_args *));
68 static int cd9660_ioctl __P((struct vop_ioctl_args *));
69 static int cd9660_pathconf __P((struct vop_pathconf_args *));
70 static int cd9660_read __P((struct vop_read_args *));
72 static int iso_uiodir __P((struct isoreaddir *idp, struct dirent *dp,
74 static int iso_shipdir __P((struct isoreaddir *idp));
75 static int cd9660_readdir __P((struct vop_readdir_args *));
76 static int cd9660_readlink __P((struct vop_readlink_args *ap));
77 static int cd9660_strategy __P((struct vop_strategy_args *));
78 static int cd9660_print __P((struct vop_print_args *));
79 static int cd9660_getpages __P((struct vop_getpages_args *));
80 static int cd9660_putpages __P((struct vop_putpages_args *));
83 * Setattr call. Only allowed for block and character special devices.
87 struct vop_setattr_args /* {
88 struct vnodeop_desc *a_desc;
95 struct vnode *vp = ap->a_vp;
96 struct vattr *vap = ap->a_vap;
98 if (vap->va_flags != (u_long)VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
99 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
100 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL)
102 if (vap->va_size != (u_quad_t)VNOVAL) {
103 switch (vp->v_type) {
122 * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
123 * The mode is shifted to select the owner/group/other fields. The
124 * super user is granted all permissions.
129 struct vop_access_args /* {
132 struct ucred *a_cred;
136 struct vnode *vp = ap->a_vp;
137 struct iso_node *ip = VTOI(vp);
138 struct ucred *cred = ap->a_cred;
139 mode_t mask, mode = ap->a_mode;
144 * Disallow write attempts unless the file is a socket,
145 * fifo, or a block or character device resident on the
149 switch (vp->v_type) {
160 /* User id 0 always gets access. */
161 if (cred->cr_uid == 0)
166 /* Otherwise, check the owner. */
167 if (cred->cr_uid == ip->inode.iso_uid) {
174 return ((ip->inode.iso_mode & mask) == mask ? 0 : EACCES);
177 /* Otherwise, check the groups. */
178 for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
179 if (ip->inode.iso_gid == *gp) {
186 return ((ip->inode.iso_mode & mask) == mask ?
190 /* Otherwise, check everyone else. */
197 return ((ip->inode.iso_mode & mask) == mask ? 0 : EACCES);
202 struct vop_getattr_args /* {
205 struct ucred *a_cred;
210 struct vnode *vp = ap->a_vp;
211 register struct vattr *vap = ap->a_vap;
212 register struct iso_node *ip = VTOI(vp);
214 vap->va_fsid = dev2udev(ip->i_dev);
215 vap->va_fileid = ip->i_number;
217 vap->va_mode = ip->inode.iso_mode;
218 vap->va_nlink = ip->inode.iso_links;
219 vap->va_uid = ip->inode.iso_uid;
220 vap->va_gid = ip->inode.iso_gid;
221 vap->va_atime = ip->inode.iso_atime;
222 vap->va_mtime = ip->inode.iso_mtime;
223 vap->va_ctime = ip->inode.iso_ctime;
224 vap->va_rdev = ip->inode.iso_rdev;
226 vap->va_size = (u_quad_t) ip->i_size;
227 if (ip->i_size == 0 && (vap->va_mode & S_IFMT) == S_IFLNK) {
228 struct vop_readlink_args rdlnk;
233 MALLOC(cp, char *, MAXPATHLEN, M_TEMP, M_WAITOK);
235 aiov.iov_len = MAXPATHLEN;
236 auio.uio_iov = &aiov;
239 auio.uio_rw = UIO_READ;
240 auio.uio_segflg = UIO_SYSSPACE;
241 auio.uio_procp = ap->a_p;
242 auio.uio_resid = MAXPATHLEN;
244 rdlnk.a_vp = ap->a_vp;
245 rdlnk.a_cred = ap->a_cred;
246 if (cd9660_readlink(&rdlnk) == 0)
247 vap->va_size = MAXPATHLEN - auio.uio_resid;
252 vap->va_blocksize = ip->i_mnt->logical_block_size;
253 vap->va_bytes = (u_quad_t) ip->i_size;
254 vap->va_type = vp->v_type;
260 * Vnode op for ioctl.
264 struct vop_ioctl_args /* {
269 struct ucred *a_cred;
273 struct vnode *vp = ap->a_vp;
274 struct iso_node *ip = VTOI(vp);
276 switch (ap->a_command) {
279 *(int *)(ap->a_data) = ip->iso_start;
287 * Vnode op for reading.
291 struct vop_read_args /* {
295 struct ucred *a_cred;
298 struct vnode *vp = ap->a_vp;
299 register struct uio *uio = ap->a_uio;
300 register struct iso_node *ip = VTOI(vp);
301 register struct iso_mnt *imp;
303 daddr_t lbn, rablock;
305 int rasize, error = 0;
309 seqcount = ap->a_ioflag >> 16;
311 if (uio->uio_resid == 0)
313 if (uio->uio_offset < 0)
315 ip->i_flag |= IN_ACCESS;
318 lbn = lblkno(imp, uio->uio_offset);
319 on = blkoff(imp, uio->uio_offset);
320 n = min((u_int)(imp->logical_block_size - on),
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((caddr_t) 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 register 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 MALLOC(idp, struct isoreaddir *, 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 MALLOC(cookies, u_long *, ncookies * sizeof(u_int), M_TEMP,
508 idp->cookies = cookies;
509 idp->ncookies = ncookies;
512 idp->curroff = 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,
599 idp->current.d_namlen = (u_char)namelen;
600 if (imp->iso_ftype == ISO_FTYPE_DEFAULT)
601 error = iso_shipdir(idp);
603 error = iso_uiodir(idp,&idp->current,idp->curroff);
609 entryoffsetinblock += reclen;
612 if (!error && imp->iso_ftype == ISO_FTYPE_DEFAULT) {
613 idp->current.d_namlen = 0;
614 error = iso_shipdir(idp);
619 if (ap->a_ncookies != NULL) {
621 free(cookies, M_TEMP);
624 * Work out the number of cookies actually used.
626 *ap->a_ncookies = ncookies - idp->ncookies;
627 *ap->a_cookies = cookies;
634 uio->uio_offset = idp->uio_off;
635 *ap->a_eofflag = idp->eofflag;
643 * Return target name of a symbolic link
644 * Shouldn't we get the parent vnode and read the data from there?
645 * This could eventually result in deadlocks in cd9660_lookup.
646 * But otherwise the block read here is in the block buffer two times.
648 typedef struct iso_directory_record ISODIR;
649 typedef struct iso_node ISONODE;
650 typedef struct iso_mnt ISOMNT;
653 struct vop_readlink_args /* {
656 struct ucred *a_cred;
672 if (imp->iso_ftype != ISO_FTYPE_RRIP)
676 * Get parents directory record block that this inode included.
678 error = bread(imp->im_devvp,
679 (ip->i_number >> imp->im_bshift) <<
680 (imp->im_bshift - DEV_BSHIFT),
681 imp->logical_block_size, NOCRED, &bp);
688 * Setup the directory pointer for this inode
690 dirp = (ISODIR *)(bp->b_data + (ip->i_number & imp->im_bmask));
693 * Just make sure, we have a right one....
694 * 1: Check not cross boundary on block
696 if ((ip->i_number & imp->im_bmask) + isonum_711(dirp->length)
697 > (unsigned)imp->logical_block_size) {
704 * Abuse a namei buffer for now.
706 if (uio->uio_segflg == UIO_SYSSPACE)
707 symname = uio->uio_iov->iov_base;
709 symname = zalloc(namei_zone);
712 * Ok, we just gathering a symbolic name in SL record.
714 if (cd9660_rrip_getsymname(dirp, symname, &symlen, imp) == 0) {
715 if (uio->uio_segflg != UIO_SYSSPACE)
716 zfree(namei_zone, symname);
721 * Don't forget before you leave from home ;-)
726 * return with the symbolic name to caller's.
728 if (uio->uio_segflg != UIO_SYSSPACE) {
729 error = uiomove(symname, symlen, uio);
730 zfree(namei_zone, symname);
733 uio->uio_resid -= symlen;
734 uio->uio_iov->iov_base += symlen;
735 uio->uio_iov->iov_len -= symlen;
740 * Calculate the logical to physical mapping if not done already,
741 * then call the device strategy routine.
745 struct vop_strategy_args /* {
750 register struct buf *bp = ap->a_bp;
751 register struct vnode *vp = bp->b_vp;
752 register struct iso_node *ip;
756 if (vp->v_type == VBLK || vp->v_type == VCHR)
757 panic("cd9660_strategy: spec");
758 if (bp->b_blkno == bp->b_lblkno) {
760 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL))) {
762 bp->b_flags |= B_ERROR;
766 if ((long)bp->b_blkno == -1)
769 if ((long)bp->b_blkno == -1) {
774 bp->b_dev = vp->v_rdev;
775 VOP_STRATEGY(vp, bp);
780 * Print out the contents of an inode.
784 struct vop_print_args /* {
789 printf("tag VT_ISOFS, isofs vnode\n");
794 * Return POSIX pathconf information applicable to cd9660 filesystems.
798 struct vop_pathconf_args /* {
801 register_t *a_retval;
805 switch (ap->a_name) {
810 if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP)
811 *ap->a_retval = NAME_MAX;
816 *ap->a_retval = PATH_MAX;
819 *ap->a_retval = PIPE_BUF;
821 case _PC_CHOWN_RESTRICTED:
836 * XXX By default, wimp out... note that a_offset is ignored (and always
841 struct vop_getpages_args *ap;
843 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
850 * XXX By default, wimp out... note that a_offset is ignored (and always
855 struct vop_putpages_args *ap;
857 return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
858 ap->a_sync, ap->a_rtvals);
862 * Global vfs data structures for cd9660
864 vop_t **cd9660_vnodeop_p;
865 static struct vnodeopv_entry_desc cd9660_vnodeop_entries[] = {
866 { &vop_default_desc, (vop_t *) vop_defaultop },
867 { &vop_access_desc, (vop_t *) cd9660_access },
868 { &vop_bmap_desc, (vop_t *) cd9660_bmap },
869 { &vop_cachedlookup_desc, (vop_t *) cd9660_lookup },
870 { &vop_getattr_desc, (vop_t *) cd9660_getattr },
871 { &vop_inactive_desc, (vop_t *) cd9660_inactive },
872 { &vop_ioctl_desc, (vop_t *) cd9660_ioctl },
873 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
874 { &vop_lock_desc, (vop_t *) vop_stdlock },
875 { &vop_lookup_desc, (vop_t *) vfs_cache_lookup },
876 { &vop_pathconf_desc, (vop_t *) cd9660_pathconf },
877 { &vop_print_desc, (vop_t *) cd9660_print },
878 { &vop_read_desc, (vop_t *) cd9660_read },
879 { &vop_readdir_desc, (vop_t *) cd9660_readdir },
880 { &vop_readlink_desc, (vop_t *) cd9660_readlink },
881 { &vop_reclaim_desc, (vop_t *) cd9660_reclaim },
882 { &vop_setattr_desc, (vop_t *) cd9660_setattr },
883 { &vop_strategy_desc, (vop_t *) cd9660_strategy },
884 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
885 { &vop_getpages_desc, (vop_t *) cd9660_getpages },
886 { &vop_putpages_desc, (vop_t *) cd9660_putpages },
889 static struct vnodeopv_desc cd9660_vnodeop_opv_desc =
890 { &cd9660_vnodeop_p, cd9660_vnodeop_entries };
891 VNODEOP_SET(cd9660_vnodeop_opv_desc);
894 * Special device vnode ops
896 vop_t **cd9660_specop_p;
897 static struct vnodeopv_entry_desc cd9660_specop_entries[] = {
898 { &vop_default_desc, (vop_t *) spec_vnoperate },
899 { &vop_access_desc, (vop_t *) cd9660_access },
900 { &vop_getattr_desc, (vop_t *) cd9660_getattr },
901 { &vop_inactive_desc, (vop_t *) cd9660_inactive },
902 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
903 { &vop_lock_desc, (vop_t *) vop_stdlock },
904 { &vop_print_desc, (vop_t *) cd9660_print },
905 { &vop_reclaim_desc, (vop_t *) cd9660_reclaim },
906 { &vop_setattr_desc, (vop_t *) cd9660_setattr },
907 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
910 static struct vnodeopv_desc cd9660_specop_opv_desc =
911 { &cd9660_specop_p, cd9660_specop_entries };
912 VNODEOP_SET(cd9660_specop_opv_desc);
914 vop_t **cd9660_fifoop_p;
915 static struct vnodeopv_entry_desc cd9660_fifoop_entries[] = {
916 { &vop_default_desc, (vop_t *) fifo_vnoperate },
917 { &vop_access_desc, (vop_t *) cd9660_access },
918 { &vop_getattr_desc, (vop_t *) cd9660_getattr },
919 { &vop_inactive_desc, (vop_t *) cd9660_inactive },
920 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
921 { &vop_lock_desc, (vop_t *) vop_stdlock },
922 { &vop_print_desc, (vop_t *) cd9660_print },
923 { &vop_reclaim_desc, (vop_t *) cd9660_reclaim },
924 { &vop_setattr_desc, (vop_t *) cd9660_setattr },
925 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
928 static struct vnodeopv_desc cd9660_fifoop_opv_desc =
929 { &cd9660_fifoop_p, cd9660_fifoop_entries };
931 VNODEOP_SET(cd9660_fifoop_opv_desc);