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. 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
35 * $FreeBSD: src/sys/isofs/cd9660/cd9660_vnops.c,v 1.62 1999/12/15 23:01:51 eivind Exp $
38 #include <sys/param.h>
39 #include <sys/systm.h>
42 #include <sys/namei.h>
43 #include <sys/kernel.h>
46 #include <sys/mount.h>
47 #include <sys/vnode.h>
48 #include <vfs/fifofs/fifo.h>
49 #include <sys/malloc.h>
50 #include <sys/dirent.h>
51 #include <sys/unistd.h>
52 #include <sys/filio.h>
53 #include <sys/lockf.h>
54 #include <sys/objcache.h>
57 #include <vm/vnode_pager.h>
62 #include "cd9660_node.h"
65 static int cd9660_access(struct vop_access_args *);
66 static int cd9660_advlock(struct vop_advlock_args *);
67 static int cd9660_getattr(struct vop_getattr_args *);
68 static int cd9660_ioctl(struct vop_ioctl_args *);
69 static int cd9660_pathconf(struct vop_pathconf_args *);
70 static int cd9660_open(struct vop_open_args *);
71 static int cd9660_read(struct vop_read_args *);
72 static int cd9660_setattr(struct vop_setattr_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 *);
79 static int cd9660_print(struct vop_print_args *);
82 * Setattr call. Only allowed for block and character special devices.
84 * cd9660_setattr(struct vnode *a_vp, struct vattr *a_vap,
85 * struct ucred *a_cred, struct proc *a_p)
88 cd9660_setattr(struct vop_setattr_args *ap)
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) {
117 * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
118 * The mode is shifted to select the owner/group/other fields. The
119 * super user is granted all permissions.
121 * cd9660_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
126 cd9660_access(struct vop_access_args *ap)
128 struct vnode *vp = ap->a_vp;
129 struct iso_node *ip = VTOI(vp);
131 KKASSERT(vp->v_mount->mnt_flag & MNT_RDONLY);
132 return (vop_helper_access(ap, ip->inode.iso_uid, ip->inode.iso_gid,
133 ip->inode.iso_mode, 0));
137 * cd9660_getattr(struct vnode *a_vp, struct vattr *a_vap)
140 cd9660_getattr(struct vop_getattr_args *ap)
142 struct vnode *vp = ap->a_vp;
143 struct vattr *vap = ap->a_vap;
144 struct iso_node *ip = VTOI(vp);
146 vap->va_fsid = devid_from_dev(ip->i_dev);
147 vap->va_fileid = ip->i_number;
149 vap->va_mode = ip->inode.iso_mode;
150 vap->va_nlink = ip->inode.iso_links;
151 vap->va_uid = ip->inode.iso_uid;
152 vap->va_gid = ip->inode.iso_gid;
153 vap->va_atime = ip->inode.iso_atime;
154 vap->va_mtime = ip->inode.iso_mtime;
155 vap->va_ctime = ip->inode.iso_ctime;
156 vap->va_rmajor = umajor(ip->inode.iso_rdev);
157 vap->va_rminor = uminor(ip->inode.iso_rdev);
159 vap->va_size = (u_quad_t)(unsigned long)ip->i_size;
160 if (ip->i_size == 0 && (vap->va_mode & S_IFMT) == S_IFLNK) {
161 struct vop_readlink_args rdlnk;
166 cp = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
168 aiov.iov_len = MAXPATHLEN;
169 auio.uio_iov = &aiov;
172 auio.uio_rw = UIO_READ;
173 auio.uio_segflg = UIO_SYSSPACE;
174 auio.uio_td = curthread;
175 auio.uio_resid = MAXPATHLEN;
177 rdlnk.a_vp = ap->a_vp;
178 rdlnk.a_cred = proc0.p_ucred; /* use root cred */
179 if (cd9660_readlink(&rdlnk) == 0)
180 vap->va_size = MAXPATHLEN - auio.uio_resid;
185 vap->va_blocksize = ip->i_mnt->logical_block_size;
186 vap->va_bytes = (u_quad_t) ip->i_size;
187 vap->va_type = vp->v_type;
193 * Vnode op for ioctl.
195 * cd9660_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data,
196 * int a_fflag, struct ucred *a_cred, struct proc *a_p)
199 cd9660_ioctl(struct vop_ioctl_args *ap)
201 struct vnode *vp = ap->a_vp;
202 struct iso_node *ip = VTOI(vp);
204 switch (ap->a_command) {
207 *(int *)(ap->a_data) = ip->iso_start;
215 * open is called when the kernel intends to read or memory map a vnode.
218 cd9660_open(struct vop_open_args *ap)
220 return(vop_stdopen(ap));
224 * Vnode op for reading.
226 * cd9660_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
227 * struct ucred *a_cred)
230 cd9660_read(struct vop_read_args *ap)
232 struct vnode *vp = ap->a_vp;
233 struct uio *uio = ap->a_uio;
234 struct iso_node *ip = VTOI(vp);
237 daddr_t lbn, rablock;
241 int rasize, error = 0;
245 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
247 if (uio->uio_resid == 0)
249 if (uio->uio_offset < 0)
251 ip->i_flag |= IN_ACCESS;
254 lbn = lblkno(imp, uio->uio_offset);
255 loffset = lblktooff(imp, lbn);
256 on = blkoff(imp, uio->uio_offset);
257 n = szmin((u_int)(imp->logical_block_size - on),
259 diff = (off_t)ip->i_size - uio->uio_offset;
264 size = blksize(imp, ip, lbn);
266 raoffset = lblktooff(imp, rablock);
267 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
268 if (raoffset < ip->i_size) {
269 error = cluster_read(vp, (off_t)ip->i_size,
272 (ap->a_ioflag >> IO_SEQSHIFT) *
276 error = bread(vp, loffset, size, &bp);
280 lblktosize(imp, rablock) < ip->i_size) {
281 rasize = blksize(imp, ip, rablock);
282 error = breadn(vp, loffset, size, &raoffset,
285 error = bread(vp, loffset, size, &bp);
287 n = imin(n, size - bp->b_resid);
293 error = uiomove(bp->b_data + on, (int)n, uio);
295 } while (error == 0 && uio->uio_resid > 0 && n != 0);
299 /* struct dirent + enough space for the maximum supported size */
302 char de_name[_DIRENT_RECLEN(NAME_MAX) - sizeof(struct dirent)];
306 * Structure for reading directories
309 struct iso_dirent saveent;
310 struct iso_dirent assocent;
311 struct iso_dirent current;
323 iso_uiodir(struct isoreaddir *idp, struct dirent *dp, off_t off)
327 dp->d_name[dp->d_namlen] = 0;
329 if (idp->uio->uio_resid < _DIRENT_DIRSIZ(dp)) {
335 if (idp->ncookies <= 0) {
340 *idp->cookies++ = off;
344 if ((error = uiomove((caddr_t) dp,_DIRENT_DIRSIZ(dp),idp->uio)) != 0)
351 iso_shipdir(struct isoreaddir *idp)
358 cl = idp->current.de.d_namlen;
359 cname = idp->current.de.d_name;
360 assoc = (cl > 1) && (*cname == ASSOCCHAR);
366 dp = &idp->saveent.de;
368 if (!(sl = dp->d_namlen)) {
369 dp = &idp->assocent.de;
370 sname = dp->d_name + 1;
371 sl = dp->d_namlen - 1;
375 || bcmp(sname,cname,sl)) {
376 if (idp->assocent.de.d_namlen) {
377 if ((error = iso_uiodir(idp,&idp->assocent.de,idp->assocoff)) != 0)
379 idp->assocent.de.d_namlen = 0;
381 if (idp->saveent.de.d_namlen) {
382 if ((error = iso_uiodir(idp,&idp->saveent.de,idp->saveoff)) != 0)
384 idp->saveent.de.d_namlen = 0;
389 idp->assocoff = idp->curroff;
390 bcopy(&idp->current,&idp->assocent,_DIRENT_DIRSIZ(&idp->current.de));
392 idp->saveoff = idp->curroff;
393 bcopy(&idp->current,&idp->saveent,_DIRENT_DIRSIZ(&idp->current.de));
399 * Vnode op for readdir
401 * cd9660_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
402 * int *a_eofflag, int *a_ncookies, off_t *a_cookies)
405 cd9660_readdir(struct vop_readdir_args *ap)
407 struct uio *uio = ap->a_uio;
408 struct isoreaddir *idp;
409 struct vnode *vdp = ap->a_vp;
412 struct buf *bp = NULL;
413 struct iso_directory_record *ep;
414 int entryoffsetinblock;
421 off_t *cookies = NULL;
425 bmask = imp->im_bmask;
427 error = vn_lock(vdp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
431 idp = kmalloc(sizeof(*idp), M_TEMP, M_WAITOK);
432 idp->saveent.de.d_namlen = idp->assocent.de.d_namlen = 0;
435 * Is it worth trying to figure out the type?
437 idp->saveent.de.d_type = DT_UNKNOWN;
438 idp->assocent.de.d_type = DT_UNKNOWN;
439 idp->current.de.d_type = DT_UNKNOWN;
441 if (ap->a_ncookies == NULL) {
445 * Guess the number of cookies needed. Guess at least
446 * 1 to avoid a degenerate case in malloc, and cap at
447 * a reasonable limit.
449 ncookies = uio->uio_resid / 16 + 1;
452 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
453 idp->cookies = cookies;
454 idp->ncookies = ncookies;
457 idp->curroff = uio->uio_offset;
459 if ((entryoffsetinblock = idp->curroff & bmask) &&
460 (error = cd9660_devblkatoff(vdp, idp->curroff, NULL, &bp))) {
464 endsearch = dp->i_size;
466 while (idp->curroff < endsearch) {
468 * If offset is on a block boundary,
469 * read the next directory block.
470 * Release previous if it exists.
472 if ((idp->curroff & bmask) == 0) {
476 cd9660_devblkatoff(vdp, idp->curroff, NULL, &bp)) != 0)
478 entryoffsetinblock = 0;
481 * Get pointer to next entry.
483 ep = (struct iso_directory_record *)
484 ((char *)bp->b_data + entryoffsetinblock);
486 reclen = isonum_711(ep->length);
488 /* skip to next block, if any */
490 (idp->curroff & ~bmask) + imp->logical_block_size;
494 if (reclen < ISO_DIRECTORY_RECORD_SIZE) {
496 /* illegal entry, stop */
500 if (entryoffsetinblock + reclen > imp->logical_block_size) {
502 /* illegal directory, so stop looking */
506 idp->current.de.d_namlen = isonum_711(ep->name_len);
508 if (reclen < ISO_DIRECTORY_RECORD_SIZE + idp->current.de.d_namlen) {
510 /* illegal entry, stop */
514 if (isonum_711(ep->flags)&2)
515 idp->current.de.d_ino = isodirino(ep, imp);
517 idp->current.de.d_ino = bp->b_bio1.bio_offset +
520 idp->curroff += reclen;
522 switch (imp->iso_ftype) {
525 ino_t cur_fileno = idp->current.de.d_ino;
526 cd9660_rrip_getname(ep,idp->current.de.d_name, &namelen,
528 idp->current.de.d_ino = cur_fileno;
529 idp->current.de.d_namlen = namelen;
530 if (idp->current.de.d_namlen)
531 error = iso_uiodir(idp,&idp->current.de,idp->curroff);
534 default: /* ISO_FTYPE_DEFAULT || ISO_FTYPE_9660 || ISO_FTYPE_HIGH_SIERRA*/
535 strcpy(idp->current.de.d_name,"..");
536 if (idp->current.de.d_namlen == 1 && ep->name[0] == 0) {
537 idp->current.de.d_namlen = 1;
538 error = iso_uiodir(idp,&idp->current.de,idp->curroff);
539 } else if (idp->current.de.d_namlen == 1 && ep->name[0] == 1) {
540 idp->current.de.d_namlen = 2;
541 error = iso_uiodir(idp,&idp->current.de,idp->curroff);
543 isofntrans(ep->name,idp->current.de.d_namlen,
544 idp->current.de.d_name, &namelen,
545 imp->iso_ftype == ISO_FTYPE_9660,
546 isonum_711(ep->flags)&4,
550 idp->current.de.d_namlen = namelen;
551 if (imp->iso_ftype == ISO_FTYPE_DEFAULT)
552 error = iso_shipdir(idp);
554 error = iso_uiodir(idp,&idp->current.de,idp->curroff);
560 entryoffsetinblock += reclen;
563 if (!error && imp->iso_ftype == ISO_FTYPE_DEFAULT) {
564 idp->current.de.d_namlen = 0;
565 error = iso_shipdir(idp);
570 if (ap->a_ncookies != NULL) {
572 kfree(cookies, M_TEMP);
575 * Work out the number of cookies actually used.
577 *ap->a_ncookies = ncookies - idp->ncookies;
578 *ap->a_cookies = cookies;
585 uio->uio_offset = idp->uio_off;
586 *ap->a_eofflag = idp->eofflag;
596 * Return target name of a symbolic link
597 * Shouldn't we get the parent vnode and read the data from there?
598 * This could eventually result in deadlocks in cd9660_lookup.
599 * But otherwise the block read here is in the block buffer two times.
601 typedef struct iso_directory_record ISODIR;
602 typedef struct iso_node ISONODE;
603 typedef struct iso_mnt ISOMNT;
605 * cd9660_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
608 cd9660_readlink(struct vop_readlink_args *ap)
623 if (imp->iso_ftype != ISO_FTYPE_RRIP)
627 * Get parents directory record block that this inode included.
629 error = bread(imp->im_devvp,
630 rounddown2((off_t)ip->i_number, 1 << imp->im_bshift),
631 imp->logical_block_size, &bp);
638 * Setup the directory pointer for this inode
640 dirp = (ISODIR *)(bp->b_data + (ip->i_number & imp->im_bmask));
643 * Just make sure, we have a right one....
644 * 1: Check not cross boundary on block
646 if ((ip->i_number & imp->im_bmask) + isonum_711(dirp->length)
647 > (unsigned)imp->logical_block_size) {
654 * Abuse a namei buffer for now.
656 if (uio->uio_segflg == UIO_SYSSPACE)
657 symname = uio->uio_iov->iov_base;
659 symname = objcache_get(namei_oc, M_WAITOK);
662 * Ok, we just gathering a symbolic name in SL record.
664 if (cd9660_rrip_getsymname(dirp, symname, &symlen, imp) == 0) {
665 if (uio->uio_segflg != UIO_SYSSPACE)
666 objcache_put(namei_oc, symname);
671 * Don't forget before you leave from home ;-)
676 * return with the symbolic name to caller's.
678 if (uio->uio_segflg != UIO_SYSSPACE) {
679 error = uiomove(symname, symlen, uio);
680 objcache_put(namei_oc, symname);
683 uio->uio_resid -= symlen;
684 uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + symlen;
685 uio->uio_iov->iov_len -= symlen;
690 * Calculate the logical to physical mapping if not done already,
691 * then call the device strategy routine.
693 * cd9660_strategy(struct buf *a_vp, struct buf *a_bio)
696 cd9660_strategy(struct vop_strategy_args *ap)
698 struct bio *bio = ap->a_bio;
700 struct buf *bp = bio->bio_buf;
701 struct vnode *vp = ap->a_vp;
706 if (vp->v_type == VBLK || vp->v_type == VCHR)
707 panic("cd9660_strategy: spec");
708 nbio = push_bio(bio);
709 if (nbio->bio_offset == NOOFFSET) {
710 error = VOP_BMAP(vp, bio->bio_offset,
711 &nbio->bio_offset, NULL, NULL, bp->b_cmd);
714 bp->b_flags |= B_ERROR;
715 /* I/O was never started on nbio, must biodone(bio) */
719 if (nbio->bio_offset == NOOFFSET)
722 if (nbio->bio_offset == NOOFFSET) {
723 /* I/O was never started on nbio, must biodone(bio) */
728 vn_strategy(vp, nbio);
733 * Print out the contents of an inode.
735 * cd9660_print(struct vnode *a_vp)
738 cd9660_print(struct vop_print_args *ap)
740 kprintf("tag VT_ISOFS, isofs vnode\n");
745 * Return POSIX pathconf information applicable to cd9660 filesystems.
747 * cd9660_pathconf(struct vnode *a_vp, int a_name, register_t *a_retval)
750 cd9660_pathconf(struct vop_pathconf_args *ap)
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 * Advisory lock support
784 cd9660_advlock(struct vop_advlock_args *ap)
786 struct iso_node *ip = VTOI(ap->a_vp);
787 return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
791 * Global vfs data structures for cd9660
793 struct vop_ops cd9660_vnode_vops = {
794 .vop_default = vop_defaultop,
795 .vop_open = cd9660_open,
796 .vop_access = cd9660_access,
797 .vop_advlock = cd9660_advlock,
798 .vop_bmap = cd9660_bmap,
799 .vop_old_lookup = cd9660_lookup,
800 .vop_getattr = cd9660_getattr,
801 .vop_inactive = cd9660_inactive,
802 .vop_ioctl = cd9660_ioctl,
803 .vop_pathconf = cd9660_pathconf,
804 .vop_print = cd9660_print,
805 .vop_read = cd9660_read,
806 .vop_readdir = cd9660_readdir,
807 .vop_readlink = cd9660_readlink,
808 .vop_reclaim = cd9660_reclaim,
809 .vop_setattr = cd9660_setattr,
810 .vop_strategy = cd9660_strategy,
811 .vop_getpages = vop_stdgetpages,
812 .vop_putpages = vop_stdputpages
816 * Special device vnode ops
818 struct vop_ops cd9660_spec_vops = {
819 .vop_default = vop_defaultop,
820 .vop_read = vop_stdnoread,
821 .vop_access = cd9660_access,
822 .vop_getattr = cd9660_getattr,
823 .vop_inactive = cd9660_inactive,
824 .vop_print = cd9660_print,
825 .vop_reclaim = cd9660_reclaim,
826 .vop_setattr = cd9660_setattr,
829 struct vop_ops cd9660_fifo_vops = {
830 .vop_default = fifo_vnoperate,
831 .vop_access = cd9660_access,
832 .vop_getattr = cd9660_getattr,
833 .vop_inactive = cd9660_inactive,
834 .vop_print = cd9660_print,
835 .vop_reclaim = cd9660_reclaim,
836 .vop_setattr = cd9660_setattr,