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>
41 #include <sys/namei.h>
42 #include <sys/kernel.h>
45 #include <sys/mount.h>
46 #include <sys/vnode.h>
47 #include <vfs/fifofs/fifo.h>
48 #include <sys/malloc.h>
49 #include <sys/dirent.h>
50 #include <sys/unistd.h>
51 #include <sys/filio.h>
52 #include <sys/lockf.h>
53 #include <sys/objcache.h>
56 #include <vm/vnode_pager.h>
61 #include "cd9660_node.h"
64 static int cd9660_access(struct vop_access_args *);
65 static int cd9660_advlock(struct vop_advlock_args *);
66 static int cd9660_getattr(struct vop_getattr_args *);
67 static int cd9660_ioctl(struct vop_ioctl_args *);
68 static int cd9660_pathconf(struct vop_pathconf_args *);
69 static int cd9660_open(struct vop_open_args *);
70 static int cd9660_read(struct vop_read_args *);
71 static int cd9660_setattr(struct vop_setattr_args *);
73 static int iso_uiodir(struct isoreaddir *idp, struct dirent *dp, off_t off);
74 static int iso_shipdir(struct isoreaddir *idp);
75 static int cd9660_readdir(struct vop_readdir_args *);
76 static int cd9660_readlink(struct vop_readlink_args *ap);
77 static int cd9660_strategy(struct vop_strategy_args *);
78 static int cd9660_print(struct vop_print_args *);
81 * Setattr call. Only allowed for block and character special devices.
83 * cd9660_setattr(struct vnode *a_vp, struct vattr *a_vap,
84 * struct ucred *a_cred, struct proc *a_p)
87 cd9660_setattr(struct vop_setattr_args *ap)
89 struct vnode *vp = ap->a_vp;
90 struct vattr *vap = ap->a_vap;
92 if (vap->va_flags != (u_long)VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
93 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
94 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL)
96 if (vap->va_size != (u_quad_t)VNOVAL) {
116 * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
117 * The mode is shifted to select the owner/group/other fields. The
118 * super user is granted all permissions.
120 * cd9660_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
125 cd9660_access(struct vop_access_args *ap)
127 struct vnode *vp = ap->a_vp;
128 struct iso_node *ip = VTOI(vp);
130 KKASSERT(vp->v_mount->mnt_flag & MNT_RDONLY);
131 return (vop_helper_access(ap, ip->inode.iso_uid, ip->inode.iso_gid,
132 ip->inode.iso_mode, 0));
136 * cd9660_getattr(struct vnode *a_vp, struct vattr *a_vap)
139 cd9660_getattr(struct vop_getattr_args *ap)
141 struct vnode *vp = ap->a_vp;
142 struct vattr *vap = ap->a_vap;
143 struct iso_node *ip = VTOI(vp);
145 vap->va_fsid = dev2udev(ip->i_dev);
146 vap->va_fileid = ip->i_number;
148 vap->va_mode = ip->inode.iso_mode;
149 vap->va_nlink = ip->inode.iso_links;
150 vap->va_uid = ip->inode.iso_uid;
151 vap->va_gid = ip->inode.iso_gid;
152 vap->va_atime = ip->inode.iso_atime;
153 vap->va_mtime = ip->inode.iso_mtime;
154 vap->va_ctime = ip->inode.iso_ctime;
155 vap->va_rmajor = umajor(ip->inode.iso_rdev);
156 vap->va_rminor = uminor(ip->inode.iso_rdev);
158 vap->va_size = (u_quad_t)(unsigned long)ip->i_size;
159 if (ip->i_size == 0 && (vap->va_mode & S_IFMT) == S_IFLNK) {
160 struct vop_readlink_args rdlnk;
165 cp = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
167 aiov.iov_len = MAXPATHLEN;
168 auio.uio_iov = &aiov;
171 auio.uio_rw = UIO_READ;
172 auio.uio_segflg = UIO_SYSSPACE;
173 auio.uio_td = curthread;
174 auio.uio_resid = MAXPATHLEN;
176 rdlnk.a_vp = ap->a_vp;
177 rdlnk.a_cred = proc0.p_ucred; /* use root cred */
178 if (cd9660_readlink(&rdlnk) == 0)
179 vap->va_size = MAXPATHLEN - auio.uio_resid;
184 vap->va_blocksize = ip->i_mnt->logical_block_size;
185 vap->va_bytes = (u_quad_t) ip->i_size;
186 vap->va_type = vp->v_type;
192 * Vnode op for ioctl.
194 * cd9660_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data,
195 * int a_fflag, struct ucred *a_cred, struct proc *a_p)
198 cd9660_ioctl(struct vop_ioctl_args *ap)
200 struct vnode *vp = ap->a_vp;
201 struct iso_node *ip = VTOI(vp);
203 switch (ap->a_command) {
206 *(int *)(ap->a_data) = ip->iso_start;
214 * open is called when the kernel intends to read or memory map a vnode.
217 cd9660_open(struct vop_open_args *ap)
219 return(vop_stdopen(ap));
223 * Vnode op for reading.
225 * cd9660_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
226 * struct ucred *a_cred)
229 cd9660_read(struct vop_read_args *ap)
231 struct vnode *vp = ap->a_vp;
232 struct uio *uio = ap->a_uio;
233 struct iso_node *ip = VTOI(vp);
236 daddr_t lbn, rablock;
240 int rasize, error = 0;
244 seqcount = ap->a_ioflag >> 16;
246 if (uio->uio_resid == 0)
248 if (uio->uio_offset < 0)
250 ip->i_flag |= IN_ACCESS;
253 lbn = lblkno(imp, uio->uio_offset);
254 loffset = lblktooff(imp, lbn);
255 on = blkoff(imp, uio->uio_offset);
256 n = szmin((u_int)(imp->logical_block_size - on),
258 diff = (off_t)ip->i_size - uio->uio_offset;
263 size = blksize(imp, ip, lbn);
265 raoffset = lblktooff(imp, rablock);
266 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
267 if (raoffset < ip->i_size) {
268 error = cluster_read(vp, (off_t)ip->i_size,
271 (ap->a_ioflag >> 16) *
275 error = bread(vp, loffset, size, &bp);
279 lblktosize(imp, rablock) < ip->i_size) {
280 rasize = blksize(imp, ip, rablock);
281 error = breadn(vp, loffset, size, &raoffset,
284 error = bread(vp, loffset, size, &bp);
286 n = imin(n, size - bp->b_resid);
292 error = uiomove(bp->b_data + on, (int)n, uio);
294 } while (error == 0 && uio->uio_resid > 0 && n != 0);
298 /* struct dirent + enough space for the maximum supported size */
301 char de_name[_DIRENT_RECLEN(NAME_MAX) - sizeof(struct dirent)];
305 * Structure for reading directories
308 struct iso_dirent saveent;
309 struct iso_dirent assocent;
310 struct iso_dirent current;
322 iso_uiodir(struct isoreaddir *idp, struct dirent *dp, off_t off)
326 dp->d_name[dp->d_namlen] = 0;
328 if (idp->uio->uio_resid < _DIRENT_DIRSIZ(dp)) {
334 if (idp->ncookies <= 0) {
339 *idp->cookies++ = off;
343 if ((error = uiomove((caddr_t) dp,_DIRENT_DIRSIZ(dp),idp->uio)) != 0)
350 iso_shipdir(struct isoreaddir *idp)
357 cl = idp->current.de.d_namlen;
358 cname = idp->current.de.d_name;
359 assoc = (cl > 1) && (*cname == ASSOCCHAR);
365 dp = &idp->saveent.de;
367 if (!(sl = dp->d_namlen)) {
368 dp = &idp->assocent.de;
369 sname = dp->d_name + 1;
370 sl = dp->d_namlen - 1;
374 || bcmp(sname,cname,sl)) {
375 if (idp->assocent.de.d_namlen) {
376 if ((error = iso_uiodir(idp,&idp->assocent.de,idp->assocoff)) != 0)
378 idp->assocent.de.d_namlen = 0;
380 if (idp->saveent.de.d_namlen) {
381 if ((error = iso_uiodir(idp,&idp->saveent.de,idp->saveoff)) != 0)
383 idp->saveent.de.d_namlen = 0;
388 idp->assocoff = idp->curroff;
389 bcopy(&idp->current,&idp->assocent,_DIRENT_DIRSIZ(&idp->current.de));
391 idp->saveoff = idp->curroff;
392 bcopy(&idp->current,&idp->saveent,_DIRENT_DIRSIZ(&idp->current.de));
398 * Vnode op for readdir
400 * cd9660_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
401 * int *a_eofflag, int *a_ncookies, off_t *a_cookies)
404 cd9660_readdir(struct vop_readdir_args *ap)
406 struct uio *uio = ap->a_uio;
407 struct isoreaddir *idp;
408 struct vnode *vdp = ap->a_vp;
411 struct buf *bp = NULL;
412 struct iso_directory_record *ep;
413 int entryoffsetinblock;
420 off_t *cookies = NULL;
424 bmask = imp->im_bmask;
426 error = vn_lock(vdp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
430 idp = kmalloc(sizeof(*idp), M_TEMP, M_WAITOK);
431 idp->saveent.de.d_namlen = idp->assocent.de.d_namlen = 0;
434 * Is it worth trying to figure out the type?
436 idp->saveent.de.d_type = DT_UNKNOWN;
437 idp->assocent.de.d_type = DT_UNKNOWN;
438 idp->current.de.d_type = DT_UNKNOWN;
440 if (ap->a_ncookies == NULL) {
444 * Guess the number of cookies needed. Guess at least
445 * 1 to avoid a degenerate case in malloc, and cap at
446 * a reasonable limit.
448 ncookies = uio->uio_resid / 16 + 1;
451 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
452 idp->cookies = cookies;
453 idp->ncookies = ncookies;
456 idp->curroff = uio->uio_offset;
458 if ((entryoffsetinblock = idp->curroff & bmask) &&
459 (error = cd9660_devblkatoff(vdp, idp->curroff, NULL, &bp))) {
463 endsearch = dp->i_size;
465 while (idp->curroff < endsearch) {
467 * If offset is on a block boundary,
468 * read the next directory block.
469 * Release previous if it exists.
471 if ((idp->curroff & bmask) == 0) {
475 cd9660_devblkatoff(vdp, idp->curroff, NULL, &bp)) != 0)
477 entryoffsetinblock = 0;
480 * Get pointer to next entry.
482 ep = (struct iso_directory_record *)
483 ((char *)bp->b_data + entryoffsetinblock);
485 reclen = isonum_711(ep->length);
487 /* skip to next block, if any */
489 (idp->curroff & ~bmask) + imp->logical_block_size;
493 if (reclen < ISO_DIRECTORY_RECORD_SIZE) {
495 /* illegal entry, stop */
499 if (entryoffsetinblock + reclen > imp->logical_block_size) {
501 /* illegal directory, so stop looking */
505 idp->current.de.d_namlen = isonum_711(ep->name_len);
507 if (reclen < ISO_DIRECTORY_RECORD_SIZE + idp->current.de.d_namlen) {
509 /* illegal entry, stop */
513 if (isonum_711(ep->flags)&2)
514 idp->current.de.d_ino = isodirino(ep, imp);
516 idp->current.de.d_ino = bp->b_bio1.bio_offset +
519 idp->curroff += reclen;
521 switch (imp->iso_ftype) {
524 ino_t cur_fileno = idp->current.de.d_ino;
525 cd9660_rrip_getname(ep,idp->current.de.d_name, &namelen,
527 idp->current.de.d_ino = cur_fileno;
528 idp->current.de.d_namlen = namelen;
529 if (idp->current.de.d_namlen)
530 error = iso_uiodir(idp,&idp->current.de,idp->curroff);
533 default: /* ISO_FTYPE_DEFAULT || ISO_FTYPE_9660 || ISO_FTYPE_HIGH_SIERRA*/
534 strcpy(idp->current.de.d_name,"..");
535 if (idp->current.de.d_namlen == 1 && ep->name[0] == 0) {
536 idp->current.de.d_namlen = 1;
537 error = iso_uiodir(idp,&idp->current.de,idp->curroff);
538 } else if (idp->current.de.d_namlen == 1 && ep->name[0] == 1) {
539 idp->current.de.d_namlen = 2;
540 error = iso_uiodir(idp,&idp->current.de,idp->curroff);
542 isofntrans(ep->name,idp->current.de.d_namlen,
543 idp->current.de.d_name, &namelen,
544 imp->iso_ftype == ISO_FTYPE_9660,
545 isonum_711(ep->flags)&4,
549 idp->current.de.d_namlen = namelen;
550 if (imp->iso_ftype == ISO_FTYPE_DEFAULT)
551 error = iso_shipdir(idp);
553 error = iso_uiodir(idp,&idp->current.de,idp->curroff);
559 entryoffsetinblock += reclen;
562 if (!error && imp->iso_ftype == ISO_FTYPE_DEFAULT) {
563 idp->current.de.d_namlen = 0;
564 error = iso_shipdir(idp);
569 if (ap->a_ncookies != NULL) {
571 kfree(cookies, M_TEMP);
574 * Work out the number of cookies actually used.
576 *ap->a_ncookies = ncookies - idp->ncookies;
577 *ap->a_cookies = cookies;
584 uio->uio_offset = idp->uio_off;
585 *ap->a_eofflag = idp->eofflag;
595 * Return target name of a symbolic link
596 * Shouldn't we get the parent vnode and read the data from there?
597 * This could eventually result in deadlocks in cd9660_lookup.
598 * But otherwise the block read here is in the block buffer two times.
600 typedef struct iso_directory_record ISODIR;
601 typedef struct iso_node ISONODE;
602 typedef struct iso_mnt ISOMNT;
604 * cd9660_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
607 cd9660_readlink(struct vop_readlink_args *ap)
622 if (imp->iso_ftype != ISO_FTYPE_RRIP)
626 * Get parents directory record block that this inode included.
628 error = bread(imp->im_devvp,
629 (off_t)ip->i_number & ~((1 << imp->im_bshift) - 1),
630 imp->logical_block_size, &bp);
637 * Setup the directory pointer for this inode
639 dirp = (ISODIR *)(bp->b_data + (ip->i_number & imp->im_bmask));
642 * Just make sure, we have a right one....
643 * 1: Check not cross boundary on block
645 if ((ip->i_number & imp->im_bmask) + isonum_711(dirp->length)
646 > (unsigned)imp->logical_block_size) {
653 * Abuse a namei buffer for now.
655 if (uio->uio_segflg == UIO_SYSSPACE)
656 symname = uio->uio_iov->iov_base;
658 symname = objcache_get(namei_oc, M_WAITOK);
661 * Ok, we just gathering a symbolic name in SL record.
663 if (cd9660_rrip_getsymname(dirp, symname, &symlen, imp) == 0) {
664 if (uio->uio_segflg != UIO_SYSSPACE)
665 objcache_put(namei_oc, symname);
670 * Don't forget before you leave from home ;-)
675 * return with the symbolic name to caller's.
677 if (uio->uio_segflg != UIO_SYSSPACE) {
678 error = uiomove(symname, symlen, uio);
679 objcache_put(namei_oc, symname);
682 uio->uio_resid -= symlen;
683 uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + symlen;
684 uio->uio_iov->iov_len -= symlen;
689 * Calculate the logical to physical mapping if not done already,
690 * then call the device strategy routine.
692 * cd9660_strategy(struct buf *a_vp, struct buf *a_bio)
695 cd9660_strategy(struct vop_strategy_args *ap)
697 struct bio *bio = ap->a_bio;
699 struct buf *bp = bio->bio_buf;
700 struct vnode *vp = ap->a_vp;
705 if (vp->v_type == VBLK || vp->v_type == VCHR)
706 panic("cd9660_strategy: spec");
707 nbio = push_bio(bio);
708 if (nbio->bio_offset == NOOFFSET) {
709 error = VOP_BMAP(vp, bio->bio_offset,
710 &nbio->bio_offset, NULL, NULL, bp->b_cmd);
713 bp->b_flags |= B_ERROR;
714 /* I/O was never started on nbio, must biodone(bio) */
718 if (nbio->bio_offset == NOOFFSET)
721 if (nbio->bio_offset == NOOFFSET) {
722 /* I/O was never started on nbio, must biodone(bio) */
727 vn_strategy(vp, nbio);
732 * Print out the contents of an inode.
734 * cd9660_print(struct vnode *a_vp)
737 cd9660_print(struct vop_print_args *ap)
739 kprintf("tag VT_ISOFS, isofs vnode\n");
744 * Return POSIX pathconf information applicable to cd9660 filesystems.
746 * cd9660_pathconf(struct vnode *a_vp, int a_name, register_t *a_retval)
749 cd9660_pathconf(struct vop_pathconf_args *ap)
751 switch (ap->a_name) {
756 if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP)
757 *ap->a_retval = NAME_MAX;
762 *ap->a_retval = PATH_MAX;
765 *ap->a_retval = PIPE_BUF;
767 case _PC_CHOWN_RESTRICTED:
780 * Advisory lock support
783 cd9660_advlock(struct vop_advlock_args *ap)
785 struct iso_node *ip = VTOI(ap->a_vp);
786 return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
790 * Global vfs data structures for cd9660
792 struct vop_ops cd9660_vnode_vops = {
793 .vop_default = vop_defaultop,
794 .vop_open = cd9660_open,
795 .vop_access = cd9660_access,
796 .vop_advlock = cd9660_advlock,
797 .vop_bmap = cd9660_bmap,
798 .vop_old_lookup = cd9660_lookup,
799 .vop_getattr = cd9660_getattr,
800 .vop_inactive = cd9660_inactive,
801 .vop_ioctl = cd9660_ioctl,
802 .vop_pathconf = cd9660_pathconf,
803 .vop_print = cd9660_print,
804 .vop_read = cd9660_read,
805 .vop_readdir = cd9660_readdir,
806 .vop_readlink = cd9660_readlink,
807 .vop_reclaim = cd9660_reclaim,
808 .vop_setattr = cd9660_setattr,
809 .vop_strategy = cd9660_strategy,
810 .vop_getpages = vop_stdgetpages,
811 .vop_putpages = vop_stdputpages
815 * Special device vnode ops
817 struct vop_ops cd9660_spec_vops = {
818 .vop_default = vop_defaultop,
819 .vop_read = vop_stdnoread,
820 .vop_access = cd9660_access,
821 .vop_getattr = cd9660_getattr,
822 .vop_inactive = cd9660_inactive,
823 .vop_print = cd9660_print,
824 .vop_reclaim = cd9660_reclaim,
825 .vop_setattr = cd9660_setattr,
828 struct vop_ops cd9660_fifo_vops = {
829 .vop_default = fifo_vnoperate,
830 .vop_access = cd9660_access,
831 .vop_getattr = cd9660_getattr,
832 .vop_inactive = cd9660_inactive,
833 .vop_print = cd9660_print,
834 .vop_reclaim = cd9660_reclaim,
835 .vop_setattr = cd9660_setattr,