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.40 2008/06/19 23:27:39 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 *);
85 * Setattr call. Only allowed for block and character special devices.
87 * cd9660_setattr(struct vnode *a_vp, struct vattr *a_vap,
88 * struct ucred *a_cred, struct proc *a_p)
91 cd9660_setattr(struct vop_setattr_args *ap)
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.
124 * cd9660_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
129 cd9660_access(struct vop_access_args *ap)
131 struct vnode *vp = ap->a_vp;
132 struct iso_node *ip = VTOI(vp);
134 KKASSERT(vp->v_mount->mnt_flag & MNT_RDONLY);
135 return (vop_helper_access(ap, ip->inode.iso_uid, ip->inode.iso_gid,
136 ip->inode.iso_mode, 0));
140 * cd9660_getattr(struct vnode *a_vp, struct vattr *a_vap)
143 cd9660_getattr(struct vop_getattr_args *ap)
145 struct vnode *vp = ap->a_vp;
146 struct vattr *vap = ap->a_vap;
147 struct iso_node *ip = VTOI(vp);
149 vap->va_fsid = dev2udev(ip->i_dev);
150 vap->va_fileid = ip->i_number;
152 vap->va_mode = ip->inode.iso_mode;
153 vap->va_nlink = ip->inode.iso_links;
154 vap->va_uid = ip->inode.iso_uid;
155 vap->va_gid = ip->inode.iso_gid;
156 vap->va_atime = ip->inode.iso_atime;
157 vap->va_mtime = ip->inode.iso_mtime;
158 vap->va_ctime = ip->inode.iso_ctime;
159 vap->va_rmajor = umajor(ip->inode.iso_rdev);
160 vap->va_rminor = uminor(ip->inode.iso_rdev);
162 vap->va_size = (u_quad_t)(unsigned long)ip->i_size;
163 if (ip->i_size == 0 && (vap->va_mode & S_IFMT) == S_IFLNK) {
164 struct vop_readlink_args rdlnk;
169 MALLOC(cp, char *, MAXPATHLEN, M_TEMP, M_WAITOK);
171 aiov.iov_len = MAXPATHLEN;
172 auio.uio_iov = &aiov;
175 auio.uio_rw = UIO_READ;
176 auio.uio_segflg = UIO_SYSSPACE;
177 auio.uio_td = curthread;
178 auio.uio_resid = MAXPATHLEN;
180 rdlnk.a_vp = ap->a_vp;
181 rdlnk.a_cred = proc0.p_ucred; /* use root cred */
182 if (cd9660_readlink(&rdlnk) == 0)
183 vap->va_size = MAXPATHLEN - auio.uio_resid;
188 vap->va_blocksize = ip->i_mnt->logical_block_size;
189 vap->va_bytes = (u_quad_t) ip->i_size;
190 vap->va_type = vp->v_type;
196 * Vnode op for ioctl.
198 * cd9660_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data,
199 * int a_fflag, struct ucred *a_cred, struct proc *a_p)
202 cd9660_ioctl(struct vop_ioctl_args *ap)
204 struct vnode *vp = ap->a_vp;
205 struct iso_node *ip = VTOI(vp);
207 switch (ap->a_command) {
210 *(int *)(ap->a_data) = ip->iso_start;
218 * open is called when the kernel intends to read or memory map a vnode.
221 cd9660_open(struct vop_open_args *ap)
223 return(vop_stdopen(ap));
227 * Vnode op for reading.
229 * cd9660_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
230 * struct ucred *a_cred)
233 cd9660_read(struct vop_read_args *ap)
235 struct vnode *vp = ap->a_vp;
236 struct uio *uio = ap->a_uio;
237 struct iso_node *ip = VTOI(vp);
240 daddr_t lbn, rablock;
244 int rasize, error = 0;
248 seqcount = ap->a_ioflag >> 16;
250 if (uio->uio_resid == 0)
252 if (uio->uio_offset < 0)
254 ip->i_flag |= IN_ACCESS;
257 lbn = lblkno(imp, uio->uio_offset);
258 loffset = lblktooff(imp, lbn);
259 on = blkoff(imp, uio->uio_offset);
260 n = szmin((u_int)(imp->logical_block_size - on),
262 diff = (off_t)ip->i_size - uio->uio_offset;
267 size = blksize(imp, ip, lbn);
269 raoffset = lblktooff(imp, rablock);
270 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
271 if (raoffset < ip->i_size) {
272 error = cluster_read(vp, (off_t)ip->i_size,
275 (ap->a_ioflag >> 16),
278 error = bread(vp, loffset, size, &bp);
282 lblktosize(imp, rablock) < ip->i_size) {
283 rasize = blksize(imp, ip, rablock);
284 error = breadn(vp, loffset, size, &raoffset,
287 error = bread(vp, loffset, size, &bp);
289 n = imin(n, size - bp->b_resid);
295 error = uiomove(bp->b_data + on, (int)n, uio);
297 } while (error == 0 && uio->uio_resid > 0 && n != 0);
301 /* struct dirent + enough space for the maximum supported size */
304 char de_name[_DIRENT_RECLEN(NAME_MAX) - sizeof(struct dirent)];
308 * Structure for reading directories
311 struct iso_dirent saveent;
312 struct iso_dirent assocent;
313 struct iso_dirent current;
325 iso_uiodir(struct isoreaddir *idp, struct dirent *dp, off_t off)
329 dp->d_name[dp->d_namlen] = 0;
331 if (idp->uio->uio_resid < _DIRENT_DIRSIZ(dp)) {
337 if (idp->ncookies <= 0) {
342 *idp->cookies++ = off;
346 if ((error = uiomove((caddr_t) dp,_DIRENT_DIRSIZ(dp),idp->uio)) != 0)
353 iso_shipdir(struct isoreaddir *idp)
360 cl = idp->current.de.d_namlen;
361 cname = idp->current.de.d_name;
362 assoc = (cl > 1) && (*cname == ASSOCCHAR);
368 dp = &idp->saveent.de;
370 if (!(sl = dp->d_namlen)) {
371 dp = &idp->assocent.de;
372 sname = dp->d_name + 1;
373 sl = dp->d_namlen - 1;
377 || bcmp(sname,cname,sl)) {
378 if (idp->assocent.de.d_namlen) {
379 if ((error = iso_uiodir(idp,&idp->assocent.de,idp->assocoff)) != 0)
381 idp->assocent.de.d_namlen = 0;
383 if (idp->saveent.de.d_namlen) {
384 if ((error = iso_uiodir(idp,&idp->saveent.de,idp->saveoff)) != 0)
386 idp->saveent.de.d_namlen = 0;
391 idp->assocoff = idp->curroff;
392 bcopy(&idp->current,&idp->assocent,_DIRENT_DIRSIZ(&idp->current.de));
394 idp->saveoff = idp->curroff;
395 bcopy(&idp->current,&idp->saveent,_DIRENT_DIRSIZ(&idp->current.de));
401 * Vnode op for readdir
403 * cd9660_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
404 * int *a_eofflag, int *a_ncookies, off_t *a_cookies)
407 cd9660_readdir(struct vop_readdir_args *ap)
409 struct uio *uio = ap->a_uio;
410 struct isoreaddir *idp;
411 struct vnode *vdp = ap->a_vp;
414 struct buf *bp = NULL;
415 struct iso_directory_record *ep;
416 int entryoffsetinblock;
423 off_t *cookies = NULL;
427 bmask = imp->im_bmask;
429 if ((error = vn_lock(vdp, LK_EXCLUSIVE|LK_RETRY)) != 0)
432 MALLOC(idp, struct isoreaddir *, sizeof(*idp), M_TEMP, M_WAITOK);
433 idp->saveent.de.d_namlen = idp->assocent.de.d_namlen = 0;
436 * Is it worth trying to figure out the type?
438 idp->saveent.de.d_type = DT_UNKNOWN;
439 idp->assocent.de.d_type = DT_UNKNOWN;
440 idp->current.de.d_type = DT_UNKNOWN;
442 if (ap->a_ncookies == NULL) {
446 * Guess the number of cookies needed. Guess at least
447 * 1 to avoid a degenerate case in malloc, and cap at
448 * a reasonable limit.
450 ncookies = uio->uio_resid / 16 + 1;
453 MALLOC(cookies, off_t *, ncookies * sizeof(off_t),
455 idp->cookies = cookies;
456 idp->ncookies = ncookies;
459 idp->curroff = uio->uio_offset;
461 if ((entryoffsetinblock = idp->curroff & bmask) &&
462 (error = cd9660_devblkatoff(vdp, (off_t)idp->curroff, NULL, &bp))) {
466 endsearch = dp->i_size;
468 while (idp->curroff < endsearch) {
470 * If offset is on a block boundary,
471 * read the next directory block.
472 * Release previous if it exists.
474 if ((idp->curroff & bmask) == 0) {
478 cd9660_devblkatoff(vdp, (off_t)idp->curroff, NULL, &bp)) != 0)
480 entryoffsetinblock = 0;
483 * Get pointer to next entry.
485 ep = (struct iso_directory_record *)
486 ((char *)bp->b_data + entryoffsetinblock);
488 reclen = isonum_711(ep->length);
490 /* skip to next block, if any */
492 (idp->curroff & ~bmask) + imp->logical_block_size;
496 if (reclen < ISO_DIRECTORY_RECORD_SIZE) {
498 /* illegal entry, stop */
502 if (entryoffsetinblock + reclen > imp->logical_block_size) {
504 /* illegal directory, so stop looking */
508 idp->current.de.d_namlen = isonum_711(ep->name_len);
510 if (reclen < ISO_DIRECTORY_RECORD_SIZE + idp->current.de.d_namlen) {
512 /* illegal entry, stop */
516 if (isonum_711(ep->flags)&2)
517 idp->current.de.d_ino = isodirino(ep, imp);
519 idp->current.de.d_ino = bp->b_bio1.bio_offset +
522 idp->curroff += reclen;
524 switch (imp->iso_ftype) {
527 ino_t cur_fileno = idp->current.de.d_ino;
528 cd9660_rrip_getname(ep,idp->current.de.d_name, &namelen,
530 idp->current.de.d_ino = cur_fileno;
531 idp->current.de.d_namlen = namelen;
532 if (idp->current.de.d_namlen)
533 error = iso_uiodir(idp,&idp->current.de,idp->curroff);
536 default: /* ISO_FTYPE_DEFAULT || ISO_FTYPE_9660 || ISO_FTYPE_HIGH_SIERRA*/
537 strcpy(idp->current.de.d_name,"..");
538 if (idp->current.de.d_namlen == 1 && ep->name[0] == 0) {
539 idp->current.de.d_namlen = 1;
540 error = iso_uiodir(idp,&idp->current.de,idp->curroff);
541 } else if (idp->current.de.d_namlen == 1 && ep->name[0] == 1) {
542 idp->current.de.d_namlen = 2;
543 error = iso_uiodir(idp,&idp->current.de,idp->curroff);
545 isofntrans(ep->name,idp->current.de.d_namlen,
546 idp->current.de.d_name, &namelen,
547 imp->iso_ftype == ISO_FTYPE_9660,
548 isonum_711(ep->flags)&4,
552 idp->current.de.d_namlen = namelen;
553 if (imp->iso_ftype == ISO_FTYPE_DEFAULT)
554 error = iso_shipdir(idp);
556 error = iso_uiodir(idp,&idp->current.de,idp->curroff);
562 entryoffsetinblock += reclen;
565 if (!error && imp->iso_ftype == ISO_FTYPE_DEFAULT) {
566 idp->current.de.d_namlen = 0;
567 error = iso_shipdir(idp);
572 if (ap->a_ncookies != NULL) {
574 kfree(cookies, M_TEMP);
577 * Work out the number of cookies actually used.
579 *ap->a_ncookies = ncookies - idp->ncookies;
580 *ap->a_cookies = cookies;
587 uio->uio_offset = idp->uio_off;
588 *ap->a_eofflag = idp->eofflag;
598 * Return target name of a symbolic link
599 * Shouldn't we get the parent vnode and read the data from there?
600 * This could eventually result in deadlocks in cd9660_lookup.
601 * But otherwise the block read here is in the block buffer two times.
603 typedef struct iso_directory_record ISODIR;
604 typedef struct iso_node ISONODE;
605 typedef struct iso_mnt ISOMNT;
607 * cd9660_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
610 cd9660_readlink(struct vop_readlink_args *ap)
625 if (imp->iso_ftype != ISO_FTYPE_RRIP)
629 * Get parents directory record block that this inode included.
631 error = bread(imp->im_devvp,
632 (off_t)ip->i_number & ~((1 << imp->im_bshift) - 1),
633 imp->logical_block_size, &bp);
640 * Setup the directory pointer for this inode
642 dirp = (ISODIR *)(bp->b_data + (ip->i_number & imp->im_bmask));
645 * Just make sure, we have a right one....
646 * 1: Check not cross boundary on block
648 if ((ip->i_number & imp->im_bmask) + isonum_711(dirp->length)
649 > (unsigned)imp->logical_block_size) {
656 * Abuse a namei buffer for now.
658 if (uio->uio_segflg == UIO_SYSSPACE)
659 symname = uio->uio_iov->iov_base;
661 symname = objcache_get(namei_oc, M_WAITOK);
664 * Ok, we just gathering a symbolic name in SL record.
666 if (cd9660_rrip_getsymname(dirp, symname, &symlen, imp) == 0) {
667 if (uio->uio_segflg != UIO_SYSSPACE)
668 objcache_put(namei_oc, symname);
673 * Don't forget before you leave from home ;-)
678 * return with the symbolic name to caller's.
680 if (uio->uio_segflg != UIO_SYSSPACE) {
681 error = uiomove(symname, symlen, uio);
682 objcache_put(namei_oc, symname);
685 uio->uio_resid -= symlen;
686 uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + symlen;
687 uio->uio_iov->iov_len -= symlen;
692 * Calculate the logical to physical mapping if not done already,
693 * then call the device strategy routine.
695 * cd9660_strategy(struct buf *a_vp, struct buf *a_bio)
698 cd9660_strategy(struct vop_strategy_args *ap)
700 struct bio *bio = ap->a_bio;
702 struct buf *bp = bio->bio_buf;
703 struct vnode *vp = ap->a_vp;
708 if (vp->v_type == VBLK || vp->v_type == VCHR)
709 panic("cd9660_strategy: spec");
710 nbio = push_bio(bio);
711 if (nbio->bio_offset == NOOFFSET) {
712 error = VOP_BMAP(vp, bio->bio_offset,
713 &nbio->bio_offset, NULL, NULL, bp->b_cmd);
716 bp->b_flags |= B_ERROR;
717 /* I/O was never started on nbio, must biodone(bio) */
721 if (nbio->bio_offset == NOOFFSET)
724 if (nbio->bio_offset == NOOFFSET) {
725 /* I/O was never started on nbio, must biodone(bio) */
730 vn_strategy(vp, nbio);
735 * Print out the contents of an inode.
737 * cd9660_print(struct vnode *a_vp)
740 cd9660_print(struct vop_print_args *ap)
742 kprintf("tag VT_ISOFS, isofs vnode\n");
747 * Return POSIX pathconf information applicable to cd9660 filesystems.
749 * cd9660_pathconf(struct vnode *a_vp, int a_name, register_t *a_retval)
752 cd9660_pathconf(struct vop_pathconf_args *ap)
754 switch (ap->a_name) {
759 if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP)
760 *ap->a_retval = NAME_MAX;
765 *ap->a_retval = PATH_MAX;
768 *ap->a_retval = PIPE_BUF;
770 case _PC_CHOWN_RESTRICTED:
783 * Advisory lock support
786 cd9660_advlock(struct vop_advlock_args *ap)
788 struct iso_node *ip = VTOI(ap->a_vp);
789 return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
794 * Global vfs data structures for cd9660
796 struct vop_ops cd9660_vnode_vops = {
797 .vop_default = vop_defaultop,
798 .vop_open = cd9660_open,
799 .vop_access = cd9660_access,
800 .vop_advlock = cd9660_advlock,
801 .vop_bmap = cd9660_bmap,
802 .vop_old_lookup = cd9660_lookup,
803 .vop_getattr = cd9660_getattr,
804 .vop_inactive = cd9660_inactive,
805 .vop_ioctl = cd9660_ioctl,
806 .vop_pathconf = cd9660_pathconf,
807 .vop_print = cd9660_print,
808 .vop_read = cd9660_read,
809 .vop_readdir = cd9660_readdir,
810 .vop_readlink = cd9660_readlink,
811 .vop_reclaim = cd9660_reclaim,
812 .vop_setattr = cd9660_setattr,
813 .vop_strategy = cd9660_strategy,
814 .vop_getpages = vop_stdgetpages,
815 .vop_putpages = vop_stdputpages
819 * Special device vnode ops
821 struct vop_ops cd9660_spec_vops = {
822 .vop_default = vop_defaultop,
823 .vop_read = vop_stdnoread,
824 .vop_access = cd9660_access,
825 .vop_getattr = cd9660_getattr,
826 .vop_inactive = cd9660_inactive,
827 .vop_print = cd9660_print,
828 .vop_reclaim = cd9660_reclaim,
829 .vop_setattr = cd9660_setattr,
832 struct vop_ops cd9660_fifo_vops = {
833 .vop_default = fifo_vnoperate,
834 .vop_access = cd9660_access,
835 .vop_getattr = cd9660_getattr,
836 .vop_inactive = cd9660_inactive,
837 .vop_print = cd9660_print,
838 .vop_reclaim = cd9660_reclaim,
839 .vop_setattr = cd9660_setattr,