Merge from vendor branch OPENSSL:
[dragonfly.git] / sys / vfs / isofs / cd9660 / cd9660_vnops.c
1 /*-
2  * Copyright (c) 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
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).
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
25  *
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
36  * SUCH DAMAGE.
37  *
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.37 2007/08/28 01:04:33 dillon Exp $
41  */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/proc.h>
46 #include <sys/namei.h>
47 #include <sys/kernel.h>
48 #include <sys/stat.h>
49 #include <sys/buf.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>
59
60 #include <vm/vm.h>
61 #include <vm/vnode_pager.h>
62
63 #include "iso.h"
64 #include "cd9660_node.h"
65 #include "iso_rrip.h"
66
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 *);
75 struct isoreaddir;
76 static int iso_uiodir (struct isoreaddir *idp, struct dirent *dp,
77                            off_t off);
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
84 /*
85  * Setattr call. Only allowed for block and character special devices.
86  *
87  * cd9660_setattr(struct vnode *a_vp, struct vattr *a_vap,
88  *                struct ucred *a_cred, struct proc *a_p)
89  */
90 int
91 cd9660_setattr(struct vop_setattr_args *ap)
92 {
93         struct vnode *vp = ap->a_vp;
94         struct vattr *vap = ap->a_vap;
95
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)
99                 return (EROFS);
100         if (vap->va_size != (u_quad_t)VNOVAL) {
101                 switch (vp->v_type) {
102                 case VDIR:
103                         return (EISDIR);
104                 case VLNK:
105                 case VREG:
106                         return (EROFS);
107                 case VCHR:
108                 case VBLK:
109                 case VSOCK:
110                 case VFIFO:
111                 case VNON:
112                 case VBAD:
113                         return (0);
114                 }
115         }
116         return (0);
117 }
118
119 /*
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.
123  *
124  * cd9660_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
125  *               struct proc *a_p)
126  */
127 /* ARGSUSED */
128 static int
129 cd9660_access(struct vop_access_args *ap)
130 {
131         struct vnode *vp = ap->a_vp;
132         struct iso_node *ip = VTOI(vp);
133         struct ucred *cred = ap->a_cred;
134         mode_t mask, mode = ap->a_mode;
135         gid_t *gp;
136         int i;
137
138         /*
139          * Disallow write attempts unless the file is a socket,
140          * fifo, or a block or character device resident on the
141          * file system.
142          */
143         if (mode & VWRITE) {
144                 switch (vp->v_type) {
145                 case VDIR:
146                 case VLNK:
147                 case VREG:
148                         return (EROFS);
149                         /* NOT REACHED */
150                 default:
151                         break;
152                 }
153         }
154
155         /* User id 0 always gets access. */
156         if (cred->cr_uid == 0)
157                 return (0);
158
159         mask = 0;
160
161         /* Otherwise, check the owner. */
162         if (cred->cr_uid == ip->inode.iso_uid) {
163                 if (mode & VEXEC)
164                         mask |= S_IXUSR;
165                 if (mode & VREAD)
166                         mask |= S_IRUSR;
167                 if (mode & VWRITE)
168                         mask |= S_IWUSR;
169                 return ((ip->inode.iso_mode & mask) == mask ? 0 : EACCES);
170         }
171
172         /* Otherwise, check the groups. */
173         for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
174                 if (ip->inode.iso_gid == *gp) {
175                         if (mode & VEXEC)
176                                 mask |= S_IXGRP;
177                         if (mode & VREAD)
178                                 mask |= S_IRGRP;
179                         if (mode & VWRITE)
180                                 mask |= S_IWGRP;
181                         return ((ip->inode.iso_mode & mask) == mask ?
182                             0 : EACCES);
183                 }
184
185         /* Otherwise, check everyone else. */
186         if (mode & VEXEC)
187                 mask |= S_IXOTH;
188         if (mode & VREAD)
189                 mask |= S_IROTH;
190         if (mode & VWRITE)
191                 mask |= S_IWOTH;
192         return ((ip->inode.iso_mode & mask) == mask ? 0 : EACCES);
193 }
194
195 /*
196  * cd9660_getattr(struct vnode *a_vp, struct vattr *a_vap)
197  */
198 static int
199 cd9660_getattr(struct vop_getattr_args *ap)
200 {
201         struct vnode *vp = ap->a_vp;
202         struct vattr *vap = ap->a_vap;
203         struct iso_node *ip = VTOI(vp);
204
205         vap->va_fsid    = dev2udev(ip->i_dev);
206         vap->va_fileid  = ip->i_number;
207
208         vap->va_mode    = ip->inode.iso_mode;
209         vap->va_nlink   = ip->inode.iso_links;
210         vap->va_uid     = ip->inode.iso_uid;
211         vap->va_gid     = ip->inode.iso_gid;
212         vap->va_atime   = ip->inode.iso_atime;
213         vap->va_mtime   = ip->inode.iso_mtime;
214         vap->va_ctime   = ip->inode.iso_ctime;
215         vap->va_rmajor  = umajor(ip->inode.iso_rdev);
216         vap->va_rminor  = uminor(ip->inode.iso_rdev);
217
218         vap->va_size    = (u_quad_t)(unsigned long)ip->i_size;
219         if (ip->i_size == 0 && (vap->va_mode & S_IFMT) == S_IFLNK) {
220                 struct vop_readlink_args rdlnk;
221                 struct iovec aiov;
222                 struct uio auio;
223                 char *cp;
224
225                 MALLOC(cp, char *, MAXPATHLEN, M_TEMP, M_WAITOK);
226                 aiov.iov_base = cp;
227                 aiov.iov_len = MAXPATHLEN;
228                 auio.uio_iov = &aiov;
229                 auio.uio_iovcnt = 1;
230                 auio.uio_offset = 0;
231                 auio.uio_rw = UIO_READ;
232                 auio.uio_segflg = UIO_SYSSPACE;
233                 auio.uio_td = curthread;
234                 auio.uio_resid = MAXPATHLEN;
235                 rdlnk.a_uio = &auio;
236                 rdlnk.a_vp = ap->a_vp;
237                 rdlnk.a_cred = proc0.p_ucred; /* use root cred */
238                 if (cd9660_readlink(&rdlnk) == 0)
239                         vap->va_size = MAXPATHLEN - auio.uio_resid;
240                 FREE(cp, M_TEMP);
241         }
242         vap->va_flags   = 0;
243         vap->va_gen = 1;
244         vap->va_blocksize = ip->i_mnt->logical_block_size;
245         vap->va_bytes   = (u_quad_t) ip->i_size;
246         vap->va_type    = vp->v_type;
247         vap->va_filerev = 0;
248         return (0);
249 }
250
251 /*
252  * Vnode op for ioctl.
253  *
254  * cd9660_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data,
255  *              int a_fflag, struct ucred *a_cred, struct proc *a_p)
256  */
257 static int
258 cd9660_ioctl(struct vop_ioctl_args *ap)
259 {
260         struct vnode *vp = ap->a_vp;
261         struct iso_node *ip = VTOI(vp);
262
263         switch (ap->a_command) {
264
265         case FIOGETLBA:
266                 *(int *)(ap->a_data) = ip->iso_start;
267                 return 0;
268         default:
269                 return (ENOTTY);
270         }
271 }
272
273 /*
274  * open is called when the kernel intends to read or memory map a vnode.
275  */
276 static int
277 cd9660_open(struct vop_open_args *ap)
278 {
279         return(vop_stdopen(ap));
280 }
281
282 /*
283  * Vnode op for reading.
284  *
285  * cd9660_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
286  *              struct ucred *a_cred)
287  */
288 static int
289 cd9660_read(struct vop_read_args *ap)
290 {
291         struct vnode *vp = ap->a_vp;
292         struct uio *uio = ap->a_uio;
293         struct iso_node *ip = VTOI(vp);
294         struct iso_mnt *imp;
295         struct buf *bp;
296         daddr_t lbn, rablock;
297         off_t raoffset;
298         off_t loffset;
299         off_t diff;
300         int rasize, error = 0;
301         int seqcount;
302         long size, n, on;
303
304         seqcount = ap->a_ioflag >> 16;
305
306         if (uio->uio_resid == 0)
307                 return (0);
308         if (uio->uio_offset < 0)
309                 return (EINVAL);
310         ip->i_flag |= IN_ACCESS;
311         imp = ip->i_mnt;
312         do {
313                 lbn = lblkno(imp, uio->uio_offset);
314                 loffset = lblktooff(imp, lbn);
315                 on = blkoff(imp, uio->uio_offset);
316                 n = min((u_int)(imp->logical_block_size - on),
317                         uio->uio_resid);
318                 diff = (off_t)ip->i_size - uio->uio_offset;
319                 if (diff <= 0)
320                         return (0);
321                 if (diff < n)
322                         n = diff;
323                 size = blksize(imp, ip, lbn);
324                 rablock = lbn + 1;
325                 raoffset = lblktooff(imp, rablock);
326                 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
327                         if (raoffset < ip->i_size)
328                                 error = cluster_read(vp, (off_t)ip->i_size,
329                                          loffset, size,
330                                          uio->uio_resid,
331                                          (ap->a_ioflag >> 16), &bp);
332                         else
333                                 error = bread(vp, loffset, size, &bp);
334                 } else {
335                         if (seqcount > 1 &&
336                             lblktosize(imp, rablock) < ip->i_size) {
337                                 rasize = blksize(imp, ip, rablock);
338                                 error = breadn(vp, loffset, size, &raoffset,
339                                                &rasize, 1, &bp);
340                         } else
341                                 error = bread(vp, loffset, size, &bp);
342                 }
343                 n = min(n, size - bp->b_resid);
344                 if (error) {
345                         brelse(bp);
346                         return (error);
347                 }
348
349                 error = uiomove(bp->b_data + on, (int)n, uio);
350                 brelse(bp);
351         } while (error == 0 && uio->uio_resid > 0 && n != 0);
352         return (error);
353 }
354
355 /* struct dirent + enough space for the maximum supported size */
356 struct iso_dirent {
357         struct dirent de;
358         char de_name[_DIRENT_RECLEN(NAME_MAX) - sizeof(struct dirent)];
359 };
360
361 /*
362  * Structure for reading directories
363  */
364 struct isoreaddir {
365         struct iso_dirent saveent;
366         struct iso_dirent assocent;
367         struct iso_dirent current;
368         off_t saveoff;
369         off_t assocoff;
370         off_t curroff;
371         struct uio *uio;
372         off_t uio_off;
373         int eofflag;
374         u_long *cookies;
375         int ncookies;
376 };
377
378 int
379 iso_uiodir(struct isoreaddir *idp, struct dirent *dp, off_t off)
380 {
381         int error;
382
383         dp->d_name[dp->d_namlen] = 0;
384
385         if (idp->uio->uio_resid < _DIRENT_DIRSIZ(dp)) {
386                 idp->eofflag = 0;
387                 return (-1);
388         }
389
390         if (idp->cookies) {
391                 if (idp->ncookies <= 0) {
392                         idp->eofflag = 0;
393                         return (-1);
394                 }
395
396                 *idp->cookies++ = off;
397                 --idp->ncookies;
398         }
399
400         if ((error = uiomove((caddr_t) dp,_DIRENT_DIRSIZ(dp),idp->uio)) != 0)
401                 return (error);
402         idp->uio_off = off;
403         return (0);
404 }
405
406 int
407 iso_shipdir(struct isoreaddir *idp)
408 {
409         struct dirent *dp;
410         int cl, sl, assoc;
411         int error;
412         char *cname, *sname;
413
414         cl = idp->current.de.d_namlen;
415         cname = idp->current.de.d_name;
416 assoc = (cl > 1) && (*cname == ASSOCCHAR);
417         if (assoc) {
418                 cl--;
419                 cname++;
420         }
421
422         dp = &idp->saveent.de;
423         sname = dp->d_name;
424         if (!(sl = dp->d_namlen)) {
425                 dp = &idp->assocent.de;
426                 sname = dp->d_name + 1;
427                 sl = dp->d_namlen - 1;
428         }
429         if (sl > 0) {
430                 if (sl != cl
431                     || bcmp(sname,cname,sl)) {
432                         if (idp->assocent.de.d_namlen) {
433                                 if ((error = iso_uiodir(idp,&idp->assocent.de,idp->assocoff)) != 0)
434                                         return (error);
435                                 idp->assocent.de.d_namlen = 0;
436                         }
437                         if (idp->saveent.de.d_namlen) {
438                                 if ((error = iso_uiodir(idp,&idp->saveent.de,idp->saveoff)) != 0)
439                                         return (error);
440                                 idp->saveent.de.d_namlen = 0;
441                         }
442                 }
443         }
444         if (assoc) {
445                 idp->assocoff = idp->curroff;
446                 bcopy(&idp->current,&idp->assocent,_DIRENT_DIRSIZ(&idp->current.de));
447         } else {
448                 idp->saveoff = idp->curroff;
449                 bcopy(&idp->current,&idp->saveent,_DIRENT_DIRSIZ(&idp->current.de));
450         }
451         return (0);
452 }
453
454 /*
455  * Vnode op for readdir
456  *
457  * cd9660_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
458  *                int *a_eofflag, int *a_ncookies, u_long *a_cookies)
459  */
460 static int
461 cd9660_readdir(struct vop_readdir_args *ap)
462 {
463         struct uio *uio = ap->a_uio;
464         struct isoreaddir *idp;
465         struct vnode *vdp = ap->a_vp;
466         struct iso_node *dp;
467         struct iso_mnt *imp;
468         struct buf *bp = NULL;
469         struct iso_directory_record *ep;
470         int entryoffsetinblock;
471         doff_t endsearch;
472         u_long bmask;
473         int error = 0;
474         int reclen;
475         u_short namelen;
476         int ncookies = 0;
477         u_long *cookies = NULL;
478
479         dp = VTOI(vdp);
480         imp = dp->i_mnt;
481         bmask = imp->im_bmask;
482
483         if ((error = vn_lock(vdp, LK_EXCLUSIVE|LK_RETRY)) != 0)
484                 return (error);
485
486         MALLOC(idp, struct isoreaddir *, sizeof(*idp), M_TEMP, M_WAITOK);
487         idp->saveent.de.d_namlen = idp->assocent.de.d_namlen = 0;
488         /*
489          * XXX
490          * Is it worth trying to figure out the type?
491          */
492         idp->saveent.de.d_type = DT_UNKNOWN;
493         idp->assocent.de.d_type = DT_UNKNOWN;
494         idp->current.de.d_type = DT_UNKNOWN;
495         idp->uio = uio;
496         if (ap->a_ncookies == NULL) {
497                 idp->cookies = NULL;
498         } else {
499                 /*
500                  * Guess the number of cookies needed.  Guess at least
501                  * 1 to avoid a degenerate case in malloc, and cap at
502                  * a reasonable limit.
503                  */
504                 ncookies = uio->uio_resid / 16 + 1;
505                 if (ncookies > 1024)
506                         ncookies = 1024;
507                 MALLOC(cookies, u_long *, ncookies * sizeof(u_int),
508                        M_TEMP, M_WAITOK);
509                 idp->cookies = cookies;
510                 idp->ncookies = ncookies;
511         }
512         idp->eofflag = 1;
513         idp->curroff = uio->uio_offset;
514
515         if ((entryoffsetinblock = idp->curroff & bmask) &&
516             (error = cd9660_devblkatoff(vdp, (off_t)idp->curroff, NULL, &bp))) {
517                 FREE(idp, M_TEMP);
518                 goto done;
519         }
520         endsearch = dp->i_size;
521
522         while (idp->curroff < endsearch) {
523                 /*
524                  * If offset is on a block boundary,
525                  * read the next directory block.
526                  * Release previous if it exists.
527                  */
528                 if ((idp->curroff & bmask) == 0) {
529                         if (bp != NULL)
530                                 brelse(bp);
531                         if ((error =
532                             cd9660_devblkatoff(vdp, (off_t)idp->curroff, NULL, &bp)) != 0)
533                                 break;
534                         entryoffsetinblock = 0;
535                 }
536                 /*
537                  * Get pointer to next entry.
538                  */
539                 ep = (struct iso_directory_record *)
540                         ((char *)bp->b_data + entryoffsetinblock);
541
542                 reclen = isonum_711(ep->length);
543                 if (reclen == 0) {
544                         /* skip to next block, if any */
545                         idp->curroff =
546                             (idp->curroff & ~bmask) + imp->logical_block_size;
547                         continue;
548                 }
549
550                 if (reclen < ISO_DIRECTORY_RECORD_SIZE) {
551                         error = EINVAL;
552                         /* illegal entry, stop */
553                         break;
554                 }
555
556                 if (entryoffsetinblock + reclen > imp->logical_block_size) {
557                         error = EINVAL;
558                         /* illegal directory, so stop looking */
559                         break;
560                 }
561
562                 idp->current.de.d_namlen = isonum_711(ep->name_len);
563
564                 if (reclen < ISO_DIRECTORY_RECORD_SIZE + idp->current.de.d_namlen) {
565                         error = EINVAL;
566                         /* illegal entry, stop */
567                         break;
568                 }
569
570                 if (isonum_711(ep->flags)&2)
571                         idp->current.de.d_ino = isodirino(ep, imp);
572                 else
573                         idp->current.de.d_ino = bp->b_bio1.bio_offset +
574                                                 entryoffsetinblock;
575
576                 idp->curroff += reclen;
577
578                 switch (imp->iso_ftype) {
579                 case ISO_FTYPE_RRIP:
580                 {
581                         ino_t cur_fileno = idp->current.de.d_ino;       
582                         cd9660_rrip_getname(ep,idp->current.de.d_name, &namelen,
583                                            &cur_fileno,imp);
584                         idp->current.de.d_ino = cur_fileno;
585                         idp->current.de.d_namlen = namelen;
586                         if (idp->current.de.d_namlen)
587                                 error = iso_uiodir(idp,&idp->current.de,idp->curroff);
588                         break;
589                 }
590                 default: /* ISO_FTYPE_DEFAULT || ISO_FTYPE_9660 || ISO_FTYPE_HIGH_SIERRA*/
591                         strcpy(idp->current.de.d_name,"..");
592                         if (idp->current.de.d_namlen == 1 && ep->name[0] == 0) {
593                                 idp->current.de.d_namlen = 1;
594                                 error = iso_uiodir(idp,&idp->current.de,idp->curroff);
595                         } else if (idp->current.de.d_namlen == 1 && ep->name[0] == 1) {
596                                 idp->current.de.d_namlen = 2;
597                                 error = iso_uiodir(idp,&idp->current.de,idp->curroff);
598                         } else {
599                                 isofntrans(ep->name,idp->current.de.d_namlen,
600                                            idp->current.de.d_name, &namelen,
601                                            imp->iso_ftype == ISO_FTYPE_9660,
602                                            isonum_711(ep->flags)&4,
603                                            imp->joliet_level);
604                                 idp->current.de.d_namlen = namelen;
605                                 if (imp->iso_ftype == ISO_FTYPE_DEFAULT)
606                                         error = iso_shipdir(idp);
607                                 else
608                                         error = iso_uiodir(idp,&idp->current.de,idp->curroff);
609                         }
610                 }
611                 if (error)
612                         break;
613
614                 entryoffsetinblock += reclen;
615         }
616
617         if (!error && imp->iso_ftype == ISO_FTYPE_DEFAULT) {
618                 idp->current.de.d_namlen = 0;
619                 error = iso_shipdir(idp);
620         }
621         if (error < 0)
622                 error = 0;
623
624         if (ap->a_ncookies != NULL) {
625                 if (error)
626                         kfree(cookies, M_TEMP);
627                 else {
628                         /*
629                          * Work out the number of cookies actually used.
630                          */
631                         *ap->a_ncookies = ncookies - idp->ncookies;
632                         *ap->a_cookies = cookies;
633                 }
634         }
635
636         if (bp)
637                 brelse (bp);
638
639         uio->uio_offset = idp->uio_off;
640         *ap->a_eofflag = idp->eofflag;
641
642         FREE(idp, M_TEMP);
643
644 done:
645         vn_unlock(vdp);
646         return (error);
647 }
648
649 /*
650  * Return target name of a symbolic link
651  * Shouldn't we get the parent vnode and read the data from there?
652  * This could eventually result in deadlocks in cd9660_lookup.
653  * But otherwise the block read here is in the block buffer two times.
654  */
655 typedef struct iso_directory_record ISODIR;
656 typedef struct iso_node             ISONODE;
657 typedef struct iso_mnt              ISOMNT;
658 /*
659  * cd9660_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
660  */
661 static int
662 cd9660_readlink(struct vop_readlink_args *ap)
663 {
664         ISONODE *ip;
665         ISODIR  *dirp;
666         ISOMNT  *imp;
667         struct  buf *bp;
668         struct  uio *uio;
669         u_short symlen;
670         int     error;
671         char    *symname;
672
673         ip  = VTOI(ap->a_vp);
674         imp = ip->i_mnt;
675         uio = ap->a_uio;
676
677         if (imp->iso_ftype != ISO_FTYPE_RRIP)
678                 return (EINVAL);
679
680         /*
681          * Get parents directory record block that this inode included.
682          */
683         error = bread(imp->im_devvp,
684                         (off_t)ip->i_number & ~((1 << imp->im_bshift) - 1),
685                       imp->logical_block_size, &bp);
686         if (error) {
687                 brelse(bp);
688                 return (EINVAL);
689         }
690
691         /*
692          * Setup the directory pointer for this inode
693          */
694         dirp = (ISODIR *)(bp->b_data + (ip->i_number & imp->im_bmask));
695
696         /*
697          * Just make sure, we have a right one....
698          *   1: Check not cross boundary on block
699          */
700         if ((ip->i_number & imp->im_bmask) + isonum_711(dirp->length)
701             > (unsigned)imp->logical_block_size) {
702                 brelse(bp);
703                 return (EINVAL);
704         }
705
706         /*
707          * Now get a buffer
708          * Abuse a namei buffer for now.
709          */
710         if (uio->uio_segflg == UIO_SYSSPACE)
711                 symname = uio->uio_iov->iov_base;
712         else
713                 symname = objcache_get(namei_oc, M_WAITOK);
714         
715         /*
716          * Ok, we just gathering a symbolic name in SL record.
717          */
718         if (cd9660_rrip_getsymname(dirp, symname, &symlen, imp) == 0) {
719                 if (uio->uio_segflg != UIO_SYSSPACE)
720                         objcache_put(namei_oc, symname);
721                 brelse(bp);
722                 return (EINVAL);
723         }
724         /*
725          * Don't forget before you leave from home ;-)
726          */
727         brelse(bp);
728
729         /*
730          * return with the symbolic name to caller's.
731          */
732         if (uio->uio_segflg != UIO_SYSSPACE) {
733                 error = uiomove(symname, symlen, uio);
734                 objcache_put(namei_oc, symname);
735                 return (error);
736         }
737         uio->uio_resid -= symlen;
738         uio->uio_iov->iov_base += symlen;
739         uio->uio_iov->iov_len -= symlen;
740         return (0);
741 }
742
743 /*
744  * Calculate the logical to physical mapping if not done already,
745  * then call the device strategy routine.
746  *
747  * cd9660_strategy(struct buf *a_vp, struct buf *a_bio)
748  */
749 static int
750 cd9660_strategy(struct vop_strategy_args *ap)
751 {
752         struct bio *bio = ap->a_bio;
753         struct bio *nbio;
754         struct buf *bp = bio->bio_buf;
755         struct vnode *vp = ap->a_vp;
756         struct iso_node *ip;
757         int error;
758
759         ip = VTOI(vp);
760         if (vp->v_type == VBLK || vp->v_type == VCHR)
761                 panic("cd9660_strategy: spec");
762         nbio = push_bio(bio);
763         if (nbio->bio_offset == NOOFFSET) {
764                 error = VOP_BMAP(vp, bio->bio_offset,
765                                  &nbio->bio_offset, NULL, NULL);
766                 if (error) {
767                         bp->b_error = error;
768                         bp->b_flags |= B_ERROR;
769                         /* I/O was never started on nbio, must biodone(bio) */
770                         biodone(bio);
771                         return (error);
772                 }
773                 if (nbio->bio_offset == NOOFFSET)
774                         clrbuf(bp);
775         }
776         if (nbio->bio_offset == NOOFFSET) {
777                 /* I/O was never started on nbio, must biodone(bio) */
778                 biodone(bio);
779                 return (0);
780         }
781         vp = ip->i_devvp;
782         vn_strategy(vp, nbio);
783         return (0);
784 }
785
786 /*
787  * Print out the contents of an inode.
788  *
789  * cd9660_print(struct vnode *a_vp)
790  */
791 static int
792 cd9660_print(struct vop_print_args *ap)
793 {
794         kprintf("tag VT_ISOFS, isofs vnode\n");
795         return (0);
796 }
797
798 /*
799  * Return POSIX pathconf information applicable to cd9660 filesystems.
800  *
801  * cd9660_pathconf(struct vnode *a_vp, int a_name, register_t *a_retval)
802  */
803 static int
804 cd9660_pathconf(struct vop_pathconf_args *ap)
805 {
806         switch (ap->a_name) {
807         case _PC_LINK_MAX:
808                 *ap->a_retval = 1;
809                 return (0);
810         case _PC_NAME_MAX:
811                 if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP)
812                         *ap->a_retval = NAME_MAX;
813                 else
814                         *ap->a_retval = 37;
815                 return (0);
816         case _PC_PATH_MAX:
817                 *ap->a_retval = PATH_MAX;
818                 return (0);
819         case _PC_PIPE_BUF:
820                 *ap->a_retval = PIPE_BUF;
821                 return (0);
822         case _PC_CHOWN_RESTRICTED:
823                 *ap->a_retval = 1;
824                 return (0);
825         case _PC_NO_TRUNC:
826                 *ap->a_retval = 1;
827                 return (0);
828         default:
829                 return (EINVAL);
830         }
831         /* NOTREACHED */
832 }
833
834 /*
835  * Advisory lock support
836  */
837 static int
838 cd9660_advlock(struct vop_advlock_args *ap)
839 {
840         struct iso_node *ip = VTOI(ap->a_vp);
841         return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
842 }
843
844
845 /*
846  * Global vfs data structures for cd9660
847  */
848 struct vop_ops cd9660_vnode_vops = {
849         .vop_default =          vop_defaultop,
850         .vop_open =             cd9660_open,
851         .vop_access =           cd9660_access,
852         .vop_advlock =          cd9660_advlock,
853         .vop_bmap =             cd9660_bmap,
854         .vop_old_lookup =       cd9660_lookup,
855         .vop_getattr =          cd9660_getattr,
856         .vop_inactive =         cd9660_inactive,
857         .vop_ioctl =            cd9660_ioctl,
858         .vop_pathconf =         cd9660_pathconf,
859         .vop_print =            cd9660_print,
860         .vop_read =             cd9660_read,
861         .vop_readdir =          cd9660_readdir,
862         .vop_readlink =         cd9660_readlink,
863         .vop_reclaim =          cd9660_reclaim,
864         .vop_setattr =          cd9660_setattr,
865         .vop_strategy =         cd9660_strategy,
866         .vop_getpages =         vop_stdgetpages,
867         .vop_putpages =         vop_stdputpages
868 };
869
870 /*
871  * Special device vnode ops
872  */
873 struct vop_ops cd9660_spec_vops = {
874         .vop_default =          spec_vnoperate,
875         .vop_access =           cd9660_access,
876         .vop_getattr =          cd9660_getattr,
877         .vop_inactive =         cd9660_inactive,
878         .vop_print =            cd9660_print,
879         .vop_reclaim =          cd9660_reclaim,
880         .vop_setattr =          cd9660_setattr,
881 };
882
883 struct vop_ops cd9660_fifo_vops = {
884         .vop_default =          fifo_vnoperate,
885         .vop_access =           cd9660_access,
886         .vop_getattr =          cd9660_getattr,
887         .vop_inactive =         cd9660_inactive,
888         .vop_print =            cd9660_print,
889         .vop_reclaim =          cd9660_reclaim,
890         .vop_setattr =          cd9660_setattr,
891 };
892