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