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.36 2007/08/13 17:31:56 dillon Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
46 #include <sys/namei.h>
47 #include <sys/kernel.h>
50 #include <sys/mount.h>
51 #include <sys/vnode.h>
52 #include <vfs/fifofs/fifo.h>
53 #include <sys/malloc.h>
54 #include <sys/dirent.h>
55 #include <sys/unistd.h>
56 #include <sys/filio.h>
57 #include <sys/lockf.h>
58 #include <sys/objcache.h>
61 #include <vm/vnode_pager.h>
64 #include "cd9660_node.h"
67 static int cd9660_access (struct vop_access_args *);
68 static int cd9660_advlock (struct vop_advlock_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_open (struct vop_open_args *);
73 static int cd9660_read (struct vop_read_args *);
74 static int cd9660_setattr (struct vop_setattr_args *);
76 static int iso_uiodir (struct isoreaddir *idp, struct dirent *dp,
78 static int iso_shipdir (struct isoreaddir *idp);
79 static int cd9660_readdir (struct vop_readdir_args *);
80 static int cd9660_readlink (struct vop_readlink_args *ap);
81 static int cd9660_strategy (struct vop_strategy_args *);
82 static int cd9660_print (struct vop_print_args *);
83 static int cd9660_getpages (struct vop_getpages_args *);
84 static int cd9660_putpages (struct vop_putpages_args *);
87 * Setattr call. Only allowed for block and character special devices.
89 * cd9660_setattr(struct vnode *a_vp, struct vattr *a_vap,
90 * struct ucred *a_cred, struct proc *a_p)
93 cd9660_setattr(struct vop_setattr_args *ap)
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.
126 * cd9660_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
131 cd9660_access(struct vop_access_args *ap)
133 struct vnode *vp = ap->a_vp;
134 struct iso_node *ip = VTOI(vp);
135 struct ucred *cred = ap->a_cred;
136 mode_t mask, mode = ap->a_mode;
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 /* User id 0 always gets access. */
158 if (cred->cr_uid == 0)
163 /* Otherwise, check the owner. */
164 if (cred->cr_uid == ip->inode.iso_uid) {
171 return ((ip->inode.iso_mode & mask) == mask ? 0 : EACCES);
174 /* Otherwise, check the groups. */
175 for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
176 if (ip->inode.iso_gid == *gp) {
183 return ((ip->inode.iso_mode & mask) == mask ?
187 /* Otherwise, check everyone else. */
194 return ((ip->inode.iso_mode & mask) == mask ? 0 : EACCES);
198 * cd9660_getattr(struct vnode *a_vp, struct vattr *a_vap)
201 cd9660_getattr(struct vop_getattr_args *ap)
203 struct vnode *vp = ap->a_vp;
204 struct vattr *vap = ap->a_vap;
205 struct iso_node *ip = VTOI(vp);
207 vap->va_fsid = dev2udev(ip->i_dev);
208 vap->va_fileid = ip->i_number;
210 vap->va_mode = ip->inode.iso_mode;
211 vap->va_nlink = ip->inode.iso_links;
212 vap->va_uid = ip->inode.iso_uid;
213 vap->va_gid = ip->inode.iso_gid;
214 vap->va_atime = ip->inode.iso_atime;
215 vap->va_mtime = ip->inode.iso_mtime;
216 vap->va_ctime = ip->inode.iso_ctime;
217 vap->va_rmajor = umajor(ip->inode.iso_rdev);
218 vap->va_rminor = uminor(ip->inode.iso_rdev);
220 vap->va_size = (u_quad_t)(unsigned long)ip->i_size;
221 if (ip->i_size == 0 && (vap->va_mode & S_IFMT) == S_IFLNK) {
222 struct vop_readlink_args rdlnk;
227 MALLOC(cp, char *, MAXPATHLEN, M_TEMP, M_WAITOK);
229 aiov.iov_len = MAXPATHLEN;
230 auio.uio_iov = &aiov;
233 auio.uio_rw = UIO_READ;
234 auio.uio_segflg = UIO_SYSSPACE;
235 auio.uio_td = curthread;
236 auio.uio_resid = MAXPATHLEN;
238 rdlnk.a_vp = ap->a_vp;
239 rdlnk.a_cred = proc0.p_ucred; /* use root cred */
240 if (cd9660_readlink(&rdlnk) == 0)
241 vap->va_size = MAXPATHLEN - auio.uio_resid;
246 vap->va_blocksize = ip->i_mnt->logical_block_size;
247 vap->va_bytes = (u_quad_t) ip->i_size;
248 vap->va_type = vp->v_type;
254 * Vnode op for ioctl.
256 * cd9660_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data,
257 * int a_fflag, struct ucred *a_cred, struct proc *a_p)
260 cd9660_ioctl(struct vop_ioctl_args *ap)
262 struct vnode *vp = ap->a_vp;
263 struct iso_node *ip = VTOI(vp);
265 switch (ap->a_command) {
268 *(int *)(ap->a_data) = ip->iso_start;
276 * open is called when the kernel intends to read or memory map a vnode.
279 cd9660_open(struct vop_open_args *ap)
281 return(vop_stdopen(ap));
285 * Vnode op for reading.
287 * cd9660_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
288 * struct ucred *a_cred)
291 cd9660_read(struct vop_read_args *ap)
293 struct vnode *vp = ap->a_vp;
294 struct uio *uio = ap->a_uio;
295 struct iso_node *ip = VTOI(vp);
298 daddr_t lbn, rablock;
302 int rasize, error = 0;
306 seqcount = ap->a_ioflag >> 16;
308 if (uio->uio_resid == 0)
310 if (uio->uio_offset < 0)
312 ip->i_flag |= IN_ACCESS;
315 lbn = lblkno(imp, uio->uio_offset);
316 loffset = lblktooff(imp, lbn);
317 on = blkoff(imp, uio->uio_offset);
318 n = min((u_int)(imp->logical_block_size - on),
320 diff = (off_t)ip->i_size - uio->uio_offset;
325 size = blksize(imp, ip, lbn);
327 raoffset = lblktooff(imp, rablock);
328 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
329 if (raoffset < ip->i_size)
330 error = cluster_read(vp, (off_t)ip->i_size,
333 (ap->a_ioflag >> 16), &bp);
335 error = bread(vp, loffset, size, &bp);
338 lblktosize(imp, rablock) < ip->i_size) {
339 rasize = blksize(imp, ip, rablock);
340 error = breadn(vp, loffset, size, &raoffset,
343 error = bread(vp, loffset, size, &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);
357 /* struct dirent + enough space for the maximum supported size */
360 char de_name[_DIRENT_RECLEN(NAME_MAX) - sizeof(struct dirent)];
364 * Structure for reading directories
367 struct iso_dirent saveent;
368 struct iso_dirent assocent;
369 struct iso_dirent current;
381 iso_uiodir(struct isoreaddir *idp, struct dirent *dp, off_t off)
385 dp->d_name[dp->d_namlen] = 0;
387 if (idp->uio->uio_resid < _DIRENT_DIRSIZ(dp)) {
393 if (idp->ncookies <= 0) {
398 *idp->cookies++ = off;
402 if ((error = uiomove((caddr_t) dp,_DIRENT_DIRSIZ(dp),idp->uio)) != 0)
409 iso_shipdir(struct isoreaddir *idp)
416 cl = idp->current.de.d_namlen;
417 cname = idp->current.de.d_name;
418 assoc = (cl > 1) && (*cname == ASSOCCHAR);
424 dp = &idp->saveent.de;
426 if (!(sl = dp->d_namlen)) {
427 dp = &idp->assocent.de;
428 sname = dp->d_name + 1;
429 sl = dp->d_namlen - 1;
433 || bcmp(sname,cname,sl)) {
434 if (idp->assocent.de.d_namlen) {
435 if ((error = iso_uiodir(idp,&idp->assocent.de,idp->assocoff)) != 0)
437 idp->assocent.de.d_namlen = 0;
439 if (idp->saveent.de.d_namlen) {
440 if ((error = iso_uiodir(idp,&idp->saveent.de,idp->saveoff)) != 0)
442 idp->saveent.de.d_namlen = 0;
447 idp->assocoff = idp->curroff;
448 bcopy(&idp->current,&idp->assocent,_DIRENT_DIRSIZ(&idp->current.de));
450 idp->saveoff = idp->curroff;
451 bcopy(&idp->current,&idp->saveent,_DIRENT_DIRSIZ(&idp->current.de));
457 * Vnode op for readdir
459 * cd9660_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
460 * int *a_eofflag, int *a_ncookies, u_long *a_cookies)
463 cd9660_readdir(struct vop_readdir_args *ap)
465 struct uio *uio = ap->a_uio;
466 struct isoreaddir *idp;
467 struct vnode *vdp = ap->a_vp;
470 struct buf *bp = NULL;
471 struct iso_directory_record *ep;
472 int entryoffsetinblock;
479 u_long *cookies = NULL;
483 bmask = imp->im_bmask;
485 if ((error = vn_lock(vdp, LK_EXCLUSIVE|LK_RETRY)) != 0)
488 MALLOC(idp, struct isoreaddir *, sizeof(*idp), M_TEMP, M_WAITOK);
489 idp->saveent.de.d_namlen = idp->assocent.de.d_namlen = 0;
492 * Is it worth trying to figure out the type?
494 idp->saveent.de.d_type = DT_UNKNOWN;
495 idp->assocent.de.d_type = DT_UNKNOWN;
496 idp->current.de.d_type = DT_UNKNOWN;
498 if (ap->a_ncookies == NULL) {
502 * Guess the number of cookies needed. Guess at least
503 * 1 to avoid a degenerate case in malloc, and cap at
504 * a reasonable limit.
506 ncookies = uio->uio_resid / 16 + 1;
509 MALLOC(cookies, u_long *, ncookies * sizeof(u_int),
511 idp->cookies = cookies;
512 idp->ncookies = ncookies;
515 idp->curroff = uio->uio_offset;
517 if ((entryoffsetinblock = idp->curroff & bmask) &&
518 (error = cd9660_devblkatoff(vdp, (off_t)idp->curroff, NULL, &bp))) {
522 endsearch = dp->i_size;
524 while (idp->curroff < endsearch) {
526 * If offset is on a block boundary,
527 * read the next directory block.
528 * Release previous if it exists.
530 if ((idp->curroff & bmask) == 0) {
534 cd9660_devblkatoff(vdp, (off_t)idp->curroff, NULL, &bp)) != 0)
536 entryoffsetinblock = 0;
539 * Get pointer to next entry.
541 ep = (struct iso_directory_record *)
542 ((char *)bp->b_data + entryoffsetinblock);
544 reclen = isonum_711(ep->length);
546 /* skip to next block, if any */
548 (idp->curroff & ~bmask) + imp->logical_block_size;
552 if (reclen < ISO_DIRECTORY_RECORD_SIZE) {
554 /* illegal entry, stop */
558 if (entryoffsetinblock + reclen > imp->logical_block_size) {
560 /* illegal directory, so stop looking */
564 idp->current.de.d_namlen = isonum_711(ep->name_len);
566 if (reclen < ISO_DIRECTORY_RECORD_SIZE + idp->current.de.d_namlen) {
568 /* illegal entry, stop */
572 if (isonum_711(ep->flags)&2)
573 idp->current.de.d_ino = isodirino(ep, imp);
575 idp->current.de.d_ino = bp->b_bio1.bio_offset +
578 idp->curroff += reclen;
580 switch (imp->iso_ftype) {
583 ino_t cur_fileno = idp->current.de.d_ino;
584 cd9660_rrip_getname(ep,idp->current.de.d_name, &namelen,
586 idp->current.de.d_ino = cur_fileno;
587 idp->current.de.d_namlen = namelen;
588 if (idp->current.de.d_namlen)
589 error = iso_uiodir(idp,&idp->current.de,idp->curroff);
592 default: /* ISO_FTYPE_DEFAULT || ISO_FTYPE_9660 || ISO_FTYPE_HIGH_SIERRA*/
593 strcpy(idp->current.de.d_name,"..");
594 if (idp->current.de.d_namlen == 1 && ep->name[0] == 0) {
595 idp->current.de.d_namlen = 1;
596 error = iso_uiodir(idp,&idp->current.de,idp->curroff);
597 } else if (idp->current.de.d_namlen == 1 && ep->name[0] == 1) {
598 idp->current.de.d_namlen = 2;
599 error = iso_uiodir(idp,&idp->current.de,idp->curroff);
601 isofntrans(ep->name,idp->current.de.d_namlen,
602 idp->current.de.d_name, &namelen,
603 imp->iso_ftype == ISO_FTYPE_9660,
604 isonum_711(ep->flags)&4,
606 idp->current.de.d_namlen = namelen;
607 if (imp->iso_ftype == ISO_FTYPE_DEFAULT)
608 error = iso_shipdir(idp);
610 error = iso_uiodir(idp,&idp->current.de,idp->curroff);
616 entryoffsetinblock += reclen;
619 if (!error && imp->iso_ftype == ISO_FTYPE_DEFAULT) {
620 idp->current.de.d_namlen = 0;
621 error = iso_shipdir(idp);
626 if (ap->a_ncookies != NULL) {
628 kfree(cookies, M_TEMP);
631 * Work out the number of cookies actually used.
633 *ap->a_ncookies = ncookies - idp->ncookies;
634 *ap->a_cookies = cookies;
641 uio->uio_offset = idp->uio_off;
642 *ap->a_eofflag = idp->eofflag;
652 * Return target name of a symbolic link
653 * Shouldn't we get the parent vnode and read the data from there?
654 * This could eventually result in deadlocks in cd9660_lookup.
655 * But otherwise the block read here is in the block buffer two times.
657 typedef struct iso_directory_record ISODIR;
658 typedef struct iso_node ISONODE;
659 typedef struct iso_mnt ISOMNT;
661 * cd9660_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
664 cd9660_readlink(struct vop_readlink_args *ap)
679 if (imp->iso_ftype != ISO_FTYPE_RRIP)
683 * Get parents directory record block that this inode included.
685 error = bread(imp->im_devvp,
686 (off_t)ip->i_number & ~((1 << imp->im_bshift) - 1),
687 imp->logical_block_size, &bp);
694 * Setup the directory pointer for this inode
696 dirp = (ISODIR *)(bp->b_data + (ip->i_number & imp->im_bmask));
699 * Just make sure, we have a right one....
700 * 1: Check not cross boundary on block
702 if ((ip->i_number & imp->im_bmask) + isonum_711(dirp->length)
703 > (unsigned)imp->logical_block_size) {
710 * Abuse a namei buffer for now.
712 if (uio->uio_segflg == UIO_SYSSPACE)
713 symname = uio->uio_iov->iov_base;
715 symname = objcache_get(namei_oc, M_WAITOK);
718 * Ok, we just gathering a symbolic name in SL record.
720 if (cd9660_rrip_getsymname(dirp, symname, &symlen, imp) == 0) {
721 if (uio->uio_segflg != UIO_SYSSPACE)
722 objcache_put(namei_oc, symname);
727 * Don't forget before you leave from home ;-)
732 * return with the symbolic name to caller's.
734 if (uio->uio_segflg != UIO_SYSSPACE) {
735 error = uiomove(symname, symlen, uio);
736 objcache_put(namei_oc, symname);
739 uio->uio_resid -= symlen;
740 uio->uio_iov->iov_base += symlen;
741 uio->uio_iov->iov_len -= symlen;
746 * Calculate the logical to physical mapping if not done already,
747 * then call the device strategy routine.
749 * cd9660_strategy(struct buf *a_vp, struct buf *a_bio)
752 cd9660_strategy(struct vop_strategy_args *ap)
754 struct bio *bio = ap->a_bio;
756 struct buf *bp = bio->bio_buf;
757 struct vnode *vp = ap->a_vp;
762 if (vp->v_type == VBLK || vp->v_type == VCHR)
763 panic("cd9660_strategy: spec");
764 nbio = push_bio(bio);
765 if (nbio->bio_offset == NOOFFSET) {
766 error = VOP_BMAP(vp, bio->bio_offset,
767 &nbio->bio_offset, NULL, NULL);
770 bp->b_flags |= B_ERROR;
771 /* I/O was never started on nbio, must biodone(bio) */
775 if (nbio->bio_offset == NOOFFSET)
778 if (nbio->bio_offset == NOOFFSET) {
779 /* I/O was never started on nbio, must biodone(bio) */
784 vn_strategy(vp, nbio);
789 * Print out the contents of an inode.
791 * cd9660_print(struct vnode *a_vp)
794 cd9660_print(struct vop_print_args *ap)
796 kprintf("tag VT_ISOFS, isofs vnode\n");
801 * Return POSIX pathconf information applicable to cd9660 filesystems.
803 * cd9660_pathconf(struct vnode *a_vp, int a_name, register_t *a_retval)
806 cd9660_pathconf(struct vop_pathconf_args *ap)
808 switch (ap->a_name) {
813 if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP)
814 *ap->a_retval = NAME_MAX;
819 *ap->a_retval = PATH_MAX;
822 *ap->a_retval = PIPE_BUF;
824 case _PC_CHOWN_RESTRICTED:
839 * XXX By default, wimp out... note that a_offset is ignored (and always
843 cd9660_getpages(struct vop_getpages_args *ap)
845 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
852 * XXX By default, wimp out... note that a_offset is ignored (and always
856 cd9660_putpages(struct vop_putpages_args *ap)
858 return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
859 ap->a_sync, ap->a_rtvals);
863 * Advisory lock support
866 cd9660_advlock(struct vop_advlock_args *ap)
868 struct iso_node *ip = VTOI(ap->a_vp);
869 return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
874 * Global vfs data structures for cd9660
876 struct vop_ops cd9660_vnode_vops = {
877 .vop_default = vop_defaultop,
878 .vop_open = cd9660_open,
879 .vop_access = cd9660_access,
880 .vop_advlock = cd9660_advlock,
881 .vop_bmap = cd9660_bmap,
882 .vop_old_lookup = cd9660_lookup,
883 .vop_getattr = cd9660_getattr,
884 .vop_inactive = cd9660_inactive,
885 .vop_ioctl = cd9660_ioctl,
886 .vop_pathconf = cd9660_pathconf,
887 .vop_print = cd9660_print,
888 .vop_read = cd9660_read,
889 .vop_readdir = cd9660_readdir,
890 .vop_readlink = cd9660_readlink,
891 .vop_reclaim = cd9660_reclaim,
892 .vop_setattr = cd9660_setattr,
893 .vop_strategy = cd9660_strategy,
894 .vop_getpages = cd9660_getpages,
895 .vop_putpages = cd9660_putpages
899 * Special device vnode ops
901 struct vop_ops cd9660_spec_vops = {
902 .vop_default = spec_vnoperate,
903 .vop_access = cd9660_access,
904 .vop_getattr = cd9660_getattr,
905 .vop_inactive = cd9660_inactive,
906 .vop_print = cd9660_print,
907 .vop_reclaim = cd9660_reclaim,
908 .vop_setattr = cd9660_setattr,
911 struct vop_ops cd9660_fifo_vops = {
912 .vop_default = fifo_vnoperate,
913 .vop_access = cd9660_access,
914 .vop_getattr = cd9660_getattr,
915 .vop_inactive = cd9660_inactive,
916 .vop_print = cd9660_print,
917 .vop_reclaim = cd9660_reclaim,
918 .vop_setattr = cd9660_setattr,