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>
66 #include "cd9660_node.h"
69 static int cd9660_access (struct vop_access_args *);
70 static int cd9660_advlock (struct vop_advlock_args *);
71 static int cd9660_getattr (struct vop_getattr_args *);
72 static int cd9660_ioctl (struct vop_ioctl_args *);
73 static int cd9660_pathconf (struct vop_pathconf_args *);
74 static int cd9660_open (struct vop_open_args *);
75 static int cd9660_read (struct vop_read_args *);
76 static int cd9660_setattr (struct vop_setattr_args *);
78 static int iso_uiodir (struct isoreaddir *idp, struct dirent *dp,
80 static int iso_shipdir (struct isoreaddir *idp);
81 static int cd9660_readdir (struct vop_readdir_args *);
82 static int cd9660_readlink (struct vop_readlink_args *ap);
83 static int cd9660_strategy (struct vop_strategy_args *);
84 static int cd9660_print (struct vop_print_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);
136 KKASSERT(vp->v_mount->mnt_flag & MNT_RDONLY);
137 return (vop_helper_access(ap, ip->inode.iso_uid, ip->inode.iso_gid,
138 ip->inode.iso_mode, 0));
142 * cd9660_getattr(struct vnode *a_vp, struct vattr *a_vap)
145 cd9660_getattr(struct vop_getattr_args *ap)
147 struct vnode *vp = ap->a_vp;
148 struct vattr *vap = ap->a_vap;
149 struct iso_node *ip = VTOI(vp);
151 vap->va_fsid = dev2udev(ip->i_dev);
152 vap->va_fileid = ip->i_number;
154 vap->va_mode = ip->inode.iso_mode;
155 vap->va_nlink = ip->inode.iso_links;
156 vap->va_uid = ip->inode.iso_uid;
157 vap->va_gid = ip->inode.iso_gid;
158 vap->va_atime = ip->inode.iso_atime;
159 vap->va_mtime = ip->inode.iso_mtime;
160 vap->va_ctime = ip->inode.iso_ctime;
161 vap->va_rmajor = umajor(ip->inode.iso_rdev);
162 vap->va_rminor = uminor(ip->inode.iso_rdev);
164 vap->va_size = (u_quad_t)(unsigned long)ip->i_size;
165 if (ip->i_size == 0 && (vap->va_mode & S_IFMT) == S_IFLNK) {
166 struct vop_readlink_args rdlnk;
171 MALLOC(cp, char *, MAXPATHLEN, M_TEMP, M_WAITOK);
173 aiov.iov_len = MAXPATHLEN;
174 auio.uio_iov = &aiov;
177 auio.uio_rw = UIO_READ;
178 auio.uio_segflg = UIO_SYSSPACE;
179 auio.uio_td = curthread;
180 auio.uio_resid = MAXPATHLEN;
182 rdlnk.a_vp = ap->a_vp;
183 rdlnk.a_cred = proc0.p_ucred; /* use root cred */
184 if (cd9660_readlink(&rdlnk) == 0)
185 vap->va_size = MAXPATHLEN - auio.uio_resid;
190 vap->va_blocksize = ip->i_mnt->logical_block_size;
191 vap->va_bytes = (u_quad_t) ip->i_size;
192 vap->va_type = vp->v_type;
198 * Vnode op for ioctl.
200 * cd9660_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data,
201 * int a_fflag, struct ucred *a_cred, struct proc *a_p)
204 cd9660_ioctl(struct vop_ioctl_args *ap)
206 struct vnode *vp = ap->a_vp;
207 struct iso_node *ip = VTOI(vp);
209 switch (ap->a_command) {
212 *(int *)(ap->a_data) = ip->iso_start;
220 * open is called when the kernel intends to read or memory map a vnode.
223 cd9660_open(struct vop_open_args *ap)
225 return(vop_stdopen(ap));
229 * Vnode op for reading.
231 * cd9660_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
232 * struct ucred *a_cred)
235 cd9660_read(struct vop_read_args *ap)
237 struct vnode *vp = ap->a_vp;
238 struct uio *uio = ap->a_uio;
239 struct iso_node *ip = VTOI(vp);
242 daddr_t lbn, rablock;
246 int rasize, error = 0;
250 seqcount = ap->a_ioflag >> 16;
252 if (uio->uio_resid == 0)
254 if (uio->uio_offset < 0)
256 ip->i_flag |= IN_ACCESS;
259 lbn = lblkno(imp, uio->uio_offset);
260 loffset = lblktooff(imp, lbn);
261 on = blkoff(imp, uio->uio_offset);
262 n = szmin((u_int)(imp->logical_block_size - on),
264 diff = (off_t)ip->i_size - uio->uio_offset;
269 size = blksize(imp, ip, lbn);
271 raoffset = lblktooff(imp, rablock);
272 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
273 if (raoffset < ip->i_size) {
274 error = cluster_read(vp, (off_t)ip->i_size,
277 (ap->a_ioflag >> 16) *
281 error = bread(vp, loffset, size, &bp);
285 lblktosize(imp, rablock) < ip->i_size) {
286 rasize = blksize(imp, ip, rablock);
287 error = breadn(vp, loffset, size, &raoffset,
290 error = bread(vp, loffset, size, &bp);
292 n = imin(n, size - bp->b_resid);
298 error = uiomove(bp->b_data + on, (int)n, uio);
300 } while (error == 0 && uio->uio_resid > 0 && n != 0);
304 /* struct dirent + enough space for the maximum supported size */
307 char de_name[_DIRENT_RECLEN(NAME_MAX) - sizeof(struct dirent)];
311 * Structure for reading directories
314 struct iso_dirent saveent;
315 struct iso_dirent assocent;
316 struct iso_dirent current;
328 iso_uiodir(struct isoreaddir *idp, struct dirent *dp, off_t off)
332 dp->d_name[dp->d_namlen] = 0;
334 if (idp->uio->uio_resid < _DIRENT_DIRSIZ(dp)) {
340 if (idp->ncookies <= 0) {
345 *idp->cookies++ = off;
349 if ((error = uiomove((caddr_t) dp,_DIRENT_DIRSIZ(dp),idp->uio)) != 0)
356 iso_shipdir(struct isoreaddir *idp)
363 cl = idp->current.de.d_namlen;
364 cname = idp->current.de.d_name;
365 assoc = (cl > 1) && (*cname == ASSOCCHAR);
371 dp = &idp->saveent.de;
373 if (!(sl = dp->d_namlen)) {
374 dp = &idp->assocent.de;
375 sname = dp->d_name + 1;
376 sl = dp->d_namlen - 1;
380 || bcmp(sname,cname,sl)) {
381 if (idp->assocent.de.d_namlen) {
382 if ((error = iso_uiodir(idp,&idp->assocent.de,idp->assocoff)) != 0)
384 idp->assocent.de.d_namlen = 0;
386 if (idp->saveent.de.d_namlen) {
387 if ((error = iso_uiodir(idp,&idp->saveent.de,idp->saveoff)) != 0)
389 idp->saveent.de.d_namlen = 0;
394 idp->assocoff = idp->curroff;
395 bcopy(&idp->current,&idp->assocent,_DIRENT_DIRSIZ(&idp->current.de));
397 idp->saveoff = idp->curroff;
398 bcopy(&idp->current,&idp->saveent,_DIRENT_DIRSIZ(&idp->current.de));
404 * Vnode op for readdir
406 * cd9660_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
407 * int *a_eofflag, int *a_ncookies, off_t *a_cookies)
410 cd9660_readdir(struct vop_readdir_args *ap)
412 struct uio *uio = ap->a_uio;
413 struct isoreaddir *idp;
414 struct vnode *vdp = ap->a_vp;
417 struct buf *bp = NULL;
418 struct iso_directory_record *ep;
419 int entryoffsetinblock;
426 off_t *cookies = NULL;
430 bmask = imp->im_bmask;
432 if ((error = vn_lock(vdp, LK_EXCLUSIVE|LK_RETRY)) != 0)
435 MALLOC(idp, struct isoreaddir *, sizeof(*idp), M_TEMP, M_WAITOK);
436 idp->saveent.de.d_namlen = idp->assocent.de.d_namlen = 0;
439 * Is it worth trying to figure out the type?
441 idp->saveent.de.d_type = DT_UNKNOWN;
442 idp->assocent.de.d_type = DT_UNKNOWN;
443 idp->current.de.d_type = DT_UNKNOWN;
445 if (ap->a_ncookies == NULL) {
449 * Guess the number of cookies needed. Guess at least
450 * 1 to avoid a degenerate case in malloc, and cap at
451 * a reasonable limit.
453 ncookies = uio->uio_resid / 16 + 1;
456 MALLOC(cookies, off_t *, ncookies * sizeof(off_t),
458 idp->cookies = cookies;
459 idp->ncookies = ncookies;
462 idp->curroff = uio->uio_offset;
464 if ((entryoffsetinblock = idp->curroff & bmask) &&
465 (error = cd9660_devblkatoff(vdp, (off_t)idp->curroff, NULL, &bp))) {
469 endsearch = dp->i_size;
471 while (idp->curroff < endsearch) {
473 * If offset is on a block boundary,
474 * read the next directory block.
475 * Release previous if it exists.
477 if ((idp->curroff & bmask) == 0) {
481 cd9660_devblkatoff(vdp, (off_t)idp->curroff, NULL, &bp)) != 0)
483 entryoffsetinblock = 0;
486 * Get pointer to next entry.
488 ep = (struct iso_directory_record *)
489 ((char *)bp->b_data + entryoffsetinblock);
491 reclen = isonum_711(ep->length);
493 /* skip to next block, if any */
495 (idp->curroff & ~bmask) + imp->logical_block_size;
499 if (reclen < ISO_DIRECTORY_RECORD_SIZE) {
501 /* illegal entry, stop */
505 if (entryoffsetinblock + reclen > imp->logical_block_size) {
507 /* illegal directory, so stop looking */
511 idp->current.de.d_namlen = isonum_711(ep->name_len);
513 if (reclen < ISO_DIRECTORY_RECORD_SIZE + idp->current.de.d_namlen) {
515 /* illegal entry, stop */
519 if (isonum_711(ep->flags)&2)
520 idp->current.de.d_ino = isodirino(ep, imp);
522 idp->current.de.d_ino = bp->b_bio1.bio_offset +
525 idp->curroff += reclen;
527 switch (imp->iso_ftype) {
530 ino_t cur_fileno = idp->current.de.d_ino;
531 cd9660_rrip_getname(ep,idp->current.de.d_name, &namelen,
533 idp->current.de.d_ino = cur_fileno;
534 idp->current.de.d_namlen = namelen;
535 if (idp->current.de.d_namlen)
536 error = iso_uiodir(idp,&idp->current.de,idp->curroff);
539 default: /* ISO_FTYPE_DEFAULT || ISO_FTYPE_9660 || ISO_FTYPE_HIGH_SIERRA*/
540 strcpy(idp->current.de.d_name,"..");
541 if (idp->current.de.d_namlen == 1 && ep->name[0] == 0) {
542 idp->current.de.d_namlen = 1;
543 error = iso_uiodir(idp,&idp->current.de,idp->curroff);
544 } else if (idp->current.de.d_namlen == 1 && ep->name[0] == 1) {
545 idp->current.de.d_namlen = 2;
546 error = iso_uiodir(idp,&idp->current.de,idp->curroff);
548 isofntrans(ep->name,idp->current.de.d_namlen,
549 idp->current.de.d_name, &namelen,
550 imp->iso_ftype == ISO_FTYPE_9660,
551 isonum_711(ep->flags)&4,
555 idp->current.de.d_namlen = namelen;
556 if (imp->iso_ftype == ISO_FTYPE_DEFAULT)
557 error = iso_shipdir(idp);
559 error = iso_uiodir(idp,&idp->current.de,idp->curroff);
565 entryoffsetinblock += reclen;
568 if (!error && imp->iso_ftype == ISO_FTYPE_DEFAULT) {
569 idp->current.de.d_namlen = 0;
570 error = iso_shipdir(idp);
575 if (ap->a_ncookies != NULL) {
577 kfree(cookies, M_TEMP);
580 * Work out the number of cookies actually used.
582 *ap->a_ncookies = ncookies - idp->ncookies;
583 *ap->a_cookies = cookies;
590 uio->uio_offset = idp->uio_off;
591 *ap->a_eofflag = idp->eofflag;
601 * Return target name of a symbolic link
602 * Shouldn't we get the parent vnode and read the data from there?
603 * This could eventually result in deadlocks in cd9660_lookup.
604 * But otherwise the block read here is in the block buffer two times.
606 typedef struct iso_directory_record ISODIR;
607 typedef struct iso_node ISONODE;
608 typedef struct iso_mnt ISOMNT;
610 * cd9660_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
613 cd9660_readlink(struct vop_readlink_args *ap)
628 if (imp->iso_ftype != ISO_FTYPE_RRIP)
632 * Get parents directory record block that this inode included.
634 error = bread(imp->im_devvp,
635 (off_t)ip->i_number & ~((1 << imp->im_bshift) - 1),
636 imp->logical_block_size, &bp);
643 * Setup the directory pointer for this inode
645 dirp = (ISODIR *)(bp->b_data + (ip->i_number & imp->im_bmask));
648 * Just make sure, we have a right one....
649 * 1: Check not cross boundary on block
651 if ((ip->i_number & imp->im_bmask) + isonum_711(dirp->length)
652 > (unsigned)imp->logical_block_size) {
659 * Abuse a namei buffer for now.
661 if (uio->uio_segflg == UIO_SYSSPACE)
662 symname = uio->uio_iov->iov_base;
664 symname = objcache_get(namei_oc, M_WAITOK);
667 * Ok, we just gathering a symbolic name in SL record.
669 if (cd9660_rrip_getsymname(dirp, symname, &symlen, imp) == 0) {
670 if (uio->uio_segflg != UIO_SYSSPACE)
671 objcache_put(namei_oc, symname);
676 * Don't forget before you leave from home ;-)
681 * return with the symbolic name to caller's.
683 if (uio->uio_segflg != UIO_SYSSPACE) {
684 error = uiomove(symname, symlen, uio);
685 objcache_put(namei_oc, symname);
688 uio->uio_resid -= symlen;
689 uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + symlen;
690 uio->uio_iov->iov_len -= symlen;
695 * Calculate the logical to physical mapping if not done already,
696 * then call the device strategy routine.
698 * cd9660_strategy(struct buf *a_vp, struct buf *a_bio)
701 cd9660_strategy(struct vop_strategy_args *ap)
703 struct bio *bio = ap->a_bio;
705 struct buf *bp = bio->bio_buf;
706 struct vnode *vp = ap->a_vp;
711 if (vp->v_type == VBLK || vp->v_type == VCHR)
712 panic("cd9660_strategy: spec");
713 nbio = push_bio(bio);
714 if (nbio->bio_offset == NOOFFSET) {
715 error = VOP_BMAP(vp, bio->bio_offset,
716 &nbio->bio_offset, NULL, NULL, bp->b_cmd);
719 bp->b_flags |= B_ERROR;
720 /* I/O was never started on nbio, must biodone(bio) */
724 if (nbio->bio_offset == NOOFFSET)
727 if (nbio->bio_offset == NOOFFSET) {
728 /* I/O was never started on nbio, must biodone(bio) */
733 vn_strategy(vp, nbio);
738 * Print out the contents of an inode.
740 * cd9660_print(struct vnode *a_vp)
743 cd9660_print(struct vop_print_args *ap)
745 kprintf("tag VT_ISOFS, isofs vnode\n");
750 * Return POSIX pathconf information applicable to cd9660 filesystems.
752 * cd9660_pathconf(struct vnode *a_vp, int a_name, register_t *a_retval)
755 cd9660_pathconf(struct vop_pathconf_args *ap)
757 switch (ap->a_name) {
762 if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP)
763 *ap->a_retval = NAME_MAX;
768 *ap->a_retval = PATH_MAX;
771 *ap->a_retval = PIPE_BUF;
773 case _PC_CHOWN_RESTRICTED:
786 * Advisory lock support
789 cd9660_advlock(struct vop_advlock_args *ap)
791 struct iso_node *ip = VTOI(ap->a_vp);
792 return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
797 * Global vfs data structures for cd9660
799 struct vop_ops cd9660_vnode_vops = {
800 .vop_default = vop_defaultop,
801 .vop_open = cd9660_open,
802 .vop_access = cd9660_access,
803 .vop_advlock = cd9660_advlock,
804 .vop_bmap = cd9660_bmap,
805 .vop_old_lookup = cd9660_lookup,
806 .vop_getattr = cd9660_getattr,
807 .vop_inactive = cd9660_inactive,
808 .vop_ioctl = cd9660_ioctl,
809 .vop_pathconf = cd9660_pathconf,
810 .vop_print = cd9660_print,
811 .vop_read = cd9660_read,
812 .vop_readdir = cd9660_readdir,
813 .vop_readlink = cd9660_readlink,
814 .vop_reclaim = cd9660_reclaim,
815 .vop_setattr = cd9660_setattr,
816 .vop_strategy = cd9660_strategy,
817 .vop_getpages = vop_stdgetpages,
818 .vop_putpages = vop_stdputpages
822 * Special device vnode ops
824 struct vop_ops cd9660_spec_vops = {
825 .vop_default = vop_defaultop,
826 .vop_read = vop_stdnoread,
827 .vop_access = cd9660_access,
828 .vop_getattr = cd9660_getattr,
829 .vop_inactive = cd9660_inactive,
830 .vop_print = cd9660_print,
831 .vop_reclaim = cd9660_reclaim,
832 .vop_setattr = cd9660_setattr,
835 struct vop_ops cd9660_fifo_vops = {
836 .vop_default = fifo_vnoperate,
837 .vop_access = cd9660_access,
838 .vop_getattr = cd9660_getattr,
839 .vop_inactive = cd9660_inactive,
840 .vop_print = cd9660_print,
841 .vop_reclaim = cd9660_reclaim,
842 .vop_setattr = cd9660_setattr,