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