Kernel - fix access checks
[dragonfly.git] / sys / vfs / isofs / cd9660 / cd9660_vfsops.c
CommitLineData
984263bc
MD
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_vfsops.c 8.18 (Berkeley) 5/22/95
39 * $FreeBSD: src/sys/isofs/cd9660/cd9660_vfsops.c,v 1.74.2.7 2002/04/08 09:39:29 bde Exp $
67863d04 40 * $DragonFly: src/sys/vfs/isofs/cd9660/cd9660_vfsops.c,v 1.46 2008/09/17 21:44:23 dillon Exp $
984263bc
MD
41 */
42
43#include <sys/param.h>
44#include <sys/systm.h>
984263bc 45#include <sys/proc.h>
895c1f85 46#include <sys/priv.h>
fad57d0e 47#include <sys/nlookup.h>
984263bc
MD
48#include <sys/kernel.h>
49#include <sys/vnode.h>
50#include <sys/mount.h>
51#include <sys/buf.h>
52#include <sys/cdio.h>
53#include <sys/conf.h>
54#include <sys/fcntl.h>
55#include <sys/malloc.h>
56#include <sys/stat.h>
57#include <sys/syslog.h>
58
59#include <vm/vm_zone.h>
60
1f2de5d4
MD
61#include "iso.h"
62#include "iso_rrip.h"
63#include "cd9660_node.h"
64#include "cd9660_mount.h"
984263bc 65
66a1ddf5
MD
66extern struct vop_ops cd9660_vnode_vops;
67extern struct vop_ops cd9660_spec_vops;
68extern struct vop_ops cd9660_fifo_vops;
0961aa92 69
984263bc
MD
70MALLOC_DEFINE(M_ISOFSMNT, "ISOFS mount", "ISOFS mount structure");
71MALLOC_DEFINE(M_ISOFSNODE, "ISOFS node", "ISOFS vnode private part");
72
acde96db
MD
73static int cd9660_mount (struct mount *, char *, caddr_t, struct ucred *);
74static int cd9660_unmount (struct mount *, int);
a6ee311a 75static int cd9660_root (struct mount *, struct vnode **);
acde96db 76static int cd9660_statfs (struct mount *, struct statfs *, struct ucred *);
b9b0a6d0 77static int cd9660_vget (struct mount *, struct vnode *, ino_t, struct vnode **);
67863d04
MD
78static int cd9660_fhtovp (struct mount *, struct vnode *rootvp,
79 struct fid *, struct vnode **);
a6ee311a
RG
80static int cd9660_checkexp (struct mount *, struct sockaddr *,
81 int *, struct ucred **);
82static int cd9660_vptofh (struct vnode *, struct fid *);
984263bc
MD
83
84static struct vfsops cd9660_vfsops = {
43c45e8f
HP
85 .vfs_mount = cd9660_mount,
86 .vfs_unmount = cd9660_unmount,
87 .vfs_root = cd9660_root,
88 .vfs_statfs = cd9660_statfs,
89 .vfs_sync = vfs_stdsync,
90 .vfs_vget = cd9660_vget,
91 .vfs_fhtovp = cd9660_fhtovp,
92 .vfs_checkexp = cd9660_checkexp,
93 .vfs_vptofh = cd9660_vptofh,
94 .vfs_init = cd9660_init,
95 .vfs_uninit = cd9660_uninit,
984263bc
MD
96};
97VFS_SET(cd9660_vfsops, cd9660, VFCF_READONLY);
98MODULE_VERSION(cd9660, 1);
99
100
101/*
102 * Called by vfs_mountroot when iso is going to be mounted as root.
103 */
104
b13267a5 105static int iso_get_ssector (cdev_t dev);
a6ee311a 106static int iso_mountfs (struct vnode *devvp, struct mount *mp,
acde96db 107 struct iso_args *argp);
984263bc
MD
108
109/*
110 * Try to find the start of the last data track on this CD-ROM. This
111 * is used to mount the last session of a multi-session CD. Bail out
112 * and return 0 if we fail, this is always a safe bet.
113 */
114static int
b13267a5 115iso_get_ssector(cdev_t dev)
984263bc
MD
116{
117 struct ioc_toc_header h;
118 struct ioc_read_toc_single_entry t;
119 int i;
984263bc 120
87baaf0c
MD
121 if (dev_dioctl(dev, CDIOREADTOCHEADER, (caddr_t)&h, FREAD,
122 proc0.p_ucred, NULL) != 0)
984263bc
MD
123 return 0;
124
125 for (i = h.ending_track; i >= 0; i--) {
126 t.address_format = CD_LBA_FORMAT;
127 t.track = i;
87baaf0c
MD
128 if (dev_dioctl(dev, CDIOREADTOCENTRY, (caddr_t)&t, FREAD,
129 proc0.p_ucred, NULL) != 0) {
984263bc 130 return 0;
335dda38 131 }
984263bc
MD
132 if ((t.entry.control & 4) != 0)
133 /* found a data track */
134 break;
135 }
136
137 if (i < 0)
138 return 0;
139
140 return ntohl(t.entry.addr.lba);
141}
142
984263bc 143static int
acde96db 144iso_mountroot(struct mount *mp)
984263bc
MD
145{
146 struct iso_args args;
21739618 147 struct vnode *rootvp;
984263bc
MD
148 int error;
149
150 if ((error = bdevvp(rootdev, &rootvp))) {
086c1d7e 151 kprintf("iso_mountroot: can't find rootvp\n");
984263bc
MD
152 return (error);
153 }
154 args.flags = ISOFSMNT_ROOT;
155
ca466bae 156 vn_lock(rootvp, LK_EXCLUSIVE | LK_RETRY);
87de5057 157 error = VOP_OPEN(rootvp, FREAD, FSCRED, NULL);
a11aaa81 158 vn_unlock(rootvp);
984263bc
MD
159 if (error)
160 return (error);
161
acde96db 162 args.ssector = iso_get_ssector(rootdev);
984263bc 163
87de5057 164 VOP_CLOSE(rootvp, FREAD);
984263bc
MD
165
166 if (bootverbose)
086c1d7e 167 kprintf("iso_mountroot(): using session at block %d\n",
984263bc 168 args.ssector);
acde96db 169 if ((error = iso_mountfs(rootvp, mp, &args)) != 0)
984263bc
MD
170 return (error);
171
acde96db 172 cd9660_statfs(mp, &mp->mnt_stat, proc0.p_ucred);
984263bc
MD
173 return (0);
174}
175
176/*
177 * VFS Operations.
178 *
179 * mount system call
180 */
181static int
acde96db 182cd9660_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
984263bc
MD
183{
184 struct vnode *devvp;
185 struct iso_args args;
186 size_t size;
187 int error;
188 mode_t accessmode;
189 struct iso_mnt *imp = 0;
fad57d0e 190 struct nlookupdata nd;
984263bc 191
83760fab 192 if ((mp->mnt_flag & (MNT_ROOTFS|MNT_UPDATE)) == MNT_ROOTFS) {
acde96db 193 return (iso_mountroot(mp));
984263bc
MD
194 }
195 if ((error = copyin(data, (caddr_t)&args, sizeof (struct iso_args))))
196 return (error);
197
198 if ((mp->mnt_flag & MNT_RDONLY) == 0)
199 return (EROFS);
200
201 /*
202 * If updating, check whether changing from read-only to
203 * read/write; if there is no device name, that's all we do.
204 */
205 if (mp->mnt_flag & MNT_UPDATE) {
206 imp = VFSTOISOFS(mp);
207 if (args.fspec == 0)
208 return (vfs_export(mp, &imp->im_export, &args.export));
209 }
210 /*
211 * Not an update, or updating the name: look up the name
212 * and verify that it refers to a sensible block device.
213 */
fad57d0e
MD
214 devvp = NULL;
215 error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
216 if (error == 0)
217 error = nlookup(&nd);
218 if (error == 0)
28623bf9 219 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
fad57d0e
MD
220 nlookup_done(&nd);
221 if (error)
984263bc 222 return (error);
984263bc
MD
223
224 if (!vn_isdisk(devvp, &error)) {
225 vrele(devvp);
226 return (error);
227 }
228
229 /*
230 * Verify that user has necessary permissions on the device,
231 * or has superuser abilities
232 */
233 accessmode = VREAD;
ca466bae 234 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
cb66845a 235 error = VOP_EACCESS(devvp, accessmode, cred);
984263bc 236 if (error)
895c1f85 237 error = priv_check_cred(cred, PRIV_ROOT, 0);
984263bc
MD
238 if (error) {
239 vput(devvp);
240 return (error);
241 }
a11aaa81 242 vn_unlock(devvp);
984263bc
MD
243
244 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
acde96db 245 error = iso_mountfs(devvp, mp, &args);
984263bc
MD
246 } else {
247 if (devvp != imp->im_devvp)
248 error = EINVAL; /* needs translation */
249 else
250 vrele(devvp);
251 }
252 if (error) {
253 vrele(devvp);
254 return error;
255 }
256 imp = VFSTOISOFS(mp);
b1ce5639 257 copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
984263bc
MD
258 &size);
259 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
acde96db 260 cd9660_statfs(mp, &mp->mnt_stat, cred);
984263bc
MD
261 return 0;
262}
263
264/*
265 * Common code for mount and mountroot
266 */
267static int
acde96db 268iso_mountfs(struct vnode *devvp, struct mount *mp, struct iso_args *argp)
3fa8192b 269{
460af697 270 struct iso_mnt *isomp = NULL;
984263bc
MD
271 struct buf *bp = NULL;
272 struct buf *pribp = NULL, *supbp = NULL;
b13267a5 273 cdev_t dev;
984263bc
MD
274 int error = EINVAL;
275 int needclose = 0;
276 int high_sierra = 0;
277 int iso_bsize;
278 int iso_blknum;
279 int joliet_level;
280 struct iso_volume_descriptor *vdp = 0;
281 struct iso_primary_descriptor *pri = NULL;
282 struct iso_sierra_primary_descriptor *pri_sierra = NULL;
283 struct iso_supplementary_descriptor *sup = NULL;
284 struct iso_directory_record *rootp;
285 int logical_block_size;
286
287 if (!(mp->mnt_flag & MNT_RDONLY))
288 return EROFS;
289
290 /*
291 * Disallow multiple mounts of the same device.
292 * Disallow mounting of a device that is currently in use
984263bc
MD
293 * Flush out any old buffers remaining from a previous use.
294 */
295 if ((error = vfs_mountedon(devvp)))
296 return error;
8be7edad 297 if (vcount(devvp) > 0)
984263bc 298 return EBUSY;
87de5057 299 if ((error = vinvalbuf(devvp, V_SAVE, 0, 0)))
984263bc
MD
300 return (error);
301
ca466bae 302 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
87de5057 303 error = VOP_OPEN(devvp, FREAD, FSCRED, NULL);
a11aaa81 304 vn_unlock(devvp);
984263bc
MD
305 if (error)
306 return error;
e4c9c0c8
MD
307 dev = devvp->v_rdev;
308 if (dev->si_iosize_max != 0)
309 mp->mnt_iosize_max = dev->si_iosize_max;
984263bc
MD
310 if (mp->mnt_iosize_max > MAXPHYS)
311 mp->mnt_iosize_max = MAXPHYS;
312
313 needclose = 1;
314
315 /* This is the "logical sector size". The standard says this
316 * should be 2048 or the physical sector size on the device,
317 * whichever is greater. For now, we'll just use a constant.
318 */
319 iso_bsize = ISO_DEFAULT_BLOCK_SIZE;
320
321 joliet_level = 0;
322 for (iso_blknum = 16 + argp->ssector;
323 iso_blknum < 100 + argp->ssector;
324 iso_blknum++) {
54078292 325 if ((error = bread(devvp, (off_t)iso_blknum * iso_bsize,
3b568787 326 iso_bsize, &bp)) != 0)
984263bc
MD
327 goto out;
328
329 vdp = (struct iso_volume_descriptor *)bp->b_data;
330 if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0) {
331 if (bcmp (vdp->id_sierra, ISO_SIERRA_ID,
332 sizeof vdp->id) != 0) {
333 error = EINVAL;
334 goto out;
335 } else
336 high_sierra = 1;
337 }
338 switch (isonum_711 (high_sierra? vdp->type_sierra: vdp->type)){
339 case ISO_VD_PRIMARY:
340 if (pribp == NULL) {
341 pribp = bp;
342 bp = NULL;
343 pri = (struct iso_primary_descriptor *)vdp;
344 pri_sierra =
345 (struct iso_sierra_primary_descriptor *)vdp;
346 }
347 break;
348
349 case ISO_VD_SUPPLEMENTARY:
350 if (supbp == NULL) {
351 supbp = bp;
352 bp = NULL;
353 sup = (struct iso_supplementary_descriptor *)vdp;
354
355 if (!(argp->flags & ISOFSMNT_NOJOLIET)) {
356 if (bcmp(sup->escape, "%/@", 3) == 0)
357 joliet_level = 1;
358 if (bcmp(sup->escape, "%/C", 3) == 0)
359 joliet_level = 2;
360 if (bcmp(sup->escape, "%/E", 3) == 0)
361 joliet_level = 3;
362
363 if ((isonum_711 (sup->flags) & 1) &&
364 (argp->flags & ISOFSMNT_BROKENJOLIET) == 0)
365 joliet_level = 0;
366 }
367 }
368 break;
369
370 case ISO_VD_END:
371 goto vd_end;
372
373 default:
374 break;
375 }
376 if (bp) {
377 brelse(bp);
378 bp = NULL;
379 }
380 }
381 vd_end:
382 if (bp) {
383 brelse(bp);
384 bp = NULL;
385 }
386
387 if (pri == NULL) {
388 error = EINVAL;
389 goto out;
390 }
391
392 logical_block_size =
393 isonum_723 (high_sierra?
394 pri_sierra->logical_block_size:
395 pri->logical_block_size);
396
397 if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
398 || (logical_block_size & (logical_block_size - 1)) != 0) {
399 error = EINVAL;
400 goto out;
401 }
402
403 rootp = (struct iso_directory_record *)
404 (high_sierra?
405 pri_sierra->root_directory_record:
406 pri->root_directory_record);
407
efda3bd0 408 isomp = kmalloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK | M_ZERO);
984263bc
MD
409 isomp->logical_block_size = logical_block_size;
410 isomp->volume_space_size =
411 isonum_733 (high_sierra?
412 pri_sierra->volume_space_size:
413 pri->volume_space_size);
414 isomp->joliet_level = 0;
415 /*
416 * Since an ISO9660 multi-session CD can also access previous
417 * sessions, we have to include them into the space consider-
418 * ations. This doesn't yield a very accurate number since
419 * parts of the old sessions might be inaccessible now, but we
420 * can't do much better. This is also important for the NFS
421 * filehandle validation.
422 */
423 isomp->volume_space_size += argp->ssector;
424 bcopy (rootp, isomp->root, sizeof isomp->root);
425 isomp->root_extent = isonum_733 (rootp->extent);
426 isomp->root_size = isonum_733 (rootp->size);
427
428 isomp->im_bmask = logical_block_size - 1;
429 isomp->im_bshift = ffs(logical_block_size) - 1;
430
431 pribp->b_flags |= B_AGE;
432 brelse(pribp);
433 pribp = NULL;
434
435 mp->mnt_data = (qaddr_t)isomp;
436 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
437 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
438 mp->mnt_maxsymlinklen = 0;
439 mp->mnt_flag |= MNT_LOCAL;
440 isomp->im_mountp = mp;
441 isomp->im_dev = dev;
442 isomp->im_devvp = devvp;
443
e4c9c0c8 444 dev->si_mountpoint = mp;
984263bc
MD
445
446 /* Check the Rock Ridge Extention support */
447 if (!(argp->flags & ISOFSMNT_NORRIP)) {
448 if ((error = bread(isomp->im_devvp,
54078292 449 lblktooff(isomp, isomp->root_extent + isonum_711(rootp->ext_attr_length)),
3b568787 450 isomp->logical_block_size, &bp)) != 0)
984263bc
MD
451 goto out;
452
453 rootp = (struct iso_directory_record *)bp->b_data;
454
455 if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
456 argp->flags |= ISOFSMNT_NORRIP;
457 } else {
458 argp->flags &= ~ISOFSMNT_GENS;
459 }
460
461 /*
462 * The contents are valid,
463 * but they will get reread as part of another vnode, so...
464 */
465 bp->b_flags |= B_AGE;
466 brelse(bp);
467 bp = NULL;
468 }
469 isomp->im_flags = argp->flags & (ISOFSMNT_NORRIP | ISOFSMNT_GENS |
470 ISOFSMNT_EXTATT | ISOFSMNT_NOJOLIET);
471
472 if (high_sierra) {
473 /* this effectively ignores all the mount flags */
474 log(LOG_INFO, "cd9660: High Sierra Format\n");
475 isomp->iso_ftype = ISO_FTYPE_HIGH_SIERRA;
0961aa92 476 } else {
984263bc
MD
477 switch (isomp->im_flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS)) {
478 default:
479 isomp->iso_ftype = ISO_FTYPE_DEFAULT;
480 break;
481 case ISOFSMNT_GENS|ISOFSMNT_NORRIP:
482 isomp->iso_ftype = ISO_FTYPE_9660;
483 break;
484 case 0:
485 log(LOG_INFO, "cd9660: RockRidge Extension\n");
486 isomp->iso_ftype = ISO_FTYPE_RRIP;
487 break;
488 }
0961aa92 489 }
984263bc
MD
490
491 /* Decide whether to use the Joliet descriptor */
492
493 if (isomp->iso_ftype != ISO_FTYPE_RRIP && joliet_level) {
494 log(LOG_INFO, "cd9660: Joliet Extension (Level %d)\n", joliet_level);
495 rootp = (struct iso_directory_record *)
496 sup->root_directory_record;
497 bcopy (rootp, isomp->root, sizeof isomp->root);
498 isomp->root_extent = isonum_733 (rootp->extent);
499 isomp->root_size = isonum_733 (rootp->size);
500 isomp->joliet_level = joliet_level;
501 supbp->b_flags |= B_AGE;
502 }
503
504 if (supbp) {
505 brelse(supbp);
506 supbp = NULL;
507 }
508
66a1ddf5
MD
509 vfs_add_vnodeops(mp, &cd9660_vnode_vops, &mp->mnt_vn_norm_ops);
510 vfs_add_vnodeops(mp, &cd9660_spec_vops, &mp->mnt_vn_spec_ops);
511 vfs_add_vnodeops(mp, &cd9660_fifo_vops, &mp->mnt_vn_fifo_ops);
0961aa92 512
984263bc
MD
513 return 0;
514out:
e4c9c0c8 515 dev->si_mountpoint = NULL;
984263bc
MD
516 if (bp)
517 brelse(bp);
518 if (pribp)
519 brelse(pribp);
520 if (supbp)
521 brelse(supbp);
522 if (needclose)
87de5057 523 VOP_CLOSE(devvp, FREAD);
984263bc 524 if (isomp) {
efda3bd0 525 kfree((caddr_t)isomp, M_ISOFSMNT);
984263bc
MD
526 mp->mnt_data = (qaddr_t)0;
527 }
528 return error;
529}
530
531/*
532 * unmount system call
533 */
534static int
acde96db 535cd9660_unmount(struct mount *mp, int mntflags)
984263bc 536{
3a1b0fdc 537 struct iso_mnt *isomp;
984263bc
MD
538 int error, flags = 0;
539
540 if (mntflags & MNT_FORCE)
541 flags |= FORCECLOSE;
542#if 0
543 mntflushbuf(mp, 0);
544 if (mntinvalbuf(mp))
545 return EBUSY;
546#endif
547 if ((error = vflush(mp, 0, flags)))
548 return (error);
549
550 isomp = VFSTOISOFS(mp);
551
e4c9c0c8 552 isomp->im_devvp->v_rdev->si_mountpoint = NULL;
87de5057 553 error = VOP_CLOSE(isomp->im_devvp, FREAD);
984263bc 554 vrele(isomp->im_devvp);
efda3bd0 555 kfree((caddr_t)isomp, M_ISOFSMNT);
984263bc
MD
556 mp->mnt_data = (qaddr_t)0;
557 mp->mnt_flag &= ~MNT_LOCAL;
558 return (error);
559}
560
561/*
562 * Return root of a filesystem
563 */
564static int
3fa8192b 565cd9660_root(struct mount *mp, struct vnode **vpp)
984263bc
MD
566{
567 struct iso_mnt *imp = VFSTOISOFS(mp);
568 struct iso_directory_record *dp =
569 (struct iso_directory_record *)imp->root;
570 ino_t ino = isodirino(dp, imp);
571
572 /*
573 * With RRIP we must use the `.' entry of the root directory.
574 * Simply tell vget, that it's a relocated directory.
575 */
576 return (cd9660_vget_internal(mp, ino, vpp,
577 imp->iso_ftype == ISO_FTYPE_RRIP, dp));
578}
579
580/*
581 * Get file system statistics.
582 */
583int
acde96db 584cd9660_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
984263bc 585{
dadab5e9 586 struct iso_mnt *isomp;
984263bc
MD
587
588 isomp = VFSTOISOFS(mp);
589
590 sbp->f_bsize = isomp->logical_block_size;
591 sbp->f_iosize = sbp->f_bsize; /* XXX */
592 sbp->f_blocks = isomp->volume_space_size;
593 sbp->f_bfree = 0; /* total free blocks */
594 sbp->f_bavail = 0; /* blocks free for non superuser */
595 sbp->f_files = 0; /* total files */
596 sbp->f_ffree = 0; /* free file nodes */
597 if (sbp != &mp->mnt_stat) {
598 sbp->f_type = mp->mnt_vfc->vfc_typenum;
984263bc
MD
599 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
600 }
601 return 0;
602}
603
604/*
605 * File handle to vnode
606 *
607 * Have to be really careful about stale file handles:
608 * - check that the inode number is in range
609 * - call iget() to get the locked inode
610 * - check for an unallocated inode (i_mode == 0)
611 * - check that the generation number matches
612 */
613
614struct ifid {
615 ushort ifid_len;
616 ushort ifid_pad;
617 int ifid_ino;
618 long ifid_start;
619};
620
621/* ARGSUSED */
622int
67863d04
MD
623cd9660_fhtovp(struct mount *mp, struct vnode *rootvp,
624 struct fid *fhp, struct vnode **vpp)
984263bc
MD
625{
626 struct ifid *ifhp = (struct ifid *)fhp;
3a1b0fdc 627 struct iso_node *ip;
984263bc
MD
628 struct vnode *nvp;
629 int error;
630
631#ifdef ISOFS_DBG
086c1d7e 632 kprintf("fhtovp: ino %d, start %ld\n",
984263bc
MD
633 ifhp->ifid_ino, ifhp->ifid_start);
634#endif
635
b9b0a6d0 636 if ((error = VFS_VGET(mp, NULL, ifhp->ifid_ino, &nvp)) != 0) {
984263bc
MD
637 *vpp = NULLVP;
638 return (error);
639 }
640 ip = VTOI(nvp);
641 if (ip->inode.iso_mode == 0) {
642 vput(nvp);
643 *vpp = NULLVP;
644 return (ESTALE);
645 }
646 *vpp = nvp;
647 return (0);
648}
649
650int
3fa8192b
CP
651cd9660_checkexp(struct mount *mp, struct sockaddr *nam, int *exflagsp,
652 struct ucred **credanonp)
984263bc 653{
3a1b0fdc
RG
654 struct netcred *np;
655 struct iso_mnt *imp;
984263bc
MD
656
657 imp = VFSTOISOFS(mp);
658
659 /*
660 * Get the export permission structure for this <mp, client> tuple.
661 */
662 np = vfs_export_lookup(mp, &imp->im_export, nam);
663 if (np == NULL)
664 return (EACCES);
665
666 *exflagsp = np->netc_exflags;
667 *credanonp = &np->netc_anon;
668 return (0);
669}
670
671int
b9b0a6d0 672cd9660_vget(struct mount *mp, struct vnode *dvp, ino_t ino, struct vnode **vpp)
984263bc
MD
673{
674
675 /*
676 * XXXX
677 * It would be nice if we didn't always set the `relocated' flag
678 * and force the extra read, but I don't want to think about fixing
679 * that right now.
680 */
681 return (cd9660_vget_internal(mp, ino, vpp,
682#if 0
683 VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
684#else
685 0,
686#endif
60233e58 687 NULL));
984263bc
MD
688}
689
690int
3fa8192b
CP
691cd9660_vget_internal(struct mount *mp, ino_t ino, struct vnode **vpp,
692 int relocated, struct iso_directory_record *isodir)
984263bc
MD
693{
694 struct iso_mnt *imp;
695 struct iso_node *ip;
696 struct buf *bp;
697 struct vnode *vp;
b13267a5 698 cdev_t dev;
984263bc
MD
699 int error;
700
701 imp = VFSTOISOFS(mp);
702 dev = imp->im_dev;
3446c007 703again:
984263bc
MD
704 if ((*vpp = cd9660_ihashget(dev, ino)) != NULLVP)
705 return (0);
706
707 /* Allocate a new vnode/iso_node. */
6ddb7618 708 error = getnewvnode(VT_ISOFS, mp, &vp, 0, 0);
3446c007 709 if (error) {
984263bc
MD
710 *vpp = NULLVP;
711 return (error);
712 }
713 MALLOC(ip, struct iso_node *, sizeof(struct iso_node), M_ISOFSNODE,
e7b4468c 714 M_WAITOK | M_ZERO);
984263bc
MD
715 ip->i_vnode = vp;
716 ip->i_dev = dev;
717 ip->i_number = ino;
718
719 /*
3446c007
MD
720 * Insert it into the inode hash table and check for a collision.
721 * If a collision occurs, throw away the vnode and try again.
984263bc 722 */
3446c007 723 if (cd9660_ihashins(ip) != 0) {
086c1d7e 724 kprintf("debug: cd9660 ihashins collision, retrying\n");
5fd012e0 725 vx_put(vp);
efda3bd0 726 kfree(ip, M_ISOFSNODE);
3446c007
MD
727 goto again;
728 }
729 vp->v_data = ip;
984263bc 730
899483f5 731 if (isodir == NULL) {
984263bc
MD
732 int lbn, off;
733
734 lbn = lblkno(imp, ino);
735 if (lbn >= imp->volume_space_size) {
5fd012e0 736 vx_put(vp);
086c1d7e 737 kprintf("fhtovp: lbn exceed volume space %d\n", lbn);
984263bc
MD
738 return (ESTALE);
739 }
740
741 off = blkoff(imp, ino);
742 if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
5fd012e0 743 vx_put(vp);
086c1d7e 744 kprintf("fhtovp: crosses block boundary %d\n",
984263bc
MD
745 off + ISO_DIRECTORY_RECORD_SIZE);
746 return (ESTALE);
747 }
748
749 error = bread(imp->im_devvp,
54078292 750 lblktooff(imp, lbn),
3b568787 751 imp->logical_block_size, &bp);
984263bc 752 if (error) {
5fd012e0 753 vx_put(vp);
984263bc 754 brelse(bp);
086c1d7e 755 kprintf("fhtovp: bread error %d\n",error);
984263bc
MD
756 return (error);
757 }
758 isodir = (struct iso_directory_record *)(bp->b_data + off);
759
760 if (off + isonum_711(isodir->length) >
761 imp->logical_block_size) {
5fd012e0 762 vx_put(vp);
899483f5 763 if (bp != NULL)
984263bc 764 brelse(bp);
086c1d7e 765 kprintf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
984263bc
MD
766 off +isonum_711(isodir->length), off,
767 isonum_711(isodir->length));
768 return (ESTALE);
769 }
770
771#if 0
772 if (isonum_733(isodir->extent) +
773 isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
899483f5 774 if (bp != NULL)
984263bc 775 brelse(bp);
086c1d7e 776 kprintf("fhtovp: file start miss %d vs %d\n",
984263bc
MD
777 isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
778 ifhp->ifid_start);
779 return (ESTALE);
780 }
781#endif
782 } else
783 bp = 0;
784
785 ip->i_mnt = imp;
786 ip->i_devvp = imp->im_devvp;
597aea93 787 vref(ip->i_devvp);
984263bc
MD
788
789 if (relocated) {
790 /*
791 * On relocated directories we must
792 * read the `.' entry out of a dir.
793 */
794 ip->iso_start = ino >> imp->im_bshift;
899483f5 795 if (bp != NULL)
984263bc 796 brelse(bp);
899483f5 797 if ((error = cd9660_devblkatoff(vp, (off_t)0, NULL, &bp)) != 0) {
5fd012e0 798 vx_put(vp);
984263bc
MD
799 return (error);
800 }
801 isodir = (struct iso_directory_record *)bp->b_data;
802 }
803
804 ip->iso_extent = isonum_733(isodir->extent);
805 ip->i_size = isonum_733(isodir->size);
806 ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
57f7b636 807
984263bc
MD
808 /*
809 * Setup time stamp, attribute
810 */
811 vp->v_type = VNON;
812 switch (imp->iso_ftype) {
813 default: /* ISO_FTYPE_9660 */
814 {
815 struct buf *bp2;
816 int off;
817 if ((imp->im_flags & ISOFSMNT_EXTATT)
818 && (off = isonum_711(isodir->ext_attr_length)))
899483f5 819 cd9660_devblkatoff(vp, (off_t)-(off << imp->im_bshift), NULL,
984263bc
MD
820 &bp2);
821 else
822 bp2 = NULL;
823 cd9660_defattr(isodir, ip, bp2, ISO_FTYPE_9660);
824 cd9660_deftstamp(isodir, ip, bp2, ISO_FTYPE_9660);
825 if (bp2)
826 brelse(bp2);
827 break;
828 }
829 case ISO_FTYPE_RRIP:
830 cd9660_rrip_analyze(isodir, ip, imp);
831 break;
832 }
833
899483f5 834 if (bp != NULL)
984263bc
MD
835 brelse(bp);
836
837 /*
838 * Initialize the associated vnode
839 */
57f7b636
MD
840 vp->v_type = IFTOVT(ip->inode.iso_mode);
841
842 switch (vp->v_type) {
984263bc 843 case VFIFO:
6ddb7618 844 vp->v_ops = &mp->mnt_vn_fifo_ops;
984263bc
MD
845 break;
846 case VCHR:
847 case VBLK:
6ddb7618 848 vp->v_ops = &mp->mnt_vn_spec_ops;
0e9b9130
MD
849 addaliasu(vp, umajor(ip->inode.iso_rdev),
850 uminor(ip->inode.iso_rdev));
984263bc 851 break;
1c843a13
MD
852 case VREG:
853 case VDIR:
854 vinitvmio(vp, ip->i_size);
855 break;
984263bc
MD
856 default:
857 break;
858 }
859
860 if (ip->iso_extent == imp->root_extent)
861 vp->v_flag |= VROOT;
862
863 /*
5fd012e0 864 * Return the locked and refd vp
984263bc 865 */
984263bc
MD
866 *vpp = vp;
867 return (0);
868}
869
870/*
871 * Vnode pointer to File handle
872 */
873/* ARGSUSED */
874int
3fa8192b 875cd9660_vptofh(struct vnode *vp, struct fid *fhp)
984263bc 876{
3a1b0fdc
RG
877 struct iso_node *ip = VTOI(vp);
878 struct ifid *ifhp;
984263bc
MD
879
880 ifhp = (struct ifid *)fhp;
881 ifhp->ifid_len = sizeof(struct ifid);
882
883 ifhp->ifid_ino = ip->i_number;
884 ifhp->ifid_start = ip->iso_start;
885
886#ifdef ISOFS_DBG
086c1d7e 887 kprintf("vptofh: ino %d, start %ld\n",
984263bc
MD
888 ifhp->ifid_ino,ifhp->ifid_start);
889#endif
890 return 0;
891}