udf_subr.c revision 1.42 1 1.42 reinoud /* $NetBSD: udf_subr.c,v 1.42 2007/11/27 18:10:42 reinoud Exp $ */
2 1.1 reinoud
3 1.1 reinoud /*
4 1.1 reinoud * Copyright (c) 2006 Reinoud Zandijk
5 1.1 reinoud * All rights reserved.
6 1.1 reinoud *
7 1.1 reinoud * Redistribution and use in source and binary forms, with or without
8 1.1 reinoud * modification, are permitted provided that the following conditions
9 1.1 reinoud * are met:
10 1.1 reinoud * 1. Redistributions of source code must retain the above copyright
11 1.1 reinoud * notice, this list of conditions and the following disclaimer.
12 1.1 reinoud * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 reinoud * notice, this list of conditions and the following disclaimer in the
14 1.1 reinoud * documentation and/or other materials provided with the distribution.
15 1.1 reinoud * 3. All advertising materials mentioning features or use of this software
16 1.1 reinoud * must display the following acknowledgement:
17 1.1 reinoud * This product includes software developed for the
18 1.1 reinoud * NetBSD Project. See http://www.NetBSD.org/ for
19 1.1 reinoud * information about NetBSD.
20 1.1 reinoud * 4. The name of the author may not be used to endorse or promote products
21 1.1 reinoud * derived from this software without specific prior written permission.
22 1.1 reinoud *
23 1.1 reinoud * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 1.1 reinoud * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.1 reinoud * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.1 reinoud * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 1.1 reinoud * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 1.1 reinoud * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 1.1 reinoud * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 1.1 reinoud * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 1.1 reinoud * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 1.1 reinoud * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.1 reinoud *
34 1.1 reinoud */
35 1.1 reinoud
36 1.1 reinoud
37 1.1 reinoud #include <sys/cdefs.h>
38 1.1 reinoud #ifndef lint
39 1.42 reinoud __RCSID("$NetBSD: udf_subr.c,v 1.42 2007/11/27 18:10:42 reinoud Exp $");
40 1.1 reinoud #endif /* not lint */
41 1.1 reinoud
42 1.1 reinoud
43 1.1 reinoud #if defined(_KERNEL_OPT)
44 1.1 reinoud #include "opt_quota.h"
45 1.1 reinoud #include "opt_compat_netbsd.h"
46 1.1 reinoud #endif
47 1.1 reinoud
48 1.1 reinoud #include <sys/param.h>
49 1.1 reinoud #include <sys/systm.h>
50 1.1 reinoud #include <sys/sysctl.h>
51 1.1 reinoud #include <sys/namei.h>
52 1.1 reinoud #include <sys/proc.h>
53 1.1 reinoud #include <sys/kernel.h>
54 1.1 reinoud #include <sys/vnode.h>
55 1.1 reinoud #include <miscfs/genfs/genfs_node.h>
56 1.1 reinoud #include <sys/mount.h>
57 1.1 reinoud #include <sys/buf.h>
58 1.1 reinoud #include <sys/file.h>
59 1.1 reinoud #include <sys/device.h>
60 1.1 reinoud #include <sys/disklabel.h>
61 1.1 reinoud #include <sys/ioctl.h>
62 1.1 reinoud #include <sys/malloc.h>
63 1.1 reinoud #include <sys/dirent.h>
64 1.1 reinoud #include <sys/stat.h>
65 1.1 reinoud #include <sys/conf.h>
66 1.8 christos #include <sys/kauth.h>
67 1.30 reinoud #include <dev/clock_subr.h>
68 1.1 reinoud
69 1.1 reinoud #include <fs/udf/ecma167-udf.h>
70 1.1 reinoud #include <fs/udf/udf_mount.h>
71 1.1 reinoud
72 1.1 reinoud #include "udf.h"
73 1.1 reinoud #include "udf_subr.h"
74 1.1 reinoud #include "udf_bswap.h"
75 1.1 reinoud
76 1.1 reinoud
77 1.1 reinoud #define VTOI(vnode) ((struct udf_node *) vnode->v_data)
78 1.1 reinoud
79 1.1 reinoud
80 1.1 reinoud /* predefines */
81 1.1 reinoud
82 1.1 reinoud
83 1.1 reinoud #if 0
84 1.1 reinoud {
85 1.1 reinoud int i, j, dlen;
86 1.1 reinoud uint8_t *blob;
87 1.1 reinoud
88 1.1 reinoud blob = (uint8_t *) fid;
89 1.1 reinoud dlen = file_size - (*offset);
90 1.1 reinoud
91 1.1 reinoud printf("blob = %p\n", blob);
92 1.1 reinoud printf("dump of %d bytes\n", dlen);
93 1.1 reinoud
94 1.1 reinoud for (i = 0; i < dlen; i+ = 16) {
95 1.1 reinoud printf("%04x ", i);
96 1.1 reinoud for (j = 0; j < 16; j++) {
97 1.1 reinoud if (i+j < dlen) {
98 1.1 reinoud printf("%02x ", blob[i+j]);
99 1.1 reinoud } else {
100 1.1 reinoud printf(" ");
101 1.9 christos }
102 1.9 christos }
103 1.1 reinoud for (j = 0; j < 16; j++) {
104 1.1 reinoud if (i+j < dlen) {
105 1.1 reinoud if (blob[i+j]>32 && blob[i+j]! = 127) {
106 1.1 reinoud printf("%c", blob[i+j]);
107 1.1 reinoud } else {
108 1.1 reinoud printf(".");
109 1.9 christos }
110 1.9 christos }
111 1.9 christos }
112 1.1 reinoud printf("\n");
113 1.9 christos }
114 1.1 reinoud printf("\n");
115 1.9 christos }
116 1.1 reinoud Debugger();
117 1.1 reinoud #endif
118 1.1 reinoud
119 1.1 reinoud
120 1.1 reinoud /* --------------------------------------------------------------------- */
121 1.1 reinoud
122 1.1 reinoud /* STUB */
123 1.1 reinoud
124 1.1 reinoud static int
125 1.1 reinoud udf_bread(struct udf_mount *ump, uint32_t sector, struct buf **bpp)
126 1.1 reinoud {
127 1.1 reinoud int sector_size = ump->discinfo.sector_size;
128 1.1 reinoud int blks = sector_size / DEV_BSIZE;
129 1.1 reinoud
130 1.1 reinoud /* NOTE bread() checks if block is in cache or not */
131 1.1 reinoud return bread(ump->devvp, sector*blks, sector_size, NOCRED, bpp);
132 1.1 reinoud }
133 1.1 reinoud
134 1.1 reinoud
135 1.1 reinoud /* --------------------------------------------------------------------- */
136 1.1 reinoud
137 1.1 reinoud /*
138 1.1 reinoud * Check if the blob starts with a good UDF tag. Tags are protected by a
139 1.1 reinoud * checksum over the reader except one byte at position 4 that is the checksum
140 1.1 reinoud * itself.
141 1.1 reinoud */
142 1.1 reinoud
143 1.1 reinoud int
144 1.1 reinoud udf_check_tag(void *blob)
145 1.1 reinoud {
146 1.1 reinoud struct desc_tag *tag = blob;
147 1.1 reinoud uint8_t *pos, sum, cnt;
148 1.1 reinoud
149 1.1 reinoud /* check TAG header checksum */
150 1.1 reinoud pos = (uint8_t *) tag;
151 1.1 reinoud sum = 0;
152 1.1 reinoud
153 1.1 reinoud for(cnt = 0; cnt < 16; cnt++) {
154 1.1 reinoud if (cnt != 4)
155 1.1 reinoud sum += *pos;
156 1.1 reinoud pos++;
157 1.1 reinoud }
158 1.1 reinoud if (sum != tag->cksum) {
159 1.1 reinoud /* bad tag header checksum; this is not a valid tag */
160 1.1 reinoud return EINVAL;
161 1.1 reinoud }
162 1.1 reinoud
163 1.1 reinoud return 0;
164 1.1 reinoud }
165 1.1 reinoud
166 1.1 reinoud /* --------------------------------------------------------------------- */
167 1.1 reinoud
168 1.1 reinoud /*
169 1.1 reinoud * check tag payload will check descriptor CRC as specified.
170 1.1 reinoud * If the descriptor is too short, it will return EIO otherwise EINVAL.
171 1.1 reinoud */
172 1.1 reinoud
173 1.1 reinoud int
174 1.1 reinoud udf_check_tag_payload(void *blob, uint32_t max_length)
175 1.1 reinoud {
176 1.1 reinoud struct desc_tag *tag = blob;
177 1.1 reinoud uint16_t crc, crc_len;
178 1.1 reinoud
179 1.1 reinoud crc_len = udf_rw16(tag->desc_crc_len);
180 1.1 reinoud
181 1.1 reinoud /* check payload CRC if applicable */
182 1.1 reinoud if (crc_len == 0)
183 1.1 reinoud return 0;
184 1.1 reinoud
185 1.1 reinoud if (crc_len > max_length)
186 1.1 reinoud return EIO;
187 1.1 reinoud
188 1.1 reinoud crc = udf_cksum(((uint8_t *) tag) + UDF_DESC_TAG_LENGTH, crc_len);
189 1.1 reinoud if (crc != udf_rw16(tag->desc_crc)) {
190 1.1 reinoud /* bad payload CRC; this is a broken tag */
191 1.1 reinoud return EINVAL;
192 1.9 christos }
193 1.1 reinoud
194 1.1 reinoud return 0;
195 1.1 reinoud }
196 1.1 reinoud
197 1.1 reinoud /* --------------------------------------------------------------------- */
198 1.1 reinoud
199 1.1 reinoud int
200 1.1 reinoud udf_validate_tag_sum(void *blob)
201 1.1 reinoud {
202 1.1 reinoud struct desc_tag *tag = blob;
203 1.1 reinoud uint8_t *pos, sum, cnt;
204 1.1 reinoud
205 1.1 reinoud /* calculate TAG header checksum */
206 1.1 reinoud pos = (uint8_t *) tag;
207 1.1 reinoud sum = 0;
208 1.1 reinoud
209 1.1 reinoud for(cnt = 0; cnt < 16; cnt++) {
210 1.1 reinoud if (cnt != 4) sum += *pos;
211 1.1 reinoud pos++;
212 1.9 christos }
213 1.1 reinoud tag->cksum = sum; /* 8 bit */
214 1.1 reinoud
215 1.1 reinoud return 0;
216 1.1 reinoud }
217 1.1 reinoud
218 1.1 reinoud /* --------------------------------------------------------------------- */
219 1.1 reinoud
220 1.6 snj /* assumes sector number of descriptor to be saved already present */
221 1.1 reinoud
222 1.1 reinoud int
223 1.1 reinoud udf_validate_tag_and_crc_sums(void *blob)
224 1.1 reinoud {
225 1.1 reinoud struct desc_tag *tag = blob;
226 1.1 reinoud uint8_t *btag = (uint8_t *) tag;
227 1.1 reinoud uint16_t crc, crc_len;
228 1.1 reinoud
229 1.1 reinoud crc_len = udf_rw16(tag->desc_crc_len);
230 1.1 reinoud
231 1.1 reinoud /* check payload CRC if applicable */
232 1.1 reinoud if (crc_len > 0) {
233 1.1 reinoud crc = udf_cksum(btag + UDF_DESC_TAG_LENGTH, crc_len);
234 1.1 reinoud tag->desc_crc = udf_rw16(crc);
235 1.9 christos }
236 1.1 reinoud
237 1.1 reinoud /* calculate TAG header checksum */
238 1.1 reinoud return udf_validate_tag_sum(blob);
239 1.1 reinoud }
240 1.1 reinoud
241 1.1 reinoud /* --------------------------------------------------------------------- */
242 1.1 reinoud
243 1.1 reinoud /*
244 1.1 reinoud * XXX note the different semantics from udfclient: for FIDs it still rounds
245 1.1 reinoud * up to sectors. Use udf_fidsize() for a correct length.
246 1.1 reinoud */
247 1.1 reinoud
248 1.1 reinoud int
249 1.1 reinoud udf_tagsize(union dscrptr *dscr, uint32_t udf_sector_size)
250 1.1 reinoud {
251 1.1 reinoud uint32_t size, tag_id, num_secs, elmsz;
252 1.1 reinoud
253 1.1 reinoud tag_id = udf_rw16(dscr->tag.id);
254 1.1 reinoud
255 1.1 reinoud switch (tag_id) {
256 1.1 reinoud case TAGID_LOGVOL :
257 1.1 reinoud size = sizeof(struct logvol_desc) - 1;
258 1.1 reinoud size += udf_rw32(dscr->lvd.mt_l);
259 1.1 reinoud break;
260 1.1 reinoud case TAGID_UNALLOC_SPACE :
261 1.1 reinoud elmsz = sizeof(struct extent_ad);
262 1.1 reinoud size = sizeof(struct unalloc_sp_desc) - elmsz;
263 1.1 reinoud size += udf_rw32(dscr->usd.alloc_desc_num) * elmsz;
264 1.1 reinoud break;
265 1.1 reinoud case TAGID_FID :
266 1.1 reinoud size = UDF_FID_SIZE + dscr->fid.l_fi + udf_rw16(dscr->fid.l_iu);
267 1.1 reinoud size = (size + 3) & ~3;
268 1.1 reinoud break;
269 1.1 reinoud case TAGID_LOGVOL_INTEGRITY :
270 1.1 reinoud size = sizeof(struct logvol_int_desc) - sizeof(uint32_t);
271 1.1 reinoud size += udf_rw32(dscr->lvid.l_iu);
272 1.1 reinoud size += (2 * udf_rw32(dscr->lvid.num_part) * sizeof(uint32_t));
273 1.1 reinoud break;
274 1.1 reinoud case TAGID_SPACE_BITMAP :
275 1.1 reinoud size = sizeof(struct space_bitmap_desc) - 1;
276 1.1 reinoud size += udf_rw32(dscr->sbd.num_bytes);
277 1.1 reinoud break;
278 1.1 reinoud case TAGID_SPARING_TABLE :
279 1.1 reinoud elmsz = sizeof(struct spare_map_entry);
280 1.1 reinoud size = sizeof(struct udf_sparing_table) - elmsz;
281 1.1 reinoud size += udf_rw16(dscr->spt.rt_l) * elmsz;
282 1.1 reinoud break;
283 1.1 reinoud case TAGID_FENTRY :
284 1.1 reinoud size = sizeof(struct file_entry);
285 1.1 reinoud size += udf_rw32(dscr->fe.l_ea) + udf_rw32(dscr->fe.l_ad)-1;
286 1.1 reinoud break;
287 1.1 reinoud case TAGID_EXTFENTRY :
288 1.1 reinoud size = sizeof(struct extfile_entry);
289 1.1 reinoud size += udf_rw32(dscr->efe.l_ea) + udf_rw32(dscr->efe.l_ad)-1;
290 1.1 reinoud break;
291 1.1 reinoud case TAGID_FSD :
292 1.1 reinoud size = sizeof(struct fileset_desc);
293 1.1 reinoud break;
294 1.1 reinoud default :
295 1.1 reinoud size = sizeof(union dscrptr);
296 1.1 reinoud break;
297 1.9 christos }
298 1.1 reinoud
299 1.1 reinoud if ((size == 0) || (udf_sector_size == 0)) return 0;
300 1.1 reinoud
301 1.1 reinoud /* round up in sectors */
302 1.1 reinoud num_secs = (size + udf_sector_size -1) / udf_sector_size;
303 1.1 reinoud return num_secs * udf_sector_size;
304 1.1 reinoud }
305 1.1 reinoud
306 1.1 reinoud
307 1.1 reinoud static int
308 1.23 christos udf_fidsize(struct fileid_desc *fid, uint32_t udf_sector_size)
309 1.1 reinoud {
310 1.1 reinoud uint32_t size;
311 1.1 reinoud
312 1.1 reinoud if (udf_rw16(fid->tag.id) != TAGID_FID)
313 1.1 reinoud panic("got udf_fidsize on non FID\n");
314 1.1 reinoud
315 1.1 reinoud size = UDF_FID_SIZE + fid->l_fi + udf_rw16(fid->l_iu);
316 1.1 reinoud size = (size + 3) & ~3;
317 1.1 reinoud
318 1.1 reinoud return size;
319 1.1 reinoud }
320 1.1 reinoud
321 1.1 reinoud /* --------------------------------------------------------------------- */
322 1.1 reinoud
323 1.1 reinoud /*
324 1.1 reinoud * Problem with read_descriptor are long descriptors spanning more than one
325 1.1 reinoud * sector. Luckily long descriptors can't be in `logical space'.
326 1.1 reinoud *
327 1.1 reinoud * Size of allocated piece is returned in multiple of sector size due to
328 1.1 reinoud * udf_calc_udf_malloc_size().
329 1.1 reinoud */
330 1.1 reinoud
331 1.1 reinoud int
332 1.1 reinoud udf_read_descriptor(struct udf_mount *ump, uint32_t sector,
333 1.1 reinoud struct malloc_type *mtype, union dscrptr **dstp)
334 1.1 reinoud {
335 1.1 reinoud union dscrptr *src, *dst;
336 1.1 reinoud struct buf *bp;
337 1.1 reinoud uint8_t *pos;
338 1.1 reinoud int blks, blk, dscrlen;
339 1.1 reinoud int i, error, sector_size;
340 1.1 reinoud
341 1.1 reinoud sector_size = ump->discinfo.sector_size;
342 1.1 reinoud
343 1.1 reinoud *dstp = dst = NULL;
344 1.1 reinoud dscrlen = sector_size;
345 1.1 reinoud
346 1.1 reinoud /* read initial piece */
347 1.1 reinoud error = udf_bread(ump, sector, &bp);
348 1.1 reinoud DPRINTFIF(DESCRIPTOR, error, ("read error (%d)\n", error));
349 1.1 reinoud
350 1.1 reinoud if (!error) {
351 1.1 reinoud /* check if its a valid tag */
352 1.1 reinoud error = udf_check_tag(bp->b_data);
353 1.1 reinoud if (error) {
354 1.1 reinoud /* check if its an empty block */
355 1.1 reinoud pos = bp->b_data;
356 1.1 reinoud for (i = 0; i < sector_size; i++, pos++) {
357 1.1 reinoud if (*pos) break;
358 1.9 christos }
359 1.1 reinoud if (i == sector_size) {
360 1.1 reinoud /* return no error but with no dscrptr */
361 1.1 reinoud /* dispose first block */
362 1.38 ad brelse(bp, 0);
363 1.1 reinoud return 0;
364 1.9 christos }
365 1.9 christos }
366 1.9 christos }
367 1.1 reinoud DPRINTFIF(DESCRIPTOR, error, ("bad tag checksum\n"));
368 1.1 reinoud if (!error) {
369 1.1 reinoud src = (union dscrptr *) bp->b_data;
370 1.1 reinoud dscrlen = udf_tagsize(src, sector_size);
371 1.1 reinoud dst = malloc(dscrlen, mtype, M_WAITOK);
372 1.15 reinoud memcpy(dst, src, sector_size);
373 1.9 christos }
374 1.1 reinoud /* dispose first block */
375 1.38 ad brelse(bp, BC_AGE);
376 1.1 reinoud
377 1.1 reinoud if (!error && (dscrlen > sector_size)) {
378 1.1 reinoud DPRINTF(DESCRIPTOR, ("multi block descriptor read\n"));
379 1.1 reinoud /*
380 1.1 reinoud * Read the rest of descriptor. Since it is only used at mount
381 1.1 reinoud * time its overdone to define and use a specific udf_breadn
382 1.1 reinoud * for this alone.
383 1.1 reinoud */
384 1.1 reinoud blks = (dscrlen + sector_size -1) / sector_size;
385 1.1 reinoud for (blk = 1; blk < blks; blk++) {
386 1.1 reinoud error = udf_bread(ump, sector + blk, &bp);
387 1.1 reinoud if (error) {
388 1.38 ad brelse(bp, 0);
389 1.1 reinoud break;
390 1.9 christos }
391 1.1 reinoud pos = (uint8_t *) dst + blk*sector_size;
392 1.1 reinoud memcpy(pos, bp->b_data, sector_size);
393 1.1 reinoud
394 1.1 reinoud /* dispose block */
395 1.38 ad brelse(bp, BC_AGE);
396 1.9 christos }
397 1.1 reinoud DPRINTFIF(DESCRIPTOR, error, ("read error on multi (%d)\n",
398 1.1 reinoud error));
399 1.9 christos }
400 1.1 reinoud if (!error) {
401 1.1 reinoud error = udf_check_tag_payload(dst, dscrlen);
402 1.1 reinoud DPRINTFIF(DESCRIPTOR, error, ("bad payload check sum\n"));
403 1.9 christos }
404 1.1 reinoud if (error && dst) {
405 1.1 reinoud free(dst, mtype);
406 1.1 reinoud dst = NULL;
407 1.9 christos }
408 1.1 reinoud *dstp = dst;
409 1.1 reinoud
410 1.1 reinoud return error;
411 1.1 reinoud }
412 1.1 reinoud
413 1.1 reinoud /* --------------------------------------------------------------------- */
414 1.1 reinoud #ifdef DEBUG
415 1.1 reinoud static void
416 1.1 reinoud udf_dump_discinfo(struct udf_mount *ump)
417 1.1 reinoud {
418 1.1 reinoud char bits[128];
419 1.1 reinoud struct mmc_discinfo *di = &ump->discinfo;
420 1.1 reinoud
421 1.1 reinoud if ((udf_verbose & UDF_DEBUG_VOLUMES) == 0)
422 1.1 reinoud return;
423 1.1 reinoud
424 1.1 reinoud printf("Device/media info :\n");
425 1.1 reinoud printf("\tMMC profile 0x%02x\n", di->mmc_profile);
426 1.1 reinoud printf("\tderived class %d\n", di->mmc_class);
427 1.1 reinoud printf("\tsector size %d\n", di->sector_size);
428 1.1 reinoud printf("\tdisc state %d\n", di->disc_state);
429 1.1 reinoud printf("\tlast ses state %d\n", di->last_session_state);
430 1.1 reinoud printf("\tbg format state %d\n", di->bg_format_state);
431 1.1 reinoud printf("\tfrst track %d\n", di->first_track);
432 1.1 reinoud printf("\tfst on last ses %d\n", di->first_track_last_session);
433 1.1 reinoud printf("\tlst on last ses %d\n", di->last_track_last_session);
434 1.1 reinoud printf("\tlink block penalty %d\n", di->link_block_penalty);
435 1.1 reinoud bitmask_snprintf(di->disc_flags, MMC_DFLAGS_FLAGBITS, bits,
436 1.1 reinoud sizeof(bits));
437 1.1 reinoud printf("\tdisc flags %s\n", bits);
438 1.1 reinoud printf("\tdisc id %x\n", di->disc_id);
439 1.1 reinoud printf("\tdisc barcode %"PRIx64"\n", di->disc_barcode);
440 1.1 reinoud
441 1.1 reinoud printf("\tnum sessions %d\n", di->num_sessions);
442 1.1 reinoud printf("\tnum tracks %d\n", di->num_tracks);
443 1.1 reinoud
444 1.1 reinoud bitmask_snprintf(di->mmc_cur, MMC_CAP_FLAGBITS, bits, sizeof(bits));
445 1.1 reinoud printf("\tcapabilities cur %s\n", bits);
446 1.1 reinoud bitmask_snprintf(di->mmc_cap, MMC_CAP_FLAGBITS, bits, sizeof(bits));
447 1.1 reinoud printf("\tcapabilities cap %s\n", bits);
448 1.1 reinoud }
449 1.1 reinoud #else
450 1.1 reinoud #define udf_dump_discinfo(a);
451 1.1 reinoud #endif
452 1.1 reinoud
453 1.1 reinoud /* not called often */
454 1.1 reinoud int
455 1.1 reinoud udf_update_discinfo(struct udf_mount *ump)
456 1.1 reinoud {
457 1.1 reinoud struct vnode *devvp = ump->devvp;
458 1.1 reinoud struct partinfo dpart;
459 1.1 reinoud struct mmc_discinfo *di;
460 1.1 reinoud int error;
461 1.1 reinoud
462 1.1 reinoud DPRINTF(VOLUMES, ("read/update disc info\n"));
463 1.1 reinoud di = &ump->discinfo;
464 1.1 reinoud memset(di, 0, sizeof(struct mmc_discinfo));
465 1.1 reinoud
466 1.1 reinoud /* check if we're on a MMC capable device, i.e. CD/DVD */
467 1.41 pooka error = VOP_IOCTL(devvp, MMCGETDISCINFO, di, FKIOCTL, NOCRED);
468 1.1 reinoud if (error == 0) {
469 1.1 reinoud udf_dump_discinfo(ump);
470 1.1 reinoud return 0;
471 1.9 christos }
472 1.1 reinoud
473 1.1 reinoud /* disc partition support */
474 1.41 pooka error = VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED);
475 1.1 reinoud if (error)
476 1.1 reinoud return ENODEV;
477 1.1 reinoud
478 1.1 reinoud /* set up a disc info profile for partitions */
479 1.1 reinoud di->mmc_profile = 0x01; /* disc type */
480 1.1 reinoud di->mmc_class = MMC_CLASS_DISC;
481 1.1 reinoud di->disc_state = MMC_STATE_CLOSED;
482 1.1 reinoud di->last_session_state = MMC_STATE_CLOSED;
483 1.1 reinoud di->bg_format_state = MMC_BGFSTATE_COMPLETED;
484 1.1 reinoud di->link_block_penalty = 0;
485 1.1 reinoud
486 1.1 reinoud di->mmc_cur = MMC_CAP_RECORDABLE | MMC_CAP_REWRITABLE |
487 1.4 reinoud MMC_CAP_ZEROLINKBLK | MMC_CAP_HW_DEFECTFREE;
488 1.1 reinoud di->mmc_cap = di->mmc_cur;
489 1.1 reinoud di->disc_flags = MMC_DFLAGS_UNRESTRICTED;
490 1.1 reinoud
491 1.1 reinoud /* TODO problem with last_possible_lba on resizable VND; request */
492 1.1 reinoud di->last_possible_lba = dpart.part->p_size;
493 1.1 reinoud di->sector_size = dpart.disklab->d_secsize;
494 1.1 reinoud
495 1.1 reinoud di->num_sessions = 1;
496 1.1 reinoud di->num_tracks = 1;
497 1.1 reinoud
498 1.1 reinoud di->first_track = 1;
499 1.1 reinoud di->first_track_last_session = di->last_track_last_session = 1;
500 1.1 reinoud
501 1.1 reinoud udf_dump_discinfo(ump);
502 1.1 reinoud return 0;
503 1.1 reinoud }
504 1.1 reinoud
505 1.1 reinoud /* --------------------------------------------------------------------- */
506 1.1 reinoud
507 1.1 reinoud int
508 1.1 reinoud udf_update_trackinfo(struct udf_mount *ump, struct mmc_trackinfo *ti)
509 1.1 reinoud {
510 1.1 reinoud struct vnode *devvp = ump->devvp;
511 1.1 reinoud struct mmc_discinfo *di = &ump->discinfo;
512 1.1 reinoud int error, class;
513 1.1 reinoud
514 1.1 reinoud DPRINTF(VOLUMES, ("read track info\n"));
515 1.1 reinoud
516 1.1 reinoud class = di->mmc_class;
517 1.1 reinoud if (class != MMC_CLASS_DISC) {
518 1.1 reinoud /* tracknr specified in struct ti */
519 1.41 pooka error = VOP_IOCTL(devvp, MMCGETTRACKINFO, ti, FKIOCTL, NOCRED);
520 1.1 reinoud return error;
521 1.9 christos }
522 1.1 reinoud
523 1.1 reinoud /* disc partition support */
524 1.1 reinoud if (ti->tracknr != 1)
525 1.1 reinoud return EIO;
526 1.1 reinoud
527 1.1 reinoud /* create fake ti (TODO check for resized vnds) */
528 1.1 reinoud ti->sessionnr = 1;
529 1.1 reinoud
530 1.1 reinoud ti->track_mode = 0; /* XXX */
531 1.1 reinoud ti->data_mode = 0; /* XXX */
532 1.1 reinoud ti->flags = MMC_TRACKINFO_LRA_VALID | MMC_TRACKINFO_NWA_VALID;
533 1.1 reinoud
534 1.1 reinoud ti->track_start = 0;
535 1.1 reinoud ti->packet_size = 1;
536 1.1 reinoud
537 1.1 reinoud /* TODO support for resizable vnd */
538 1.1 reinoud ti->track_size = di->last_possible_lba;
539 1.1 reinoud ti->next_writable = di->last_possible_lba;
540 1.1 reinoud ti->last_recorded = ti->next_writable;
541 1.1 reinoud ti->free_blocks = 0;
542 1.1 reinoud
543 1.1 reinoud return 0;
544 1.1 reinoud }
545 1.1 reinoud
546 1.1 reinoud /* --------------------------------------------------------------------- */
547 1.1 reinoud
548 1.1 reinoud /* track/session searching for mounting */
549 1.1 reinoud
550 1.1 reinoud static int
551 1.1 reinoud udf_search_tracks(struct udf_mount *ump, struct udf_args *args,
552 1.1 reinoud int *first_tracknr, int *last_tracknr)
553 1.1 reinoud {
554 1.1 reinoud struct mmc_trackinfo trackinfo;
555 1.1 reinoud uint32_t tracknr, start_track, num_tracks;
556 1.1 reinoud int error;
557 1.1 reinoud
558 1.1 reinoud /* if negative, sessionnr is relative to last session */
559 1.1 reinoud if (args->sessionnr < 0) {
560 1.1 reinoud args->sessionnr += ump->discinfo.num_sessions;
561 1.1 reinoud /* sanity */
562 1.1 reinoud if (args->sessionnr < 0)
563 1.1 reinoud args->sessionnr = 0;
564 1.9 christos }
565 1.1 reinoud
566 1.1 reinoud /* sanity */
567 1.1 reinoud if (args->sessionnr > ump->discinfo.num_sessions)
568 1.1 reinoud args->sessionnr = ump->discinfo.num_sessions;
569 1.1 reinoud
570 1.1 reinoud /* search the tracks for this session, zero session nr indicates last */
571 1.42 reinoud if (args->sessionnr == 0)
572 1.1 reinoud args->sessionnr = ump->discinfo.num_sessions;
573 1.42 reinoud if (ump->discinfo.last_session_state == MMC_STATE_EMPTY)
574 1.42 reinoud args->sessionnr--;
575 1.42 reinoud
576 1.42 reinoud /* sanity */
577 1.42 reinoud if (args->sessionnr == 0)
578 1.42 reinoud args->sessionnr = 1;
579 1.1 reinoud
580 1.1 reinoud /* search the first and last track of the specified session */
581 1.1 reinoud num_tracks = ump->discinfo.num_tracks;
582 1.1 reinoud start_track = ump->discinfo.first_track;
583 1.1 reinoud
584 1.1 reinoud /* search for first track of this session */
585 1.1 reinoud for (tracknr = start_track; tracknr <= num_tracks; tracknr++) {
586 1.1 reinoud /* get track info */
587 1.1 reinoud trackinfo.tracknr = tracknr;
588 1.1 reinoud error = udf_update_trackinfo(ump, &trackinfo);
589 1.1 reinoud if (error)
590 1.1 reinoud return error;
591 1.1 reinoud
592 1.1 reinoud if (trackinfo.sessionnr == args->sessionnr)
593 1.1 reinoud break;
594 1.1 reinoud }
595 1.1 reinoud *first_tracknr = tracknr;
596 1.1 reinoud
597 1.1 reinoud /* search for last track of this session */
598 1.1 reinoud for (;tracknr <= num_tracks; tracknr++) {
599 1.1 reinoud /* get track info */
600 1.1 reinoud trackinfo.tracknr = tracknr;
601 1.1 reinoud error = udf_update_trackinfo(ump, &trackinfo);
602 1.1 reinoud if (error || (trackinfo.sessionnr != args->sessionnr)) {
603 1.1 reinoud tracknr--;
604 1.1 reinoud break;
605 1.9 christos }
606 1.9 christos }
607 1.1 reinoud if (tracknr > num_tracks)
608 1.1 reinoud tracknr--;
609 1.1 reinoud
610 1.1 reinoud *last_tracknr = tracknr;
611 1.1 reinoud
612 1.1 reinoud assert(*last_tracknr >= *first_tracknr);
613 1.1 reinoud return 0;
614 1.1 reinoud }
615 1.1 reinoud
616 1.1 reinoud /* --------------------------------------------------------------------- */
617 1.1 reinoud
618 1.1 reinoud static int
619 1.1 reinoud udf_read_anchor(struct udf_mount *ump, uint32_t sector, struct anchor_vdp **dst)
620 1.1 reinoud {
621 1.1 reinoud int error;
622 1.1 reinoud
623 1.1 reinoud error = udf_read_descriptor(ump, sector, M_UDFVOLD,
624 1.1 reinoud (union dscrptr **) dst);
625 1.1 reinoud if (!error) {
626 1.1 reinoud /* blank terminator blocks are not allowed here */
627 1.1 reinoud if (*dst == NULL)
628 1.1 reinoud return ENOENT;
629 1.1 reinoud if (udf_rw16((*dst)->tag.id) != TAGID_ANCHOR) {
630 1.1 reinoud error = ENOENT;
631 1.1 reinoud free(*dst, M_UDFVOLD);
632 1.1 reinoud *dst = NULL;
633 1.1 reinoud DPRINTF(VOLUMES, ("Not an anchor\n"));
634 1.9 christos }
635 1.9 christos }
636 1.1 reinoud
637 1.1 reinoud return error;
638 1.1 reinoud }
639 1.1 reinoud
640 1.1 reinoud
641 1.1 reinoud int
642 1.1 reinoud udf_read_anchors(struct udf_mount *ump, struct udf_args *args)
643 1.1 reinoud {
644 1.1 reinoud struct mmc_trackinfo first_track;
645 1.1 reinoud struct mmc_trackinfo last_track;
646 1.1 reinoud struct anchor_vdp **anchorsp;
647 1.1 reinoud uint32_t track_start;
648 1.1 reinoud uint32_t track_end;
649 1.1 reinoud uint32_t positions[4];
650 1.1 reinoud int first_tracknr, last_tracknr;
651 1.1 reinoud int error, anch, ok, first_anchor;
652 1.1 reinoud
653 1.1 reinoud /* search the first and last track of the specified session */
654 1.1 reinoud error = udf_search_tracks(ump, args, &first_tracknr, &last_tracknr);
655 1.1 reinoud if (!error) {
656 1.1 reinoud first_track.tracknr = first_tracknr;
657 1.1 reinoud error = udf_update_trackinfo(ump, &first_track);
658 1.9 christos }
659 1.1 reinoud if (!error) {
660 1.1 reinoud last_track.tracknr = last_tracknr;
661 1.1 reinoud error = udf_update_trackinfo(ump, &last_track);
662 1.9 christos }
663 1.1 reinoud if (error) {
664 1.1 reinoud printf("UDF mount: reading disc geometry failed\n");
665 1.1 reinoud return 0;
666 1.9 christos }
667 1.1 reinoud
668 1.1 reinoud track_start = first_track.track_start;
669 1.1 reinoud
670 1.1 reinoud /* `end' is not as straitforward as start. */
671 1.1 reinoud track_end = last_track.track_start
672 1.1 reinoud + last_track.track_size - last_track.free_blocks - 1;
673 1.1 reinoud
674 1.1 reinoud if (ump->discinfo.mmc_cur & MMC_CAP_SEQUENTIAL) {
675 1.1 reinoud /* end of track is not straitforward here */
676 1.1 reinoud if (last_track.flags & MMC_TRACKINFO_LRA_VALID)
677 1.1 reinoud track_end = last_track.last_recorded;
678 1.1 reinoud else if (last_track.flags & MMC_TRACKINFO_NWA_VALID)
679 1.1 reinoud track_end = last_track.next_writable
680 1.1 reinoud - ump->discinfo.link_block_penalty;
681 1.9 christos }
682 1.1 reinoud
683 1.1 reinoud /* its no use reading a blank track */
684 1.1 reinoud first_anchor = 0;
685 1.1 reinoud if (first_track.flags & MMC_TRACKINFO_BLANK)
686 1.1 reinoud first_anchor = 1;
687 1.1 reinoud
688 1.1 reinoud /* read anchors start+256, start+512, end-256, end */
689 1.1 reinoud positions[0] = track_start+256;
690 1.1 reinoud positions[1] = track_end-256;
691 1.1 reinoud positions[2] = track_end;
692 1.1 reinoud positions[3] = track_start+512; /* [UDF 2.60/6.11.2] */
693 1.1 reinoud /* XXX shouldn't +512 be prefered above +256 for compat with Roxio CD */
694 1.1 reinoud
695 1.1 reinoud ok = 0;
696 1.1 reinoud anchorsp = ump->anchors;
697 1.1 reinoud for (anch = first_anchor; anch < 4; anch++) {
698 1.1 reinoud DPRINTF(VOLUMES, ("Read anchor %d at sector %d\n", anch,
699 1.1 reinoud positions[anch]));
700 1.1 reinoud error = udf_read_anchor(ump, positions[anch], anchorsp);
701 1.1 reinoud if (!error) {
702 1.1 reinoud anchorsp++;
703 1.1 reinoud ok++;
704 1.9 christos }
705 1.9 christos }
706 1.1 reinoud
707 1.13 reinoud /* VATs are only recorded on sequential media, but initialise */
708 1.42 reinoud ump->first_possible_vat_location = track_start + 2;
709 1.42 reinoud ump->last_possible_vat_location = track_end + last_track.packet_size;
710 1.13 reinoud
711 1.1 reinoud return ok;
712 1.1 reinoud }
713 1.1 reinoud
714 1.1 reinoud /* --------------------------------------------------------------------- */
715 1.1 reinoud
716 1.1 reinoud /* we dont try to be smart; we just record the parts */
717 1.1 reinoud #define UDF_UPDATE_DSCR(name, dscr) \
718 1.1 reinoud if (name) \
719 1.1 reinoud free(name, M_UDFVOLD); \
720 1.1 reinoud name = dscr;
721 1.1 reinoud
722 1.1 reinoud static int
723 1.1 reinoud udf_process_vds_descriptor(struct udf_mount *ump, union dscrptr *dscr)
724 1.1 reinoud {
725 1.33 reinoud struct part_desc *part;
726 1.33 reinoud uint16_t phys_part, raw_phys_part;
727 1.1 reinoud
728 1.1 reinoud DPRINTF(VOLUMES, ("\tprocessing VDS descr %d\n",
729 1.1 reinoud udf_rw16(dscr->tag.id)));
730 1.1 reinoud switch (udf_rw16(dscr->tag.id)) {
731 1.1 reinoud case TAGID_PRI_VOL : /* primary partition */
732 1.1 reinoud UDF_UPDATE_DSCR(ump->primary_vol, &dscr->pvd);
733 1.1 reinoud break;
734 1.1 reinoud case TAGID_LOGVOL : /* logical volume */
735 1.1 reinoud UDF_UPDATE_DSCR(ump->logical_vol, &dscr->lvd);
736 1.1 reinoud break;
737 1.1 reinoud case TAGID_UNALLOC_SPACE : /* unallocated space */
738 1.1 reinoud UDF_UPDATE_DSCR(ump->unallocated, &dscr->usd);
739 1.1 reinoud break;
740 1.1 reinoud case TAGID_IMP_VOL : /* implementation */
741 1.1 reinoud /* XXX do we care about multiple impl. descr ? */
742 1.1 reinoud UDF_UPDATE_DSCR(ump->implementation, &dscr->ivd);
743 1.1 reinoud break;
744 1.1 reinoud case TAGID_PARTITION : /* physical partition */
745 1.1 reinoud /* not much use if its not allocated */
746 1.1 reinoud if ((udf_rw16(dscr->pd.flags) & UDF_PART_FLAG_ALLOCATED) == 0) {
747 1.1 reinoud free(dscr, M_UDFVOLD);
748 1.1 reinoud break;
749 1.9 christos }
750 1.1 reinoud
751 1.33 reinoud /*
752 1.33 reinoud * BUGALERT: some rogue implementations use random physical
753 1.33 reinoud * partion numbers to break other implementations so lookup
754 1.33 reinoud * the number.
755 1.33 reinoud */
756 1.33 reinoud raw_phys_part = udf_rw16(dscr->pd.part_num);
757 1.33 reinoud for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) {
758 1.33 reinoud part = ump->partitions[phys_part];
759 1.33 reinoud if (part == NULL)
760 1.33 reinoud break;
761 1.33 reinoud if (udf_rw16(part->part_num) == raw_phys_part)
762 1.33 reinoud break;
763 1.33 reinoud }
764 1.33 reinoud if (phys_part == UDF_PARTITIONS) {
765 1.20 reinoud free(dscr, M_UDFVOLD);
766 1.1 reinoud return EINVAL;
767 1.20 reinoud }
768 1.1 reinoud
769 1.33 reinoud UDF_UPDATE_DSCR(ump->partitions[phys_part], &dscr->pd);
770 1.1 reinoud break;
771 1.1 reinoud case TAGID_VOL : /* volume space extender; rare */
772 1.1 reinoud DPRINTF(VOLUMES, ("VDS extender ignored\n"));
773 1.1 reinoud free(dscr, M_UDFVOLD);
774 1.1 reinoud break;
775 1.1 reinoud default :
776 1.1 reinoud DPRINTF(VOLUMES, ("Unhandled VDS type %d\n",
777 1.1 reinoud udf_rw16(dscr->tag.id)));
778 1.1 reinoud free(dscr, M_UDFVOLD);
779 1.9 christos }
780 1.1 reinoud
781 1.1 reinoud return 0;
782 1.1 reinoud }
783 1.1 reinoud #undef UDF_UPDATE_DSCR
784 1.1 reinoud
785 1.1 reinoud /* --------------------------------------------------------------------- */
786 1.1 reinoud
787 1.1 reinoud static int
788 1.1 reinoud udf_read_vds_extent(struct udf_mount *ump, uint32_t loc, uint32_t len)
789 1.1 reinoud {
790 1.1 reinoud union dscrptr *dscr;
791 1.1 reinoud uint32_t sector_size, dscr_size;
792 1.1 reinoud int error;
793 1.1 reinoud
794 1.1 reinoud sector_size = ump->discinfo.sector_size;
795 1.1 reinoud
796 1.1 reinoud /* loc is sectornr, len is in bytes */
797 1.1 reinoud error = EIO;
798 1.1 reinoud while (len) {
799 1.1 reinoud error = udf_read_descriptor(ump, loc, M_UDFVOLD, &dscr);
800 1.1 reinoud if (error)
801 1.1 reinoud return error;
802 1.1 reinoud
803 1.1 reinoud /* blank block is a terminator */
804 1.1 reinoud if (dscr == NULL)
805 1.1 reinoud return 0;
806 1.1 reinoud
807 1.1 reinoud /* TERM descriptor is a terminator */
808 1.20 reinoud if (udf_rw16(dscr->tag.id) == TAGID_TERM) {
809 1.20 reinoud free(dscr, M_UDFVOLD);
810 1.1 reinoud return 0;
811 1.20 reinoud }
812 1.1 reinoud
813 1.1 reinoud /* process all others */
814 1.1 reinoud dscr_size = udf_tagsize(dscr, sector_size);
815 1.1 reinoud error = udf_process_vds_descriptor(ump, dscr);
816 1.1 reinoud if (error) {
817 1.1 reinoud free(dscr, M_UDFVOLD);
818 1.1 reinoud break;
819 1.9 christos }
820 1.1 reinoud assert((dscr_size % sector_size) == 0);
821 1.1 reinoud
822 1.1 reinoud len -= dscr_size;
823 1.1 reinoud loc += dscr_size / sector_size;
824 1.9 christos }
825 1.1 reinoud
826 1.1 reinoud return error;
827 1.1 reinoud }
828 1.1 reinoud
829 1.1 reinoud
830 1.1 reinoud int
831 1.1 reinoud udf_read_vds_space(struct udf_mount *ump)
832 1.1 reinoud {
833 1.1 reinoud struct anchor_vdp *anchor, *anchor2;
834 1.1 reinoud size_t size;
835 1.1 reinoud uint32_t main_loc, main_len;
836 1.1 reinoud uint32_t reserve_loc, reserve_len;
837 1.1 reinoud int error;
838 1.1 reinoud
839 1.1 reinoud /*
840 1.1 reinoud * read in VDS space provided by the anchors; if one descriptor read
841 1.1 reinoud * fails, try the mirror sector.
842 1.1 reinoud *
843 1.1 reinoud * check if 2nd anchor is different from 1st; if so, go for 2nd. This
844 1.1 reinoud * avoids the `compatibility features' of DirectCD that may confuse
845 1.1 reinoud * stuff completely.
846 1.1 reinoud */
847 1.1 reinoud
848 1.1 reinoud anchor = ump->anchors[0];
849 1.1 reinoud anchor2 = ump->anchors[1];
850 1.1 reinoud assert(anchor);
851 1.1 reinoud
852 1.1 reinoud if (anchor2) {
853 1.1 reinoud size = sizeof(struct extent_ad);
854 1.1 reinoud if (memcmp(&anchor->main_vds_ex, &anchor2->main_vds_ex, size))
855 1.1 reinoud anchor = anchor2;
856 1.1 reinoud /* reserve is specified to be a literal copy of main */
857 1.9 christos }
858 1.1 reinoud
859 1.1 reinoud main_loc = udf_rw32(anchor->main_vds_ex.loc);
860 1.1 reinoud main_len = udf_rw32(anchor->main_vds_ex.len);
861 1.1 reinoud
862 1.1 reinoud reserve_loc = udf_rw32(anchor->reserve_vds_ex.loc);
863 1.1 reinoud reserve_len = udf_rw32(anchor->reserve_vds_ex.len);
864 1.1 reinoud
865 1.1 reinoud error = udf_read_vds_extent(ump, main_loc, main_len);
866 1.1 reinoud if (error) {
867 1.1 reinoud printf("UDF mount: reading in reserve VDS extent\n");
868 1.1 reinoud error = udf_read_vds_extent(ump, reserve_loc, reserve_len);
869 1.9 christos }
870 1.1 reinoud
871 1.1 reinoud return error;
872 1.1 reinoud }
873 1.1 reinoud
874 1.1 reinoud /* --------------------------------------------------------------------- */
875 1.1 reinoud
876 1.1 reinoud /*
877 1.1 reinoud * Read in the logical volume integrity sequence pointed to by our logical
878 1.1 reinoud * volume descriptor. Its a sequence that can be extended using fields in the
879 1.1 reinoud * integrity descriptor itself. On sequential media only one is found, on
880 1.1 reinoud * rewritable media a sequence of descriptors can be found as a form of
881 1.1 reinoud * history keeping and on non sequential write-once media the chain is vital
882 1.1 reinoud * to allow more and more descriptors to be written. The last descriptor
883 1.1 reinoud * written in an extent needs to claim space for a new extent.
884 1.1 reinoud */
885 1.1 reinoud
886 1.1 reinoud static int
887 1.1 reinoud udf_retrieve_lvint(struct udf_mount *ump, struct logvol_int_desc **lvintp)
888 1.1 reinoud {
889 1.1 reinoud union dscrptr *dscr;
890 1.1 reinoud struct logvol_int_desc *lvint;
891 1.1 reinoud uint32_t sector_size, sector, len;
892 1.1 reinoud int dscr_type, error;
893 1.1 reinoud
894 1.1 reinoud sector_size = ump->discinfo.sector_size;
895 1.1 reinoud len = udf_rw32(ump->logical_vol->integrity_seq_loc.len);
896 1.1 reinoud sector = udf_rw32(ump->logical_vol->integrity_seq_loc.loc);
897 1.1 reinoud
898 1.1 reinoud lvint = NULL;
899 1.1 reinoud dscr = NULL;
900 1.1 reinoud error = 0;
901 1.1 reinoud while (len) {
902 1.1 reinoud /* read in our integrity descriptor */
903 1.1 reinoud error = udf_read_descriptor(ump, sector, M_UDFVOLD, &dscr);
904 1.1 reinoud if (!error) {
905 1.1 reinoud if (dscr == NULL)
906 1.1 reinoud break; /* empty terminates */
907 1.1 reinoud dscr_type = udf_rw16(dscr->tag.id);
908 1.1 reinoud if (dscr_type == TAGID_TERM) {
909 1.1 reinoud break; /* clean terminator */
910 1.9 christos }
911 1.1 reinoud if (dscr_type != TAGID_LOGVOL_INTEGRITY) {
912 1.1 reinoud /* fatal... corrupt disc */
913 1.1 reinoud error = ENOENT;
914 1.1 reinoud break;
915 1.9 christos }
916 1.1 reinoud if (lvint)
917 1.1 reinoud free(lvint, M_UDFVOLD);
918 1.1 reinoud lvint = &dscr->lvid;
919 1.1 reinoud dscr = NULL;
920 1.9 christos } /* else hope for the best... maybe the next is ok */
921 1.1 reinoud
922 1.1 reinoud DPRINTFIF(VOLUMES, lvint, ("logvol integrity read, state %s\n",
923 1.1 reinoud udf_rw32(lvint->integrity_type) ? "CLOSED" : "OPEN"));
924 1.1 reinoud
925 1.1 reinoud /* proceed sequential */
926 1.1 reinoud sector += 1;
927 1.1 reinoud len -= sector_size;
928 1.1 reinoud
929 1.1 reinoud /* are we linking to a new piece? */
930 1.1 reinoud if (lvint->next_extent.len) {
931 1.1 reinoud len = udf_rw32(lvint->next_extent.len);
932 1.1 reinoud sector = udf_rw32(lvint->next_extent.loc);
933 1.9 christos }
934 1.9 christos }
935 1.1 reinoud
936 1.1 reinoud /* clean up the mess, esp. when there is an error */
937 1.1 reinoud if (dscr)
938 1.1 reinoud free(dscr, M_UDFVOLD);
939 1.3 reinoud
940 1.3 reinoud if (error && lvint) {
941 1.1 reinoud free(lvint, M_UDFVOLD);
942 1.3 reinoud lvint = NULL;
943 1.9 christos }
944 1.1 reinoud
945 1.1 reinoud if (!lvint)
946 1.1 reinoud error = ENOENT;
947 1.1 reinoud
948 1.3 reinoud *lvintp = lvint;
949 1.1 reinoud return error;
950 1.1 reinoud }
951 1.1 reinoud
952 1.1 reinoud /* --------------------------------------------------------------------- */
953 1.1 reinoud
954 1.1 reinoud /*
955 1.1 reinoud * Checks if ump's vds information is correct and complete
956 1.1 reinoud */
957 1.1 reinoud
958 1.1 reinoud int
959 1.23 christos udf_process_vds(struct udf_mount *ump, struct udf_args *args)
960 1.22 christos {
961 1.1 reinoud union udf_pmap *mapping;
962 1.1 reinoud struct logvol_int_desc *lvint;
963 1.1 reinoud struct udf_logvol_info *lvinfo;
964 1.33 reinoud struct part_desc *part;
965 1.1 reinoud uint32_t n_pm, mt_l;
966 1.1 reinoud uint8_t *pmap_pos;
967 1.1 reinoud char *domain_name, *map_name;
968 1.1 reinoud const char *check_name;
969 1.1 reinoud int pmap_stype, pmap_size;
970 1.33 reinoud int pmap_type, log_part, phys_part, raw_phys_part;
971 1.1 reinoud int n_phys, n_virt, n_spar, n_meta;
972 1.1 reinoud int len, error;
973 1.1 reinoud
974 1.1 reinoud if (ump == NULL)
975 1.1 reinoud return ENOENT;
976 1.1 reinoud
977 1.1 reinoud /* we need at least an anchor (trivial, but for safety) */
978 1.1 reinoud if (ump->anchors[0] == NULL)
979 1.1 reinoud return EINVAL;
980 1.1 reinoud
981 1.1 reinoud /* we need at least one primary and one logical volume descriptor */
982 1.1 reinoud if ((ump->primary_vol == NULL) || (ump->logical_vol) == NULL)
983 1.1 reinoud return EINVAL;
984 1.1 reinoud
985 1.1 reinoud /* we need at least one partition descriptor */
986 1.1 reinoud if (ump->partitions[0] == NULL)
987 1.1 reinoud return EINVAL;
988 1.1 reinoud
989 1.1 reinoud /* check logical volume sector size verses device sector size */
990 1.1 reinoud if (udf_rw32(ump->logical_vol->lb_size) != ump->discinfo.sector_size) {
991 1.1 reinoud printf("UDF mount: format violation, lb_size != sector size\n");
992 1.1 reinoud return EINVAL;
993 1.9 christos }
994 1.1 reinoud
995 1.1 reinoud domain_name = ump->logical_vol->domain_id.id;
996 1.1 reinoud if (strncmp(domain_name, "*OSTA UDF Compliant", 20)) {
997 1.1 reinoud printf("mount_udf: disc not OSTA UDF Compliant, aborting\n");
998 1.1 reinoud return EINVAL;
999 1.9 christos }
1000 1.1 reinoud
1001 1.1 reinoud /* retrieve logical volume integrity sequence */
1002 1.1 reinoud error = udf_retrieve_lvint(ump, &ump->logvol_integrity);
1003 1.1 reinoud
1004 1.1 reinoud /*
1005 1.1 reinoud * We need at least one logvol integrity descriptor recorded. Note
1006 1.1 reinoud * that its OK to have an open logical volume integrity here. The VAT
1007 1.1 reinoud * will close/update the integrity.
1008 1.1 reinoud */
1009 1.1 reinoud if (ump->logvol_integrity == NULL)
1010 1.1 reinoud return EINVAL;
1011 1.1 reinoud
1012 1.1 reinoud /* process derived structures */
1013 1.1 reinoud n_pm = udf_rw32(ump->logical_vol->n_pm); /* num partmaps */
1014 1.1 reinoud lvint = ump->logvol_integrity;
1015 1.1 reinoud lvinfo = (struct udf_logvol_info *) (&lvint->tables[2 * n_pm]);
1016 1.1 reinoud ump->logvol_info = lvinfo;
1017 1.1 reinoud
1018 1.1 reinoud /* TODO check udf versions? */
1019 1.1 reinoud
1020 1.1 reinoud /*
1021 1.1 reinoud * check logvol mappings: effective virt->log partmap translation
1022 1.1 reinoud * check and recording of the mapping results. Saves expensive
1023 1.1 reinoud * strncmp() in tight places.
1024 1.1 reinoud */
1025 1.1 reinoud DPRINTF(VOLUMES, ("checking logvol mappings\n"));
1026 1.1 reinoud n_pm = udf_rw32(ump->logical_vol->n_pm); /* num partmaps */
1027 1.1 reinoud mt_l = udf_rw32(ump->logical_vol->mt_l); /* partmaps data length */
1028 1.1 reinoud pmap_pos = ump->logical_vol->maps;
1029 1.1 reinoud
1030 1.1 reinoud if (n_pm > UDF_PMAPS) {
1031 1.1 reinoud printf("UDF mount: too many mappings\n");
1032 1.1 reinoud return EINVAL;
1033 1.9 christos }
1034 1.1 reinoud
1035 1.1 reinoud n_phys = n_virt = n_spar = n_meta = 0;
1036 1.1 reinoud for (log_part = 0; log_part < n_pm; log_part++) {
1037 1.1 reinoud mapping = (union udf_pmap *) pmap_pos;
1038 1.1 reinoud pmap_stype = pmap_pos[0];
1039 1.1 reinoud pmap_size = pmap_pos[1];
1040 1.1 reinoud switch (pmap_stype) {
1041 1.1 reinoud case 1: /* physical mapping */
1042 1.1 reinoud /* volseq = udf_rw16(mapping->pm1.vol_seq_num); */
1043 1.33 reinoud raw_phys_part = udf_rw16(mapping->pm1.part_num);
1044 1.1 reinoud pmap_type = UDF_VTOP_TYPE_PHYS;
1045 1.1 reinoud n_phys++;
1046 1.1 reinoud break;
1047 1.1 reinoud case 2: /* virtual/sparable/meta mapping */
1048 1.1 reinoud map_name = mapping->pm2.part_id.id;
1049 1.1 reinoud /* volseq = udf_rw16(mapping->pm2.vol_seq_num); */
1050 1.33 reinoud raw_phys_part = udf_rw16(mapping->pm2.part_num);
1051 1.1 reinoud pmap_type = UDF_VTOP_TYPE_UNKNOWN;
1052 1.1 reinoud len = UDF_REGID_ID_SIZE;
1053 1.1 reinoud
1054 1.1 reinoud check_name = "*UDF Virtual Partition";
1055 1.1 reinoud if (strncmp(map_name, check_name, len) == 0) {
1056 1.1 reinoud pmap_type = UDF_VTOP_TYPE_VIRT;
1057 1.1 reinoud n_virt++;
1058 1.1 reinoud break;
1059 1.9 christos }
1060 1.1 reinoud check_name = "*UDF Sparable Partition";
1061 1.1 reinoud if (strncmp(map_name, check_name, len) == 0) {
1062 1.1 reinoud pmap_type = UDF_VTOP_TYPE_SPARABLE;
1063 1.1 reinoud n_spar++;
1064 1.1 reinoud break;
1065 1.9 christos }
1066 1.1 reinoud check_name = "*UDF Metadata Partition";
1067 1.1 reinoud if (strncmp(map_name, check_name, len) == 0) {
1068 1.1 reinoud pmap_type = UDF_VTOP_TYPE_META;
1069 1.1 reinoud n_meta++;
1070 1.1 reinoud break;
1071 1.9 christos }
1072 1.1 reinoud break;
1073 1.1 reinoud default:
1074 1.1 reinoud return EINVAL;
1075 1.9 christos }
1076 1.1 reinoud
1077 1.33 reinoud /*
1078 1.33 reinoud * BUGALERT: some rogue implementations use random physical
1079 1.33 reinoud * partion numbers to break other implementations so lookup
1080 1.33 reinoud * the number.
1081 1.33 reinoud */
1082 1.33 reinoud for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) {
1083 1.33 reinoud part = ump->partitions[phys_part];
1084 1.33 reinoud if (part == NULL)
1085 1.33 reinoud continue;
1086 1.33 reinoud if (udf_rw16(part->part_num) == raw_phys_part)
1087 1.33 reinoud break;
1088 1.33 reinoud }
1089 1.33 reinoud
1090 1.33 reinoud DPRINTF(VOLUMES, ("\t%d -> %d(%d) type %d\n", log_part,
1091 1.33 reinoud raw_phys_part, phys_part, pmap_type));
1092 1.33 reinoud
1093 1.33 reinoud if (phys_part == UDF_PARTITIONS)
1094 1.33 reinoud return EINVAL;
1095 1.1 reinoud if (pmap_type == UDF_VTOP_TYPE_UNKNOWN)
1096 1.1 reinoud return EINVAL;
1097 1.1 reinoud
1098 1.1 reinoud ump->vtop [log_part] = phys_part;
1099 1.1 reinoud ump->vtop_tp[log_part] = pmap_type;
1100 1.1 reinoud
1101 1.1 reinoud pmap_pos += pmap_size;
1102 1.9 christos }
1103 1.1 reinoud /* not winning the beauty contest */
1104 1.1 reinoud ump->vtop_tp[UDF_VTOP_RAWPART] = UDF_VTOP_TYPE_RAW;
1105 1.1 reinoud
1106 1.1 reinoud /* test some basic UDF assertions/requirements */
1107 1.1 reinoud if ((n_virt > 1) || (n_spar > 1) || (n_meta > 1))
1108 1.1 reinoud return EINVAL;
1109 1.1 reinoud
1110 1.1 reinoud if (n_virt) {
1111 1.1 reinoud if ((n_phys == 0) || n_spar || n_meta)
1112 1.1 reinoud return EINVAL;
1113 1.9 christos }
1114 1.1 reinoud if (n_spar + n_phys == 0)
1115 1.1 reinoud return EINVAL;
1116 1.1 reinoud
1117 1.1 reinoud /* vat's can only be on a sequential media */
1118 1.1 reinoud ump->data_alloc = UDF_ALLOC_SPACEMAP;
1119 1.1 reinoud if (n_virt)
1120 1.1 reinoud ump->data_alloc = UDF_ALLOC_SEQUENTIAL;
1121 1.1 reinoud
1122 1.1 reinoud ump->meta_alloc = UDF_ALLOC_SPACEMAP;
1123 1.1 reinoud if (n_virt)
1124 1.1 reinoud ump->meta_alloc = UDF_ALLOC_VAT;
1125 1.1 reinoud if (n_meta)
1126 1.1 reinoud ump->meta_alloc = UDF_ALLOC_METABITMAP;
1127 1.1 reinoud
1128 1.1 reinoud /* special cases for pseudo-overwrite */
1129 1.1 reinoud if (ump->discinfo.mmc_cur & MMC_CAP_PSEUDOOVERWRITE) {
1130 1.1 reinoud ump->data_alloc = UDF_ALLOC_SEQUENTIAL;
1131 1.1 reinoud if (n_meta) {
1132 1.1 reinoud ump->meta_alloc = UDF_ALLOC_METASEQUENTIAL;
1133 1.1 reinoud } else {
1134 1.1 reinoud ump->meta_alloc = UDF_ALLOC_RELAXEDSEQUENTIAL;
1135 1.9 christos }
1136 1.9 christos }
1137 1.1 reinoud
1138 1.1 reinoud DPRINTF(VOLUMES, ("\tdata alloc scheme %d, meta alloc scheme %d\n",
1139 1.1 reinoud ump->data_alloc, ump->meta_alloc));
1140 1.1 reinoud /* TODO determine partitions to write data and metadata ? */
1141 1.1 reinoud
1142 1.1 reinoud /* signal its OK for now */
1143 1.1 reinoud return 0;
1144 1.1 reinoud }
1145 1.1 reinoud
1146 1.1 reinoud /* --------------------------------------------------------------------- */
1147 1.1 reinoud
1148 1.1 reinoud /*
1149 1.1 reinoud * Read in complete VAT file and check if its indeed a VAT file descriptor
1150 1.1 reinoud */
1151 1.1 reinoud
1152 1.1 reinoud static int
1153 1.1 reinoud udf_check_for_vat(struct udf_node *vat_node)
1154 1.1 reinoud {
1155 1.1 reinoud struct udf_mount *ump;
1156 1.1 reinoud struct icb_tag *icbtag;
1157 1.1 reinoud struct timestamp *mtime;
1158 1.1 reinoud struct regid *regid;
1159 1.1 reinoud struct udf_vat *vat;
1160 1.1 reinoud struct udf_logvol_info *lvinfo;
1161 1.1 reinoud uint32_t vat_length, alloc_length;
1162 1.1 reinoud uint32_t vat_offset, vat_entries;
1163 1.1 reinoud uint32_t sector_size;
1164 1.1 reinoud uint32_t sectors;
1165 1.1 reinoud uint32_t *raw_vat;
1166 1.1 reinoud char *regid_name;
1167 1.1 reinoud int filetype;
1168 1.1 reinoud int error;
1169 1.1 reinoud
1170 1.1 reinoud /* vat_length is really 64 bits though impossible */
1171 1.1 reinoud
1172 1.1 reinoud DPRINTF(VOLUMES, ("Checking for VAT\n"));
1173 1.1 reinoud if (!vat_node)
1174 1.1 reinoud return ENOENT;
1175 1.1 reinoud
1176 1.1 reinoud /* get mount info */
1177 1.1 reinoud ump = vat_node->ump;
1178 1.1 reinoud
1179 1.1 reinoud /* check assertions */
1180 1.1 reinoud assert(vat_node->fe || vat_node->efe);
1181 1.1 reinoud assert(ump->logvol_integrity);
1182 1.1 reinoud
1183 1.1 reinoud /* get information from fe/efe */
1184 1.1 reinoud if (vat_node->fe) {
1185 1.1 reinoud vat_length = udf_rw64(vat_node->fe->inf_len);
1186 1.1 reinoud icbtag = &vat_node->fe->icbtag;
1187 1.1 reinoud mtime = &vat_node->fe->mtime;
1188 1.1 reinoud } else {
1189 1.1 reinoud vat_length = udf_rw64(vat_node->efe->inf_len);
1190 1.1 reinoud icbtag = &vat_node->efe->icbtag;
1191 1.1 reinoud mtime = &vat_node->efe->mtime;
1192 1.9 christos }
1193 1.1 reinoud
1194 1.1 reinoud /* Check icb filetype! it has to be 0 or UDF_ICB_FILETYPE_VAT */
1195 1.1 reinoud filetype = icbtag->file_type;
1196 1.1 reinoud if ((filetype != 0) && (filetype != UDF_ICB_FILETYPE_VAT))
1197 1.1 reinoud return ENOENT;
1198 1.1 reinoud
1199 1.1 reinoud DPRINTF(VOLUMES, ("\tPossible VAT length %d\n", vat_length));
1200 1.1 reinoud /* place a sanity check on the length; currently 1Mb in size */
1201 1.1 reinoud if (vat_length > 1*1024*1024)
1202 1.1 reinoud return ENOENT;
1203 1.1 reinoud
1204 1.1 reinoud /* get sector size */
1205 1.1 reinoud sector_size = vat_node->ump->discinfo.sector_size;
1206 1.1 reinoud
1207 1.1 reinoud /* calculate how many sectors to read in and how much to allocate */
1208 1.1 reinoud sectors = (vat_length + sector_size -1) / sector_size;
1209 1.1 reinoud alloc_length = (sectors + 2) * sector_size;
1210 1.1 reinoud
1211 1.1 reinoud /* try to allocate the space */
1212 1.1 reinoud ump->vat_table_alloc_length = alloc_length;
1213 1.21 reinoud ump->vat_table = malloc(alloc_length, M_UDFVOLD, M_CANFAIL | M_WAITOK);
1214 1.1 reinoud if (!ump->vat_table)
1215 1.1 reinoud return ENOMEM; /* impossible to allocate */
1216 1.1 reinoud DPRINTF(VOLUMES, ("\talloced fine\n"));
1217 1.1 reinoud
1218 1.1 reinoud /* read it in! */
1219 1.1 reinoud raw_vat = (uint32_t *) ump->vat_table;
1220 1.1 reinoud error = udf_read_file_extent(vat_node, 0, sectors, (uint8_t *) raw_vat);
1221 1.1 reinoud if (error) {
1222 1.1 reinoud DPRINTF(VOLUMES, ("\tread failed : %d\n", error));
1223 1.1 reinoud /* not completely readable... :( bomb out */
1224 1.21 reinoud free(ump->vat_table, M_UDFVOLD);
1225 1.1 reinoud ump->vat_table = NULL;
1226 1.1 reinoud return error;
1227 1.9 christos }
1228 1.1 reinoud DPRINTF(VOLUMES, ("VAT read in fine!\n"));
1229 1.1 reinoud
1230 1.1 reinoud /*
1231 1.1 reinoud * check contents of the file if its the old 1.50 VAT table format.
1232 1.1 reinoud * Its notoriously broken and allthough some implementations support an
1233 1.1 reinoud * extention as defined in the UDF 1.50 errata document, its doubtfull
1234 1.1 reinoud * to be useable since a lot of implementations don't maintain it.
1235 1.1 reinoud */
1236 1.1 reinoud lvinfo = ump->logvol_info;
1237 1.1 reinoud
1238 1.1 reinoud if (filetype == 0) {
1239 1.1 reinoud /* definition */
1240 1.1 reinoud vat_offset = 0;
1241 1.1 reinoud vat_entries = (vat_length-36)/4;
1242 1.1 reinoud
1243 1.1 reinoud /* check 1.50 VAT */
1244 1.1 reinoud regid = (struct regid *) (raw_vat + vat_entries);
1245 1.1 reinoud regid_name = (char *) regid->id;
1246 1.1 reinoud error = strncmp(regid_name, "*UDF Virtual Alloc Tbl", 22);
1247 1.1 reinoud if (error) {
1248 1.1 reinoud DPRINTF(VOLUMES, ("VAT format 1.50 rejected\n"));
1249 1.21 reinoud free(ump->vat_table, M_UDFVOLD);
1250 1.1 reinoud ump->vat_table = NULL;
1251 1.1 reinoud return ENOENT;
1252 1.9 christos }
1253 1.1 reinoud /* TODO update LVID from "*UDF VAT LVExtension" ext. attr. */
1254 1.1 reinoud } else {
1255 1.1 reinoud vat = (struct udf_vat *) raw_vat;
1256 1.1 reinoud
1257 1.1 reinoud /* definition */
1258 1.1 reinoud vat_offset = vat->header_len;
1259 1.1 reinoud vat_entries = (vat_length - vat_offset)/4;
1260 1.1 reinoud
1261 1.1 reinoud assert(lvinfo);
1262 1.1 reinoud lvinfo->num_files = vat->num_files;
1263 1.1 reinoud lvinfo->num_directories = vat->num_directories;
1264 1.1 reinoud lvinfo->min_udf_readver = vat->min_udf_readver;
1265 1.1 reinoud lvinfo->min_udf_writever = vat->min_udf_writever;
1266 1.1 reinoud lvinfo->max_udf_writever = vat->max_udf_writever;
1267 1.9 christos }
1268 1.1 reinoud
1269 1.1 reinoud ump->vat_offset = vat_offset;
1270 1.1 reinoud ump->vat_entries = vat_entries;
1271 1.1 reinoud
1272 1.1 reinoud DPRINTF(VOLUMES, ("VAT format accepted, marking it closed\n"));
1273 1.1 reinoud ump->logvol_integrity->integrity_type = udf_rw32(UDF_INTEGRITY_CLOSED);
1274 1.1 reinoud ump->logvol_integrity->time = *mtime;
1275 1.1 reinoud
1276 1.1 reinoud return 0; /* success! */
1277 1.1 reinoud }
1278 1.1 reinoud
1279 1.1 reinoud /* --------------------------------------------------------------------- */
1280 1.1 reinoud
1281 1.1 reinoud static int
1282 1.1 reinoud udf_search_vat(struct udf_mount *ump, union udf_pmap *mapping)
1283 1.1 reinoud {
1284 1.1 reinoud struct udf_node *vat_node;
1285 1.1 reinoud struct long_ad icb_loc;
1286 1.1 reinoud uint32_t early_vat_loc, late_vat_loc, vat_loc;
1287 1.1 reinoud int error;
1288 1.1 reinoud
1289 1.1 reinoud /* mapping info not needed */
1290 1.1 reinoud mapping = mapping;
1291 1.1 reinoud
1292 1.13 reinoud vat_loc = ump->last_possible_vat_location;
1293 1.42 reinoud early_vat_loc = vat_loc - 256; /* 8 blocks of 32 sectors */
1294 1.13 reinoud early_vat_loc = MAX(early_vat_loc, ump->first_possible_vat_location);
1295 1.1 reinoud late_vat_loc = vat_loc + 1024;
1296 1.1 reinoud
1297 1.1 reinoud /* TODO first search last sector? */
1298 1.1 reinoud do {
1299 1.1 reinoud DPRINTF(VOLUMES, ("Checking for VAT at sector %d\n", vat_loc));
1300 1.1 reinoud icb_loc.loc.part_num = udf_rw16(UDF_VTOP_RAWPART);
1301 1.1 reinoud icb_loc.loc.lb_num = udf_rw32(vat_loc);
1302 1.1 reinoud
1303 1.1 reinoud error = udf_get_node(ump, &icb_loc, &vat_node);
1304 1.1 reinoud if (!error) error = udf_check_for_vat(vat_node);
1305 1.1 reinoud if (!error) break;
1306 1.1 reinoud if (vat_node) {
1307 1.1 reinoud vput(vat_node->vnode);
1308 1.40 reinoud vat_node = NULL;
1309 1.9 christos }
1310 1.1 reinoud vat_loc--; /* walk backwards */
1311 1.1 reinoud } while (vat_loc >= early_vat_loc);
1312 1.1 reinoud
1313 1.1 reinoud /* we don't need our VAT node anymore */
1314 1.1 reinoud if (vat_node) {
1315 1.1 reinoud vput(vat_node->vnode);
1316 1.1 reinoud udf_dispose_node(vat_node);
1317 1.9 christos }
1318 1.1 reinoud
1319 1.1 reinoud return error;
1320 1.1 reinoud }
1321 1.1 reinoud
1322 1.1 reinoud /* --------------------------------------------------------------------- */
1323 1.1 reinoud
1324 1.1 reinoud static int
1325 1.1 reinoud udf_read_sparables(struct udf_mount *ump, union udf_pmap *mapping)
1326 1.1 reinoud {
1327 1.1 reinoud union dscrptr *dscr;
1328 1.26 reinoud struct part_map_spare *pms = &mapping->pms;
1329 1.1 reinoud uint32_t lb_num;
1330 1.1 reinoud int spar, error;
1331 1.1 reinoud
1332 1.1 reinoud /*
1333 1.1 reinoud * The partition mapping passed on to us specifies the information we
1334 1.1 reinoud * need to locate and initialise the sparable partition mapping
1335 1.1 reinoud * information we need.
1336 1.1 reinoud */
1337 1.1 reinoud
1338 1.1 reinoud DPRINTF(VOLUMES, ("Read sparable table\n"));
1339 1.1 reinoud ump->sparable_packet_len = udf_rw16(pms->packet_len);
1340 1.1 reinoud for (spar = 0; spar < pms->n_st; spar++) {
1341 1.1 reinoud lb_num = pms->st_loc[spar];
1342 1.1 reinoud DPRINTF(VOLUMES, ("Checking for sparing table %d\n", lb_num));
1343 1.1 reinoud error = udf_read_descriptor(ump, lb_num, M_UDFVOLD, &dscr);
1344 1.1 reinoud if (!error && dscr) {
1345 1.1 reinoud if (udf_rw16(dscr->tag.id) == TAGID_SPARING_TABLE) {
1346 1.1 reinoud if (ump->sparing_table)
1347 1.1 reinoud free(ump->sparing_table, M_UDFVOLD);
1348 1.1 reinoud ump->sparing_table = &dscr->spt;
1349 1.1 reinoud dscr = NULL;
1350 1.1 reinoud DPRINTF(VOLUMES,
1351 1.1 reinoud ("Sparing table accepted (%d entries)\n",
1352 1.1 reinoud udf_rw16(ump->sparing_table->rt_l)));
1353 1.1 reinoud break; /* we're done */
1354 1.9 christos }
1355 1.9 christos }
1356 1.1 reinoud if (dscr)
1357 1.1 reinoud free(dscr, M_UDFVOLD);
1358 1.9 christos }
1359 1.1 reinoud
1360 1.1 reinoud if (ump->sparing_table)
1361 1.1 reinoud return 0;
1362 1.1 reinoud
1363 1.1 reinoud return ENOENT;
1364 1.1 reinoud }
1365 1.1 reinoud
1366 1.1 reinoud /* --------------------------------------------------------------------- */
1367 1.1 reinoud
1368 1.26 reinoud #define UDF_SET_SYSTEMFILE(vp) \
1369 1.39 ad /* XXXAD Is the vnode locked? */ \
1370 1.39 ad (vp)->v_vflag |= VV_SYSTEM; \
1371 1.26 reinoud vref(vp); \
1372 1.26 reinoud vput(vp); \
1373 1.26 reinoud
1374 1.26 reinoud static int
1375 1.26 reinoud udf_read_metadata_files(struct udf_mount *ump, union udf_pmap *mapping)
1376 1.26 reinoud {
1377 1.26 reinoud struct part_map_meta *pmm = &mapping->pmm;
1378 1.26 reinoud struct long_ad icb_loc;
1379 1.26 reinoud struct vnode *vp;
1380 1.26 reinoud int error;
1381 1.26 reinoud
1382 1.26 reinoud DPRINTF(VOLUMES, ("Reading in Metadata files\n"));
1383 1.26 reinoud icb_loc.loc.part_num = pmm->part_num;
1384 1.26 reinoud icb_loc.loc.lb_num = pmm->meta_file_lbn;
1385 1.26 reinoud DPRINTF(VOLUMES, ("Metadata file\n"));
1386 1.26 reinoud error = udf_get_node(ump, &icb_loc, &ump->metadata_file);
1387 1.26 reinoud if (ump->metadata_file) {
1388 1.26 reinoud vp = ump->metadata_file->vnode;
1389 1.26 reinoud UDF_SET_SYSTEMFILE(vp);
1390 1.26 reinoud }
1391 1.26 reinoud
1392 1.26 reinoud icb_loc.loc.lb_num = pmm->meta_mirror_file_lbn;
1393 1.26 reinoud if (icb_loc.loc.lb_num != -1) {
1394 1.26 reinoud DPRINTF(VOLUMES, ("Metadata copy file\n"));
1395 1.26 reinoud error = udf_get_node(ump, &icb_loc, &ump->metadatamirror_file);
1396 1.26 reinoud if (ump->metadatamirror_file) {
1397 1.26 reinoud vp = ump->metadatamirror_file->vnode;
1398 1.26 reinoud UDF_SET_SYSTEMFILE(vp);
1399 1.26 reinoud }
1400 1.26 reinoud }
1401 1.26 reinoud
1402 1.26 reinoud icb_loc.loc.lb_num = pmm->meta_bitmap_file_lbn;
1403 1.26 reinoud if (icb_loc.loc.lb_num != -1) {
1404 1.26 reinoud DPRINTF(VOLUMES, ("Metadata bitmap file\n"));
1405 1.26 reinoud error = udf_get_node(ump, &icb_loc, &ump->metadatabitmap_file);
1406 1.26 reinoud if (ump->metadatabitmap_file) {
1407 1.26 reinoud vp = ump->metadatabitmap_file->vnode;
1408 1.26 reinoud UDF_SET_SYSTEMFILE(vp);
1409 1.26 reinoud }
1410 1.26 reinoud }
1411 1.26 reinoud
1412 1.26 reinoud /* if we're mounting read-only we relax the requirements */
1413 1.26 reinoud if (ump->vfs_mountp->mnt_flag & MNT_RDONLY) {
1414 1.26 reinoud error = EFAULT;
1415 1.26 reinoud if (ump->metadata_file)
1416 1.26 reinoud error = 0;
1417 1.26 reinoud if ((ump->metadata_file == NULL) && (ump->metadatamirror_file)) {
1418 1.26 reinoud printf( "udf mount: Metadata file not readable, "
1419 1.26 reinoud "substituting Metadata copy file\n");
1420 1.26 reinoud ump->metadata_file = ump->metadatamirror_file;
1421 1.26 reinoud ump->metadatamirror_file = NULL;
1422 1.26 reinoud error = 0;
1423 1.26 reinoud }
1424 1.26 reinoud } else {
1425 1.26 reinoud /* mounting read/write */
1426 1.26 reinoud DPRINTF(VOLUMES, ("udf mount: read only file system\n"));
1427 1.26 reinoud error = EROFS;
1428 1.26 reinoud }
1429 1.26 reinoud DPRINTFIF(VOLUMES, error, ("udf mount: failed to read "
1430 1.26 reinoud "metadata files\n"));
1431 1.26 reinoud return error;
1432 1.26 reinoud }
1433 1.26 reinoud #undef UDF_SET_SYSTEMFILE
1434 1.26 reinoud
1435 1.26 reinoud /* --------------------------------------------------------------------- */
1436 1.26 reinoud
1437 1.1 reinoud int
1438 1.23 christos udf_read_vds_tables(struct udf_mount *ump, struct udf_args *args)
1439 1.1 reinoud {
1440 1.1 reinoud union udf_pmap *mapping;
1441 1.1 reinoud uint32_t n_pm, mt_l;
1442 1.1 reinoud uint32_t log_part;
1443 1.1 reinoud uint8_t *pmap_pos;
1444 1.1 reinoud int pmap_size;
1445 1.1 reinoud int error;
1446 1.1 reinoud
1447 1.1 reinoud /* We have to iterate again over the part mappings for locations */
1448 1.1 reinoud n_pm = udf_rw32(ump->logical_vol->n_pm); /* num partmaps */
1449 1.1 reinoud mt_l = udf_rw32(ump->logical_vol->mt_l); /* partmaps data length */
1450 1.1 reinoud pmap_pos = ump->logical_vol->maps;
1451 1.1 reinoud
1452 1.1 reinoud for (log_part = 0; log_part < n_pm; log_part++) {
1453 1.1 reinoud mapping = (union udf_pmap *) pmap_pos;
1454 1.1 reinoud switch (ump->vtop_tp[log_part]) {
1455 1.1 reinoud case UDF_VTOP_TYPE_PHYS :
1456 1.1 reinoud /* nothing */
1457 1.1 reinoud break;
1458 1.1 reinoud case UDF_VTOP_TYPE_VIRT :
1459 1.1 reinoud /* search and load VAT */
1460 1.1 reinoud error = udf_search_vat(ump, mapping);
1461 1.1 reinoud if (error)
1462 1.1 reinoud return ENOENT;
1463 1.1 reinoud break;
1464 1.1 reinoud case UDF_VTOP_TYPE_SPARABLE :
1465 1.1 reinoud /* load one of the sparable tables */
1466 1.1 reinoud error = udf_read_sparables(ump, mapping);
1467 1.16 reinoud if (error)
1468 1.16 reinoud return ENOENT;
1469 1.1 reinoud break;
1470 1.1 reinoud case UDF_VTOP_TYPE_META :
1471 1.26 reinoud /* load the associated file descriptors */
1472 1.26 reinoud error = udf_read_metadata_files(ump, mapping);
1473 1.26 reinoud if (error)
1474 1.26 reinoud return ENOENT;
1475 1.26 reinoud break;
1476 1.1 reinoud default:
1477 1.1 reinoud break;
1478 1.9 christos }
1479 1.1 reinoud pmap_size = pmap_pos[1];
1480 1.1 reinoud pmap_pos += pmap_size;
1481 1.9 christos }
1482 1.1 reinoud
1483 1.1 reinoud return 0;
1484 1.1 reinoud }
1485 1.1 reinoud
1486 1.1 reinoud /* --------------------------------------------------------------------- */
1487 1.1 reinoud
1488 1.1 reinoud int
1489 1.23 christos udf_read_rootdirs(struct udf_mount *ump, struct udf_args *args)
1490 1.1 reinoud {
1491 1.1 reinoud struct udf_node *rootdir_node, *streamdir_node;
1492 1.1 reinoud union dscrptr *dscr;
1493 1.1 reinoud struct long_ad fsd_loc, *dir_loc;
1494 1.1 reinoud uint32_t lb_num, dummy;
1495 1.1 reinoud uint32_t fsd_len;
1496 1.1 reinoud int dscr_type;
1497 1.1 reinoud int error;
1498 1.1 reinoud
1499 1.25 wiz /* TODO implement FSD reading in separate function like integrity? */
1500 1.1 reinoud /* get fileset descriptor sequence */
1501 1.1 reinoud fsd_loc = ump->logical_vol->lv_fsd_loc;
1502 1.1 reinoud fsd_len = udf_rw32(fsd_loc.len);
1503 1.1 reinoud
1504 1.1 reinoud dscr = NULL;
1505 1.1 reinoud error = 0;
1506 1.1 reinoud while (fsd_len || error) {
1507 1.1 reinoud DPRINTF(VOLUMES, ("fsd_len = %d\n", fsd_len));
1508 1.1 reinoud /* translate fsd_loc to lb_num */
1509 1.1 reinoud error = udf_translate_vtop(ump, &fsd_loc, &lb_num, &dummy);
1510 1.1 reinoud if (error)
1511 1.1 reinoud break;
1512 1.1 reinoud DPRINTF(VOLUMES, ("Reading FSD at lb %d\n", lb_num));
1513 1.1 reinoud error = udf_read_descriptor(ump, lb_num, M_UDFVOLD, &dscr);
1514 1.1 reinoud /* end markers */
1515 1.1 reinoud if (error || (dscr == NULL))
1516 1.1 reinoud break;
1517 1.1 reinoud
1518 1.1 reinoud /* analyse */
1519 1.1 reinoud dscr_type = udf_rw16(dscr->tag.id);
1520 1.1 reinoud if (dscr_type == TAGID_TERM)
1521 1.1 reinoud break;
1522 1.1 reinoud if (dscr_type != TAGID_FSD) {
1523 1.1 reinoud free(dscr, M_UDFVOLD);
1524 1.1 reinoud return ENOENT;
1525 1.9 christos }
1526 1.1 reinoud
1527 1.1 reinoud /*
1528 1.1 reinoud * TODO check for multiple fileset descriptors; its only
1529 1.1 reinoud * picking the last now. Also check for FSD
1530 1.1 reinoud * correctness/interpretability
1531 1.1 reinoud */
1532 1.1 reinoud
1533 1.1 reinoud /* update */
1534 1.1 reinoud if (ump->fileset_desc) {
1535 1.1 reinoud free(ump->fileset_desc, M_UDFVOLD);
1536 1.9 christos }
1537 1.1 reinoud ump->fileset_desc = &dscr->fsd;
1538 1.1 reinoud dscr = NULL;
1539 1.1 reinoud
1540 1.1 reinoud /* continue to the next fsd */
1541 1.1 reinoud fsd_len -= ump->discinfo.sector_size;
1542 1.1 reinoud fsd_loc.loc.lb_num = udf_rw32(udf_rw32(fsd_loc.loc.lb_num)+1);
1543 1.1 reinoud
1544 1.1 reinoud /* follow up to fsd->next_ex (long_ad) if its not null */
1545 1.1 reinoud if (udf_rw32(ump->fileset_desc->next_ex.len)) {
1546 1.1 reinoud DPRINTF(VOLUMES, ("follow up FSD extent\n"));
1547 1.1 reinoud fsd_loc = ump->fileset_desc->next_ex;
1548 1.1 reinoud fsd_len = udf_rw32(ump->fileset_desc->next_ex.len);
1549 1.9 christos }
1550 1.9 christos }
1551 1.1 reinoud if (dscr)
1552 1.1 reinoud free(dscr, M_UDFVOLD);
1553 1.1 reinoud
1554 1.1 reinoud /* there has to be one */
1555 1.1 reinoud if (ump->fileset_desc == NULL)
1556 1.1 reinoud return ENOENT;
1557 1.1 reinoud
1558 1.1 reinoud DPRINTF(VOLUMES, ("FSD read in fine\n"));
1559 1.1 reinoud
1560 1.1 reinoud /*
1561 1.1 reinoud * Now the FSD is known, read in the rootdirectory and if one exists,
1562 1.1 reinoud * the system stream dir. Some files in the system streamdir are not
1563 1.1 reinoud * wanted in this implementation since they are not maintained. If
1564 1.1 reinoud * writing is enabled we'll delete these files if they exist.
1565 1.1 reinoud */
1566 1.1 reinoud
1567 1.1 reinoud rootdir_node = streamdir_node = NULL;
1568 1.1 reinoud dir_loc = NULL;
1569 1.1 reinoud
1570 1.1 reinoud /* try to read in the rootdir */
1571 1.1 reinoud dir_loc = &ump->fileset_desc->rootdir_icb;
1572 1.1 reinoud error = udf_get_node(ump, dir_loc, &rootdir_node);
1573 1.1 reinoud if (error)
1574 1.1 reinoud return ENOENT;
1575 1.1 reinoud
1576 1.1 reinoud /* aparently it read in fine */
1577 1.1 reinoud
1578 1.1 reinoud /*
1579 1.1 reinoud * Try the system stream directory; not very likely in the ones we
1580 1.1 reinoud * test, but for completeness.
1581 1.1 reinoud */
1582 1.1 reinoud dir_loc = &ump->fileset_desc->streamdir_icb;
1583 1.1 reinoud if (udf_rw32(dir_loc->len)) {
1584 1.1 reinoud error = udf_get_node(ump, dir_loc, &streamdir_node);
1585 1.1 reinoud if (error)
1586 1.1 reinoud printf("udf mount: streamdir defined but ignored\n");
1587 1.1 reinoud if (!error) {
1588 1.1 reinoud /*
1589 1.1 reinoud * TODO process streamdir `baddies' i.e. files we dont
1590 1.1 reinoud * want if R/W
1591 1.1 reinoud */
1592 1.9 christos }
1593 1.9 christos }
1594 1.1 reinoud
1595 1.1 reinoud DPRINTF(VOLUMES, ("Rootdir(s) read in fine\n"));
1596 1.1 reinoud
1597 1.1 reinoud /* release the vnodes again; they'll be auto-recycled later */
1598 1.1 reinoud if (streamdir_node) {
1599 1.1 reinoud vput(streamdir_node->vnode);
1600 1.9 christos }
1601 1.1 reinoud if (rootdir_node) {
1602 1.1 reinoud vput(rootdir_node->vnode);
1603 1.9 christos }
1604 1.1 reinoud
1605 1.1 reinoud return 0;
1606 1.1 reinoud }
1607 1.1 reinoud
1608 1.1 reinoud /* --------------------------------------------------------------------- */
1609 1.1 reinoud
1610 1.1 reinoud int
1611 1.1 reinoud udf_translate_vtop(struct udf_mount *ump, struct long_ad *icb_loc,
1612 1.1 reinoud uint32_t *lb_numres, uint32_t *extres)
1613 1.1 reinoud {
1614 1.1 reinoud struct part_desc *pdesc;
1615 1.1 reinoud struct spare_map_entry *sme;
1616 1.26 reinoud struct file_entry *fe;
1617 1.26 reinoud struct extfile_entry *efe;
1618 1.26 reinoud struct short_ad *s_ad;
1619 1.26 reinoud struct long_ad *l_ad;
1620 1.26 reinoud uint64_t cur_offset;
1621 1.1 reinoud uint32_t *trans;
1622 1.26 reinoud uint32_t lb_num, plb_num, lb_rel, lb_packet;
1623 1.26 reinoud uint32_t sector_size, len, alloclen;
1624 1.26 reinoud uint8_t *pos;
1625 1.26 reinoud int rel, vpart, part, addr_type, icblen, icbflags, flags;
1626 1.1 reinoud
1627 1.1 reinoud assert(ump && icb_loc && lb_numres);
1628 1.1 reinoud
1629 1.1 reinoud vpart = udf_rw16(icb_loc->loc.part_num);
1630 1.1 reinoud lb_num = udf_rw32(icb_loc->loc.lb_num);
1631 1.1 reinoud if (vpart < 0 || vpart > UDF_VTOP_RAWPART)
1632 1.1 reinoud return EINVAL;
1633 1.1 reinoud
1634 1.26 reinoud part = ump->vtop[vpart];
1635 1.26 reinoud pdesc = ump->partitions[part];
1636 1.26 reinoud
1637 1.1 reinoud switch (ump->vtop_tp[vpart]) {
1638 1.1 reinoud case UDF_VTOP_TYPE_RAW :
1639 1.1 reinoud /* 1:1 to the end of the device */
1640 1.1 reinoud *lb_numres = lb_num;
1641 1.1 reinoud *extres = INT_MAX;
1642 1.1 reinoud return 0;
1643 1.1 reinoud case UDF_VTOP_TYPE_PHYS :
1644 1.1 reinoud /* transform into its disc logical block */
1645 1.1 reinoud if (lb_num > udf_rw32(pdesc->part_len))
1646 1.1 reinoud return EINVAL;
1647 1.1 reinoud *lb_numres = lb_num + udf_rw32(pdesc->start_loc);
1648 1.1 reinoud
1649 1.1 reinoud /* extent from here to the end of the partition */
1650 1.1 reinoud *extres = udf_rw32(pdesc->part_len) - lb_num;
1651 1.1 reinoud return 0;
1652 1.1 reinoud case UDF_VTOP_TYPE_VIRT :
1653 1.1 reinoud /* only maps one sector, lookup in VAT */
1654 1.1 reinoud if (lb_num >= ump->vat_entries) /* XXX > or >= ? */
1655 1.1 reinoud return EINVAL;
1656 1.1 reinoud
1657 1.1 reinoud /* lookup in virtual allocation table */
1658 1.1 reinoud trans = (uint32_t *) (ump->vat_table + ump->vat_offset);
1659 1.1 reinoud lb_num = udf_rw32(trans[lb_num]);
1660 1.1 reinoud
1661 1.1 reinoud /* transform into its disc logical block */
1662 1.1 reinoud if (lb_num > udf_rw32(pdesc->part_len))
1663 1.1 reinoud return EINVAL;
1664 1.1 reinoud *lb_numres = lb_num + udf_rw32(pdesc->start_loc);
1665 1.1 reinoud
1666 1.1 reinoud /* just one logical block */
1667 1.1 reinoud *extres = 1;
1668 1.1 reinoud return 0;
1669 1.1 reinoud case UDF_VTOP_TYPE_SPARABLE :
1670 1.1 reinoud /* check if the packet containing the lb_num is remapped */
1671 1.1 reinoud lb_packet = lb_num / ump->sparable_packet_len;
1672 1.1 reinoud lb_rel = lb_num % ump->sparable_packet_len;
1673 1.1 reinoud
1674 1.1 reinoud for (rel = 0; rel < udf_rw16(ump->sparing_table->rt_l); rel++) {
1675 1.1 reinoud sme = &ump->sparing_table->entries[rel];
1676 1.1 reinoud if (lb_packet == udf_rw32(sme->org)) {
1677 1.1 reinoud /* NOTE maps to absolute disc logical block! */
1678 1.1 reinoud *lb_numres = udf_rw32(sme->map) + lb_rel;
1679 1.1 reinoud *extres = ump->sparable_packet_len - lb_rel;
1680 1.1 reinoud return 0;
1681 1.9 christos }
1682 1.9 christos }
1683 1.1 reinoud
1684 1.1 reinoud /* transform into its disc logical block */
1685 1.1 reinoud if (lb_num > udf_rw32(pdesc->part_len))
1686 1.1 reinoud return EINVAL;
1687 1.1 reinoud *lb_numres = lb_num + udf_rw32(pdesc->start_loc);
1688 1.1 reinoud
1689 1.1 reinoud /* rest of block */
1690 1.1 reinoud *extres = ump->sparable_packet_len - lb_rel;
1691 1.1 reinoud return 0;
1692 1.1 reinoud case UDF_VTOP_TYPE_META :
1693 1.26 reinoud /* we have to look into the file's allocation descriptors */
1694 1.26 reinoud /* free after udf_translate_file_extent() */
1695 1.26 reinoud /* XXX sector size or lb_size? */
1696 1.26 reinoud sector_size = ump->discinfo.sector_size;
1697 1.26 reinoud /* XXX should we claim exclusive access to the metafile ? */
1698 1.26 reinoud fe = ump->metadata_file->fe;
1699 1.26 reinoud efe = ump->metadata_file->efe;
1700 1.26 reinoud if (fe) {
1701 1.26 reinoud alloclen = udf_rw32(fe->l_ad);
1702 1.26 reinoud pos = &fe->data[0] + udf_rw32(fe->l_ea);
1703 1.26 reinoud icbflags = udf_rw16(fe->icbtag.flags);
1704 1.27 reinoud } else {
1705 1.27 reinoud assert(efe);
1706 1.26 reinoud alloclen = udf_rw32(efe->l_ad);
1707 1.26 reinoud pos = &efe->data[0] + udf_rw32(efe->l_ea);
1708 1.26 reinoud icbflags = udf_rw16(efe->icbtag.flags);
1709 1.26 reinoud }
1710 1.26 reinoud addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
1711 1.26 reinoud
1712 1.26 reinoud cur_offset = 0;
1713 1.26 reinoud while (alloclen) {
1714 1.26 reinoud if (addr_type == UDF_ICB_SHORT_ALLOC) {
1715 1.26 reinoud icblen = sizeof(struct short_ad);
1716 1.26 reinoud s_ad = (struct short_ad *) pos;
1717 1.26 reinoud len = udf_rw32(s_ad->len);
1718 1.26 reinoud plb_num = udf_rw32(s_ad->lb_num);
1719 1.26 reinoud } else {
1720 1.26 reinoud /* should not be present, but why not */
1721 1.26 reinoud icblen = sizeof(struct long_ad);
1722 1.26 reinoud l_ad = (struct long_ad *) pos;
1723 1.26 reinoud len = udf_rw32(l_ad->len);
1724 1.26 reinoud plb_num = udf_rw32(l_ad->loc.lb_num);
1725 1.26 reinoud /* pvpart_num = udf_rw16(l_ad->loc.part_num); */
1726 1.26 reinoud }
1727 1.26 reinoud /* process extent */
1728 1.26 reinoud flags = UDF_EXT_FLAGS(len);
1729 1.26 reinoud len = UDF_EXT_LEN(len);
1730 1.26 reinoud
1731 1.26 reinoud if (cur_offset + len > lb_num * sector_size) {
1732 1.26 reinoud if (flags != UDF_EXT_ALLOCATED)
1733 1.26 reinoud return EINVAL;
1734 1.26 reinoud lb_rel = lb_num - cur_offset / sector_size;
1735 1.26 reinoud /* remainder of this extent */
1736 1.26 reinoud *lb_numres = plb_num + lb_rel +
1737 1.26 reinoud udf_rw32(pdesc->start_loc);
1738 1.26 reinoud *extres = (len / sector_size) - lb_rel;
1739 1.26 reinoud return 0;
1740 1.26 reinoud }
1741 1.26 reinoud cur_offset += len;
1742 1.26 reinoud pos += icblen;
1743 1.26 reinoud alloclen -= icblen;
1744 1.26 reinoud }
1745 1.26 reinoud /* not found */
1746 1.26 reinoud DPRINTF(TRANSLATE, ("Metadata partition translation failed\n"));
1747 1.26 reinoud return EINVAL;
1748 1.1 reinoud default:
1749 1.1 reinoud printf("UDF vtop translation scheme %d unimplemented yet\n",
1750 1.1 reinoud ump->vtop_tp[vpart]);
1751 1.9 christos }
1752 1.1 reinoud
1753 1.1 reinoud return EINVAL;
1754 1.1 reinoud }
1755 1.1 reinoud
1756 1.1 reinoud /* --------------------------------------------------------------------- */
1757 1.1 reinoud
1758 1.1 reinoud /* To make absolutely sure we are NOT returning zero, add one :) */
1759 1.1 reinoud
1760 1.1 reinoud long
1761 1.1 reinoud udf_calchash(struct long_ad *icbptr)
1762 1.1 reinoud {
1763 1.1 reinoud /* ought to be enough since each mountpoint has its own chain */
1764 1.1 reinoud return udf_rw32(icbptr->loc.lb_num) + 1;
1765 1.1 reinoud }
1766 1.1 reinoud
1767 1.1 reinoud /* --------------------------------------------------------------------- */
1768 1.1 reinoud
1769 1.1 reinoud static struct udf_node *
1770 1.1 reinoud udf_hashget(struct udf_mount *ump, struct long_ad *icbptr)
1771 1.1 reinoud {
1772 1.1 reinoud struct udf_node *unp;
1773 1.1 reinoud struct vnode *vp;
1774 1.1 reinoud uint32_t hashline;
1775 1.1 reinoud
1776 1.1 reinoud loop:
1777 1.39 ad mutex_enter(&ump->ihash_lock);
1778 1.1 reinoud
1779 1.1 reinoud hashline = udf_calchash(icbptr) & UDF_INODE_HASHMASK;
1780 1.1 reinoud LIST_FOREACH(unp, &ump->udf_nodes[hashline], hashchain) {
1781 1.1 reinoud assert(unp);
1782 1.1 reinoud if (unp->loc.loc.lb_num == icbptr->loc.lb_num &&
1783 1.1 reinoud unp->loc.loc.part_num == icbptr->loc.part_num) {
1784 1.1 reinoud vp = unp->vnode;
1785 1.1 reinoud assert(vp);
1786 1.1 reinoud simple_lock(&vp->v_interlock);
1787 1.39 ad mutex_exit(&ump->ihash_lock);
1788 1.1 reinoud if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
1789 1.1 reinoud goto loop;
1790 1.1 reinoud return unp;
1791 1.9 christos }
1792 1.9 christos }
1793 1.39 ad mutex_exit(&ump->ihash_lock);
1794 1.1 reinoud
1795 1.1 reinoud return NULL;
1796 1.9 christos }
1797 1.1 reinoud
1798 1.1 reinoud /* --------------------------------------------------------------------- */
1799 1.1 reinoud
1800 1.1 reinoud static void
1801 1.1 reinoud udf_hashins(struct udf_node *unp)
1802 1.1 reinoud {
1803 1.1 reinoud struct udf_mount *ump;
1804 1.1 reinoud uint32_t hashline;
1805 1.1 reinoud
1806 1.1 reinoud ump = unp->ump;
1807 1.39 ad mutex_enter(&ump->ihash_lock);
1808 1.1 reinoud
1809 1.1 reinoud hashline = udf_calchash(&unp->loc) & UDF_INODE_HASHMASK;
1810 1.1 reinoud LIST_INSERT_HEAD(&ump->udf_nodes[hashline], unp, hashchain);
1811 1.1 reinoud
1812 1.39 ad mutex_exit(&ump->ihash_lock);
1813 1.1 reinoud }
1814 1.1 reinoud
1815 1.1 reinoud /* --------------------------------------------------------------------- */
1816 1.1 reinoud
1817 1.1 reinoud static void
1818 1.1 reinoud udf_hashrem(struct udf_node *unp)
1819 1.1 reinoud {
1820 1.1 reinoud struct udf_mount *ump;
1821 1.1 reinoud
1822 1.1 reinoud ump = unp->ump;
1823 1.39 ad mutex_enter(&ump->ihash_lock);
1824 1.1 reinoud
1825 1.1 reinoud LIST_REMOVE(unp, hashchain);
1826 1.1 reinoud
1827 1.39 ad mutex_exit(&ump->ihash_lock);
1828 1.1 reinoud }
1829 1.1 reinoud
1830 1.1 reinoud /* --------------------------------------------------------------------- */
1831 1.1 reinoud
1832 1.1 reinoud int
1833 1.1 reinoud udf_dispose_locked_node(struct udf_node *node)
1834 1.1 reinoud {
1835 1.1 reinoud if (!node)
1836 1.1 reinoud return 0;
1837 1.1 reinoud if (node->vnode)
1838 1.1 reinoud VOP_UNLOCK(node->vnode, 0);
1839 1.1 reinoud return udf_dispose_node(node);
1840 1.1 reinoud }
1841 1.1 reinoud
1842 1.1 reinoud /* --------------------------------------------------------------------- */
1843 1.1 reinoud
1844 1.1 reinoud int
1845 1.1 reinoud udf_dispose_node(struct udf_node *node)
1846 1.1 reinoud {
1847 1.1 reinoud struct vnode *vp;
1848 1.1 reinoud
1849 1.1 reinoud DPRINTF(NODE, ("udf_dispose_node called on node %p\n", node));
1850 1.1 reinoud if (!node) {
1851 1.1 reinoud DPRINTF(NODE, ("UDF: Dispose node on node NULL, ignoring\n"));
1852 1.1 reinoud return 0;
1853 1.9 christos }
1854 1.1 reinoud
1855 1.1 reinoud vp = node->vnode;
1856 1.1 reinoud
1857 1.1 reinoud /* TODO extended attributes and streamdir */
1858 1.1 reinoud
1859 1.1 reinoud /* remove from our hash lookup table */
1860 1.1 reinoud udf_hashrem(node);
1861 1.1 reinoud
1862 1.32 ad /* destroy genfs structures */
1863 1.32 ad genfs_node_destroy(vp);
1864 1.32 ad
1865 1.1 reinoud /* dissociate our udf_node from the vnode */
1866 1.1 reinoud vp->v_data = NULL;
1867 1.1 reinoud
1868 1.1 reinoud /* free associated memory and the node itself */
1869 1.1 reinoud if (node->fe)
1870 1.14 reinoud pool_put(node->ump->desc_pool, node->fe);
1871 1.1 reinoud if (node->efe)
1872 1.14 reinoud pool_put(node->ump->desc_pool, node->efe);
1873 1.1 reinoud pool_put(&udf_node_pool, node);
1874 1.1 reinoud
1875 1.1 reinoud return 0;
1876 1.1 reinoud }
1877 1.1 reinoud
1878 1.1 reinoud /* --------------------------------------------------------------------- */
1879 1.1 reinoud
1880 1.1 reinoud /*
1881 1.1 reinoud * Genfs interfacing
1882 1.1 reinoud *
1883 1.1 reinoud * static const struct genfs_ops udffs_genfsops = {
1884 1.1 reinoud * .gop_size = genfs_size,
1885 1.1 reinoud * size of transfers
1886 1.1 reinoud * .gop_alloc = udf_gop_alloc,
1887 1.1 reinoud * unknown
1888 1.1 reinoud * .gop_write = genfs_gop_write,
1889 1.1 reinoud * putpages interface code
1890 1.1 reinoud * .gop_markupdate = udf_gop_markupdate,
1891 1.1 reinoud * set update/modify flags etc.
1892 1.9 christos * }
1893 1.1 reinoud */
1894 1.1 reinoud
1895 1.1 reinoud /*
1896 1.1 reinoud * Genfs interface. These four functions are the only ones defined though not
1897 1.1 reinoud * documented... great.... why is chosen for the `.' initialisers i dont know
1898 1.1 reinoud * but other filingsystems seem to use it this way.
1899 1.1 reinoud */
1900 1.1 reinoud
1901 1.1 reinoud static int
1902 1.23 christos udf_gop_alloc(struct vnode *vp, off_t off,
1903 1.23 christos off_t len, int flags, kauth_cred_t cred)
1904 1.1 reinoud {
1905 1.1 reinoud return 0;
1906 1.1 reinoud }
1907 1.1 reinoud
1908 1.1 reinoud
1909 1.1 reinoud static void
1910 1.23 christos udf_gop_markupdate(struct vnode *vp, int flags)
1911 1.1 reinoud {
1912 1.1 reinoud struct udf_node *udf_node = VTOI(vp);
1913 1.1 reinoud u_long mask;
1914 1.1 reinoud
1915 1.1 reinoud udf_node = udf_node; /* shut up gcc */
1916 1.1 reinoud
1917 1.1 reinoud mask = 0;
1918 1.1 reinoud #ifdef notyet
1919 1.1 reinoud if ((flags & GOP_UPDATE_ACCESSED) != 0) {
1920 1.1 reinoud mask = UDF_SET_ACCESS;
1921 1.1 reinoud }
1922 1.1 reinoud if ((flags & GOP_UPDATE_MODIFIED) != 0) {
1923 1.1 reinoud mask |= UDF_SET_UPDATE;
1924 1.1 reinoud }
1925 1.1 reinoud if (mask) {
1926 1.1 reinoud udf_node->update_flag |= mask;
1927 1.1 reinoud }
1928 1.1 reinoud #endif
1929 1.1 reinoud /* msdosfs doesn't do it, but shouldn't we update the times here? */
1930 1.1 reinoud }
1931 1.1 reinoud
1932 1.1 reinoud
1933 1.1 reinoud static const struct genfs_ops udf_genfsops = {
1934 1.1 reinoud .gop_size = genfs_size,
1935 1.1 reinoud .gop_alloc = udf_gop_alloc,
1936 1.1 reinoud .gop_write = genfs_gop_write,
1937 1.1 reinoud .gop_markupdate = udf_gop_markupdate,
1938 1.1 reinoud };
1939 1.1 reinoud
1940 1.1 reinoud /* --------------------------------------------------------------------- */
1941 1.1 reinoud
1942 1.1 reinoud /*
1943 1.1 reinoud * Each node can have an attached streamdir node though not
1944 1.1 reinoud * recursively. These are otherwise known as named substreams/named
1945 1.1 reinoud * extended attributes that have no size limitations.
1946 1.1 reinoud *
1947 1.1 reinoud * `Normal' extended attributes are indicated with a number and are recorded
1948 1.1 reinoud * in either the fe/efe descriptor itself for small descriptors or recorded in
1949 1.1 reinoud * the attached extended attribute file. Since this file can get fragmented,
1950 1.1 reinoud * care ought to be taken.
1951 1.1 reinoud */
1952 1.1 reinoud
1953 1.1 reinoud int
1954 1.1 reinoud udf_get_node(struct udf_mount *ump, struct long_ad *node_icb_loc,
1955 1.1 reinoud struct udf_node **noderes)
1956 1.1 reinoud {
1957 1.1 reinoud union dscrptr *dscr, *tmpdscr;
1958 1.1 reinoud struct udf_node *node;
1959 1.1 reinoud struct vnode *nvp;
1960 1.1 reinoud struct long_ad icb_loc;
1961 1.1 reinoud extern int (**udf_vnodeop_p)(void *);
1962 1.1 reinoud uint64_t file_size;
1963 1.1 reinoud uint32_t lb_size, sector, dummy;
1964 1.1 reinoud int udf_file_type, dscr_type, strat, strat4096, needs_indirect;
1965 1.1 reinoud int error;
1966 1.1 reinoud
1967 1.1 reinoud DPRINTF(NODE, ("udf_get_node called\n"));
1968 1.1 reinoud *noderes = node = NULL;
1969 1.1 reinoud
1970 1.1 reinoud /* lock to disallow simultanious creation of same node */
1971 1.39 ad mutex_enter(&ump->get_node_lock);
1972 1.1 reinoud
1973 1.1 reinoud DPRINTF(NODE, ("\tlookup in hash table\n"));
1974 1.1 reinoud /* lookup in hash table */
1975 1.1 reinoud assert(ump);
1976 1.1 reinoud assert(node_icb_loc);
1977 1.1 reinoud node = udf_hashget(ump, node_icb_loc);
1978 1.1 reinoud if (node) {
1979 1.1 reinoud DPRINTF(NODE, ("\tgot it from the hash!\n"));
1980 1.1 reinoud /* vnode is returned locked */
1981 1.1 reinoud *noderes = node;
1982 1.39 ad mutex_exit(&ump->get_node_lock);
1983 1.1 reinoud return 0;
1984 1.9 christos }
1985 1.1 reinoud
1986 1.1 reinoud /* garbage check: translate node_icb_loc to sectornr */
1987 1.1 reinoud error = udf_translate_vtop(ump, node_icb_loc, §or, &dummy);
1988 1.1 reinoud if (error) {
1989 1.1 reinoud /* no use, this will fail anyway */
1990 1.39 ad mutex_exit(&ump->get_node_lock);
1991 1.1 reinoud return EINVAL;
1992 1.9 christos }
1993 1.1 reinoud
1994 1.1 reinoud /* build node (do initialise!) */
1995 1.1 reinoud node = pool_get(&udf_node_pool, PR_WAITOK);
1996 1.1 reinoud memset(node, 0, sizeof(struct udf_node));
1997 1.1 reinoud
1998 1.1 reinoud DPRINTF(NODE, ("\tget new vnode\n"));
1999 1.1 reinoud /* give it a vnode */
2000 1.1 reinoud error = getnewvnode(VT_UDF, ump->vfs_mountp, udf_vnodeop_p, &nvp);
2001 1.1 reinoud if (error) {
2002 1.1 reinoud pool_put(&udf_node_pool, node);
2003 1.39 ad mutex_exit(&ump->get_node_lock);
2004 1.1 reinoud return error;
2005 1.9 christos }
2006 1.1 reinoud
2007 1.34 msaitoh /* always return locked vnode */
2008 1.1 reinoud if ((error = vn_lock(nvp, LK_EXCLUSIVE | LK_RETRY))) {
2009 1.1 reinoud /* recycle vnode and unlock; simultanious will fail too */
2010 1.1 reinoud ungetnewvnode(nvp);
2011 1.39 ad mutex_exit(&ump->get_node_lock);
2012 1.1 reinoud return error;
2013 1.9 christos }
2014 1.1 reinoud
2015 1.1 reinoud /* initialise crosslinks, note location of fe/efe for hashing */
2016 1.1 reinoud node->ump = ump;
2017 1.1 reinoud node->vnode = nvp;
2018 1.1 reinoud nvp->v_data = node;
2019 1.1 reinoud node->loc = *node_icb_loc;
2020 1.1 reinoud node->lockf = 0;
2021 1.1 reinoud
2022 1.1 reinoud /* insert into the hash lookup */
2023 1.1 reinoud udf_hashins(node);
2024 1.1 reinoud
2025 1.1 reinoud /* safe to unlock, the entry is in the hash table, vnode is locked */
2026 1.39 ad mutex_exit(&ump->get_node_lock);
2027 1.1 reinoud
2028 1.1 reinoud icb_loc = *node_icb_loc;
2029 1.1 reinoud needs_indirect = 0;
2030 1.1 reinoud strat4096 = 0;
2031 1.1 reinoud udf_file_type = UDF_ICB_FILETYPE_UNKNOWN;
2032 1.1 reinoud file_size = 0;
2033 1.1 reinoud lb_size = udf_rw32(ump->logical_vol->lb_size);
2034 1.1 reinoud
2035 1.1 reinoud do {
2036 1.1 reinoud error = udf_translate_vtop(ump, &icb_loc, §or, &dummy);
2037 1.1 reinoud if (error)
2038 1.1 reinoud break;
2039 1.1 reinoud
2040 1.1 reinoud /* try to read in fe/efe */
2041 1.1 reinoud error = udf_read_descriptor(ump, sector, M_UDFTEMP, &tmpdscr);
2042 1.1 reinoud
2043 1.1 reinoud /* blank sector marks end of sequence, check this */
2044 1.1 reinoud if ((tmpdscr == NULL) && (!strat4096))
2045 1.1 reinoud error = ENOENT;
2046 1.1 reinoud
2047 1.1 reinoud /* break if read error or blank sector */
2048 1.1 reinoud if (error || (tmpdscr == NULL))
2049 1.1 reinoud break;
2050 1.1 reinoud
2051 1.1 reinoud /* process descriptor based on the descriptor type */
2052 1.1 reinoud dscr_type = udf_rw16(tmpdscr->tag.id);
2053 1.1 reinoud
2054 1.1 reinoud /* if dealing with an indirect entry, follow the link */
2055 1.1 reinoud if (dscr_type == TAGID_INDIRECT_ENTRY) {
2056 1.1 reinoud needs_indirect = 0;
2057 1.1 reinoud icb_loc = tmpdscr->inde.indirect_icb;
2058 1.1 reinoud free(tmpdscr, M_UDFTEMP);
2059 1.1 reinoud continue;
2060 1.9 christos }
2061 1.1 reinoud
2062 1.1 reinoud /* only file entries and extended file entries allowed here */
2063 1.1 reinoud if ((dscr_type != TAGID_FENTRY) &&
2064 1.1 reinoud (dscr_type != TAGID_EXTFENTRY)) {
2065 1.1 reinoud free(tmpdscr, M_UDFTEMP);
2066 1.1 reinoud error = ENOENT;
2067 1.1 reinoud break;
2068 1.9 christos }
2069 1.1 reinoud
2070 1.1 reinoud /* get descriptor space from our pool */
2071 1.1 reinoud KASSERT(udf_tagsize(tmpdscr, lb_size) == lb_size);
2072 1.1 reinoud
2073 1.14 reinoud dscr = pool_get(ump->desc_pool, PR_WAITOK);
2074 1.1 reinoud memcpy(dscr, tmpdscr, lb_size);
2075 1.1 reinoud free(tmpdscr, M_UDFTEMP);
2076 1.1 reinoud
2077 1.1 reinoud /* record and process/update (ext)fentry */
2078 1.1 reinoud if (dscr_type == TAGID_FENTRY) {
2079 1.1 reinoud if (node->fe)
2080 1.14 reinoud pool_put(ump->desc_pool, node->fe);
2081 1.1 reinoud node->fe = &dscr->fe;
2082 1.1 reinoud strat = udf_rw16(node->fe->icbtag.strat_type);
2083 1.1 reinoud udf_file_type = node->fe->icbtag.file_type;
2084 1.1 reinoud file_size = udf_rw64(node->fe->inf_len);
2085 1.1 reinoud } else {
2086 1.1 reinoud if (node->efe)
2087 1.14 reinoud pool_put(ump->desc_pool, node->efe);
2088 1.1 reinoud node->efe = &dscr->efe;
2089 1.1 reinoud strat = udf_rw16(node->efe->icbtag.strat_type);
2090 1.1 reinoud udf_file_type = node->efe->icbtag.file_type;
2091 1.1 reinoud file_size = udf_rw64(node->efe->inf_len);
2092 1.9 christos }
2093 1.1 reinoud
2094 1.1 reinoud /* check recording strategy (structure) */
2095 1.1 reinoud
2096 1.1 reinoud /*
2097 1.1 reinoud * Strategy 4096 is a daisy linked chain terminating with an
2098 1.1 reinoud * unrecorded sector or a TERM descriptor. The next
2099 1.1 reinoud * descriptor is to be found in the sector that follows the
2100 1.1 reinoud * current sector.
2101 1.1 reinoud */
2102 1.1 reinoud if (strat == 4096) {
2103 1.1 reinoud strat4096 = 1;
2104 1.1 reinoud needs_indirect = 1;
2105 1.1 reinoud
2106 1.1 reinoud icb_loc.loc.lb_num = udf_rw32(icb_loc.loc.lb_num) + 1;
2107 1.9 christos }
2108 1.1 reinoud
2109 1.1 reinoud /*
2110 1.1 reinoud * Strategy 4 is the normal strategy and terminates, but if
2111 1.1 reinoud * we're in strategy 4096, we can't have strategy 4 mixed in
2112 1.1 reinoud */
2113 1.1 reinoud
2114 1.1 reinoud if (strat == 4) {
2115 1.1 reinoud if (strat4096) {
2116 1.1 reinoud error = EINVAL;
2117 1.1 reinoud break;
2118 1.9 christos }
2119 1.1 reinoud break; /* done */
2120 1.9 christos }
2121 1.1 reinoud } while (!error);
2122 1.1 reinoud
2123 1.1 reinoud if (error) {
2124 1.1 reinoud /* recycle udf_node */
2125 1.1 reinoud udf_dispose_node(node);
2126 1.1 reinoud
2127 1.1 reinoud /* recycle vnode */
2128 1.1 reinoud nvp->v_data = NULL;
2129 1.1 reinoud ungetnewvnode(nvp);
2130 1.1 reinoud
2131 1.1 reinoud return EINVAL; /* error code ok? */
2132 1.9 christos }
2133 1.1 reinoud
2134 1.1 reinoud /* post process and initialise node */
2135 1.1 reinoud
2136 1.1 reinoud /* assert no references to dscr anymore beyong this point */
2137 1.1 reinoud assert((node->fe) || (node->efe));
2138 1.1 reinoud dscr = NULL;
2139 1.1 reinoud
2140 1.1 reinoud /*
2141 1.1 reinoud * Record where to record an updated version of the descriptor. If
2142 1.1 reinoud * there is a sequence of indirect entries, icb_loc will have been
2143 1.1 reinoud * updated. Its the write disipline to allocate new space and to make
2144 1.1 reinoud * sure the chain is maintained.
2145 1.1 reinoud *
2146 1.1 reinoud * `needs_indirect' flags if the next location is to be filled with
2147 1.1 reinoud * with an indirect entry.
2148 1.1 reinoud */
2149 1.1 reinoud node->next_loc = icb_loc;
2150 1.1 reinoud node->needs_indirect = needs_indirect;
2151 1.1 reinoud
2152 1.1 reinoud /*
2153 1.1 reinoud * Translate UDF filetypes into vnode types.
2154 1.1 reinoud *
2155 1.1 reinoud * Systemfiles like the meta main and mirror files are not treated as
2156 1.1 reinoud * normal files, so we type them as having no type. UDF dictates that
2157 1.1 reinoud * they are not allowed to be visible.
2158 1.1 reinoud */
2159 1.1 reinoud
2160 1.1 reinoud /* TODO specfs, fifofs etc etc. vnops setting */
2161 1.1 reinoud switch (udf_file_type) {
2162 1.1 reinoud case UDF_ICB_FILETYPE_DIRECTORY :
2163 1.1 reinoud case UDF_ICB_FILETYPE_STREAMDIR :
2164 1.1 reinoud nvp->v_type = VDIR;
2165 1.1 reinoud break;
2166 1.1 reinoud case UDF_ICB_FILETYPE_BLOCKDEVICE :
2167 1.1 reinoud nvp->v_type = VBLK;
2168 1.1 reinoud break;
2169 1.1 reinoud case UDF_ICB_FILETYPE_CHARDEVICE :
2170 1.1 reinoud nvp->v_type = VCHR;
2171 1.1 reinoud break;
2172 1.1 reinoud case UDF_ICB_FILETYPE_SYMLINK :
2173 1.1 reinoud nvp->v_type = VLNK;
2174 1.1 reinoud break;
2175 1.28 reinoud case UDF_ICB_FILETYPE_VAT :
2176 1.1 reinoud case UDF_ICB_FILETYPE_META_MAIN :
2177 1.1 reinoud case UDF_ICB_FILETYPE_META_MIRROR :
2178 1.1 reinoud nvp->v_type = VNON;
2179 1.1 reinoud break;
2180 1.1 reinoud case UDF_ICB_FILETYPE_RANDOMACCESS :
2181 1.28 reinoud case UDF_ICB_FILETYPE_REALTIME :
2182 1.1 reinoud nvp->v_type = VREG;
2183 1.1 reinoud break;
2184 1.1 reinoud default:
2185 1.1 reinoud /* YIKES, either a block/char device, fifo or something else */
2186 1.1 reinoud nvp->v_type = VNON;
2187 1.9 christos }
2188 1.1 reinoud
2189 1.1 reinoud /* initialise genfs */
2190 1.1 reinoud genfs_node_init(nvp, &udf_genfsops);
2191 1.1 reinoud
2192 1.1 reinoud /* don't forget to set vnode's v_size */
2193 1.35 pooka uvm_vnp_setsize(nvp, file_size);
2194 1.1 reinoud
2195 1.1 reinoud /* TODO ext attr and streamdir nodes */
2196 1.1 reinoud
2197 1.1 reinoud *noderes = node;
2198 1.1 reinoud
2199 1.1 reinoud return 0;
2200 1.1 reinoud }
2201 1.1 reinoud
2202 1.1 reinoud /* --------------------------------------------------------------------- */
2203 1.1 reinoud
2204 1.1 reinoud /* UDF<->unix converters */
2205 1.1 reinoud
2206 1.1 reinoud /* --------------------------------------------------------------------- */
2207 1.1 reinoud
2208 1.1 reinoud static mode_t
2209 1.1 reinoud udf_perm_to_unix_mode(uint32_t perm)
2210 1.1 reinoud {
2211 1.1 reinoud mode_t mode;
2212 1.1 reinoud
2213 1.1 reinoud mode = ((perm & UDF_FENTRY_PERM_USER_MASK) );
2214 1.1 reinoud mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK ) >> 2);
2215 1.1 reinoud mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4);
2216 1.1 reinoud
2217 1.1 reinoud return mode;
2218 1.1 reinoud }
2219 1.1 reinoud
2220 1.1 reinoud /* --------------------------------------------------------------------- */
2221 1.1 reinoud
2222 1.1 reinoud #ifdef notyet
2223 1.1 reinoud static uint32_t
2224 1.1 reinoud unix_mode_to_udf_perm(mode_t mode)
2225 1.1 reinoud {
2226 1.1 reinoud uint32_t perm;
2227 1.1 reinoud
2228 1.1 reinoud perm = ((mode & S_IRWXO) );
2229 1.1 reinoud perm |= ((mode & S_IRWXG) << 2);
2230 1.1 reinoud perm |= ((mode & S_IRWXU) << 4);
2231 1.1 reinoud perm |= ((mode & S_IWOTH) << 3);
2232 1.1 reinoud perm |= ((mode & S_IWGRP) << 5);
2233 1.1 reinoud perm |= ((mode & S_IWUSR) << 7);
2234 1.1 reinoud
2235 1.1 reinoud return perm;
2236 1.1 reinoud }
2237 1.1 reinoud #endif
2238 1.1 reinoud
2239 1.1 reinoud /* --------------------------------------------------------------------- */
2240 1.1 reinoud
2241 1.1 reinoud static uint32_t
2242 1.1 reinoud udf_icb_to_unix_filetype(uint32_t icbftype)
2243 1.1 reinoud {
2244 1.1 reinoud switch (icbftype) {
2245 1.1 reinoud case UDF_ICB_FILETYPE_DIRECTORY :
2246 1.1 reinoud case UDF_ICB_FILETYPE_STREAMDIR :
2247 1.1 reinoud return S_IFDIR;
2248 1.1 reinoud case UDF_ICB_FILETYPE_FIFO :
2249 1.1 reinoud return S_IFIFO;
2250 1.1 reinoud case UDF_ICB_FILETYPE_CHARDEVICE :
2251 1.1 reinoud return S_IFCHR;
2252 1.1 reinoud case UDF_ICB_FILETYPE_BLOCKDEVICE :
2253 1.1 reinoud return S_IFBLK;
2254 1.1 reinoud case UDF_ICB_FILETYPE_RANDOMACCESS :
2255 1.29 reinoud case UDF_ICB_FILETYPE_REALTIME :
2256 1.1 reinoud return S_IFREG;
2257 1.1 reinoud case UDF_ICB_FILETYPE_SYMLINK :
2258 1.1 reinoud return S_IFLNK;
2259 1.1 reinoud case UDF_ICB_FILETYPE_SOCKET :
2260 1.1 reinoud return S_IFSOCK;
2261 1.9 christos }
2262 1.1 reinoud /* no idea what this is */
2263 1.1 reinoud return 0;
2264 1.1 reinoud }
2265 1.1 reinoud
2266 1.1 reinoud /* --------------------------------------------------------------------- */
2267 1.1 reinoud
2268 1.1 reinoud /* TODO KNF-ify */
2269 1.1 reinoud
2270 1.1 reinoud void
2271 1.1 reinoud udf_to_unix_name(char *result, char *id, int len, struct charspec *chsp)
2272 1.1 reinoud {
2273 1.9 christos uint16_t *raw_name, *unix_name;
2274 1.1 reinoud uint16_t *inchp, ch;
2275 1.1 reinoud uint8_t *outchp;
2276 1.1 reinoud int ucode_chars, nice_uchars;
2277 1.1 reinoud
2278 1.11 reinoud raw_name = malloc(2048 * sizeof(uint16_t), M_UDFTEMP, M_WAITOK);
2279 1.11 reinoud unix_name = raw_name + 1024; /* split space in half */
2280 1.1 reinoud assert(sizeof(char) == sizeof(uint8_t));
2281 1.1 reinoud outchp = (uint8_t *) result;
2282 1.1 reinoud if ((chsp->type == 0) && (strcmp((char*) chsp->inf, "OSTA Compressed Unicode") == 0)) {
2283 1.1 reinoud *raw_name = *unix_name = 0;
2284 1.1 reinoud ucode_chars = udf_UncompressUnicode(len, (uint8_t *) id, raw_name);
2285 1.1 reinoud ucode_chars = MIN(ucode_chars, UnicodeLength((unicode_t *) raw_name));
2286 1.1 reinoud nice_uchars = UDFTransName(unix_name, raw_name, ucode_chars);
2287 1.1 reinoud for (inchp = unix_name; nice_uchars>0; inchp++, nice_uchars--) {
2288 1.1 reinoud ch = *inchp;
2289 1.1 reinoud /* XXX sloppy unicode -> latin */
2290 1.1 reinoud *outchp++ = ch & 255;
2291 1.1 reinoud if (!ch) break;
2292 1.9 christos }
2293 1.1 reinoud *outchp++ = 0;
2294 1.1 reinoud } else {
2295 1.1 reinoud /* assume 8bit char length byte latin-1 */
2296 1.1 reinoud assert(*id == 8);
2297 1.1 reinoud strncpy((char *) result, (char *) (id+1), strlen((char *) (id+1)));
2298 1.9 christos }
2299 1.9 christos free(raw_name, M_UDFTEMP);
2300 1.1 reinoud }
2301 1.1 reinoud
2302 1.1 reinoud /* --------------------------------------------------------------------- */
2303 1.1 reinoud
2304 1.1 reinoud /* TODO KNF-ify */
2305 1.1 reinoud
2306 1.1 reinoud void
2307 1.1 reinoud unix_to_udf_name(char *result, char *name,
2308 1.1 reinoud uint8_t *result_len, struct charspec *chsp)
2309 1.1 reinoud {
2310 1.9 christos uint16_t *raw_name;
2311 1.1 reinoud int udf_chars, name_len;
2312 1.1 reinoud char *inchp;
2313 1.1 reinoud uint16_t *outchp;
2314 1.1 reinoud
2315 1.9 christos raw_name = malloc(1024, M_UDFTEMP, M_WAITOK);
2316 1.1 reinoud /* convert latin-1 or whatever to unicode-16 */
2317 1.1 reinoud *raw_name = 0;
2318 1.1 reinoud name_len = 0;
2319 1.1 reinoud inchp = name;
2320 1.1 reinoud outchp = raw_name;
2321 1.1 reinoud while (*inchp) {
2322 1.1 reinoud *outchp++ = (uint16_t) (*inchp++);
2323 1.1 reinoud name_len++;
2324 1.9 christos }
2325 1.1 reinoud
2326 1.1 reinoud if ((chsp->type == 0) && (strcmp((char *) chsp->inf, "OSTA Compressed Unicode") == 0)) {
2327 1.1 reinoud udf_chars = udf_CompressUnicode(name_len, 8, (unicode_t *) raw_name, (byte *) result);
2328 1.1 reinoud } else {
2329 1.1 reinoud /* XXX assume 8bit char length byte latin-1 */
2330 1.1 reinoud *result++ = 8; udf_chars = 1;
2331 1.1 reinoud strncpy(result, name + 1, strlen(name+1));
2332 1.1 reinoud udf_chars += strlen(name);
2333 1.9 christos }
2334 1.1 reinoud *result_len = udf_chars;
2335 1.9 christos free(raw_name, M_UDFTEMP);
2336 1.1 reinoud }
2337 1.1 reinoud
2338 1.1 reinoud /* --------------------------------------------------------------------- */
2339 1.1 reinoud
2340 1.1 reinoud void
2341 1.1 reinoud udf_timestamp_to_timespec(struct udf_mount *ump,
2342 1.1 reinoud struct timestamp *timestamp,
2343 1.30 reinoud struct timespec *timespec)
2344 1.1 reinoud {
2345 1.30 reinoud struct clock_ymdhms ymdhms;
2346 1.1 reinoud uint32_t usecs, secs, nsecs;
2347 1.1 reinoud uint16_t tz;
2348 1.1 reinoud
2349 1.30 reinoud /* fill in ymdhms structure from timestamp */
2350 1.30 reinoud memset(&ymdhms, 0, sizeof(ymdhms));
2351 1.30 reinoud ymdhms.dt_year = udf_rw16(timestamp->year);
2352 1.30 reinoud ymdhms.dt_mon = timestamp->month;
2353 1.30 reinoud ymdhms.dt_day = timestamp->day;
2354 1.30 reinoud ymdhms.dt_wday = 0; /* ? */
2355 1.30 reinoud ymdhms.dt_hour = timestamp->hour;
2356 1.30 reinoud ymdhms.dt_min = timestamp->minute;
2357 1.30 reinoud ymdhms.dt_sec = timestamp->second;
2358 1.1 reinoud
2359 1.30 reinoud secs = clock_ymdhms_to_secs(&ymdhms);
2360 1.1 reinoud usecs = timestamp->usec +
2361 1.1 reinoud 100*timestamp->hund_usec + 10000*timestamp->centisec;
2362 1.1 reinoud nsecs = usecs * 1000;
2363 1.1 reinoud
2364 1.1 reinoud /*
2365 1.1 reinoud * Calculate the time zone. The timezone is 12 bit signed 2's
2366 1.1 reinoud * compliment, so we gotta do some extra magic to handle it right.
2367 1.1 reinoud */
2368 1.1 reinoud tz = udf_rw16(timestamp->type_tz);
2369 1.1 reinoud tz &= 0x0fff; /* only lower 12 bits are significant */
2370 1.1 reinoud if (tz & 0x0800) /* sign extention */
2371 1.1 reinoud tz |= 0xf000;
2372 1.1 reinoud
2373 1.1 reinoud /* TODO check timezone conversion */
2374 1.1 reinoud /* check if we are specified a timezone to convert */
2375 1.1 reinoud if (udf_rw16(timestamp->type_tz) & 0x1000) {
2376 1.1 reinoud if ((int16_t) tz != -2047)
2377 1.1 reinoud secs -= (int16_t) tz * 60;
2378 1.1 reinoud } else {
2379 1.1 reinoud secs -= ump->mount_args.gmtoff;
2380 1.9 christos }
2381 1.1 reinoud
2382 1.1 reinoud timespec->tv_sec = secs;
2383 1.1 reinoud timespec->tv_nsec = nsecs;
2384 1.1 reinoud }
2385 1.1 reinoud
2386 1.1 reinoud /* --------------------------------------------------------------------- */
2387 1.1 reinoud
2388 1.1 reinoud /*
2389 1.1 reinoud * Attribute and filetypes converters with get/set pairs
2390 1.1 reinoud */
2391 1.1 reinoud
2392 1.1 reinoud uint32_t
2393 1.1 reinoud udf_getaccessmode(struct udf_node *udf_node)
2394 1.1 reinoud {
2395 1.1 reinoud struct file_entry *fe;
2396 1.1 reinoud struct extfile_entry *efe;
2397 1.1 reinoud uint32_t udf_perm, icbftype;
2398 1.1 reinoud uint32_t mode, ftype;
2399 1.1 reinoud uint16_t icbflags;
2400 1.1 reinoud
2401 1.1 reinoud if (udf_node->fe) {
2402 1.1 reinoud fe = udf_node->fe;
2403 1.1 reinoud udf_perm = udf_rw32(fe->perm);
2404 1.1 reinoud icbftype = fe->icbtag.file_type;
2405 1.1 reinoud icbflags = udf_rw16(fe->icbtag.flags);
2406 1.1 reinoud } else {
2407 1.1 reinoud assert(udf_node->efe);
2408 1.1 reinoud efe = udf_node->efe;
2409 1.1 reinoud udf_perm = udf_rw32(efe->perm);
2410 1.1 reinoud icbftype = efe->icbtag.file_type;
2411 1.1 reinoud icbflags = udf_rw16(efe->icbtag.flags);
2412 1.9 christos }
2413 1.1 reinoud
2414 1.1 reinoud mode = udf_perm_to_unix_mode(udf_perm);
2415 1.1 reinoud ftype = udf_icb_to_unix_filetype(icbftype);
2416 1.1 reinoud
2417 1.1 reinoud /* set suid, sgid, sticky from flags in fe/efe */
2418 1.1 reinoud if (icbflags & UDF_ICB_TAG_FLAGS_SETUID)
2419 1.1 reinoud mode |= S_ISUID;
2420 1.1 reinoud if (icbflags & UDF_ICB_TAG_FLAGS_SETGID)
2421 1.1 reinoud mode |= S_ISGID;
2422 1.1 reinoud if (icbflags & UDF_ICB_TAG_FLAGS_STICKY)
2423 1.1 reinoud mode |= S_ISVTX;
2424 1.1 reinoud
2425 1.1 reinoud return mode | ftype;
2426 1.1 reinoud }
2427 1.1 reinoud
2428 1.1 reinoud /* --------------------------------------------------------------------- */
2429 1.1 reinoud
2430 1.1 reinoud /*
2431 1.1 reinoud * Directory read and manipulation functions
2432 1.1 reinoud */
2433 1.1 reinoud
2434 1.1 reinoud int
2435 1.1 reinoud udf_lookup_name_in_dir(struct vnode *vp, const char *name, int namelen,
2436 1.1 reinoud struct long_ad *icb_loc)
2437 1.1 reinoud {
2438 1.1 reinoud struct udf_node *dir_node = VTOI(vp);
2439 1.1 reinoud struct file_entry *fe;
2440 1.1 reinoud struct extfile_entry *efe;
2441 1.1 reinoud struct fileid_desc *fid;
2442 1.37 rumble struct dirent *dirent;
2443 1.1 reinoud uint64_t file_size, diroffset;
2444 1.1 reinoud uint32_t lb_size;
2445 1.1 reinoud int found, error;
2446 1.1 reinoud
2447 1.1 reinoud /* get directory filesize */
2448 1.1 reinoud if (dir_node->fe) {
2449 1.1 reinoud fe = dir_node->fe;
2450 1.1 reinoud file_size = udf_rw64(fe->inf_len);
2451 1.1 reinoud } else {
2452 1.1 reinoud assert(dir_node->efe);
2453 1.1 reinoud efe = dir_node->efe;
2454 1.1 reinoud file_size = udf_rw64(efe->inf_len);
2455 1.9 christos }
2456 1.1 reinoud
2457 1.1 reinoud /* allocate temporary space for fid */
2458 1.1 reinoud lb_size = udf_rw32(dir_node->ump->logical_vol->lb_size);
2459 1.37 rumble fid = malloc(lb_size, M_UDFTEMP, M_WAITOK);
2460 1.1 reinoud
2461 1.1 reinoud found = 0;
2462 1.18 reinoud diroffset = dir_node->last_diroffset;
2463 1.31 reinoud
2464 1.31 reinoud /*
2465 1.31 reinoud * if the directory is trunced or if we have never visited it yet,
2466 1.31 reinoud * start at the end.
2467 1.31 reinoud */
2468 1.31 reinoud if ((diroffset >= file_size) || (diroffset == 0)) {
2469 1.31 reinoud diroffset = dir_node->last_diroffset = file_size;
2470 1.31 reinoud }
2471 1.31 reinoud
2472 1.37 rumble dirent = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK);
2473 1.37 rumble
2474 1.18 reinoud while (!found) {
2475 1.31 reinoud /* if at the end, go trough zero */
2476 1.18 reinoud if (diroffset >= file_size)
2477 1.18 reinoud diroffset = 0;
2478 1.18 reinoud
2479 1.1 reinoud /* transfer a new fid/dirent */
2480 1.37 rumble error = udf_read_fid_stream(vp, &diroffset, fid, dirent);
2481 1.1 reinoud if (error)
2482 1.1 reinoud break;
2483 1.1 reinoud
2484 1.19 reinoud /* skip deleted entries */
2485 1.19 reinoud if ((fid->file_char & UDF_FILE_CHAR_DEL) == 0) {
2486 1.37 rumble if ((strlen(dirent->d_name) == namelen) &&
2487 1.37 rumble (strncmp(dirent->d_name, name, namelen) == 0)) {
2488 1.19 reinoud found = 1;
2489 1.19 reinoud *icb_loc = fid->icb;
2490 1.19 reinoud }
2491 1.19 reinoud }
2492 1.19 reinoud
2493 1.18 reinoud if (diroffset == dir_node->last_diroffset) {
2494 1.18 reinoud /* we have cycled */
2495 1.18 reinoud break;
2496 1.18 reinoud }
2497 1.9 christos }
2498 1.37 rumble free(fid, M_UDFTEMP);
2499 1.37 rumble free(dirent, M_UDFTEMP);
2500 1.18 reinoud dir_node->last_diroffset = diroffset;
2501 1.1 reinoud
2502 1.1 reinoud return found;
2503 1.1 reinoud }
2504 1.1 reinoud
2505 1.1 reinoud /* --------------------------------------------------------------------- */
2506 1.1 reinoud
2507 1.1 reinoud /*
2508 1.1 reinoud * Read one fid and process it into a dirent and advance to the next (*fid)
2509 1.1 reinoud * has to be allocated a logical block in size, (*dirent) struct dirent length
2510 1.1 reinoud */
2511 1.1 reinoud
2512 1.1 reinoud int
2513 1.1 reinoud udf_read_fid_stream(struct vnode *vp, uint64_t *offset,
2514 1.1 reinoud struct fileid_desc *fid, struct dirent *dirent)
2515 1.1 reinoud {
2516 1.1 reinoud struct udf_node *dir_node = VTOI(vp);
2517 1.1 reinoud struct udf_mount *ump = dir_node->ump;
2518 1.1 reinoud struct file_entry *fe;
2519 1.1 reinoud struct extfile_entry *efe;
2520 1.1 reinoud struct uio dir_uio;
2521 1.1 reinoud struct iovec dir_iovec;
2522 1.1 reinoud uint32_t entry_length, lb_size;
2523 1.1 reinoud uint64_t file_size;
2524 1.1 reinoud char *fid_name;
2525 1.1 reinoud int enough, error;
2526 1.1 reinoud
2527 1.1 reinoud assert(fid);
2528 1.1 reinoud assert(dirent);
2529 1.1 reinoud assert(dir_node);
2530 1.1 reinoud assert(offset);
2531 1.1 reinoud assert(*offset != 1);
2532 1.1 reinoud
2533 1.1 reinoud DPRINTF(FIDS, ("read_fid_stream called\n"));
2534 1.1 reinoud /* check if we're past the end of the directory */
2535 1.1 reinoud if (dir_node->fe) {
2536 1.1 reinoud fe = dir_node->fe;
2537 1.1 reinoud file_size = udf_rw64(fe->inf_len);
2538 1.1 reinoud } else {
2539 1.1 reinoud assert(dir_node->efe);
2540 1.1 reinoud efe = dir_node->efe;
2541 1.1 reinoud file_size = udf_rw64(efe->inf_len);
2542 1.9 christos }
2543 1.1 reinoud if (*offset >= file_size)
2544 1.1 reinoud return EINVAL;
2545 1.1 reinoud
2546 1.1 reinoud /* get maximum length of FID descriptor */
2547 1.1 reinoud lb_size = udf_rw32(ump->logical_vol->lb_size);
2548 1.1 reinoud
2549 1.1 reinoud /* initialise return values */
2550 1.1 reinoud entry_length = 0;
2551 1.1 reinoud memset(dirent, 0, sizeof(struct dirent));
2552 1.1 reinoud memset(fid, 0, lb_size);
2553 1.1 reinoud
2554 1.1 reinoud /* TODO use vn_rdwr instead of creating our own uio */
2555 1.1 reinoud /* read part of the directory */
2556 1.1 reinoud memset(&dir_uio, 0, sizeof(struct uio));
2557 1.1 reinoud dir_uio.uio_rw = UIO_READ; /* read into this space */
2558 1.1 reinoud dir_uio.uio_iovcnt = 1;
2559 1.1 reinoud dir_uio.uio_iov = &dir_iovec;
2560 1.5 yamt UIO_SETUP_SYSSPACE(&dir_uio);
2561 1.1 reinoud dir_iovec.iov_base = fid;
2562 1.1 reinoud dir_iovec.iov_len = lb_size;
2563 1.1 reinoud dir_uio.uio_offset = *offset;
2564 1.1 reinoud
2565 1.1 reinoud /* limit length of read in piece */
2566 1.1 reinoud dir_uio.uio_resid = MIN(file_size - (*offset), lb_size);
2567 1.1 reinoud
2568 1.1 reinoud /* read the part into the fid space */
2569 1.1 reinoud error = VOP_READ(vp, &dir_uio, IO_ALTSEMANTICS, NOCRED);
2570 1.1 reinoud if (error)
2571 1.1 reinoud return error;
2572 1.1 reinoud
2573 1.1 reinoud /*
2574 1.1 reinoud * Check if we got a whole descriptor.
2575 1.1 reinoud * XXX Try to `resync' directory stream when something is very wrong.
2576 1.1 reinoud *
2577 1.1 reinoud */
2578 1.1 reinoud enough = (dir_uio.uio_offset - (*offset) >= UDF_FID_SIZE);
2579 1.1 reinoud if (!enough) {
2580 1.1 reinoud /* short dir ... */
2581 1.1 reinoud return EIO;
2582 1.9 christos }
2583 1.1 reinoud
2584 1.1 reinoud /* check if our FID header is OK */
2585 1.1 reinoud error = udf_check_tag(fid);
2586 1.1 reinoud DPRINTFIF(FIDS, error, ("read fids: tag check failed\n"));
2587 1.1 reinoud if (!error) {
2588 1.1 reinoud if (udf_rw16(fid->tag.id) != TAGID_FID)
2589 1.1 reinoud error = ENOENT;
2590 1.9 christos }
2591 1.1 reinoud DPRINTFIF(FIDS, !error, ("\ttag checked ok: got TAGID_FID\n"));
2592 1.1 reinoud
2593 1.1 reinoud /* check for length */
2594 1.1 reinoud if (!error) {
2595 1.1 reinoud entry_length = udf_fidsize(fid, lb_size);
2596 1.1 reinoud enough = (dir_uio.uio_offset - (*offset) >= entry_length);
2597 1.9 christos }
2598 1.1 reinoud DPRINTFIF(FIDS, !error, ("\tentry_length = %d, enough = %s\n",
2599 1.1 reinoud entry_length, enough?"yes":"no"));
2600 1.1 reinoud
2601 1.1 reinoud if (!enough) {
2602 1.1 reinoud /* short dir ... bomb out */
2603 1.1 reinoud return EIO;
2604 1.9 christos }
2605 1.1 reinoud
2606 1.1 reinoud /* check FID contents */
2607 1.1 reinoud if (!error) {
2608 1.1 reinoud error = udf_check_tag_payload((union dscrptr *) fid, lb_size);
2609 1.1 reinoud DPRINTF(FIDS, ("\tpayload checked ok\n"));
2610 1.9 christos }
2611 1.1 reinoud if (error) {
2612 1.1 reinoud /* note that is sometimes a bit quick to report */
2613 1.1 reinoud printf("BROKEN DIRECTORY ENTRY\n");
2614 1.1 reinoud /* RESYNC? */
2615 1.1 reinoud /* TODO: use udf_resync_fid_stream */
2616 1.1 reinoud return EIO;
2617 1.9 christos }
2618 1.1 reinoud DPRINTF(FIDS, ("\tinterpret FID\n"));
2619 1.1 reinoud
2620 1.1 reinoud /* we got a whole and valid descriptor! */
2621 1.1 reinoud
2622 1.1 reinoud /* create resulting dirent structure */
2623 1.1 reinoud fid_name = (char *) fid->data + udf_rw16(fid->l_iu);
2624 1.1 reinoud udf_to_unix_name(dirent->d_name,
2625 1.1 reinoud fid_name, fid->l_fi, &ump->logical_vol->desc_charset);
2626 1.1 reinoud
2627 1.1 reinoud /* '..' has no name, so provide one */
2628 1.1 reinoud if (fid->file_char & UDF_FILE_CHAR_PAR)
2629 1.1 reinoud strcpy(dirent->d_name, "..");
2630 1.1 reinoud
2631 1.1 reinoud dirent->d_fileno = udf_calchash(&fid->icb); /* inode hash XXX */
2632 1.1 reinoud dirent->d_namlen = strlen(dirent->d_name);
2633 1.1 reinoud dirent->d_reclen = _DIRENT_SIZE(dirent);
2634 1.1 reinoud
2635 1.1 reinoud /*
2636 1.1 reinoud * Note that its not worth trying to go for the filetypes now... its
2637 1.1 reinoud * too expensive too
2638 1.1 reinoud */
2639 1.1 reinoud dirent->d_type = DT_UNKNOWN;
2640 1.1 reinoud
2641 1.1 reinoud /* initial guess for filetype we can make */
2642 1.1 reinoud if (fid->file_char & UDF_FILE_CHAR_DIR)
2643 1.1 reinoud dirent->d_type = DT_DIR;
2644 1.1 reinoud
2645 1.1 reinoud /* advance */
2646 1.1 reinoud *offset += entry_length;
2647 1.1 reinoud
2648 1.1 reinoud return error;
2649 1.1 reinoud }
2650 1.1 reinoud
2651 1.1 reinoud /* --------------------------------------------------------------------- */
2652 1.1 reinoud
2653 1.1 reinoud /*
2654 1.1 reinoud * block based file reading and writing
2655 1.1 reinoud */
2656 1.1 reinoud
2657 1.1 reinoud static int
2658 1.1 reinoud udf_read_internal(struct udf_node *node, uint8_t *blob)
2659 1.1 reinoud {
2660 1.1 reinoud struct udf_mount *ump;
2661 1.1 reinoud struct file_entry *fe;
2662 1.1 reinoud struct extfile_entry *efe;
2663 1.1 reinoud uint64_t inflen;
2664 1.1 reinoud uint32_t sector_size;
2665 1.1 reinoud uint8_t *pos;
2666 1.1 reinoud int icbflags, addr_type;
2667 1.1 reinoud
2668 1.1 reinoud /* shut up gcc */
2669 1.1 reinoud inflen = addr_type = icbflags = 0;
2670 1.1 reinoud pos = NULL;
2671 1.1 reinoud
2672 1.1 reinoud /* get extent and do some paranoia checks */
2673 1.1 reinoud ump = node->ump;
2674 1.1 reinoud sector_size = ump->discinfo.sector_size;
2675 1.1 reinoud
2676 1.1 reinoud fe = node->fe;
2677 1.1 reinoud efe = node->efe;
2678 1.1 reinoud if (fe) {
2679 1.1 reinoud inflen = udf_rw64(fe->inf_len);
2680 1.1 reinoud pos = &fe->data[0] + udf_rw32(fe->l_ea);
2681 1.1 reinoud icbflags = udf_rw16(fe->icbtag.flags);
2682 1.9 christos }
2683 1.1 reinoud if (efe) {
2684 1.1 reinoud inflen = udf_rw64(efe->inf_len);
2685 1.1 reinoud pos = &efe->data[0] + udf_rw32(efe->l_ea);
2686 1.1 reinoud icbflags = udf_rw16(efe->icbtag.flags);
2687 1.9 christos }
2688 1.1 reinoud addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
2689 1.1 reinoud
2690 1.1 reinoud assert(addr_type == UDF_ICB_INTERN_ALLOC);
2691 1.1 reinoud assert(inflen < sector_size);
2692 1.1 reinoud
2693 1.1 reinoud /* copy out info */
2694 1.1 reinoud memset(blob, 0, sector_size);
2695 1.1 reinoud memcpy(blob, pos, inflen);
2696 1.1 reinoud
2697 1.1 reinoud return 0;
2698 1.1 reinoud }
2699 1.1 reinoud
2700 1.1 reinoud /* --------------------------------------------------------------------- */
2701 1.1 reinoud
2702 1.1 reinoud /*
2703 1.1 reinoud * Read file extent reads an extent specified in sectors from the file. It is
2704 1.1 reinoud * sector based; i.e. no `fancy' offsets.
2705 1.1 reinoud */
2706 1.1 reinoud
2707 1.1 reinoud int
2708 1.1 reinoud udf_read_file_extent(struct udf_node *node,
2709 1.1 reinoud uint32_t from, uint32_t sectors,
2710 1.1 reinoud uint8_t *blob)
2711 1.1 reinoud {
2712 1.1 reinoud struct buf buf;
2713 1.1 reinoud uint32_t sector_size;
2714 1.1 reinoud
2715 1.1 reinoud BUF_INIT(&buf);
2716 1.1 reinoud
2717 1.1 reinoud sector_size = node->ump->discinfo.sector_size;
2718 1.1 reinoud
2719 1.1 reinoud buf.b_bufsize = sectors * sector_size;
2720 1.1 reinoud buf.b_data = blob;
2721 1.1 reinoud buf.b_bcount = buf.b_bufsize;
2722 1.1 reinoud buf.b_resid = buf.b_bcount;
2723 1.1 reinoud buf.b_flags = B_BUSY | B_READ;
2724 1.1 reinoud buf.b_vp = node->vnode;
2725 1.1 reinoud buf.b_proc = NULL;
2726 1.1 reinoud
2727 1.1 reinoud buf.b_blkno = from;
2728 1.1 reinoud buf.b_lblkno = 0;
2729 1.1 reinoud BIO_SETPRIO(&buf, BPRIO_TIMELIMITED);
2730 1.1 reinoud
2731 1.1 reinoud udf_read_filebuf(node, &buf);
2732 1.1 reinoud return biowait(&buf);
2733 1.1 reinoud }
2734 1.1 reinoud
2735 1.1 reinoud
2736 1.1 reinoud /* --------------------------------------------------------------------- */
2737 1.1 reinoud
2738 1.1 reinoud /*
2739 1.1 reinoud * Read file extent in the buffer.
2740 1.1 reinoud *
2741 1.25 wiz * The splitup of the extent into separate request-buffers is to minimise
2742 1.1 reinoud * copying around as much as possible.
2743 1.1 reinoud */
2744 1.1 reinoud
2745 1.1 reinoud
2746 1.12 reinoud /* maximum of 128 translations (!) (64 kb in 512 byte sectors) */
2747 1.1 reinoud #define FILEBUFSECT 128
2748 1.1 reinoud
2749 1.1 reinoud void
2750 1.1 reinoud udf_read_filebuf(struct udf_node *node, struct buf *buf)
2751 1.1 reinoud {
2752 1.1 reinoud struct buf *nestbuf;
2753 1.10 christos uint64_t *mapping;
2754 1.1 reinoud uint64_t run_start;
2755 1.1 reinoud uint32_t sector_size;
2756 1.1 reinoud uint32_t buf_offset, sector, rbuflen, rblk;
2757 1.1 reinoud uint8_t *buf_pos;
2758 1.1 reinoud int error, run_length;
2759 1.1 reinoud
2760 1.1 reinoud uint32_t from;
2761 1.1 reinoud uint32_t sectors;
2762 1.1 reinoud
2763 1.1 reinoud sector_size = node->ump->discinfo.sector_size;
2764 1.1 reinoud
2765 1.1 reinoud from = buf->b_blkno;
2766 1.1 reinoud sectors = buf->b_bcount / sector_size;
2767 1.1 reinoud
2768 1.1 reinoud /* assure we have enough translation slots */
2769 1.1 reinoud KASSERT(buf->b_bcount / sector_size <= FILEBUFSECT);
2770 1.1 reinoud KASSERT(MAXPHYS / sector_size <= FILEBUFSECT);
2771 1.1 reinoud
2772 1.1 reinoud if (sectors > FILEBUFSECT) {
2773 1.1 reinoud printf("udf_read_filebuf: implementation limit on bufsize\n");
2774 1.1 reinoud buf->b_error = EIO;
2775 1.1 reinoud biodone(buf);
2776 1.1 reinoud return;
2777 1.9 christos }
2778 1.1 reinoud
2779 1.37 rumble mapping = malloc(sizeof(*mapping) * FILEBUFSECT, M_UDFTEMP, M_WAITOK);
2780 1.10 christos
2781 1.1 reinoud error = 0;
2782 1.1 reinoud DPRINTF(READ, ("\ttranslate %d-%d\n", from, sectors));
2783 1.1 reinoud error = udf_translate_file_extent(node, from, sectors, mapping);
2784 1.1 reinoud if (error) {
2785 1.1 reinoud buf->b_error = error;
2786 1.1 reinoud biodone(buf);
2787 1.10 christos goto out;
2788 1.9 christos }
2789 1.1 reinoud DPRINTF(READ, ("\ttranslate extent went OK\n"));
2790 1.1 reinoud
2791 1.1 reinoud /* pre-check if internal or parts are zero */
2792 1.1 reinoud if (*mapping == UDF_TRANS_INTERN) {
2793 1.1 reinoud error = udf_read_internal(node, (uint8_t *) buf->b_data);
2794 1.1 reinoud if (error) {
2795 1.1 reinoud buf->b_error = error;
2796 1.9 christos }
2797 1.1 reinoud biodone(buf);
2798 1.10 christos goto out;
2799 1.9 christos }
2800 1.1 reinoud DPRINTF(READ, ("\tnot intern\n"));
2801 1.1 reinoud
2802 1.24 wiz /* request read-in of data from disc scheduler */
2803 1.1 reinoud buf->b_resid = buf->b_bcount;
2804 1.1 reinoud for (sector = 0; sector < sectors; sector++) {
2805 1.1 reinoud buf_offset = sector * sector_size;
2806 1.1 reinoud buf_pos = (uint8_t *) buf->b_data + buf_offset;
2807 1.1 reinoud DPRINTF(READ, ("\tprocessing rel sector %d\n", sector));
2808 1.1 reinoud
2809 1.1 reinoud switch (mapping[sector]) {
2810 1.1 reinoud case UDF_TRANS_UNMAPPED:
2811 1.1 reinoud case UDF_TRANS_ZERO:
2812 1.1 reinoud /* copy zero sector */
2813 1.1 reinoud memset(buf_pos, 0, sector_size);
2814 1.1 reinoud DPRINTF(READ, ("\treturning zero sector\n"));
2815 1.1 reinoud nestiobuf_done(buf, sector_size, 0);
2816 1.1 reinoud break;
2817 1.1 reinoud default :
2818 1.1 reinoud DPRINTF(READ, ("\tread sector "
2819 1.1 reinoud "%"PRIu64"\n", mapping[sector]));
2820 1.1 reinoud
2821 1.1 reinoud run_start = mapping[sector];
2822 1.1 reinoud run_length = 1;
2823 1.1 reinoud while (sector < sectors-1) {
2824 1.1 reinoud if (mapping[sector+1] != mapping[sector]+1)
2825 1.1 reinoud break;
2826 1.1 reinoud run_length++;
2827 1.1 reinoud sector++;
2828 1.9 christos }
2829 1.1 reinoud
2830 1.1 reinoud /*
2831 1.1 reinoud * nest an iobuf and mark it for async reading. Since
2832 1.1 reinoud * we're using nested buffers, they can't be cached by
2833 1.1 reinoud * design.
2834 1.1 reinoud */
2835 1.1 reinoud rbuflen = run_length * sector_size;
2836 1.1 reinoud rblk = run_start * (sector_size/DEV_BSIZE);
2837 1.1 reinoud
2838 1.1 reinoud nestbuf = getiobuf();
2839 1.1 reinoud nestiobuf_setup(buf, nestbuf, buf_offset, rbuflen);
2840 1.1 reinoud /* nestbuf is B_ASYNC */
2841 1.1 reinoud
2842 1.24 wiz /* CD schedules on raw blkno */
2843 1.1 reinoud nestbuf->b_blkno = rblk;
2844 1.1 reinoud nestbuf->b_proc = NULL;
2845 1.1 reinoud nestbuf->b_cylinder = 0;
2846 1.1 reinoud nestbuf->b_rawblkno = rblk;
2847 1.1 reinoud VOP_STRATEGY(node->ump->devvp, nestbuf);
2848 1.9 christos }
2849 1.9 christos }
2850 1.10 christos out:
2851 1.1 reinoud DPRINTF(READ, ("\tend of read_filebuf\n"));
2852 1.37 rumble free(mapping, M_UDFTEMP);
2853 1.10 christos return;
2854 1.1 reinoud }
2855 1.1 reinoud #undef FILEBUFSECT
2856 1.1 reinoud
2857 1.1 reinoud
2858 1.1 reinoud /* --------------------------------------------------------------------- */
2859 1.1 reinoud
2860 1.1 reinoud /*
2861 1.1 reinoud * Translate an extent (in sectors) into sector numbers; used for read and
2862 1.1 reinoud * write operations. DOESNT't check extents.
2863 1.1 reinoud */
2864 1.1 reinoud
2865 1.1 reinoud int
2866 1.1 reinoud udf_translate_file_extent(struct udf_node *node,
2867 1.1 reinoud uint32_t from, uint32_t pages,
2868 1.1 reinoud uint64_t *map)
2869 1.1 reinoud {
2870 1.1 reinoud struct udf_mount *ump;
2871 1.1 reinoud struct file_entry *fe;
2872 1.1 reinoud struct extfile_entry *efe;
2873 1.1 reinoud struct short_ad *s_ad;
2874 1.1 reinoud struct long_ad *l_ad, t_ad;
2875 1.1 reinoud uint64_t transsec;
2876 1.1 reinoud uint32_t sector_size, transsec32;
2877 1.1 reinoud uint32_t overlap, translen;
2878 1.1 reinoud uint32_t vpart_num, lb_num, len, alloclen;
2879 1.1 reinoud uint8_t *pos;
2880 1.1 reinoud int error, flags, addr_type, icblen, icbflags;
2881 1.1 reinoud
2882 1.1 reinoud if (!node)
2883 1.1 reinoud return ENOENT;
2884 1.1 reinoud
2885 1.1 reinoud /* shut up gcc */
2886 1.1 reinoud alloclen = addr_type = icbflags = 0;
2887 1.1 reinoud pos = NULL;
2888 1.1 reinoud
2889 1.1 reinoud /* do the work */
2890 1.1 reinoud ump = node->ump;
2891 1.1 reinoud sector_size = ump->discinfo.sector_size;
2892 1.1 reinoud fe = node->fe;
2893 1.1 reinoud efe = node->efe;
2894 1.1 reinoud if (fe) {
2895 1.1 reinoud alloclen = udf_rw32(fe->l_ad);
2896 1.1 reinoud pos = &fe->data[0] + udf_rw32(fe->l_ea);
2897 1.1 reinoud icbflags = udf_rw16(fe->icbtag.flags);
2898 1.9 christos }
2899 1.1 reinoud if (efe) {
2900 1.1 reinoud alloclen = udf_rw32(efe->l_ad);
2901 1.1 reinoud pos = &efe->data[0] + udf_rw32(efe->l_ea);
2902 1.1 reinoud icbflags = udf_rw16(efe->icbtag.flags);
2903 1.9 christos }
2904 1.1 reinoud addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
2905 1.1 reinoud
2906 1.1 reinoud DPRINTF(TRANSLATE, ("udf trans: alloc_len = %d, addr_type %d, "
2907 1.1 reinoud "fe %p, efe %p\n", alloclen, addr_type, fe, efe));
2908 1.1 reinoud
2909 1.1 reinoud vpart_num = udf_rw16(node->loc.loc.part_num);
2910 1.1 reinoud lb_num = len = icblen = 0; /* shut up gcc */
2911 1.1 reinoud while (pages && alloclen) {
2912 1.1 reinoud DPRINTF(TRANSLATE, ("\taddr_type %d\n", addr_type));
2913 1.1 reinoud switch (addr_type) {
2914 1.1 reinoud case UDF_ICB_INTERN_ALLOC :
2915 1.1 reinoud /* TODO check extents? */
2916 1.1 reinoud *map = UDF_TRANS_INTERN;
2917 1.1 reinoud return 0;
2918 1.1 reinoud case UDF_ICB_SHORT_ALLOC :
2919 1.1 reinoud icblen = sizeof(struct short_ad);
2920 1.1 reinoud s_ad = (struct short_ad *) pos;
2921 1.1 reinoud len = udf_rw32(s_ad->len);
2922 1.1 reinoud lb_num = udf_rw32(s_ad->lb_num);
2923 1.1 reinoud break;
2924 1.1 reinoud case UDF_ICB_LONG_ALLOC :
2925 1.1 reinoud icblen = sizeof(struct long_ad);
2926 1.1 reinoud l_ad = (struct long_ad *) pos;
2927 1.1 reinoud len = udf_rw32(l_ad->len);
2928 1.1 reinoud lb_num = udf_rw32(l_ad->loc.lb_num);
2929 1.1 reinoud vpart_num = udf_rw16(l_ad->loc.part_num);
2930 1.1 reinoud DPRINTFIF(TRANSLATE,
2931 1.1 reinoud (l_ad->impl.im_used.flags &
2932 1.1 reinoud UDF_ADIMP_FLAGS_EXTENT_ERASED),
2933 1.1 reinoud ("UDF: got an `extent erased' flag in long_ad\n"));
2934 1.1 reinoud break;
2935 1.1 reinoud default:
2936 1.1 reinoud /* can't be here */
2937 1.1 reinoud return EINVAL; /* for sure */
2938 1.9 christos }
2939 1.1 reinoud
2940 1.1 reinoud /* process extent */
2941 1.1 reinoud flags = UDF_EXT_FLAGS(len);
2942 1.1 reinoud len = UDF_EXT_LEN(len);
2943 1.1 reinoud
2944 1.1 reinoud overlap = (len + sector_size -1) / sector_size;
2945 1.1 reinoud if (from) {
2946 1.1 reinoud if (from > overlap) {
2947 1.1 reinoud from -= overlap;
2948 1.1 reinoud overlap = 0;
2949 1.1 reinoud } else {
2950 1.1 reinoud lb_num += from; /* advance in extent */
2951 1.1 reinoud overlap -= from;
2952 1.1 reinoud from = 0;
2953 1.9 christos }
2954 1.9 christos }
2955 1.1 reinoud
2956 1.1 reinoud overlap = MIN(overlap, pages);
2957 1.1 reinoud while (overlap) {
2958 1.1 reinoud switch (flags) {
2959 1.1 reinoud case UDF_EXT_REDIRECT :
2960 1.1 reinoud /* no support for allocation extentions yet */
2961 1.1 reinoud /* TODO support for allocation extention */
2962 1.1 reinoud return ENOENT;
2963 1.1 reinoud case UDF_EXT_FREED :
2964 1.1 reinoud case UDF_EXT_FREE :
2965 1.1 reinoud transsec = UDF_TRANS_ZERO;
2966 1.1 reinoud translen = overlap;
2967 1.1 reinoud while (overlap && pages && translen) {
2968 1.1 reinoud *map++ = transsec;
2969 1.17 reinoud lb_num++;
2970 1.1 reinoud overlap--; pages--; translen--;
2971 1.9 christos }
2972 1.1 reinoud break;
2973 1.1 reinoud case UDF_EXT_ALLOCATED :
2974 1.1 reinoud t_ad.loc.lb_num = udf_rw32(lb_num);
2975 1.1 reinoud t_ad.loc.part_num = udf_rw16(vpart_num);
2976 1.1 reinoud error = udf_translate_vtop(ump,
2977 1.1 reinoud &t_ad, &transsec32, &translen);
2978 1.1 reinoud transsec = transsec32;
2979 1.1 reinoud if (error)
2980 1.1 reinoud return error;
2981 1.1 reinoud while (overlap && pages && translen) {
2982 1.1 reinoud *map++ = transsec;
2983 1.17 reinoud lb_num++; transsec++;
2984 1.1 reinoud overlap--; pages--; translen--;
2985 1.9 christos }
2986 1.1 reinoud break;
2987 1.9 christos }
2988 1.9 christos }
2989 1.1 reinoud pos += icblen;
2990 1.1 reinoud alloclen -= icblen;
2991 1.9 christos }
2992 1.1 reinoud return 0;
2993 1.1 reinoud }
2994 1.1 reinoud
2995 1.1 reinoud /* --------------------------------------------------------------------- */
2996 1.1 reinoud
2997