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