| Commit | Line | Data |
|---|---|---|
| 03998195 JS |
1 | /*- |
| 2 | * Copyright (c) 2001, 2002 Scott Long <scottl@freebsd.org> | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions | |
| 7 | * are met: | |
| 8 | * 1. Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * | |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |
| 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
| 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
| 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 24 | * SUCH DAMAGE. | |
| 25 | * | |
| 26 | * $FreeBSD: src/sys/fs/udf/udf_vnops.c,v 1.33 2003/12/07 05:04:49 scottl Exp $ | |
| 84009d92 | 27 | * $DragonFly: src/sys/vfs/udf/udf_vnops.c,v 1.32 2007/11/20 21:03:51 dillon Exp $ |
| 03998195 JS |
28 | */ |
| 29 | ||
| 30 | /* udf_vnops.c */ | |
| 31 | /* Take care of the vnode side of things */ | |
| 32 | ||
| 33 | #include <sys/param.h> | |
| 34 | #include <sys/systm.h> | |
| 35 | #include <sys/namei.h> | |
| 36 | #include <sys/kernel.h> | |
| 37 | #include <sys/malloc.h> | |
| 38 | #include <sys/stat.h> | |
| 39 | #include <sys/module.h> | |
| 40 | #include <sys/buf.h> | |
| 41 | #include <sys/iconv.h> | |
| 42 | #include <sys/mount.h> | |
| 43 | #include <sys/vnode.h> | |
| 44 | #include <sys/dirent.h> | |
| 45 | #include <sys/queue.h> | |
| 46 | #include <sys/unistd.h> | |
| 47 | ||
| f91a71dd JS |
48 | #include <machine/inttypes.h> |
| 49 | ||
| 03998195 JS |
50 | #include <vfs/udf/ecma167-udf.h> |
| 51 | #include <vfs/udf/osta.h> | |
| 52 | #include <vfs/udf/udf.h> | |
| 53 | #include <vfs/udf/udf_mount.h> | |
| 54 | ||
| 55 | static int udf_access(struct vop_access_args *); | |
| 56 | static int udf_getattr(struct vop_getattr_args *); | |
| 57 | static int udf_ioctl(struct vop_ioctl_args *); | |
| 58 | static int udf_pathconf(struct vop_pathconf_args *); | |
| 59 | static int udf_read(struct vop_read_args *); | |
| 60 | static int udf_readdir(struct vop_readdir_args *); | |
| 61 | static int udf_readlink(struct vop_readlink_args *ap); | |
| 62 | static int udf_strategy(struct vop_strategy_args *); | |
| 63 | static int udf_bmap(struct vop_bmap_args *); | |
| e62afb5f | 64 | static int udf_lookup(struct vop_old_lookup_args *); |
| 03998195 JS |
65 | static int udf_reclaim(struct vop_reclaim_args *); |
| 66 | static int udf_readatoffset(struct udf_node *, int *, int, struct buf **, uint8_t **); | |
| 67 | static int udf_bmap_internal(struct udf_node *, uint32_t, daddr_t *, uint32_t *); | |
| 68 | ||
| 66a1ddf5 MD |
69 | struct vop_ops udf_vnode_vops = { |
| 70 | .vop_default = vop_defaultop, | |
| 71 | .vop_access = udf_access, | |
| 72 | .vop_bmap = udf_bmap, | |
| 73 | .vop_old_lookup = udf_lookup, | |
| 74 | .vop_getattr = udf_getattr, | |
| 75 | .vop_ioctl = udf_ioctl, | |
| 76 | .vop_pathconf = udf_pathconf, | |
| 77 | .vop_read = udf_read, | |
| 78 | .vop_readdir = udf_readdir, | |
| 79 | .vop_readlink = udf_readlink, | |
| 80 | .vop_reclaim = udf_reclaim, | |
| 81 | .vop_strategy = udf_strategy | |
| 03998195 | 82 | }; |
| 03998195 JS |
83 | |
| 84 | MALLOC_DEFINE(M_UDFFID, "UDF FID", "UDF FileId structure"); | |
| 85 | MALLOC_DEFINE(M_UDFDS, "UDF DS", "UDF Dirstream structure"); | |
| 86 | ||
| 87 | #define UDF_INVALID_BMAP -1 | |
| 88 | ||
| 89 | /* Look up a udf_node based on the ino_t passed in and return it's vnode */ | |
| 90 | int | |
| 91 | udf_hashlookup(struct udf_mnt *udfmp, ino_t id, struct vnode **vpp) | |
| 92 | { | |
| 93 | struct udf_node *node; | |
| 94 | struct udf_hash_lh *lh; | |
| 5fd012e0 | 95 | struct vnode *vp; |
| 03998195 JS |
96 | |
| 97 | *vpp = NULL; | |
| 98 | ||
| 3b998fa9 | 99 | lwkt_gettoken(&udfmp->hash_token); |
| 03998195 JS |
100 | loop: |
| 101 | lh = &udfmp->hashtbl[id % udfmp->hashsz]; | |
| 5fd012e0 | 102 | if (lh == NULL) { |
| 3b998fa9 | 103 | lwkt_reltoken(&udfmp->hash_token); |
| 03998195 | 104 | return(ENOENT); |
| 5fd012e0 | 105 | } |
| 03998195 JS |
106 | LIST_FOREACH(node, lh, le) { |
| 107 | if (node->hash_id != id) | |
| 108 | continue; | |
| 5fd012e0 | 109 | vp = node->i_vnode; |
| 87de5057 | 110 | if (vget(vp, LK_EXCLUSIVE)) |
| 5fd012e0 | 111 | goto loop; |
| 03998195 JS |
112 | /* |
| 113 | * We must check to see if the inode has been ripped | |
| 114 | * out from under us after blocking. | |
| 115 | */ | |
| 116 | lh = &udfmp->hashtbl[id % udfmp->hashsz]; | |
| 3446c007 | 117 | LIST_FOREACH(node, lh, le) { |
| 03998195 JS |
118 | if (node->hash_id == id) |
| 119 | break; | |
| 3446c007 | 120 | } |
| 5fd012e0 MD |
121 | if (node == NULL || vp != node->i_vnode) { |
| 122 | vput(vp); | |
| 03998195 JS |
123 | goto loop; |
| 124 | } | |
| 3b998fa9 | 125 | lwkt_reltoken(&udfmp->hash_token); |
| 5fd012e0 | 126 | *vpp = vp; |
| 03998195 JS |
127 | return(0); |
| 128 | } | |
| 129 | ||
| 3b998fa9 | 130 | lwkt_reltoken(&udfmp->hash_token); |
| 03998195 JS |
131 | return(0); |
| 132 | } | |
| 133 | ||
| 134 | int | |
| 135 | udf_hashins(struct udf_node *node) | |
| 136 | { | |
| 137 | struct udf_mnt *udfmp; | |
| 138 | struct udf_hash_lh *lh; | |
| 03998195 JS |
139 | |
| 140 | udfmp = node->udfmp; | |
| 141 | ||
| 3b998fa9 | 142 | lwkt_gettoken(&udfmp->hash_token); |
| 03998195 | 143 | lh = &udfmp->hashtbl[node->hash_id % udfmp->hashsz]; |
| 03998195 | 144 | LIST_INSERT_HEAD(lh, node, le); |
| 3b998fa9 | 145 | lwkt_reltoken(&udfmp->hash_token); |
| 03998195 JS |
146 | |
| 147 | return(0); | |
| 148 | } | |
| 149 | ||
| 150 | int | |
| 151 | udf_hashrem(struct udf_node *node) | |
| 152 | { | |
| 153 | struct udf_mnt *udfmp; | |
| 154 | struct udf_hash_lh *lh; | |
| 03998195 JS |
155 | |
| 156 | udfmp = node->udfmp; | |
| 157 | ||
| 3b998fa9 | 158 | lwkt_gettoken(&udfmp->hash_token); |
| 03998195 JS |
159 | lh = &udfmp->hashtbl[node->hash_id % udfmp->hashsz]; |
| 160 | if (lh == NULL) | |
| f91a71dd | 161 | panic("hash entry is NULL, node->hash_id= %"PRId64"\n", node->hash_id); |
| 03998195 | 162 | LIST_REMOVE(node, le); |
| 3b998fa9 | 163 | lwkt_reltoken(&udfmp->hash_token); |
| 03998195 JS |
164 | |
| 165 | return(0); | |
| 166 | } | |
| 167 | ||
| 168 | int | |
| 169 | udf_allocv(struct mount *mp, struct vnode **vpp) | |
| 170 | { | |
| 171 | int error; | |
| 172 | struct vnode *vp; | |
| 173 | ||
| 6ddb7618 | 174 | error = getnewvnode(VT_UDF, mp, &vp, 0, 0); |
| 03998195 | 175 | if (error) { |
| 086c1d7e | 176 | kprintf("udf_allocv: failed to allocate new vnode\n"); |
| 03998195 JS |
177 | return(error); |
| 178 | } | |
| 179 | ||
| 180 | *vpp = vp; | |
| 181 | return(0); | |
| 182 | } | |
| 183 | ||
| 184 | /* Convert file entry permission (5 bits per owner/group/user) to a mode_t */ | |
| 185 | static mode_t | |
| 186 | udf_permtomode(struct udf_node *node) | |
| 187 | { | |
| 188 | uint32_t perm; | |
| 189 | uint32_t flags; | |
| 190 | mode_t mode; | |
| 191 | ||
| 192 | perm = node->fentry->perm; | |
| 193 | flags = node->fentry->icbtag.flags; | |
| 194 | ||
| 195 | mode = perm & UDF_FENTRY_PERM_USER_MASK; | |
| 196 | mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK) >> 2); | |
| 197 | mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4); | |
| 198 | mode |= ((flags & UDF_ICB_TAG_FLAGS_STICKY) << 4); | |
| 199 | mode |= ((flags & UDF_ICB_TAG_FLAGS_SETGID) << 6); | |
| 200 | mode |= ((flags & UDF_ICB_TAG_FLAGS_SETUID) << 8); | |
| 201 | ||
| 202 | return(mode); | |
| 203 | } | |
| 204 | ||
| 205 | static int | |
| 206 | udf_access(struct vop_access_args *a) | |
| 207 | { | |
| 208 | struct vnode *vp; | |
| 209 | struct udf_node *node; | |
| 03998195 JS |
210 | |
| 211 | vp = a->a_vp; | |
| 212 | node = VTON(vp); | |
| 7ca841cc NT |
213 | KKASSERT(vp->v_mount->mnt_flag & MNT_RDONLY); |
| 214 | return (vop_helper_access(a, node->fentry->uid, node->fentry->gid, | |
| 215 | udf_permtomode(node), 0)); | |
| 03998195 JS |
216 | } |
| 217 | ||
| 218 | static int mon_lens[2][12] = { | |
| 219 | {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, | |
| 220 | {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} | |
| 221 | }; | |
| 222 | ||
| 223 | static int | |
| 224 | udf_isaleapyear(int year) | |
| 225 | { | |
| 226 | int i; | |
| 227 | ||
| 228 | i = (year % 4) ? 0 : 1; | |
| 229 | i &= (year % 100) ? 1 : 0; | |
| 230 | i |= (year % 400) ? 0 : 1; | |
| 231 | ||
| 232 | return(i); | |
| 233 | } | |
| 234 | ||
| 235 | /* | |
| 236 | * XXX This is just a rough hack. Daylight savings isn't calculated and tv_nsec | |
| 237 | * is ignored. | |
| 238 | * Timezone calculation compliments of Julian Elischer <julian@elischer.org>. | |
| 239 | */ | |
| 240 | static void | |
| 241 | udf_timetotimespec(struct timestamp *time, struct timespec *t) | |
| 242 | { | |
| 243 | int i, lpyear, daysinyear; | |
| 244 | union { | |
| 245 | uint16_t u_tz_offset; | |
| 246 | int16_t s_tz_offset; | |
| 247 | } tz; | |
| 248 | ||
| 249 | t->tv_nsec = 0; | |
| 250 | ||
| 251 | /* DirectCD seems to like using bogus year values */ | |
| 252 | if (time->year < 1970) { | |
| 253 | t->tv_sec = 0; | |
| 254 | return; | |
| 255 | } | |
| 256 | ||
| 257 | /* Calculate the time and day */ | |
| 258 | t->tv_sec = time->second; | |
| 259 | t->tv_sec += time->minute * 60; | |
| 260 | t->tv_sec += time->hour * 3600; | |
| 261 | t->tv_sec += time->day * 3600 * 24; | |
| 262 | ||
| 263 | /* Calclulate the month */ | |
| 264 | lpyear = udf_isaleapyear(time->year); | |
| 265 | for (i = 1; i < time->month; i++) | |
| 266 | t->tv_sec += mon_lens[lpyear][i] * 3600 * 24; | |
| 267 | ||
| 268 | /* Speed up the calculation */ | |
| 269 | if (time->year > 1979) | |
| 270 | t->tv_sec += 315532800; | |
| 271 | if (time->year > 1989) | |
| 272 | t->tv_sec += 315619200; | |
| 273 | if (time->year > 1999) | |
| 274 | t->tv_sec += 315532800; | |
| 275 | for (i = 2000; i < time->year; i++) { | |
| 276 | daysinyear = udf_isaleapyear(i) + 365 ; | |
| 277 | t->tv_sec += daysinyear * 3600 * 24; | |
| 278 | } | |
| 279 | ||
| 280 | /* | |
| 281 | * Calculate the time zone. The timezone is 12 bit signed 2's | |
| 282 | * compliment, so we gotta do some extra magic to handle it right. | |
| 283 | */ | |
| 284 | tz.u_tz_offset = time->type_tz; | |
| 285 | tz.u_tz_offset &= 0x0fff; | |
| 286 | if (tz.u_tz_offset & 0x0800) | |
| 287 | tz.u_tz_offset |= 0xf000; /* extend the sign to 16 bits */ | |
| 288 | if ((time->type_tz & 0x1000) && (tz.s_tz_offset != -2047)) | |
| 289 | t->tv_sec -= tz.s_tz_offset * 60; | |
| 290 | ||
| 291 | return; | |
| 292 | } | |
| 293 | ||
| 294 | static int | |
| 295 | udf_getattr(struct vop_getattr_args *a) | |
| 296 | { | |
| 297 | struct vnode *vp; | |
| 298 | struct udf_node *node; | |
| 299 | struct vattr *vap; | |
| 300 | struct file_entry *fentry; | |
| 301 | struct timespec ts; | |
| 302 | ||
| 303 | ts.tv_sec = 0; | |
| 304 | ||
| 305 | vp = a->a_vp; | |
| 306 | vap = a->a_vap; | |
| 307 | node = VTON(vp); | |
| 308 | fentry = node->fentry; | |
| 309 | ||
| 310 | vap->va_fsid = dev2udev(node->i_dev); | |
| 311 | vap->va_fileid = node->hash_id; | |
| 312 | vap->va_mode = udf_permtomode(node); | |
| 313 | vap->va_nlink = fentry->link_cnt; | |
| 314 | /* | |
| 315 | * XXX The spec says that -1 is valid for uid/gid and indicates an | |
| 316 | * invalid uid/gid. How should this be represented? | |
| 317 | */ | |
| 318 | vap->va_uid = (fentry->uid == 0xffffffff) ? 0 : fentry->uid; | |
| 319 | vap->va_gid = (fentry->gid == 0xffffffff) ? 0 : fentry->gid; | |
| 320 | udf_timetotimespec(&fentry->atime, &vap->va_atime); | |
| 321 | udf_timetotimespec(&fentry->mtime, &vap->va_mtime); | |
| 322 | vap->va_ctime = vap->va_mtime; /* XXX Stored as an Extended Attribute */ | |
| 0e9b9130 MD |
323 | vap->va_rmajor = VNOVAL; |
| 324 | vap->va_rminor = VNOVAL; | |
| 03998195 JS |
325 | if (vp->v_type & VDIR) { |
| 326 | /* | |
| 327 | * Directories that are recorded within their ICB will show | |
| 328 | * as having 0 blocks recorded. Since tradition dictates | |
| 329 | * that directories consume at least one logical block, | |
| 330 | * make it appear so. | |
| 331 | */ | |
| 332 | if (fentry->logblks_rec != 0) | |
| 333 | vap->va_size = fentry->logblks_rec * node->udfmp->bsize; | |
| 334 | else | |
| 335 | vap->va_size = node->udfmp->bsize; | |
| 336 | } else | |
| 337 | vap->va_size = fentry->inf_len; | |
| 338 | vap->va_flags = 0; | |
| 339 | vap->va_gen = 1; | |
| 340 | vap->va_blocksize = node->udfmp->bsize; | |
| 341 | vap->va_bytes = fentry->inf_len; | |
| 342 | vap->va_type = vp->v_type; | |
| 343 | vap->va_filerev = 0; /* XXX */ | |
| 344 | return(0); | |
| 345 | } | |
| 346 | ||
| 347 | /* | |
| 348 | * File specific ioctls. DeCSS candidate? | |
| 349 | */ | |
| 350 | static int | |
| 351 | udf_ioctl(struct vop_ioctl_args *a) | |
| 352 | { | |
| 086c1d7e | 353 | kprintf("%s called\n", __func__); |
| 03998195 JS |
354 | return(ENOTTY); |
| 355 | } | |
| 356 | ||
| 357 | /* | |
| 358 | * I'm not sure that this has much value in a read-only filesystem, but | |
| 359 | * cd9660 has it too. | |
| 360 | */ | |
| 361 | static int | |
| 362 | udf_pathconf(struct vop_pathconf_args *a) | |
| 363 | { | |
| 364 | ||
| 365 | switch (a->a_name) { | |
| 366 | case _PC_LINK_MAX: | |
| 367 | *a->a_retval = 65535; | |
| 368 | return(0); | |
| 369 | case _PC_NAME_MAX: | |
| 370 | *a->a_retval = NAME_MAX; | |
| 371 | return(0); | |
| 372 | case _PC_PATH_MAX: | |
| 373 | *a->a_retval = PATH_MAX; | |
| 374 | return(0); | |
| 375 | case _PC_NO_TRUNC: | |
| 376 | *a->a_retval = 1; | |
| 377 | return(0); | |
| 378 | default: | |
| 379 | return(EINVAL); | |
| 380 | } | |
| 381 | } | |
| 382 | ||
| 383 | static int | |
| 384 | udf_read(struct vop_read_args *a) | |
| 385 | { | |
| 386 | struct vnode *vp = a->a_vp; | |
| 387 | struct uio *uio = a->a_uio; | |
| 388 | struct udf_node *node = VTON(vp); | |
| 389 | struct buf *bp; | |
| 390 | uint8_t *data; | |
| 391 | int error = 0; | |
| 392 | int size, fsize, offset; | |
| 393 | ||
| 394 | if (uio->uio_offset < 0) | |
| 395 | return(EINVAL); | |
| 396 | ||
| 397 | fsize = node->fentry->inf_len; | |
| 398 | ||
| 399 | while (uio->uio_offset < fsize && uio->uio_resid > 0) { | |
| 400 | offset = uio->uio_offset; | |
| 401 | size = uio->uio_resid; | |
| 402 | error = udf_readatoffset(node, &size, offset, &bp, &data); | |
| ffaa7d78 MD |
403 | if (error == 0) |
| 404 | error = uiomove(data, size, uio); | |
| 03998195 JS |
405 | if (bp != NULL) |
| 406 | brelse(bp); | |
| 407 | if (error) | |
| 408 | break; | |
| 4a77c3ab | 409 | } |
| 03998195 JS |
410 | |
| 411 | return(error); | |
| 412 | } | |
| 413 | ||
| 414 | /* | |
| 415 | * Call the OSTA routines to translate the name from a CS0 dstring to a | |
| 416 | * 16-bit Unicode String. Hooks need to be placed in here to translate from | |
| 417 | * Unicode to the encoding that the kernel/user expects. Return the length | |
| 418 | * of the translated string. | |
| 419 | */ | |
| 420 | static int | |
| 421 | udf_transname(char *cs0string, char *destname, int len, struct udf_mnt *udfmp) | |
| 422 | { | |
| 423 | unicode_t *transname; | |
| 424 | int i, unilen = 0, destlen; | |
| 425 | ||
| 426 | /* Convert 16-bit Unicode to destname */ | |
| 427 | /* allocate a buffer big enough to hold an 8->16 bit expansion */ | |
| efda3bd0 | 428 | transname = kmalloc(NAME_MAX * sizeof(unicode_t), M_TEMP, M_WAITOK | M_ZERO); |
| 03998195 JS |
429 | |
| 430 | if ((unilen = udf_UncompressUnicode(len, cs0string, transname)) == -1) { | |
| 086c1d7e | 431 | kprintf("udf: Unicode translation failed\n"); |
| efda3bd0 | 432 | kfree(transname, M_TEMP); |
| 03998195 JS |
433 | return(0); |
| 434 | } | |
| 435 | ||
| 436 | for (i = 0; i < unilen ; i++) | |
| 437 | if (transname[i] & 0xff00) | |
| 438 | destname[i] = '.'; /* Fudge the 16bit chars */ | |
| 439 | else | |
| 440 | destname[i] = transname[i] & 0xff; | |
| efda3bd0 | 441 | kfree(transname, M_TEMP); |
| 03998195 JS |
442 | destname[unilen] = 0; |
| 443 | destlen = unilen; | |
| 444 | ||
| 445 | return(destlen); | |
| 446 | } | |
| 447 | ||
| 448 | /* | |
| 449 | * Compare a CS0 dstring with a name passed in from the VFS layer. Return | |
| 450 | * 0 on a successful match, nonzero therwise. Unicode work may need to be done | |
| 451 | * here also. | |
| 452 | */ | |
| 453 | static int | |
| 454 | udf_cmpname(char *cs0string, char *cmpname, int cs0len, int cmplen, struct udf_mnt *udfmp) | |
| 455 | { | |
| 456 | char *transname; | |
| 457 | int error = 0; | |
| 458 | ||
| 459 | /* This is overkill, but not worth creating a new zone */ | |
| 460 | ||
| efda3bd0 | 461 | transname = kmalloc(NAME_MAX * sizeof(unicode_t), M_TEMP, |
| 03998195 JS |
462 | M_WAITOK | M_ZERO); |
| 463 | ||
| 464 | cs0len = udf_transname(cs0string, transname, cs0len, udfmp); | |
| 465 | ||
| 466 | /* Easy check. If they aren't the same length, they aren't equal */ | |
| 467 | if ((cs0len == 0) || (cs0len != cmplen)) | |
| 468 | error = -1; | |
| 469 | else | |
| 470 | error = bcmp(transname, cmpname, cmplen); | |
| 471 | ||
| efda3bd0 | 472 | kfree(transname, M_TEMP); |
| 03998195 JS |
473 | return(error); |
| 474 | } | |
| 475 | ||
| 476 | struct udf_uiodir { | |
| 477 | struct dirent *dirent; | |
| 84009d92 | 478 | off_t *cookies; |
| 03998195 JS |
479 | int ncookies; |
| 480 | int acookies; | |
| 481 | int eofflag; | |
| 482 | }; | |
| 483 | ||
| 03998195 JS |
484 | static struct udf_dirstream * |
| 485 | udf_opendir(struct udf_node *node, int offset, int fsize, struct udf_mnt *udfmp) | |
| 486 | { | |
| 487 | struct udf_dirstream *ds; | |
| 488 | ||
| efda3bd0 | 489 | ds = kmalloc(sizeof(*ds), M_UDFDS, M_WAITOK | M_ZERO); |
| 03998195 JS |
490 | |
| 491 | ds->node = node; | |
| 492 | ds->offset = offset; | |
| 493 | ds->udfmp = udfmp; | |
| 494 | ds->fsize = fsize; | |
| 495 | ||
| 496 | return(ds); | |
| 497 | } | |
| 498 | ||
| 499 | static struct fileid_desc * | |
| 500 | udf_getfid(struct udf_dirstream *ds) | |
| 501 | { | |
| 502 | struct fileid_desc *fid; | |
| 503 | int error, frag_size = 0, total_fid_size; | |
| 504 | ||
| 505 | /* End of directory? */ | |
| 506 | if (ds->offset + ds->off >= ds->fsize) { | |
| 507 | ds->error = 0; | |
| 508 | return(NULL); | |
| 509 | } | |
| 510 | ||
| 511 | /* Grab the first extent of the directory */ | |
| 512 | if (ds->off == 0) { | |
| 513 | ds->size = 0; | |
| ffaa7d78 MD |
514 | if (ds->bp != NULL) |
| 515 | brelse(ds->bp); | |
| 03998195 JS |
516 | error = udf_readatoffset(ds->node, &ds->size, ds->offset, |
| 517 | &ds->bp, &ds->data); | |
| 518 | if (error) { | |
| 519 | ds->error = error; | |
| 520 | return(NULL); | |
| 521 | } | |
| 522 | } | |
| 523 | ||
| 524 | /* | |
| 525 | * Clean up from a previous fragmented FID. | |
| 526 | * XXX Is this the right place for this? | |
| 527 | */ | |
| 528 | if (ds->fid_fragment && ds->buf != NULL) { | |
| 529 | ds->fid_fragment = 0; | |
| efda3bd0 | 530 | kfree(ds->buf, M_UDFFID); |
| 03998195 JS |
531 | } |
| 532 | ||
| 533 | fid = (struct fileid_desc*)&ds->data[ds->off]; | |
| 534 | ||
| 535 | /* | |
| 536 | * Check to see if the fid is fragmented. The first test | |
| 537 | * ensures that we don't wander off the end of the buffer | |
| 538 | * looking for the l_iu and l_fi fields. | |
| 539 | */ | |
| 540 | if (ds->off + UDF_FID_SIZE > ds->size || | |
| 541 | ds->off + fid->l_iu + fid->l_fi + UDF_FID_SIZE > ds->size) { | |
| 542 | ||
| 543 | /* Copy what we have of the fid into a buffer */ | |
| 544 | frag_size = ds->size - ds->off; | |
| 545 | if (frag_size >= ds->udfmp->bsize) { | |
| 086c1d7e | 546 | kprintf("udf: invalid FID fragment\n"); |
| 03998195 JS |
547 | ds->error = EINVAL; |
| 548 | return(NULL); | |
| 549 | } | |
| 550 | ||
| 551 | /* | |
| 552 | * File ID descriptors can only be at most one | |
| 553 | * logical sector in size. | |
| 554 | */ | |
| efda3bd0 | 555 | ds->buf = kmalloc(ds->udfmp->bsize, M_UDFFID, M_WAITOK | M_ZERO); |
| 03998195 JS |
556 | bcopy(fid, ds->buf, frag_size); |
| 557 | ||
| 558 | /* Reduce all of the casting magic */ | |
| 559 | fid = (struct fileid_desc*)ds->buf; | |
| 560 | ||
| 561 | if (ds->bp != NULL) | |
| 562 | brelse(ds->bp); | |
| 563 | ||
| 564 | /* Fetch the next allocation */ | |
| 565 | ds->offset += ds->size; | |
| 566 | ds->size = 0; | |
| 567 | error = udf_readatoffset(ds->node, &ds->size, ds->offset, | |
| 568 | &ds->bp, &ds->data); | |
| 569 | if (error) { | |
| 570 | ds->error = error; | |
| 571 | return(NULL); | |
| 572 | } | |
| 573 | ||
| 574 | /* | |
| 575 | * If the fragment was so small that we didn't get | |
| 576 | * the l_iu and l_fi fields, copy those in. | |
| 577 | */ | |
| 578 | if (frag_size < UDF_FID_SIZE) | |
| 579 | bcopy(ds->data, &ds->buf[frag_size], | |
| 580 | UDF_FID_SIZE - frag_size); | |
| 581 | ||
| 582 | /* | |
| 583 | * Now that we have enough of the fid to work with, | |
| 584 | * copy in the rest of the fid from the new | |
| 585 | * allocation. | |
| 586 | */ | |
| 587 | total_fid_size = UDF_FID_SIZE + fid->l_iu + fid->l_fi; | |
| 588 | if (total_fid_size > ds->udfmp->bsize) { | |
| 086c1d7e | 589 | kprintf("udf: invalid FID\n"); |
| 03998195 JS |
590 | ds->error = EIO; |
| 591 | return(NULL); | |
| 592 | } | |
| 593 | bcopy(ds->data, &ds->buf[frag_size], | |
| 594 | total_fid_size - frag_size); | |
| 595 | ||
| 596 | ds->fid_fragment = 1; | |
| 597 | } else | |
| 598 | total_fid_size = fid->l_iu + fid->l_fi + UDF_FID_SIZE; | |
| 599 | ||
| 600 | /* | |
| 601 | * Update the offset. Align on a 4 byte boundary because the | |
| 602 | * UDF spec says so. | |
| 603 | */ | |
| 604 | ds->this_off = ds->off; | |
| 605 | if (!ds->fid_fragment) | |
| 606 | ds->off += (total_fid_size + 3) & ~0x03; | |
| 607 | else | |
| 608 | ds->off = (total_fid_size - frag_size + 3) & ~0x03; | |
| 609 | ||
| 610 | return(fid); | |
| 611 | } | |
| 612 | ||
| 613 | static void | |
| 614 | udf_closedir(struct udf_dirstream *ds) | |
| 615 | { | |
| 616 | ||
| 617 | if (ds->bp != NULL) | |
| 618 | brelse(ds->bp); | |
| 619 | ||
| 620 | if (ds->fid_fragment && ds->buf != NULL) | |
| efda3bd0 | 621 | kfree(ds->buf, M_UDFFID); |
| 03998195 | 622 | |
| efda3bd0 | 623 | kfree(ds, M_UDFDS); |
| 03998195 JS |
624 | } |
| 625 | ||
| 626 | static int | |
| 627 | udf_readdir(struct vop_readdir_args *a) | |
| 628 | { | |
| 629 | struct vnode *vp; | |
| 630 | struct uio *uio; | |
| 03998195 JS |
631 | struct udf_node *node; |
| 632 | struct udf_mnt *udfmp; | |
| 633 | struct fileid_desc *fid; | |
| 634 | struct udf_uiodir uiodir; | |
| 635 | struct udf_dirstream *ds; | |
| 84009d92 | 636 | off_t *cookies = NULL; |
| 03998195 JS |
637 | int ncookies; |
| 638 | int error = 0; | |
| d6427990 | 639 | char *name; |
| 03998195 JS |
640 | |
| 641 | vp = a->a_vp; | |
| 885ecb13 MD |
642 | |
| 643 | if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) | |
| 644 | return (error); | |
| 645 | ||
| 03998195 JS |
646 | uio = a->a_uio; |
| 647 | node = VTON(vp); | |
| 648 | udfmp = node->udfmp; | |
| 649 | uiodir.eofflag = 1; | |
| 650 | ||
| 651 | if (a->a_ncookies != NULL) { | |
| 652 | /* | |
| 653 | * Guess how many entries are needed. If we run out, this | |
| 654 | * function will be called again and thing will pick up were | |
| 655 | * it left off. | |
| 656 | */ | |
| fb0466c9 MD |
657 | ncookies = uio->uio_resid / 8 + 1; |
| 658 | if (ncookies > 1024) | |
| 659 | ncookies = 1024; | |
| 84009d92 | 660 | cookies = kmalloc(sizeof(off_t) * ncookies, M_TEMP, M_WAITOK); |
| 03998195 JS |
661 | uiodir.ncookies = ncookies; |
| 662 | uiodir.cookies = cookies; | |
| 663 | uiodir.acookies = 0; | |
| d557216f | 664 | } else { |
| 03998195 | 665 | uiodir.cookies = NULL; |
| d557216f MD |
666 | uiodir.ncookies = 0; |
| 667 | } | |
| 03998195 JS |
668 | |
| 669 | /* | |
| 670 | * Iterate through the file id descriptors. Give the parent dir | |
| 671 | * entry special attention. | |
| 672 | */ | |
| 673 | ds = udf_opendir(node, uio->uio_offset, node->fentry->inf_len, | |
| 674 | node->udfmp); | |
| 675 | ||
| efda3bd0 | 676 | name = kmalloc(NAME_MAX, M_TEMP, M_WAITOK); |
| d6427990 | 677 | |
| 03998195 JS |
678 | while ((fid = udf_getfid(ds)) != NULL) { |
| 679 | ||
| 680 | /* XXX Should we return an error on a bad fid? */ | |
| 681 | if (udf_checktag(&fid->tag, TAGID_FID)) { | |
| 086c1d7e | 682 | kprintf("Invalid FID tag\n"); |
| 03998195 JS |
683 | error = EIO; |
| 684 | break; | |
| 685 | } | |
| 686 | ||
| 687 | /* Is this a deleted file? */ | |
| 688 | if (fid->file_char & UDF_FILE_CHAR_DEL) | |
| 689 | continue; | |
| 690 | ||
| 691 | if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) { | |
| 692 | /* Do up the '.' and '..' entries. Dummy values are | |
| 693 | * used for the cookies since the offset here is | |
| 694 | * usually zero, and NFS doesn't like that value | |
| 695 | */ | |
| d6427990 JS |
696 | if (uiodir.cookies != NULL) { |
| 697 | if (++uiodir.acookies > uiodir.ncookies) { | |
| 698 | uiodir.eofflag = 0; | |
| 699 | break; | |
| 700 | } | |
| 701 | *uiodir.cookies++ = 1; | |
| 702 | } | |
| 703 | if (vop_write_dirent(&error, uio, node->hash_id, DT_DIR, | |
| 704 | 1, ".")) { | |
| 705 | uiodir.eofflag = 0; | |
| 03998195 | 706 | break; |
| d6427990 JS |
707 | } |
| 708 | if (error) { | |
| 709 | uiodir.eofflag = 0; | |
| 710 | break; | |
| 711 | } | |
| 712 | if (uiodir.cookies != NULL) { | |
| 713 | if (++uiodir.acookies > uiodir.ncookies) { | |
| 714 | uiodir.eofflag = 0; | |
| 715 | break; | |
| 716 | } | |
| 717 | *uiodir.cookies++ = 2; | |
| 718 | } | |
| 719 | if (vop_write_dirent(&error, uio, udf_getid(&fid->icb), | |
| 720 | DT_DIR, 2, "..")) { | |
| 721 | uiodir.eofflag = 0; | |
| 722 | break; | |
| 723 | } | |
| 724 | if (error) { | |
| 725 | uiodir.eofflag = 0; | |
| 726 | break; | |
| 727 | } | |
| 03998195 | 728 | } else { |
| d6427990 | 729 | uint8_t d_type = (fid->file_char & UDF_FILE_CHAR_DIR) ? |
| 03998195 | 730 | DT_DIR : DT_UNKNOWN; |
| d6427990 JS |
731 | uint16_t namelen = udf_transname(&fid->data[fid->l_iu], |
| 732 | name, fid->l_fi, udfmp); | |
| 733 | ||
| 734 | if (uiodir.cookies != NULL) { | |
| 735 | if (++uiodir.acookies > uiodir.ncookies) { | |
| 736 | uiodir.eofflag = 0; | |
| 737 | break; | |
| 738 | } | |
| 739 | *uiodir.cookies++ = ds->this_off; | |
| 740 | } | |
| 741 | if (vop_write_dirent(&error, uio, udf_getid(&fid->icb), | |
| 742 | d_type, namelen, name)) { | |
| 743 | uiodir.eofflag = 0; | |
| 744 | break; | |
| 745 | } | |
| 746 | if (error) { | |
| 747 | uiodir.eofflag = 0; | |
| 748 | break; | |
| 749 | } | |
| 03998195 JS |
750 | } |
| 751 | if (error) { | |
| 086c1d7e | 752 | kprintf("uiomove returned %d\n", error); |
| 03998195 JS |
753 | break; |
| 754 | } | |
| 755 | ||
| 756 | } | |
| 757 | ||
| efda3bd0 | 758 | kfree(name, M_TEMP); |
| d6427990 | 759 | |
| 03998195 JS |
760 | /* tell the calling layer whether we need to be called again */ |
| 761 | *a->a_eofflag = uiodir.eofflag; | |
| 762 | uio->uio_offset = ds->offset + ds->off; | |
| 763 | ||
| 764 | if (!error) | |
| 765 | error = ds->error; | |
| 766 | ||
| 767 | udf_closedir(ds); | |
| 768 | ||
| 769 | if (a->a_ncookies != NULL) { | |
| 770 | if (error) | |
| efda3bd0 | 771 | kfree(cookies, M_TEMP); |
| 03998195 JS |
772 | else { |
| 773 | *a->a_ncookies = uiodir.acookies; | |
| 774 | *a->a_cookies = cookies; | |
| 775 | } | |
| 776 | } | |
| 777 | ||
| 885ecb13 | 778 | vn_unlock(vp); |
| 03998195 JS |
779 | return(error); |
| 780 | } | |
| 781 | ||
| 782 | /* Are there any implementations out there that do soft-links? */ | |
| 783 | static int | |
| 784 | udf_readlink(struct vop_readlink_args *ap) | |
| 785 | { | |
| 086c1d7e | 786 | kprintf("%s called\n", __func__); |
| 03998195 JS |
787 | return(EOPNOTSUPP); |
| 788 | } | |
| 789 | ||
| 790 | static int | |
| 81b5c339 | 791 | udf_strategy(struct vop_strategy_args *ap) |
| 03998195 | 792 | { |
| 81b5c339 MD |
793 | struct bio *bio; |
| 794 | struct bio *nbio; | |
| 03998195 JS |
795 | struct buf *bp; |
| 796 | struct vnode *vp; | |
| 797 | struct udf_node *node; | |
| 798 | int maxsize; | |
| 54078292 | 799 | daddr_t dblkno; |
| 03998195 | 800 | |
| 81b5c339 MD |
801 | bio = ap->a_bio; |
| 802 | bp = bio->bio_buf; | |
| 803 | vp = ap->a_vp; | |
| 03998195 JS |
804 | node = VTON(vp); |
| 805 | ||
| 81b5c339 | 806 | nbio = push_bio(bio); |
| 54078292 | 807 | if (nbio->bio_offset == NOOFFSET) { |
| 03998195 JS |
808 | /* |
| 809 | * Files that are embedded in the fentry don't translate well | |
| 810 | * to a block number. Reject. | |
| 811 | */ | |
| 81b5c339 | 812 | if (udf_bmap_internal(node, |
| 54078292 MD |
813 | bio->bio_offset, |
| 814 | &dblkno, &maxsize)) { | |
| 03998195 | 815 | clrbuf(bp); |
| 54078292 MD |
816 | nbio->bio_offset = NOOFFSET; |
| 817 | } else { | |
| 818 | nbio->bio_offset = dbtob(dblkno); | |
| 03998195 JS |
819 | } |
| 820 | } | |
| 54078292 | 821 | if (nbio->bio_offset == NOOFFSET) { |
| 81b5c339 MD |
822 | /* I/O was never started on nbio, must biodone(bio) */ |
| 823 | biodone(bio); | |
| 03998195 JS |
824 | return(0); |
| 825 | } | |
| 81b5c339 | 826 | vn_strategy(node->i_devvp, nbio); |
| 03998195 JS |
827 | return(0); |
| 828 | } | |
| 829 | ||
| 830 | static int | |
| 831 | udf_bmap(struct vop_bmap_args *a) | |
| 832 | { | |
| 833 | struct udf_node *node; | |
| 834 | uint32_t max_size; | |
| 835 | daddr_t lsector; | |
| 836 | int error; | |
| 837 | ||
| 838 | node = VTON(a->a_vp); | |
| 839 | ||
| 54078292 | 840 | if (a->a_doffsetp == NULL) |
| 03998195 | 841 | return(0); |
| 03998195 | 842 | |
| 54078292 MD |
843 | KKASSERT(a->a_loffset % node->udfmp->bsize == 0); |
| 844 | ||
| 845 | error = udf_bmap_internal(node, a->a_loffset, &lsector, &max_size); | |
| 03998195 JS |
846 | if (error) |
| 847 | return(error); | |
| 848 | ||
| 849 | /* Translate logical to physical sector number */ | |
| 54078292 | 850 | *a->a_doffsetp = (off_t)lsector << node->udfmp->bshift; |
| 03998195 JS |
851 | |
| 852 | /* Punt on read-ahead for now */ | |
| 853 | if (a->a_runp) | |
| 854 | *a->a_runp = 0; | |
| 54078292 MD |
855 | if (a->a_runb) |
| 856 | *a->a_runb = 0; | |
| 03998195 JS |
857 | return(0); |
| 858 | } | |
| 859 | ||
| 860 | /* | |
| 861 | * The all powerful VOP_LOOKUP(). | |
| 862 | */ | |
| 863 | static int | |
| e62afb5f | 864 | udf_lookup(struct vop_old_lookup_args *a) |
| 03998195 JS |
865 | { |
| 866 | struct vnode *dvp; | |
| 867 | struct vnode *tdp = NULL; | |
| 868 | struct vnode **vpp = a->a_vpp; | |
| 869 | struct udf_node *node; | |
| 870 | struct udf_mnt *udfmp; | |
| 871 | struct fileid_desc *fid = NULL; | |
| 872 | struct udf_dirstream *ds; | |
| 873 | struct thread *td; | |
| 874 | u_long nameiop; | |
| 875 | u_long flags; | |
| 876 | char *nameptr; | |
| 877 | long namelen; | |
| 878 | ino_t id = 0; | |
| 879 | int offset, error = 0; | |
| 880 | int numdirpasses, fsize; | |
| 881 | ||
| 882 | dvp = a->a_dvp; | |
| 883 | node = VTON(dvp); | |
| 884 | udfmp = node->udfmp; | |
| 885 | nameiop = a->a_cnp->cn_nameiop; | |
| 886 | flags = a->a_cnp->cn_flags; | |
| 887 | nameptr = a->a_cnp->cn_nameptr; | |
| 888 | namelen = a->a_cnp->cn_namelen; | |
| 889 | fsize = node->fentry->inf_len; | |
| 890 | td = a->a_cnp->cn_td; | |
| 891 | ||
| 880cdd2f | 892 | *vpp = NULL; |
| b808830c | 893 | |
| 03998195 JS |
894 | /* |
| 895 | * If this is a LOOKUP and we've already partially searched through | |
| 896 | * the directory, pick up where we left off and flag that the | |
| 897 | * directory may need to be searched twice. For a full description, | |
| 898 | * see /sys/isofs/cd9660/cd9660_lookup.c:cd9660_lookup() | |
| 899 | */ | |
| 900 | if (nameiop != NAMEI_LOOKUP || node->diroff == 0 || | |
| 901 | node->diroff > fsize) { | |
| 902 | offset = 0; | |
| 903 | numdirpasses = 1; | |
| 904 | } else { | |
| 905 | offset = node->diroff; | |
| 906 | numdirpasses = 2; | |
| 03998195 JS |
907 | } |
| 908 | ||
| 909 | lookloop: | |
| 910 | ds = udf_opendir(node, offset, fsize, udfmp); | |
| 911 | ||
| 912 | while ((fid = udf_getfid(ds)) != NULL) { | |
| 913 | /* XXX Should we return an error on a bad fid? */ | |
| 914 | if (udf_checktag(&fid->tag, TAGID_FID)) { | |
| 086c1d7e | 915 | kprintf("udf_lookup: Invalid tag\n"); |
| 03998195 JS |
916 | error = EIO; |
| 917 | break; | |
| 918 | } | |
| 919 | ||
| 920 | /* Is this a deleted file? */ | |
| 921 | if (fid->file_char & UDF_FILE_CHAR_DEL) | |
| 922 | continue; | |
| 923 | ||
| 924 | if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) { | |
| 925 | if (flags & CNP_ISDOTDOT) { | |
| 926 | id = udf_getid(&fid->icb); | |
| 927 | break; | |
| 928 | } | |
| 929 | } else { | |
| 930 | if (!(udf_cmpname(&fid->data[fid->l_iu], | |
| 931 | nameptr, fid->l_fi, namelen, udfmp))) { | |
| 932 | id = udf_getid(&fid->icb); | |
| 933 | break; | |
| 934 | } | |
| 935 | } | |
| 936 | } | |
| 937 | ||
| 938 | if (!error) | |
| 939 | error = ds->error; | |
| 940 | ||
| 941 | /* XXX Bail out here? */ | |
| 942 | if (error) { | |
| 943 | udf_closedir(ds); | |
| 944 | return (error); | |
| 945 | } | |
| 946 | ||
| 947 | /* Did we have a match? */ | |
| 948 | if (id) { | |
| b9b0a6d0 | 949 | error = udf_vget(udfmp->im_mountp, NULL, id, &tdp); |
| 03998195 JS |
950 | if (!error) { |
| 951 | /* | |
| 952 | * Remember where this entry was if it's the final | |
| 953 | * component. | |
| 954 | */ | |
| fad57d0e | 955 | if (nameiop == NAMEI_LOOKUP) |
| 03998195 | 956 | node->diroff = ds->offset + ds->off; |
| fad57d0e | 957 | if ((flags & CNP_LOCKPARENT) == 0) { |
| 03998195 | 958 | a->a_cnp->cn_flags |= CNP_PDIRUNLOCK; |
| a11aaa81 | 959 | vn_unlock(dvp); |
| 03998195 JS |
960 | } |
| 961 | ||
| 962 | *vpp = tdp; | |
| 03998195 JS |
963 | } |
| 964 | } else { | |
| 965 | /* Name wasn't found on this pass. Do another pass? */ | |
| 966 | if (numdirpasses == 2) { | |
| 967 | numdirpasses--; | |
| 968 | offset = 0; | |
| 969 | udf_closedir(ds); | |
| 970 | goto lookloop; | |
| 971 | } | |
| fad57d0e | 972 | if (nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME) { |
| 03998195 JS |
973 | error = EROFS; |
| 974 | } else { | |
| 975 | error = ENOENT; | |
| 976 | } | |
| 977 | } | |
| 978 | ||
| 979 | udf_closedir(ds); | |
| 980 | return(error); | |
| 981 | } | |
| 982 | ||
| 983 | static int | |
| 984 | udf_reclaim(struct vop_reclaim_args *a) | |
| 985 | { | |
| 986 | struct vnode *vp; | |
| 987 | struct udf_node *unode; | |
| 988 | ||
| 989 | vp = a->a_vp; | |
| 990 | unode = VTON(vp); | |
| 991 | ||
| 992 | if (unode != NULL) { | |
| 993 | udf_hashrem(unode); | |
| 994 | if (unode->i_devvp) { | |
| 995 | vrele(unode->i_devvp); | |
| 996 | unode->i_devvp = 0; | |
| 997 | } | |
| 998 | ||
| 999 | if (unode->fentry != NULL) | |
| efda3bd0 MD |
1000 | kfree(unode->fentry, M_UDFFENTRY); |
| 1001 | kfree(unode, M_UDFNODE); | |
| 03998195 JS |
1002 | vp->v_data = NULL; |
| 1003 | } | |
| 1004 | ||
| 1005 | return(0); | |
| 1006 | } | |
| 1007 | ||
| 1008 | /* | |
| 1009 | * Read the block and then set the data pointer to correspond with the | |
| 1010 | * offset passed in. Only read in at most 'size' bytes, and then set 'size' | |
| 1011 | * to the number of bytes pointed to. If 'size' is zero, try to read in a | |
| 1012 | * whole extent. | |
| ffaa7d78 MD |
1013 | * |
| 1014 | * Note that *bp may be assigned error or not. | |
| 1015 | * | |
| 03998195 JS |
1016 | * XXX 'size' is limited to the logical block size for now due to problems |
| 1017 | * with udf_read() | |
| 1018 | */ | |
| 1019 | static int | |
| 1020 | udf_readatoffset(struct udf_node *node, int *size, int offset, struct buf **bp, | |
| 1021 | uint8_t **data) | |
| 1022 | { | |
| 1023 | struct udf_mnt *udfmp; | |
| 1024 | struct file_entry *fentry = NULL; | |
| 1025 | struct buf *bp1; | |
| 1026 | uint32_t max_size; | |
| 1027 | daddr_t sector; | |
| 1028 | int error; | |
| 1029 | ||
| 1030 | udfmp = node->udfmp; | |
| 1031 | ||
| ffaa7d78 | 1032 | *bp = NULL; |
| 03998195 JS |
1033 | error = udf_bmap_internal(node, offset, §or, &max_size); |
| 1034 | if (error == UDF_INVALID_BMAP) { | |
| 1035 | /* | |
| 1036 | * This error means that the file *data* is stored in the | |
| 1037 | * allocation descriptor field of the file entry. | |
| 1038 | */ | |
| 1039 | fentry = node->fentry; | |
| 1040 | *data = &fentry->data[fentry->l_ea]; | |
| 1041 | *size = fentry->l_ad; | |
| 03998195 | 1042 | return(0); |
| ffaa7d78 | 1043 | } else if (error != 0) { |
| 03998195 | 1044 | return(error); |
| ffaa7d78 | 1045 | } |
| 03998195 JS |
1046 | |
| 1047 | /* Adjust the size so that it is within range */ | |
| 1048 | if (*size == 0 || *size > max_size) | |
| 1049 | *size = max_size; | |
| 1050 | *size = min(*size, MAXBSIZE); | |
| 1051 | ||
| 1052 | if ((error = udf_readlblks(udfmp, sector, *size, bp))) { | |
| 086c1d7e | 1053 | kprintf("warning: udf_readlblks returned error %d\n", error); |
| ffaa7d78 | 1054 | /* note: *bp may be non-NULL */ |
| 03998195 JS |
1055 | return(error); |
| 1056 | } | |
| 1057 | ||
| 1058 | bp1 = *bp; | |
| 1059 | *data = (uint8_t *)&bp1->b_data[offset % udfmp->bsize]; | |
| 1060 | return(0); | |
| 1061 | } | |
| 1062 | ||
| 1063 | /* | |
| 1064 | * Translate a file offset into a logical block and then into a physical | |
| 1065 | * block. | |
| 1066 | */ | |
| 1067 | static int | |
| 1068 | udf_bmap_internal(struct udf_node *node, uint32_t offset, daddr_t *sector, uint32_t *max_size) | |
| 1069 | { | |
| 1070 | struct udf_mnt *udfmp; | |
| 1071 | struct file_entry *fentry; | |
| 1072 | void *icb; | |
| 1073 | struct icb_tag *tag; | |
| 1074 | uint32_t icblen = 0; | |
| 1075 | daddr_t lsector; | |
| 1076 | int ad_offset, ad_num = 0; | |
| 1077 | int i, p_offset; | |
| 1078 | ||
| 1079 | udfmp = node->udfmp; | |
| 1080 | fentry = node->fentry; | |
| 1081 | tag = &fentry->icbtag; | |
| 1082 | ||
| 1083 | switch (tag->strat_type) { | |
| 1084 | case 4: | |
| 1085 | break; | |
| 1086 | ||
| 1087 | case 4096: | |
| 086c1d7e | 1088 | kprintf("Cannot deal with strategy4096 yet!\n"); |
| 03998195 JS |
1089 | return(ENODEV); |
| 1090 | ||
| 1091 | default: | |
| 086c1d7e | 1092 | kprintf("Unknown strategy type %d\n", tag->strat_type); |
| 03998195 JS |
1093 | return(ENODEV); |
| 1094 | } | |
| 1095 | ||
| 1096 | switch (tag->flags & 0x7) { | |
| 1097 | case 0: | |
| 1098 | /* | |
| 1099 | * The allocation descriptor field is filled with short_ad's. | |
| 1100 | * If the offset is beyond the current extent, look for the | |
| 1101 | * next extent. | |
| 1102 | */ | |
| 1103 | do { | |
| 1104 | offset -= icblen; | |
| 1105 | ad_offset = sizeof(struct short_ad) * ad_num; | |
| 1106 | if (ad_offset > fentry->l_ad) { | |
| 086c1d7e | 1107 | kprintf("File offset out of bounds\n"); |
| 03998195 JS |
1108 | return(EINVAL); |
| 1109 | } | |
| 1110 | icb = GETICB(long_ad, fentry, fentry->l_ea + ad_offset); | |
| 1111 | icblen = GETICBLEN(short_ad, icb); | |
| 1112 | ad_num++; | |
| 1113 | } while(offset >= icblen); | |
| 1114 | ||
| 1115 | lsector = (offset >> udfmp->bshift) + | |
| 1116 | ((struct short_ad *)(icb))->pos; | |
| 1117 | ||
| 1118 | *max_size = GETICBLEN(short_ad, icb); | |
| 1119 | ||
| 1120 | break; | |
| 1121 | case 1: | |
| 1122 | /* | |
| 1123 | * The allocation descriptor field is filled with long_ad's | |
| 1124 | * If the offset is beyond the current extent, look for the | |
| 1125 | * next extent. | |
| 1126 | */ | |
| 1127 | do { | |
| 1128 | offset -= icblen; | |
| 1129 | ad_offset = sizeof(struct long_ad) * ad_num; | |
| 1130 | if (ad_offset > fentry->l_ad) { | |
| 086c1d7e | 1131 | kprintf("File offset out of bounds\n"); |
| 03998195 JS |
1132 | return(EINVAL); |
| 1133 | } | |
| 1134 | icb = GETICB(long_ad, fentry, fentry->l_ea + ad_offset); | |
| 1135 | icblen = GETICBLEN(long_ad, icb); | |
| 1136 | ad_num++; | |
| 1137 | } while(offset >= icblen); | |
| 1138 | ||
| 1139 | lsector = (offset >> udfmp->bshift) + | |
| 1140 | ((struct long_ad *)(icb))->loc.lb_num; | |
| 1141 | ||
| 1142 | *max_size = GETICBLEN(long_ad, icb); | |
| 1143 | ||
| 1144 | break; | |
| 1145 | case 3: | |
| 1146 | /* | |
| 1147 | * This type means that the file *data* is stored in the | |
| 1148 | * allocation descriptor field of the file entry. | |
| 1149 | */ | |
| 1150 | *max_size = 0; | |
| 1151 | *sector = node->hash_id + udfmp->part_start; | |
| 1152 | ||
| 1153 | return(UDF_INVALID_BMAP); | |
| 1154 | case 2: | |
| 1155 | /* DirectCD does not use extended_ad's */ | |
| 1156 | default: | |
| 086c1d7e | 1157 | kprintf("Unsupported allocation descriptor %d\n", |
| 03998195 JS |
1158 | tag->flags & 0x7); |
| 1159 | return(ENODEV); | |
| 1160 | } | |
| 1161 | ||
| 1162 | *sector = lsector + udfmp->part_start; | |
| 1163 | ||
| 1164 | /* | |
| 1165 | * Check the sparing table. Each entry represents the beginning of | |
| 1166 | * a packet. | |
| 1167 | */ | |
| 1168 | if (udfmp->s_table != NULL) { | |
| 1169 | for (i = 0; i< udfmp->s_table_entries; i++) { | |
| 1170 | p_offset = lsector - udfmp->s_table->entries[i].org; | |
| 1171 | if ((p_offset < udfmp->p_sectors) && (p_offset >= 0)) { | |
| 1172 | *sector = udfmp->s_table->entries[i].map + | |
| 1173 | p_offset; | |
| 1174 | break; | |
| 1175 | } | |
| 1176 | } | |
| 1177 | } | |
| 1178 | ||
| 1179 | return(0); | |
| 1180 | } |