Deleted Added
full compact
udf_vfsops.c (118047) udf_vfsops.c (118607)
1/*-
2 * Copyright (c) 2001, 2002 Scott Long <scottl@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
1/*-
2 * Copyright (c) 2001, 2002 Scott Long <scottl@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/fs/udf/udf_vfsops.c 118047 2003-07-26 07:32:23Z phk $
26 * $FreeBSD: head/sys/fs/udf/udf_vfsops.c 118607 2003-08-07 15:04:27Z jhb $
27 */
28
29/* udf_vfsops.c */
30/* Implement the VFS side of things */
31
32/*
33 * Ok, here's how it goes. The UDF specs are pretty clear on how each data
34 * structure is made up, but not very clear on how they relate to each other.
35 * Here is the skinny... This demostrates a filesystem with one file in the
36 * root directory. Subdirectories are treated just as normal files, but they
37 * have File Id Descriptors of their children as their file data. As for the
38 * Anchor Volume Descriptor Pointer, it can exist in two of the following three
39 * places: sector 256, sector n (the max sector of the disk), or sector
40 * n - 256. It's a pretty good bet that one will exist at sector 256 though.
41 * One caveat is unclosed CD media. For that, sector 256 cannot be written,
42 * so the Anchor Volume Descriptor Pointer can exist at sector 512 until the
43 * media is closed.
44 *
45 * Sector:
46 * 256:
47 * n: Anchor Volume Descriptor Pointer
48 * n - 256: |
49 * |
50 * |-->Main Volume Descriptor Sequence
51 * | |
52 * | |
53 * | |-->Logical Volume Descriptor
54 * | |
55 * |-->Partition Descriptor |
56 * | |
57 * | |
58 * |-->Fileset Descriptor
59 * |
60 * |
61 * |-->Root Dir File Entry
62 * |
63 * |
64 * |-->File data:
65 * File Id Descriptor
66 * |
67 * |
68 * |-->File Entry
69 * |
70 * |
71 * |-->File data
72 */
73#include <sys/types.h>
74#include <sys/param.h>
75#include <sys/systm.h>
76#include <sys/uio.h>
77#include <sys/bio.h>
78#include <sys/buf.h>
79#include <sys/conf.h>
80#include <sys/dirent.h>
81#include <sys/fcntl.h>
82#include <sys/kernel.h>
83#include <sys/malloc.h>
84#include <sys/mount.h>
85#include <sys/namei.h>
86#include <sys/proc.h>
87#include <sys/queue.h>
88#include <sys/vnode.h>
89
90#include <vm/uma.h>
91
92#include <fs/udf/ecma167-udf.h>
93#include <fs/udf/udf.h>
94#include <fs/udf/osta.h>
95
96MALLOC_DEFINE(M_UDFMOUNT, "UDF mount", "UDF mount structure");
97MALLOC_DEFINE(M_UDFFENTRY, "UDF fentry", "UDF file entry structure");
98
99/* Zones */
100uma_zone_t udf_zone_trans = NULL;
101uma_zone_t udf_zone_node = NULL;
102uma_zone_t udf_zone_ds = NULL;
103
104static vfs_init_t udf_init;
105static vfs_uninit_t udf_uninit;
106static vfs_nmount_t udf_mount;
107static vfs_root_t udf_root;
108static vfs_statfs_t udf_statfs;
109static vfs_unmount_t udf_unmount;
110static vfs_fhtovp_t udf_fhtovp;
111static vfs_vptofh_t udf_vptofh;
112
113static int udf_find_partmaps(struct udf_mnt *, struct logvol_desc *);
114
115static struct vfsops udf_vfsops = {
116 .vfs_fhtovp = udf_fhtovp,
117 .vfs_init = udf_init,
118 .vfs_nmount = udf_mount,
119 .vfs_root = udf_root,
120 .vfs_statfs = udf_statfs,
121 .vfs_uninit = udf_uninit,
122 .vfs_unmount = udf_unmount,
123 .vfs_vget = udf_vget,
124 .vfs_vptofh = udf_vptofh,
125};
126VFS_SET(udf_vfsops, udf, VFCF_READONLY);
127
128static int udf_mountfs(struct vnode *, struct mount *, struct thread *);
129
130static int
131udf_init(struct vfsconf *foo)
132{
133
134 /*
135 * This code used to pre-allocate a certain number of pages for each
136 * pool, reducing the need to grow the zones later on. UMA doesn't
137 * advertise any such functionality, unfortunately =-<
138 */
139 udf_zone_trans = uma_zcreate("UDF translation buffer, zone", MAXNAMLEN *
140 sizeof(unicode_t), NULL, NULL, NULL, NULL, 0, 0);
141
142 udf_zone_node = uma_zcreate("UDF Node zone", sizeof(struct udf_node),
143 NULL, NULL, NULL, NULL, 0, 0);
144
145 udf_zone_ds = uma_zcreate("UDF Dirstream zone",
146 sizeof(struct udf_dirstream), NULL, NULL, NULL, NULL, 0, 0);
147
148 if ((udf_zone_node == NULL) || (udf_zone_trans == NULL) ||
149 (udf_zone_ds == NULL)) {
150 printf("Cannot create allocation zones.\n");
151 return (ENOMEM);
152 }
153
154 return 0;
155}
156
157static int
158udf_uninit(struct vfsconf *foo)
159{
160
161 if (udf_zone_trans != NULL) {
162 uma_zdestroy(udf_zone_trans);
163 udf_zone_trans = NULL;
164 }
165
166 if (udf_zone_node != NULL) {
167 uma_zdestroy(udf_zone_node);
168 udf_zone_node = NULL;
169 }
170
171 if (udf_zone_ds != NULL) {
172 uma_zdestroy(udf_zone_ds);
173 udf_zone_ds = NULL;
174 }
175
176 return (0);
177}
178
179static int
180udf_mount(struct mount *mp, struct nameidata *ndp, struct thread *td)
181{
182 struct vnode *devvp; /* vnode of the mount device */
183 struct udf_mnt *imp = 0;
184 struct export_args *export;
185 struct vfsoptlist *opts;
186 char *fspec;
187 size_t size;
188 int error, len;
189
190 opts = mp->mnt_optnew;
191
192 if ((mp->mnt_flag & MNT_RDONLY) == 0)
193 return (EROFS);
194
195 /*
196 * No root filesystem support. Probably not a big deal, since the
197 * bootloader doesn't understand UDF.
198 */
199 if (mp->mnt_flag & MNT_ROOTFS)
200 return (ENOTSUP);
201
202 fspec = NULL;
203 error = vfs_getopt(opts, "from", (void **)&fspec, &len);
204 if (!error && fspec[len - 1] != '\0')
205 return (EINVAL);
206
207 if (mp->mnt_flag & MNT_UPDATE) {
208 imp = VFSTOUDFFS(mp);
209 if (fspec == NULL) {
210 error = vfs_getopt(opts, "export", (void **)&export,
211 &len);
212 if (error || len != sizeof(struct export_args))
213 return (EINVAL);
214 return (vfs_export(mp, export));
215 }
216 }
217
218 /* Check that the mount device exists */
219 if (fspec == NULL)
220 return (EINVAL);
221 NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fspec, td);
222 if ((error = namei(ndp)))
223 return (error);
224 NDFREE(ndp, NDF_ONLY_PNBUF);
225 devvp = ndp->ni_vp;
226
227 if (vn_isdisk(devvp, &error) == 0) {
228 vrele(devvp);
229 return (error);
230 }
231
232 /* Check the access rights on the mount device */
233 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
234 error = VOP_ACCESS(devvp, VREAD, td->td_ucred, td);
235 if (error)
236 error = suser(td);
237 if (error) {
238 vput(devvp);
239 return (error);
240 }
241 VOP_UNLOCK(devvp, 0, td);
242
243 if ((error = udf_mountfs(devvp, mp, td))) {
244 vrele(devvp);
245 return (error);
246 }
247
248 imp = VFSTOUDFFS(mp);
249 copystr(fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
250 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
251 udf_statfs(mp, &mp->mnt_stat, td);
252 return 0;
253};
254
255/*
256 * Check the descriptor tag for both the correct id and correct checksum.
257 * Return zero if all is good, EINVAL if not.
258 */
259int
260udf_checktag(struct desc_tag *tag, uint16_t id)
261{
262 uint8_t *itag;
263 uint8_t i, cksum = 0;
264
265 itag = (uint8_t *)tag;
266
267 if (tag->id != id)
268 return (EINVAL);
269
270 for (i = 0; i < 15; i++)
271 cksum = cksum + itag[i];
272 cksum = cksum - itag[4];
273
274 if (cksum == tag->cksum)
275 return (0);
276
277 return (EINVAL);
278}
279
280static int
281udf_mountfs(struct vnode *devvp, struct mount *mp, struct thread *td) {
282 struct buf *bp = NULL;
283 struct anchor_vdp avdp;
284 struct udf_mnt *udfmp = NULL;
285 struct part_desc *pd;
286 struct logvol_desc *lvd;
287 struct fileset_desc *fsd;
288 struct file_entry *root_fentry;
289 uint32_t sector, size, mvds_start, mvds_end;
290 uint32_t fsd_offset = 0;
291 uint16_t part_num = 0, fsd_part = 0;
292 int error = EINVAL, needclose = 0;
293 int logvol_found = 0, part_found = 0, fsd_found = 0;
294 int bsize;
295
296 /*
297 * Disallow multiple mounts of the same device. Flush the buffer
298 * cache for the device.
299 */
300 if ((error = vfs_mountedon(devvp)))
301 return (error);
302 if (vcount(devvp) > 1)
303 return (EBUSY);
304 if ((error = vinvalbuf(devvp, V_SAVE, td->td_ucred, td, 0, 0)))
305 return (error);
306
307 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
308 error = VOP_OPEN(devvp, FREAD, FSCRED, td, -1);
309 VOP_UNLOCK(devvp, 0, td);
310 if (error)
311 return error;
312 needclose = 1;
313
314 MALLOC(udfmp, struct udf_mnt *, sizeof(struct udf_mnt), M_UDFMOUNT,
315 M_NOWAIT | M_ZERO);
316 if (udfmp == NULL) {
317 printf("Cannot allocate UDF mount struct\n");
318 error = ENOMEM;
319 goto bail;
320 }
321
322 mp->mnt_data = (qaddr_t)udfmp;
323 mp->mnt_stat.f_fsid.val[0] = dev2udev(devvp->v_rdev);
324 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
325 mp->mnt_flag |= MNT_LOCAL;
326 udfmp->im_mountp = mp;
327 udfmp->im_dev = devvp->v_rdev;
328 udfmp->im_devvp = devvp;
329
330 bsize = 2048; /* XXX Should probe the media for it's size */
331
332 /*
333 * Get the Anchor Volume Descriptor Pointer from sector 256.
334 * XXX Should also check sector n - 256, n, and 512.
335 */
336 sector = 256;
337 if ((error = bread(devvp, sector * btodb(bsize), bsize, NOCRED,
338 &bp)) != 0)
339 goto bail;
340 if ((error = udf_checktag((struct desc_tag *)bp->b_data, TAGID_ANCHOR)))
341 goto bail;
342
343 bcopy(bp->b_data, &avdp, sizeof(struct anchor_vdp));
344 brelse(bp);
345 bp = NULL;
346
347 /*
348 * Extract the Partition Descriptor and Logical Volume Descriptor
349 * from the Volume Descriptor Sequence.
350 * XXX Should we care about the partition type right now?
351 * XXX What about multiple partitions?
352 */
353 mvds_start = avdp.main_vds_ex.loc;
354 mvds_end = mvds_start + (avdp.main_vds_ex.len - 1) / bsize;
355 for (sector = mvds_start; sector < mvds_end; sector++) {
356 if ((error = bread(devvp, sector * btodb(bsize), bsize,
357 NOCRED, &bp)) != 0) {
358 printf("Can't read sector %d of VDS\n", sector);
359 goto bail;
360 }
361 lvd = (struct logvol_desc *)bp->b_data;
362 if (!udf_checktag(&lvd->tag, TAGID_LOGVOL)) {
363 udfmp->bsize = lvd->lb_size;
364 udfmp->bmask = udfmp->bsize - 1;
365 udfmp->bshift = ffs(udfmp->bsize) - 1;
366 fsd_part = lvd->_lvd_use.fsd_loc.loc.part_num;
367 fsd_offset = lvd->_lvd_use.fsd_loc.loc.lb_num;
368 if (udf_find_partmaps(udfmp, lvd))
369 break;
370 logvol_found = 1;
371 }
372 pd = (struct part_desc *)bp->b_data;
373 if (!udf_checktag(&pd->tag, TAGID_PARTITION)) {
374 part_found = 1;
375 part_num = pd->part_num;
376 udfmp->part_len = pd->part_len;
377 udfmp->part_start = pd->start_loc;
378 }
379
380 brelse(bp);
381 bp = NULL;
382 if ((part_found) && (logvol_found))
383 break;
384 }
385
386 if (!part_found || !logvol_found) {
387 error = EINVAL;
388 goto bail;
389 }
390
391 if (fsd_part != part_num) {
392 printf("FSD does not lie within the partition!\n");
393 error = EINVAL;
394 goto bail;
395 }
396
397
398 /*
399 * Grab the Fileset Descriptor
400 * Thanks to Chuck McCrobie <mccrobie@cablespeed.com> for pointing
401 * me in the right direction here.
402 */
403 sector = udfmp->part_start + fsd_offset;
404 if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
405 printf("Cannot read sector %d of FSD\n", sector);
406 goto bail;
407 }
408 fsd = (struct fileset_desc *)bp->b_data;
409 if (!udf_checktag(&fsd->tag, TAGID_FSD)) {
410 fsd_found = 1;
411 bcopy(&fsd->rootdir_icb, &udfmp->root_icb,
412 sizeof(struct long_ad));
413 }
414
415 brelse(bp);
416 bp = NULL;
417
418 if (!fsd_found) {
419 printf("Couldn't find the fsd\n");
420 error = EINVAL;
421 goto bail;
422 }
423
424 /*
425 * Find the file entry for the root directory.
426 */
427 sector = udfmp->root_icb.loc.lb_num + udfmp->part_start;
428 size = udfmp->root_icb.len;
429 if ((error = udf_readlblks(udfmp, sector, size, &bp)) != 0) {
430 printf("Cannot read sector %d\n", sector);
431 goto bail;
432 }
433
434 root_fentry = (struct file_entry *)bp->b_data;
435 if ((error = udf_checktag(&root_fentry->tag, TAGID_FENTRY))) {
436 printf("Invalid root file entry!\n");
437 goto bail;
438 }
439
440 brelse(bp);
441 bp = NULL;
442
443 devvp->v_rdev->si_mountpoint = mp;
444
445 mtx_init(&udfmp->hash_mtx, "udf_hash", NULL, MTX_DEF);
446 udfmp->hashtbl = phashinit(UDF_HASHTBLSIZE, M_UDFMOUNT, &udfmp->hashsz);
447
448 return 0;
449
450bail:
451 if (udfmp != NULL)
452 FREE(udfmp, M_UDFMOUNT);
453 if (bp != NULL)
454 brelse(bp);
455 if (needclose)
456 VOP_CLOSE(devvp, FREAD, NOCRED, td);
457 return error;
458};
459
460static int
461udf_unmount(struct mount *mp, int mntflags, struct thread *td)
462{
463 struct udf_mnt *udfmp;
464 int error, flags = 0;
465
466 udfmp = VFSTOUDFFS(mp);
467
468 if (mntflags & MNT_FORCE)
469 flags |= FORCECLOSE;
470
471 if ((error = vflush(mp, 0, flags)))
472 return (error);
473
474 udfmp->im_devvp->v_rdev->si_mountpoint = NULL;
475 error = VOP_CLOSE(udfmp->im_devvp, FREAD, NOCRED, td);
476 vrele(udfmp->im_devvp);
477
478 if (udfmp->s_table != NULL)
479 FREE(udfmp->s_table, M_UDFMOUNT);
480
481 if (udfmp->hashtbl != NULL)
482 FREE(udfmp->hashtbl, M_UDFMOUNT);
483
484 FREE(udfmp, M_UDFMOUNT);
485
486 mp->mnt_data = (qaddr_t)0;
487 mp->mnt_flag &= ~MNT_LOCAL;
488
489 return (0);
490}
491
492static int
493udf_root(struct mount *mp, struct vnode **vpp)
494{
495 struct udf_mnt *udfmp;
496 struct vnode *vp;
497 ino_t id;
498 int error;
499
500 udfmp = VFSTOUDFFS(mp);
501
502 id = udf_getid(&udfmp->root_icb);
503
504 error = udf_vget(mp, id, LK_EXCLUSIVE, vpp);
505 if (error)
506 return error;
507
508 vp = *vpp;
509 vp->v_vflag |= VV_ROOT;
510 udfmp->root_vp = vp;
511
512 return (0);
513}
514
515static int
516udf_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
517{
518 struct udf_mnt *udfmp;
519
520 udfmp = VFSTOUDFFS(mp);
521
522 sbp->f_bsize = udfmp->bsize;
523 sbp->f_iosize = udfmp->bsize;
524 sbp->f_blocks = udfmp->part_len;
525 sbp->f_bfree = 0;
526 sbp->f_bavail = 0;
527 sbp->f_files = 0;
528 sbp->f_ffree = 0;
529 if (sbp != &mp->mnt_stat) {
530 sbp->f_type = mp->mnt_vfc->vfc_typenum;
531 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
532 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
533 }
534
535 return 0;
536}
537
538int
539udf_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
540{
541 struct buf *bp;
542 struct vnode *devvp;
543 struct udf_mnt *udfmp;
544 struct thread *td;
545 struct vnode *vp;
546 struct udf_node *unode;
547 struct file_entry *fe;
548 int error, sector, size;
549
550 td = curthread;
551 udfmp = VFSTOUDFFS(mp);
552
553 /* See if we already have this in the cache */
554 if ((error = udf_hashlookup(udfmp, ino, flags, vpp)) != 0)
555 return (error);
556 if (*vpp != NULL) {
557 return (0);
558 }
559
560 /*
561 * Allocate memory and check the tag id's before grabbing a new
562 * vnode, since it's hard to roll back if there is a problem.
563 */
564 unode = uma_zalloc(udf_zone_node, M_WAITOK);
565 if (unode == NULL) {
566 printf("Cannot allocate udf node\n");
567 return (ENOMEM);
568 }
569
570 /*
571 * Copy in the file entry. Per the spec, the size can only be 1 block.
572 */
573 sector = ino + udfmp->part_start;
574 devvp = udfmp->im_devvp;
575 if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
576 printf("Cannot read sector %d\n", sector);
577 uma_zfree(udf_zone_node, unode);
578 return (error);
579 }
580
581 fe = (struct file_entry *)bp->b_data;
582 if (udf_checktag(&fe->tag, TAGID_FENTRY)) {
583 printf("Invalid file entry!\n");
584 uma_zfree(udf_zone_node, unode);
585 brelse(bp);
586 return (ENOMEM);
587 }
588 size = UDF_FENTRY_SIZE + fe->l_ea + fe->l_ad;
589 MALLOC(unode->fentry, struct file_entry *, size, M_UDFFENTRY,
590 M_NOWAIT | M_ZERO);
591 if (unode->fentry == NULL) {
592 printf("Cannot allocate file entry block\n");
593 uma_zfree(udf_zone_node, unode);
594 brelse(bp);
595 return (ENOMEM);
596 }
597
598 bcopy(bp->b_data, unode->fentry, size);
599
600 brelse(bp);
601 bp = NULL;
602
603 if ((error = udf_allocv(mp, &vp, td))) {
604 printf("Error from udf_allocv\n");
605 uma_zfree(udf_zone_node, unode);
606 return (error);
607 }
608
609 unode->i_vnode = vp;
610 unode->hash_id = ino;
611 unode->i_devvp = udfmp->im_devvp;
612 unode->i_dev = udfmp->im_dev;
613 unode->udfmp = udfmp;
614 vp->v_data = unode;
615 VREF(udfmp->im_devvp);
616 udf_hashins(unode);
617
618 switch (unode->fentry->icbtag.file_type) {
619 default:
620 vp->v_type = VBAD;
621 break;
622 case 4:
623 vp->v_type = VDIR;
624 break;
625 case 5:
626 vp->v_type = VREG;
627 break;
628 case 6:
629 vp->v_type = VBLK;
630 break;
631 case 7:
632 vp->v_type = VCHR;
633 break;
634 case 9:
635 vp->v_type = VFIFO;
636 break;
637 case 10:
638 vp->v_type = VSOCK;
639 break;
640 case 12:
641 vp->v_type = VLNK;
642 break;
643 }
644 *vpp = vp;
645
646 return (0);
647}
648
649struct ifid {
27 */
28
29/* udf_vfsops.c */
30/* Implement the VFS side of things */
31
32/*
33 * Ok, here's how it goes. The UDF specs are pretty clear on how each data
34 * structure is made up, but not very clear on how they relate to each other.
35 * Here is the skinny... This demostrates a filesystem with one file in the
36 * root directory. Subdirectories are treated just as normal files, but they
37 * have File Id Descriptors of their children as their file data. As for the
38 * Anchor Volume Descriptor Pointer, it can exist in two of the following three
39 * places: sector 256, sector n (the max sector of the disk), or sector
40 * n - 256. It's a pretty good bet that one will exist at sector 256 though.
41 * One caveat is unclosed CD media. For that, sector 256 cannot be written,
42 * so the Anchor Volume Descriptor Pointer can exist at sector 512 until the
43 * media is closed.
44 *
45 * Sector:
46 * 256:
47 * n: Anchor Volume Descriptor Pointer
48 * n - 256: |
49 * |
50 * |-->Main Volume Descriptor Sequence
51 * | |
52 * | |
53 * | |-->Logical Volume Descriptor
54 * | |
55 * |-->Partition Descriptor |
56 * | |
57 * | |
58 * |-->Fileset Descriptor
59 * |
60 * |
61 * |-->Root Dir File Entry
62 * |
63 * |
64 * |-->File data:
65 * File Id Descriptor
66 * |
67 * |
68 * |-->File Entry
69 * |
70 * |
71 * |-->File data
72 */
73#include <sys/types.h>
74#include <sys/param.h>
75#include <sys/systm.h>
76#include <sys/uio.h>
77#include <sys/bio.h>
78#include <sys/buf.h>
79#include <sys/conf.h>
80#include <sys/dirent.h>
81#include <sys/fcntl.h>
82#include <sys/kernel.h>
83#include <sys/malloc.h>
84#include <sys/mount.h>
85#include <sys/namei.h>
86#include <sys/proc.h>
87#include <sys/queue.h>
88#include <sys/vnode.h>
89
90#include <vm/uma.h>
91
92#include <fs/udf/ecma167-udf.h>
93#include <fs/udf/udf.h>
94#include <fs/udf/osta.h>
95
96MALLOC_DEFINE(M_UDFMOUNT, "UDF mount", "UDF mount structure");
97MALLOC_DEFINE(M_UDFFENTRY, "UDF fentry", "UDF file entry structure");
98
99/* Zones */
100uma_zone_t udf_zone_trans = NULL;
101uma_zone_t udf_zone_node = NULL;
102uma_zone_t udf_zone_ds = NULL;
103
104static vfs_init_t udf_init;
105static vfs_uninit_t udf_uninit;
106static vfs_nmount_t udf_mount;
107static vfs_root_t udf_root;
108static vfs_statfs_t udf_statfs;
109static vfs_unmount_t udf_unmount;
110static vfs_fhtovp_t udf_fhtovp;
111static vfs_vptofh_t udf_vptofh;
112
113static int udf_find_partmaps(struct udf_mnt *, struct logvol_desc *);
114
115static struct vfsops udf_vfsops = {
116 .vfs_fhtovp = udf_fhtovp,
117 .vfs_init = udf_init,
118 .vfs_nmount = udf_mount,
119 .vfs_root = udf_root,
120 .vfs_statfs = udf_statfs,
121 .vfs_uninit = udf_uninit,
122 .vfs_unmount = udf_unmount,
123 .vfs_vget = udf_vget,
124 .vfs_vptofh = udf_vptofh,
125};
126VFS_SET(udf_vfsops, udf, VFCF_READONLY);
127
128static int udf_mountfs(struct vnode *, struct mount *, struct thread *);
129
130static int
131udf_init(struct vfsconf *foo)
132{
133
134 /*
135 * This code used to pre-allocate a certain number of pages for each
136 * pool, reducing the need to grow the zones later on. UMA doesn't
137 * advertise any such functionality, unfortunately =-<
138 */
139 udf_zone_trans = uma_zcreate("UDF translation buffer, zone", MAXNAMLEN *
140 sizeof(unicode_t), NULL, NULL, NULL, NULL, 0, 0);
141
142 udf_zone_node = uma_zcreate("UDF Node zone", sizeof(struct udf_node),
143 NULL, NULL, NULL, NULL, 0, 0);
144
145 udf_zone_ds = uma_zcreate("UDF Dirstream zone",
146 sizeof(struct udf_dirstream), NULL, NULL, NULL, NULL, 0, 0);
147
148 if ((udf_zone_node == NULL) || (udf_zone_trans == NULL) ||
149 (udf_zone_ds == NULL)) {
150 printf("Cannot create allocation zones.\n");
151 return (ENOMEM);
152 }
153
154 return 0;
155}
156
157static int
158udf_uninit(struct vfsconf *foo)
159{
160
161 if (udf_zone_trans != NULL) {
162 uma_zdestroy(udf_zone_trans);
163 udf_zone_trans = NULL;
164 }
165
166 if (udf_zone_node != NULL) {
167 uma_zdestroy(udf_zone_node);
168 udf_zone_node = NULL;
169 }
170
171 if (udf_zone_ds != NULL) {
172 uma_zdestroy(udf_zone_ds);
173 udf_zone_ds = NULL;
174 }
175
176 return (0);
177}
178
179static int
180udf_mount(struct mount *mp, struct nameidata *ndp, struct thread *td)
181{
182 struct vnode *devvp; /* vnode of the mount device */
183 struct udf_mnt *imp = 0;
184 struct export_args *export;
185 struct vfsoptlist *opts;
186 char *fspec;
187 size_t size;
188 int error, len;
189
190 opts = mp->mnt_optnew;
191
192 if ((mp->mnt_flag & MNT_RDONLY) == 0)
193 return (EROFS);
194
195 /*
196 * No root filesystem support. Probably not a big deal, since the
197 * bootloader doesn't understand UDF.
198 */
199 if (mp->mnt_flag & MNT_ROOTFS)
200 return (ENOTSUP);
201
202 fspec = NULL;
203 error = vfs_getopt(opts, "from", (void **)&fspec, &len);
204 if (!error && fspec[len - 1] != '\0')
205 return (EINVAL);
206
207 if (mp->mnt_flag & MNT_UPDATE) {
208 imp = VFSTOUDFFS(mp);
209 if (fspec == NULL) {
210 error = vfs_getopt(opts, "export", (void **)&export,
211 &len);
212 if (error || len != sizeof(struct export_args))
213 return (EINVAL);
214 return (vfs_export(mp, export));
215 }
216 }
217
218 /* Check that the mount device exists */
219 if (fspec == NULL)
220 return (EINVAL);
221 NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fspec, td);
222 if ((error = namei(ndp)))
223 return (error);
224 NDFREE(ndp, NDF_ONLY_PNBUF);
225 devvp = ndp->ni_vp;
226
227 if (vn_isdisk(devvp, &error) == 0) {
228 vrele(devvp);
229 return (error);
230 }
231
232 /* Check the access rights on the mount device */
233 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
234 error = VOP_ACCESS(devvp, VREAD, td->td_ucred, td);
235 if (error)
236 error = suser(td);
237 if (error) {
238 vput(devvp);
239 return (error);
240 }
241 VOP_UNLOCK(devvp, 0, td);
242
243 if ((error = udf_mountfs(devvp, mp, td))) {
244 vrele(devvp);
245 return (error);
246 }
247
248 imp = VFSTOUDFFS(mp);
249 copystr(fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
250 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
251 udf_statfs(mp, &mp->mnt_stat, td);
252 return 0;
253};
254
255/*
256 * Check the descriptor tag for both the correct id and correct checksum.
257 * Return zero if all is good, EINVAL if not.
258 */
259int
260udf_checktag(struct desc_tag *tag, uint16_t id)
261{
262 uint8_t *itag;
263 uint8_t i, cksum = 0;
264
265 itag = (uint8_t *)tag;
266
267 if (tag->id != id)
268 return (EINVAL);
269
270 for (i = 0; i < 15; i++)
271 cksum = cksum + itag[i];
272 cksum = cksum - itag[4];
273
274 if (cksum == tag->cksum)
275 return (0);
276
277 return (EINVAL);
278}
279
280static int
281udf_mountfs(struct vnode *devvp, struct mount *mp, struct thread *td) {
282 struct buf *bp = NULL;
283 struct anchor_vdp avdp;
284 struct udf_mnt *udfmp = NULL;
285 struct part_desc *pd;
286 struct logvol_desc *lvd;
287 struct fileset_desc *fsd;
288 struct file_entry *root_fentry;
289 uint32_t sector, size, mvds_start, mvds_end;
290 uint32_t fsd_offset = 0;
291 uint16_t part_num = 0, fsd_part = 0;
292 int error = EINVAL, needclose = 0;
293 int logvol_found = 0, part_found = 0, fsd_found = 0;
294 int bsize;
295
296 /*
297 * Disallow multiple mounts of the same device. Flush the buffer
298 * cache for the device.
299 */
300 if ((error = vfs_mountedon(devvp)))
301 return (error);
302 if (vcount(devvp) > 1)
303 return (EBUSY);
304 if ((error = vinvalbuf(devvp, V_SAVE, td->td_ucred, td, 0, 0)))
305 return (error);
306
307 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
308 error = VOP_OPEN(devvp, FREAD, FSCRED, td, -1);
309 VOP_UNLOCK(devvp, 0, td);
310 if (error)
311 return error;
312 needclose = 1;
313
314 MALLOC(udfmp, struct udf_mnt *, sizeof(struct udf_mnt), M_UDFMOUNT,
315 M_NOWAIT | M_ZERO);
316 if (udfmp == NULL) {
317 printf("Cannot allocate UDF mount struct\n");
318 error = ENOMEM;
319 goto bail;
320 }
321
322 mp->mnt_data = (qaddr_t)udfmp;
323 mp->mnt_stat.f_fsid.val[0] = dev2udev(devvp->v_rdev);
324 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
325 mp->mnt_flag |= MNT_LOCAL;
326 udfmp->im_mountp = mp;
327 udfmp->im_dev = devvp->v_rdev;
328 udfmp->im_devvp = devvp;
329
330 bsize = 2048; /* XXX Should probe the media for it's size */
331
332 /*
333 * Get the Anchor Volume Descriptor Pointer from sector 256.
334 * XXX Should also check sector n - 256, n, and 512.
335 */
336 sector = 256;
337 if ((error = bread(devvp, sector * btodb(bsize), bsize, NOCRED,
338 &bp)) != 0)
339 goto bail;
340 if ((error = udf_checktag((struct desc_tag *)bp->b_data, TAGID_ANCHOR)))
341 goto bail;
342
343 bcopy(bp->b_data, &avdp, sizeof(struct anchor_vdp));
344 brelse(bp);
345 bp = NULL;
346
347 /*
348 * Extract the Partition Descriptor and Logical Volume Descriptor
349 * from the Volume Descriptor Sequence.
350 * XXX Should we care about the partition type right now?
351 * XXX What about multiple partitions?
352 */
353 mvds_start = avdp.main_vds_ex.loc;
354 mvds_end = mvds_start + (avdp.main_vds_ex.len - 1) / bsize;
355 for (sector = mvds_start; sector < mvds_end; sector++) {
356 if ((error = bread(devvp, sector * btodb(bsize), bsize,
357 NOCRED, &bp)) != 0) {
358 printf("Can't read sector %d of VDS\n", sector);
359 goto bail;
360 }
361 lvd = (struct logvol_desc *)bp->b_data;
362 if (!udf_checktag(&lvd->tag, TAGID_LOGVOL)) {
363 udfmp->bsize = lvd->lb_size;
364 udfmp->bmask = udfmp->bsize - 1;
365 udfmp->bshift = ffs(udfmp->bsize) - 1;
366 fsd_part = lvd->_lvd_use.fsd_loc.loc.part_num;
367 fsd_offset = lvd->_lvd_use.fsd_loc.loc.lb_num;
368 if (udf_find_partmaps(udfmp, lvd))
369 break;
370 logvol_found = 1;
371 }
372 pd = (struct part_desc *)bp->b_data;
373 if (!udf_checktag(&pd->tag, TAGID_PARTITION)) {
374 part_found = 1;
375 part_num = pd->part_num;
376 udfmp->part_len = pd->part_len;
377 udfmp->part_start = pd->start_loc;
378 }
379
380 brelse(bp);
381 bp = NULL;
382 if ((part_found) && (logvol_found))
383 break;
384 }
385
386 if (!part_found || !logvol_found) {
387 error = EINVAL;
388 goto bail;
389 }
390
391 if (fsd_part != part_num) {
392 printf("FSD does not lie within the partition!\n");
393 error = EINVAL;
394 goto bail;
395 }
396
397
398 /*
399 * Grab the Fileset Descriptor
400 * Thanks to Chuck McCrobie <mccrobie@cablespeed.com> for pointing
401 * me in the right direction here.
402 */
403 sector = udfmp->part_start + fsd_offset;
404 if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
405 printf("Cannot read sector %d of FSD\n", sector);
406 goto bail;
407 }
408 fsd = (struct fileset_desc *)bp->b_data;
409 if (!udf_checktag(&fsd->tag, TAGID_FSD)) {
410 fsd_found = 1;
411 bcopy(&fsd->rootdir_icb, &udfmp->root_icb,
412 sizeof(struct long_ad));
413 }
414
415 brelse(bp);
416 bp = NULL;
417
418 if (!fsd_found) {
419 printf("Couldn't find the fsd\n");
420 error = EINVAL;
421 goto bail;
422 }
423
424 /*
425 * Find the file entry for the root directory.
426 */
427 sector = udfmp->root_icb.loc.lb_num + udfmp->part_start;
428 size = udfmp->root_icb.len;
429 if ((error = udf_readlblks(udfmp, sector, size, &bp)) != 0) {
430 printf("Cannot read sector %d\n", sector);
431 goto bail;
432 }
433
434 root_fentry = (struct file_entry *)bp->b_data;
435 if ((error = udf_checktag(&root_fentry->tag, TAGID_FENTRY))) {
436 printf("Invalid root file entry!\n");
437 goto bail;
438 }
439
440 brelse(bp);
441 bp = NULL;
442
443 devvp->v_rdev->si_mountpoint = mp;
444
445 mtx_init(&udfmp->hash_mtx, "udf_hash", NULL, MTX_DEF);
446 udfmp->hashtbl = phashinit(UDF_HASHTBLSIZE, M_UDFMOUNT, &udfmp->hashsz);
447
448 return 0;
449
450bail:
451 if (udfmp != NULL)
452 FREE(udfmp, M_UDFMOUNT);
453 if (bp != NULL)
454 brelse(bp);
455 if (needclose)
456 VOP_CLOSE(devvp, FREAD, NOCRED, td);
457 return error;
458};
459
460static int
461udf_unmount(struct mount *mp, int mntflags, struct thread *td)
462{
463 struct udf_mnt *udfmp;
464 int error, flags = 0;
465
466 udfmp = VFSTOUDFFS(mp);
467
468 if (mntflags & MNT_FORCE)
469 flags |= FORCECLOSE;
470
471 if ((error = vflush(mp, 0, flags)))
472 return (error);
473
474 udfmp->im_devvp->v_rdev->si_mountpoint = NULL;
475 error = VOP_CLOSE(udfmp->im_devvp, FREAD, NOCRED, td);
476 vrele(udfmp->im_devvp);
477
478 if (udfmp->s_table != NULL)
479 FREE(udfmp->s_table, M_UDFMOUNT);
480
481 if (udfmp->hashtbl != NULL)
482 FREE(udfmp->hashtbl, M_UDFMOUNT);
483
484 FREE(udfmp, M_UDFMOUNT);
485
486 mp->mnt_data = (qaddr_t)0;
487 mp->mnt_flag &= ~MNT_LOCAL;
488
489 return (0);
490}
491
492static int
493udf_root(struct mount *mp, struct vnode **vpp)
494{
495 struct udf_mnt *udfmp;
496 struct vnode *vp;
497 ino_t id;
498 int error;
499
500 udfmp = VFSTOUDFFS(mp);
501
502 id = udf_getid(&udfmp->root_icb);
503
504 error = udf_vget(mp, id, LK_EXCLUSIVE, vpp);
505 if (error)
506 return error;
507
508 vp = *vpp;
509 vp->v_vflag |= VV_ROOT;
510 udfmp->root_vp = vp;
511
512 return (0);
513}
514
515static int
516udf_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
517{
518 struct udf_mnt *udfmp;
519
520 udfmp = VFSTOUDFFS(mp);
521
522 sbp->f_bsize = udfmp->bsize;
523 sbp->f_iosize = udfmp->bsize;
524 sbp->f_blocks = udfmp->part_len;
525 sbp->f_bfree = 0;
526 sbp->f_bavail = 0;
527 sbp->f_files = 0;
528 sbp->f_ffree = 0;
529 if (sbp != &mp->mnt_stat) {
530 sbp->f_type = mp->mnt_vfc->vfc_typenum;
531 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
532 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
533 }
534
535 return 0;
536}
537
538int
539udf_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
540{
541 struct buf *bp;
542 struct vnode *devvp;
543 struct udf_mnt *udfmp;
544 struct thread *td;
545 struct vnode *vp;
546 struct udf_node *unode;
547 struct file_entry *fe;
548 int error, sector, size;
549
550 td = curthread;
551 udfmp = VFSTOUDFFS(mp);
552
553 /* See if we already have this in the cache */
554 if ((error = udf_hashlookup(udfmp, ino, flags, vpp)) != 0)
555 return (error);
556 if (*vpp != NULL) {
557 return (0);
558 }
559
560 /*
561 * Allocate memory and check the tag id's before grabbing a new
562 * vnode, since it's hard to roll back if there is a problem.
563 */
564 unode = uma_zalloc(udf_zone_node, M_WAITOK);
565 if (unode == NULL) {
566 printf("Cannot allocate udf node\n");
567 return (ENOMEM);
568 }
569
570 /*
571 * Copy in the file entry. Per the spec, the size can only be 1 block.
572 */
573 sector = ino + udfmp->part_start;
574 devvp = udfmp->im_devvp;
575 if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
576 printf("Cannot read sector %d\n", sector);
577 uma_zfree(udf_zone_node, unode);
578 return (error);
579 }
580
581 fe = (struct file_entry *)bp->b_data;
582 if (udf_checktag(&fe->tag, TAGID_FENTRY)) {
583 printf("Invalid file entry!\n");
584 uma_zfree(udf_zone_node, unode);
585 brelse(bp);
586 return (ENOMEM);
587 }
588 size = UDF_FENTRY_SIZE + fe->l_ea + fe->l_ad;
589 MALLOC(unode->fentry, struct file_entry *, size, M_UDFFENTRY,
590 M_NOWAIT | M_ZERO);
591 if (unode->fentry == NULL) {
592 printf("Cannot allocate file entry block\n");
593 uma_zfree(udf_zone_node, unode);
594 brelse(bp);
595 return (ENOMEM);
596 }
597
598 bcopy(bp->b_data, unode->fentry, size);
599
600 brelse(bp);
601 bp = NULL;
602
603 if ((error = udf_allocv(mp, &vp, td))) {
604 printf("Error from udf_allocv\n");
605 uma_zfree(udf_zone_node, unode);
606 return (error);
607 }
608
609 unode->i_vnode = vp;
610 unode->hash_id = ino;
611 unode->i_devvp = udfmp->im_devvp;
612 unode->i_dev = udfmp->im_dev;
613 unode->udfmp = udfmp;
614 vp->v_data = unode;
615 VREF(udfmp->im_devvp);
616 udf_hashins(unode);
617
618 switch (unode->fentry->icbtag.file_type) {
619 default:
620 vp->v_type = VBAD;
621 break;
622 case 4:
623 vp->v_type = VDIR;
624 break;
625 case 5:
626 vp->v_type = VREG;
627 break;
628 case 6:
629 vp->v_type = VBLK;
630 break;
631 case 7:
632 vp->v_type = VCHR;
633 break;
634 case 9:
635 vp->v_type = VFIFO;
636 break;
637 case 10:
638 vp->v_type = VSOCK;
639 break;
640 case 12:
641 vp->v_type = VLNK;
642 break;
643 }
644 *vpp = vp;
645
646 return (0);
647}
648
649struct ifid {
650 ushort ifid_len;
651 ushort ifid_pad;
650 u_short ifid_len;
651 u_short ifid_pad;
652 int ifid_ino;
653 long ifid_start;
654};
655
656static int
657udf_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
658{
659 struct ifid *ifhp;
660 struct vnode *nvp;
661 int error;
662
663 ifhp = (struct ifid *)fhp;
664
665 if ((error = VFS_VGET(mp, ifhp->ifid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
666 *vpp = NULLVP;
667 return (error);
668 }
669
670 *vpp = nvp;
671 return (0);
672}
673
674static int
675udf_vptofh (struct vnode *vp, struct fid *fhp)
676{
677 struct udf_node *node;
678 struct ifid *ifhp;
679
680 node = VTON(vp);
681 ifhp = (struct ifid *)fhp;
682 ifhp->ifid_len = sizeof(struct ifid);
683 ifhp->ifid_ino = node->hash_id;
684
685 return (0);
686}
687
688static int
689udf_find_partmaps(struct udf_mnt *udfmp, struct logvol_desc *lvd)
690{
691 union udf_pmap *pmap;
692 struct part_map_spare *pms;
693 struct regid *pmap_id;
694 struct buf *bp;
695 unsigned char regid_id[UDF_REGID_ID_SIZE + 1];
696 int i, ptype, psize, error;
697
698 for (i = 0; i < lvd->n_pm; i++) {
699 pmap = (union udf_pmap *)&lvd->maps[i * UDF_PMAP_SIZE];
700 ptype = pmap->data[0];
701 psize = pmap->data[1];
702 if (((ptype != 1) && (ptype != 2)) ||
703 ((psize != UDF_PMAP_SIZE) && (psize != 6))) {
704 printf("Invalid partition map found\n");
705 return (1);
706 }
707
708 if (ptype == 1) {
709 /* Type 1 map. We don't care */
710 continue;
711 }
712
713 /* Type 2 map. Gotta find out the details */
714 pmap_id = (struct regid *)&pmap->data[4];
715 bzero(&regid_id[0], UDF_REGID_ID_SIZE);
716 bcopy(&pmap_id->id[0], &regid_id[0], UDF_REGID_ID_SIZE);
717
718 if (bcmp(&regid_id[0], "*UDF Sparable Partition",
719 UDF_REGID_ID_SIZE)) {
720 printf("Unsupported partition map: %s\n", &regid_id[0]);
721 return (1);
722 }
723
724 pms = &pmap->pms;
725 MALLOC(udfmp->s_table, struct udf_sparing_table *, pms->st_size,
726 M_UDFMOUNT, M_NOWAIT | M_ZERO);
727 if (udfmp->s_table == NULL)
728 return (ENOMEM);
729
730 /* Calculate the number of sectors per packet. */
731 /* XXX Logical or physical? */
732 udfmp->p_sectors = pms->packet_len / udfmp->bsize;
733
734 /*
735 * XXX If reading the first Sparing Table fails, should look
736 * for another table.
737 */
738 if ((error = udf_readlblks(udfmp, pms->st_loc[0], pms->st_size,
739 &bp)) != 0) {
740 printf("Failed to read Sparing Table at sector %d\n",
741 pms->st_loc[0]);
742 return (error);
743 }
744 bcopy(bp->b_data, udfmp->s_table, pms->st_size);
745 brelse(bp);
746
747 if (udf_checktag(&udfmp->s_table->tag, 0)) {
748 printf("Invalid sparing table found\n");
749 return (EINVAL);
750 }
751
752 /* See how many valid entries there are here. The list is
753 * supposed to be sorted. 0xfffffff0 and higher are not valid
754 */
755 for (i = 0; i < udfmp->s_table->rt_l; i++) {
756 udfmp->s_table_entries = i;
757 if (udfmp->s_table->entries[i].org >= 0xfffffff0)
758 break;
759 }
760 }
761
762 return (0);
763}
652 int ifid_ino;
653 long ifid_start;
654};
655
656static int
657udf_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
658{
659 struct ifid *ifhp;
660 struct vnode *nvp;
661 int error;
662
663 ifhp = (struct ifid *)fhp;
664
665 if ((error = VFS_VGET(mp, ifhp->ifid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
666 *vpp = NULLVP;
667 return (error);
668 }
669
670 *vpp = nvp;
671 return (0);
672}
673
674static int
675udf_vptofh (struct vnode *vp, struct fid *fhp)
676{
677 struct udf_node *node;
678 struct ifid *ifhp;
679
680 node = VTON(vp);
681 ifhp = (struct ifid *)fhp;
682 ifhp->ifid_len = sizeof(struct ifid);
683 ifhp->ifid_ino = node->hash_id;
684
685 return (0);
686}
687
688static int
689udf_find_partmaps(struct udf_mnt *udfmp, struct logvol_desc *lvd)
690{
691 union udf_pmap *pmap;
692 struct part_map_spare *pms;
693 struct regid *pmap_id;
694 struct buf *bp;
695 unsigned char regid_id[UDF_REGID_ID_SIZE + 1];
696 int i, ptype, psize, error;
697
698 for (i = 0; i < lvd->n_pm; i++) {
699 pmap = (union udf_pmap *)&lvd->maps[i * UDF_PMAP_SIZE];
700 ptype = pmap->data[0];
701 psize = pmap->data[1];
702 if (((ptype != 1) && (ptype != 2)) ||
703 ((psize != UDF_PMAP_SIZE) && (psize != 6))) {
704 printf("Invalid partition map found\n");
705 return (1);
706 }
707
708 if (ptype == 1) {
709 /* Type 1 map. We don't care */
710 continue;
711 }
712
713 /* Type 2 map. Gotta find out the details */
714 pmap_id = (struct regid *)&pmap->data[4];
715 bzero(&regid_id[0], UDF_REGID_ID_SIZE);
716 bcopy(&pmap_id->id[0], &regid_id[0], UDF_REGID_ID_SIZE);
717
718 if (bcmp(&regid_id[0], "*UDF Sparable Partition",
719 UDF_REGID_ID_SIZE)) {
720 printf("Unsupported partition map: %s\n", &regid_id[0]);
721 return (1);
722 }
723
724 pms = &pmap->pms;
725 MALLOC(udfmp->s_table, struct udf_sparing_table *, pms->st_size,
726 M_UDFMOUNT, M_NOWAIT | M_ZERO);
727 if (udfmp->s_table == NULL)
728 return (ENOMEM);
729
730 /* Calculate the number of sectors per packet. */
731 /* XXX Logical or physical? */
732 udfmp->p_sectors = pms->packet_len / udfmp->bsize;
733
734 /*
735 * XXX If reading the first Sparing Table fails, should look
736 * for another table.
737 */
738 if ((error = udf_readlblks(udfmp, pms->st_loc[0], pms->st_size,
739 &bp)) != 0) {
740 printf("Failed to read Sparing Table at sector %d\n",
741 pms->st_loc[0]);
742 return (error);
743 }
744 bcopy(bp->b_data, udfmp->s_table, pms->st_size);
745 brelse(bp);
746
747 if (udf_checktag(&udfmp->s_table->tag, 0)) {
748 printf("Invalid sparing table found\n");
749 return (EINVAL);
750 }
751
752 /* See how many valid entries there are here. The list is
753 * supposed to be sorted. 0xfffffff0 and higher are not valid
754 */
755 for (i = 0; i < udfmp->s_table->rt_l; i++) {
756 udfmp->s_table_entries = i;
757 if (udfmp->s_table->entries[i].org >= 0xfffffff0)
758 break;
759 }
760 }
761
762 return (0);
763}