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