Merge branch 'vendor/BINUTILS_ALL'
[dragonfly.git] / sys / vfs / hpfs / hpfs_vnops.c
1 /*-
2  * Copyright (c) 1998, 1999 Semen Ustimenko (semenu@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/hpfs/hpfs_vnops.c,v 1.2.2.2 2002/01/15 18:35:09 semenu Exp $
27  * $DragonFly: src/sys/vfs/hpfs/hpfs_vnops.c,v 1.47 2008/06/19 23:27:39 dillon Exp $
28  */
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/proc.h>
34 #include <sys/priv.h>
35 #include <sys/time.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/vnode.h>
39 #include <sys/mount.h>
40 #include <sys/namei.h>
41 #include <sys/malloc.h>
42 #include <sys/buf.h>
43 #include <sys/dirent.h>
44
45 #include <machine/limits.h>
46
47 #include <vm/vm.h>
48 #include <vm/vm_param.h>
49 #if !defined(__DragonFly__)
50 #include <vm/vm_prot.h>
51 #endif
52 #include <vm/vm_page.h>
53 #include <vm/vm_object.h>
54 #include <vm/vm_pager.h>
55 #include <vm/vm_zone.h>
56 #if defined(__DragonFly__)
57 #include <vm/vnode_pager.h>
58 #endif
59 #include <vm/vm_extern.h>
60
61 #include <sys/buf2.h>
62
63 #if !defined(__DragonFly__)
64 #include <miscfs/specfs/specdev.h>
65 #include <miscfs/genfs/genfs.h>
66 #endif
67
68 #include <sys/unistd.h> /* for pathconf(2) constants */
69
70 #include "hpfs.h"
71 #include "hpfsmount.h"
72 #include "hpfs_subr.h"
73 #include "hpfs_ioctl.h"
74
75 static int      hpfs_de_uiomove (int *, struct hpfsmount *,
76                                  struct hpfsdirent *, struct uio *);
77 static int      hpfs_ioctl (struct vop_ioctl_args *ap);
78 static int      hpfs_read (struct vop_read_args *);
79 static int      hpfs_write (struct vop_write_args *ap);
80 static int      hpfs_getattr (struct vop_getattr_args *ap);
81 static int      hpfs_setattr (struct vop_setattr_args *ap);
82 static int      hpfs_inactive (struct vop_inactive_args *ap);
83 static int      hpfs_print (struct vop_print_args *ap);
84 static int      hpfs_reclaim (struct vop_reclaim_args *ap);
85 static int      hpfs_strategy (struct vop_strategy_args *ap);
86 static int      hpfs_access (struct vop_access_args *ap);
87 static int      hpfs_readdir (struct vop_readdir_args *ap);
88 static int      hpfs_lookup (struct vop_old_lookup_args *ap);
89 static int      hpfs_create (struct vop_old_create_args *);
90 static int      hpfs_remove (struct vop_old_remove_args *);
91 static int      hpfs_bmap (struct vop_bmap_args *ap);
92 #if defined(__DragonFly__)
93 static int      hpfs_fsync (struct vop_fsync_args *ap);
94 #endif
95 static int      hpfs_pathconf (struct vop_pathconf_args *ap);
96
97 #if defined(__DragonFly__)
98
99 /*
100  * hpfs_fsync(struct vnode *a_vp, int a_waitfor)
101  */
102 static int
103 hpfs_fsync(struct vop_fsync_args *ap)
104 {
105         struct vnode *vp = ap->a_vp;
106
107         /*
108          * Flush all dirty buffers associated with a vnode.
109          */
110 #ifdef DIAGNOSTIC
111 loop:
112 #endif
113         vfsync(vp, ap->a_waitfor, 0, NULL, NULL);
114 #ifdef DIAGNOSTIC
115         if (ap->a_waitfor == MNT_WAIT && !RB_EMPTY(&vp->v_rbdirty_tree)) {
116                 vprint("hpfs_fsync: dirty", vp);
117                 goto loop;
118         }
119 #endif
120
121         /*
122          * Write out the on-disc version of the vnode.
123          */
124         return hpfs_update(VTOHP(vp));
125 }
126
127 #endif
128
129 /*
130  * hpfs_ioctl(struct vnode *a_vp, u_long a_command, caddr_t a_data,
131  *            int a_fflag, struct ucred *a_cred)
132  */
133 static int
134 hpfs_ioctl(struct vop_ioctl_args *ap)
135 {
136         struct vnode *vp = ap->a_vp;
137         struct hpfsnode *hp = VTOHP(vp);
138         int error;
139
140         kprintf("hpfs_ioctl(0x%x, 0x%lx, 0x%p, 0x%x): ",
141                 hp->h_no, ap->a_command, ap->a_data, ap->a_fflag);
142
143         switch (ap->a_command) {
144         case HPFSIOCGEANUM: {
145                 u_long eanum;
146                 u_long passed;
147                 struct ea *eap;
148
149                 eanum = 0;
150
151                 if (hp->h_fn.fn_ealen > 0) {
152                         eap = (struct ea *)&(hp->h_fn.fn_int);
153                         passed = 0;
154
155                         while (passed < hp->h_fn.fn_ealen) {
156
157                                 kprintf("EAname: %s\n", EA_NAME(eap));
158
159                                 eanum++;
160                                 passed += sizeof(struct ea) +
161                                           eap->ea_namelen + 1 + eap->ea_vallen;
162                                 eap = (struct ea *)((caddr_t)hp->h_fn.fn_int +
163                                                 passed);
164                         }
165                         error = 0;
166                 } else {
167                         error = ENOENT;
168                 }
169
170                 kprintf("%lu eas\n", eanum);
171
172                 *(u_long *)ap->a_data = eanum;
173
174                 break;
175         }
176         case HPFSIOCGEASZ: {
177                 u_long eanum;
178                 u_long passed;
179                 struct ea *eap;
180
181                 kprintf("EA%ld\n", *(u_long *)ap->a_data);
182
183                 eanum = 0;
184                 if (hp->h_fn.fn_ealen > 0) {
185                         eap = (struct ea *)&(hp->h_fn.fn_int);
186                         passed = 0;
187
188                         error = ENOENT;
189                         while (passed < hp->h_fn.fn_ealen) {
190                                 kprintf("EAname: %s\n", EA_NAME(eap));
191
192                                 if (eanum == *(u_long *)ap->a_data) {
193                                         *(u_long *)ap->a_data =
194                                                 eap->ea_namelen + 1 +
195                                                 eap->ea_vallen;
196
197                                         error = 0;
198                                         break;
199                                 }
200
201                                 eanum++;
202                                 passed += sizeof(struct ea) +
203                                           eap->ea_namelen + 1 + eap->ea_vallen;
204                                 eap = (struct ea *)((caddr_t)hp->h_fn.fn_int +
205                                                 passed);
206                         }
207                 } else {
208                         error = ENOENT;
209                 }
210
211                 break;
212         }
213         case HPFSIOCRDEA: {
214                 u_long eanum;
215                 u_long passed;
216                 struct hpfs_rdea *rdeap;
217                 struct ea *eap;
218
219                 rdeap = (struct hpfs_rdea *)ap->a_data;
220                 kprintf("EA%ld\n", rdeap->ea_no);
221
222                 eanum = 0;
223                 if (hp->h_fn.fn_ealen > 0) {
224                         eap = (struct ea *)&(hp->h_fn.fn_int);
225                         passed = 0;
226
227                         error = ENOENT;
228                         while (passed < hp->h_fn.fn_ealen) {
229                                 kprintf("EAname: %s\n", EA_NAME(eap));
230
231                                 if (eanum == rdeap->ea_no) {
232                                         rdeap->ea_sz = eap->ea_namelen + 1 +
233                                                         eap->ea_vallen;
234                                         copyout(EA_NAME(eap),rdeap->ea_data,
235                                                 rdeap->ea_sz);
236                                         error = 0;
237                                         break;
238                                 }
239
240                                 eanum++;
241                                 passed += sizeof(struct ea) +
242                                           eap->ea_namelen + 1 + eap->ea_vallen;
243                                 eap = (struct ea *)((caddr_t)hp->h_fn.fn_int +
244                                                 passed);
245                         }
246                 } else {
247                         error = ENOENT;
248                 }
249
250                 break;
251         }
252         default:
253                 error = EOPNOTSUPP;
254                 break;
255         }
256         return (error);
257 }
258
259 /*
260  * Map file offset to disk offset.
261  *
262  * hpfs_bmap(struct vnode *a_vp, off_t a_loffset,
263  *           off_t *a_doffsetp, int *a_runp, int *a_runb)
264  */
265 int
266 hpfs_bmap(struct vop_bmap_args *ap)
267 {
268         struct hpfsnode *hp = VTOHP(ap->a_vp);
269         int error;
270         daddr_t lbn;
271         daddr_t dbn;
272
273         if (ap->a_runb != NULL)
274                 *ap->a_runb = 0;
275         if (ap->a_doffsetp == NULL)
276                 return (0);
277
278         dprintf(("hpfs_bmap(0x%x, 0x%x): ",hp->h_no, ap->a_bn));
279
280         lbn = ap->a_loffset >> DEV_BSHIFT;
281         KKASSERT(((int)ap->a_loffset & DEV_BMASK) == 0);
282
283         error = hpfs_hpbmap (hp, lbn, &dbn, ap->a_runp);
284         if (error || dbn == (daddr_t)-1) {
285                 *ap->a_doffsetp = NOOFFSET;
286         } else {
287                 *ap->a_doffsetp = (off_t)dbn << DEV_BSHIFT;
288         }
289         return (error);
290 }
291
292 /*
293  * hpfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
294  *           struct ucred *a_cred)
295  */
296 static int
297 hpfs_read(struct vop_read_args *ap)
298 {
299         struct vnode *vp = ap->a_vp;
300         struct hpfsnode *hp = VTOHP(vp);
301         struct uio *uio = ap->a_uio;
302         struct buf *bp;
303         u_int xfersz, toread;
304         u_int off;
305         daddr_t lbn, bn;
306         int resid;
307         int runl;
308         int error = 0;
309
310         resid = (int)szmin(uio->uio_resid, hp->h_fn.fn_size - uio->uio_offset);
311
312         dprintf(("hpfs_read(0x%x, off: %d resid: %d, segflg: %d): "
313                  "[resid: 0x%lx]\n",
314                  hp->h_no, (u_int32_t)uio->uio_offset,
315                  uio->uio_resid, uio->uio_segflg, resid));
316
317         while (resid) {
318                 lbn = uio->uio_offset >> DEV_BSHIFT;
319                 off = uio->uio_offset & (DEV_BSIZE - 1);
320                 dprintf(("hpfs_read: resid: 0x%lx lbn: 0x%x off: 0x%x\n",
321                         uio->uio_resid, lbn, off));
322                 error = hpfs_hpbmap(hp, lbn, &bn, &runl);
323                 if (error)
324                         return (error);
325
326                 toread = min(off + resid, min(DFLTPHYS, (runl+1)*DEV_BSIZE));
327                 xfersz = (toread + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
328                 dprintf(("hpfs_read: bn: 0x%x (0x%x) toread: 0x%x (0x%x)\n",
329                         bn, runl, toread, xfersz));
330
331                 if (toread == 0) 
332                         break;
333
334                 error = bread(hp->h_devvp, dbtodoff(bn), xfersz, &bp);
335                 if (error) {
336                         brelse(bp);
337                         break;
338                 }
339
340                 error = uiomove(bp->b_data + off, (size_t)(toread - off), uio);
341                 if(error) {
342                         brelse(bp);
343                         break;
344                 }
345                 brelse(bp);
346                 resid -= toread;
347         }
348         dprintf(("hpfs_read: successful\n"));
349         return (error);
350 }
351
352 /*
353  * hpfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
354  *            struct ucred *a_cred)
355  */
356 static int
357 hpfs_write(struct vop_write_args *ap)
358 {
359         struct vnode *vp = ap->a_vp;
360         struct hpfsnode *hp = VTOHP(vp);
361         struct uio *uio = ap->a_uio;
362         struct buf *bp;
363         u_int xfersz, towrite;
364         u_int off;
365         daddr_t lbn, bn;
366         int runl;
367         int error = 0;
368
369         dprintf(("hpfs_write(0x%x, off: %d resid: %ld, segflg: %d):\n",
370                 hp->h_no, (u_int32_t)uio->uio_offset,
371                 uio->uio_resid, uio->uio_segflg));
372
373         if (ap->a_ioflag & IO_APPEND) {
374                 dprintf(("hpfs_write: APPEND mode\n"));
375                 uio->uio_offset = hp->h_fn.fn_size;
376         }
377         if (uio->uio_offset + uio->uio_resid > hp->h_fn.fn_size) {
378                 error = hpfs_extend (hp, uio->uio_offset + uio->uio_resid);
379                 if (error) {
380                         kprintf("hpfs_write: hpfs_extend FAILED %d\n", error);
381                         return (error);
382                 }
383         }
384
385         while (uio->uio_resid) {
386                 lbn = uio->uio_offset >> DEV_BSHIFT;
387                 off = uio->uio_offset & (DEV_BSIZE - 1);
388                 dprintf(("hpfs_write: resid: 0x%lx lbn: 0x%x off: 0x%x\n",
389                         uio->uio_resid, lbn, off));
390                 error = hpfs_hpbmap(hp, lbn, &bn, &runl);
391                 if (error)
392                         return (error);
393
394                 towrite = szmin(off + uio->uio_resid,
395                                 min(DFLTPHYS, (runl+1)*DEV_BSIZE));
396                 xfersz = (towrite + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
397                 dprintf(("hpfs_write: bn: 0x%x (0x%x) towrite: 0x%x (0x%x)\n",
398                         bn, runl, towrite, xfersz));
399
400                 /*
401                  * We do not have to issue a read-before-write if the xfer
402                  * size does not cover the whole block.
403                  *
404                  * In the UIO_NOCOPY case, however, we are not overwriting
405                  * anything and must do a read-before-write to fill in
406                  * any missing pieces.
407                  */
408                 if (off == 0 && towrite == xfersz &&
409                     uio->uio_segflg != UIO_NOCOPY) {
410                         bp = getblk(hp->h_devvp, dbtodoff(bn), xfersz, 0, 0);
411                         clrbuf(bp);
412                 } else {
413                         error = bread(hp->h_devvp, dbtodoff(bn), xfersz, &bp);
414                         if (error) {
415                                 brelse(bp);
416                                 return (error);
417                         }
418                 }
419
420                 error = uiomove(bp->b_data + off, (size_t)(towrite - off), uio);
421                 if(error) {
422                         brelse(bp);
423                         return (error);
424                 }
425
426                 if (ap->a_ioflag & IO_SYNC)
427                         bwrite(bp);
428                 else
429                         bawrite(bp);
430         }
431
432         dprintf(("hpfs_write: successful\n"));
433         return (0);
434 }
435
436 /*
437  * XXXXX do we need hpfsnode locking inside?
438  *
439  * hpfs_getattr(struct vnode *a_vp, struct vattr *a_vap)
440  */
441 static int
442 hpfs_getattr(struct vop_getattr_args *ap)
443 {
444         struct vnode *vp = ap->a_vp;
445         struct hpfsnode *hp = VTOHP(vp);
446         struct vattr *vap = ap->a_vap;
447         int error;
448
449         dprintf(("hpfs_getattr(0x%x):\n", hp->h_no));
450
451 #if defined(__DragonFly__)
452         vap->va_fsid = dev2udev(hp->h_dev);
453 #else /* defined(__NetBSD__) */
454         vap->va_fsid = ip->i_dev;
455 #endif
456         vap->va_fileid = hp->h_no;
457         vap->va_mode = hp->h_mode;
458         vap->va_nlink = 1;
459         vap->va_uid = hp->h_uid;
460         vap->va_gid = hp->h_gid;
461         vap->va_rmajor = VNOVAL;
462         vap->va_rminor = VNOVAL;
463         vap->va_size = hp->h_fn.fn_size;
464         vap->va_bytes = ((hp->h_fn.fn_size + DEV_BSIZE-1) & ~(DEV_BSIZE-1)) +
465                         DEV_BSIZE;
466
467         if (!(hp->h_flag & H_PARVALID)) {
468                 error = hpfs_validateparent(hp);
469                 if (error) 
470                         return (error);
471         }
472         vap->va_atime = hpfstimetounix(hp->h_atime);
473         vap->va_mtime = hpfstimetounix(hp->h_mtime);
474         vap->va_ctime = hpfstimetounix(hp->h_ctime);
475
476         vap->va_flags = 0;
477         vap->va_gen = 0;
478         vap->va_blocksize = DEV_BSIZE;
479         vap->va_type = vp->v_type;
480         vap->va_filerev = 0;
481
482         return (0);
483 }
484
485 /*
486  * XXXXX do we need hpfsnode locking inside?
487  *
488  * hpfs_setattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
489  */
490 static int
491 hpfs_setattr(struct vop_setattr_args *ap)
492 {
493         struct vnode *vp = ap->a_vp;
494         struct hpfsnode *hp = VTOHP(vp);
495         struct vattr *vap = ap->a_vap;
496         struct ucred *cred = ap->a_cred;
497         int error;
498
499         dprintf(("hpfs_setattr(0x%x):\n", hp->h_no));
500
501         /*
502          * Check for unsettable attributes.
503          */
504         if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
505             (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
506             (vap->va_blocksize != VNOVAL) || (vap->va_rmajor != VNOVAL) ||
507             (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
508                 dprintf(("hpfs_setattr: changing nonsettable attr\n"));
509                 return (EINVAL);
510         }
511
512         /* Can't change flags XXX Could be implemented */
513         if (vap->va_flags != VNOVAL) {
514                 kprintf("hpfs_setattr: FLAGS CANNOT BE SET\n");
515                 return (EINVAL);
516         }
517
518         /* Can't change uid/gid XXX Could be implemented */
519         if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
520                 kprintf("hpfs_setattr: UID/GID CANNOT BE SET\n");
521                 return (EINVAL);
522         }
523
524         /* Can't change mode XXX Could be implemented */
525         if (vap->va_mode != (mode_t)VNOVAL) {
526                 kprintf("hpfs_setattr: MODE CANNOT BE SET\n");
527                 return (EINVAL);
528         }
529
530         /* Update times */
531         if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
532                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
533                         return (EROFS);
534                 if (cred->cr_uid != hp->h_uid &&
535                     (error = priv_check_cred(cred, PRIV_VFS_SETATTR, 0)) &&
536                     ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
537                     (error = VOP_EACCESS(vp, VWRITE, cred))))
538                         return (error);
539                 if (vap->va_atime.tv_sec != VNOVAL)
540                         hp->h_atime = vap->va_atime.tv_sec;
541                 if (vap->va_mtime.tv_sec != VNOVAL)
542                         hp->h_mtime = vap->va_mtime.tv_sec;
543
544                 hp->h_flag |= H_PARCHANGE;
545         }
546
547         if (vap->va_size != VNOVAL) {
548                 switch (vp->v_type) {
549                 case VDIR:
550                         return (EISDIR);
551                 case VREG:
552                         if (vp->v_mount->mnt_flag & MNT_RDONLY)
553                                 return (EROFS);
554                         break;
555                 default:
556                         kprintf("hpfs_setattr: WRONG v_type\n");
557                         return (EINVAL);
558                 }
559
560                 if (vap->va_size < hp->h_fn.fn_size) {
561 #if defined(__DragonFly__)
562                         error = vtruncbuf(vp, vap->va_size, DEV_BSIZE);
563                         if (error)
564                                 return (error);
565 #else /* defined(__NetBSD__) */
566 #error Need alternation for vtruncbuf()
567 #endif
568                         error = hpfs_truncate(hp, vap->va_size);
569                         if (error)
570                                 return (error);
571
572                 } else if (vap->va_size > hp->h_fn.fn_size) {
573 #if defined(__DragonFly__)
574                         vnode_pager_setsize(vp, vap->va_size);
575 #endif
576                         error = hpfs_extend(hp, vap->va_size);
577                         if (error)
578                                 return (error);
579                 }
580         }
581
582         return (0);
583 }
584
585 /*
586  * Last reference to an node.  If necessary, write or delete it.
587  *
588  * hpfs_inactive(struct vnode *a_vp)
589  */
590 int
591 hpfs_inactive(struct vop_inactive_args *ap)
592 {
593         struct vnode *vp = ap->a_vp;
594         struct hpfsnode *hp = VTOHP(vp);
595         int error;
596
597         dprintf(("hpfs_inactive(0x%x): \n", hp->h_no));
598
599         if (hp->h_flag & H_CHANGE) {
600                 dprintf(("hpfs_inactive: node changed, update\n"));
601                 error = hpfs_update (hp);
602                 if (error)
603                         return (error);
604         }
605
606         if (hp->h_flag & H_PARCHANGE) {
607                 dprintf(("hpfs_inactive: parent node changed, update\n"));
608                 error = hpfs_updateparent (hp);
609                 if (error)
610                         return (error);
611         }
612
613         if (prtactive && vp->v_sysref.refcnt > 1)
614                 vprint("hpfs_inactive: pushing active", vp);
615
616         if (hp->h_flag & H_INVAL) {
617 #if defined(__DragonFly__)
618                 vrecycle(vp);
619 #else /* defined(__NetBSD__) */
620                 vgone(vp);
621 #endif
622                 return (0);
623         }
624         return (0);
625 }
626
627 /*
628  * Reclaim an inode so that it can be used for other purposes.
629  *
630  * hpfs_reclaim(struct vnode *a_vp)
631  */
632 int
633 hpfs_reclaim(struct vop_reclaim_args *ap)
634 {
635         struct vnode *vp = ap->a_vp;
636         struct hpfsnode *hp = VTOHP(vp);
637
638         dprintf(("hpfs_reclaim(0x%x0): \n", hp->h_no));
639
640         hpfs_hphashrem(hp);
641
642         /* Purge old data structures associated with the inode. */
643         if (hp->h_devvp) {
644                 vrele(hp->h_devvp);
645                 hp->h_devvp = NULL;
646         }
647
648         vp->v_data = NULL;
649
650         FREE(hp, M_HPFSNO);
651
652         return (0);
653 }
654
655 /*
656  * hpfs_print(struct vnode *a_vp)
657  */
658 static int
659 hpfs_print(struct vop_print_args *ap)
660 {
661         struct vnode *vp = ap->a_vp;
662         struct hpfsnode *hp = VTOHP(vp);
663
664         kprintf("tag VT_HPFS, ino 0x%x",hp->h_no);
665         lockmgr_printinfo(&vp->v_lock);
666         kprintf("\n");
667         return (0);
668 }
669
670 /*
671  * Calculate the logical to physical mapping if not done already,
672  * then call the device strategy routine.
673  *
674  * In order to be able to swap to a file, the VOP_BMAP operation may not
675  * deadlock on memory.  See hpfs_bmap() for details. XXXXXXX (not impl)
676  *
677  * hpfs_strategy(struct vnode *a_vp, struct bio *a_bio)
678  */
679 int
680 hpfs_strategy(struct vop_strategy_args *ap)
681 {
682         struct bio *bio = ap->a_bio;
683         struct bio *nbio;
684         struct buf *bp = bio->bio_buf;
685         struct vnode *vp = ap->a_vp;
686         struct hpfsnode *hp;
687         int error;
688
689         dprintf(("hpfs_strategy(): \n"));
690
691         if (vp->v_type == VBLK || vp->v_type == VCHR)
692                 panic("hpfs_strategy: spec");
693
694         nbio = push_bio(bio);
695         if (nbio->bio_offset == NOOFFSET) {
696                 error = VOP_BMAP(vp, bio->bio_offset, &nbio->bio_offset,
697                                  NULL, NULL, bp->b_cmd);
698                 if (error) {
699                         kprintf("hpfs_strategy: VOP_BMAP FAILED %d\n", error);
700                         bp->b_error = error;
701                         bp->b_flags |= B_ERROR;
702                         /* I/O was never started on nbio, must biodone(bio) */
703                         biodone(bio);
704                         return (error);
705                 }
706                 if (nbio->bio_offset == NOOFFSET)
707                         vfs_bio_clrbuf(bp);
708         }
709         if (nbio->bio_offset == NOOFFSET) {
710                 /* I/O was never started on nbio, must biodone(bio) */
711                 biodone(bio);
712                 return (0);
713         }
714         hp = VTOHP(ap->a_vp);
715         vn_strategy(hp->h_devvp, nbio);
716         return (0);
717 }
718
719 /*
720  * XXXXX do we need hpfsnode locking inside?
721  *
722  * hpfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
723  */
724 int
725 hpfs_access(struct vop_access_args *ap)
726 {
727         struct vnode *vp = ap->a_vp;
728         struct hpfsnode *hp = VTOHP(vp);
729
730         dprintf(("hpfs_access(0x%x):\n", hp->h_no));
731         return (vop_helper_access(ap, hp->h_uid, hp->h_gid, hp->h_mode, 0));
732 }
733
734 static int
735 hpfs_de_uiomove(int *error, struct hpfsmount *hpmp, struct hpfsdirent *dep,
736                 struct uio *uio)
737 {
738         char convname[HPFS_MAXFILENAME + 1];
739         int i, success;
740
741         dprintf(("[no: 0x%x, size: %d, name: %2d:%.*s, flag: 0x%x] ",
742                 dep->de_fnode, dep->de_size, dep->de_namelen,
743                 dep->de_namelen, dep->de_name, dep->de_flag));
744
745         /*strncpy(cde.d_name, dep->de_name, dep->de_namelen);*/
746         for (i=0; i<dep->de_namelen; i++) 
747                 convname[i] = hpfs_d2u(hpmp, dep->de_name[i]);
748         convname[dep->de_namelen] = '\0';
749
750         success = vop_write_dirent(error, uio, dep->de_fnode,
751                         (dep->de_flag & DE_DIR) ? DT_DIR : DT_REG,
752                         dep->de_namelen, convname);
753
754         dprintf(("[0x%lx] ", uio->uio_resid));
755         return (success);
756 }
757
758
759 /*
760  * hpfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
761  *              int *a_ncookies, off_t **cookies)
762  */
763 int
764 hpfs_readdir(struct vop_readdir_args *ap)
765 {
766         struct vnode *vp = ap->a_vp;
767         struct hpfsnode *hp = VTOHP(vp);
768         struct hpfsmount *hpmp = hp->h_hpmp;
769         struct uio *uio = ap->a_uio;
770         int ncookies = 0, i, num, cnum;
771         int error = 0;
772         struct buf *bp;
773         struct dirblk *dp;
774         struct hpfsdirent *dep;
775         lsn_t olsn;
776         lsn_t lsn;
777         int level;
778
779         dprintf(("hpfs_readdir(0x%x, 0x%x, 0x%lx): ",
780                 hp->h_no, (u_int32_t)uio->uio_offset, uio->uio_resid));
781
782         /*
783          * As we need to fake up . and .., and the remaining directory structure
784          * can't be expressed in one off_t as well, we just increment uio_offset
785          * by 1 for each entry.
786          *
787          * num is the entry we need to start reporting
788          * cnum is the current entry
789          */
790         if (uio->uio_offset < 0 || uio->uio_offset > INT_MAX)
791                 return(EINVAL);
792         if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0)
793                 return (error);
794
795         num = uio->uio_offset;
796         cnum = 0;
797
798         if( num <= cnum ) {
799                 dprintf((". faked, "));
800                 if (vop_write_dirent(&error, uio, hp->h_no, DT_DIR, 1, "."))
801                         goto done;
802                 if (error)
803                         goto done;
804                 ncookies ++;
805         }
806         cnum++;
807
808         if( num <= cnum ) {
809                 dprintf((".. faked, "));
810                 if (vop_write_dirent(&error, uio, hp->h_fn.fn_parent, DT_DIR, 2, ".."))
811                         goto readdone;
812                 if (error)
813                         goto done;
814                 ncookies ++;
815         }
816         cnum++;
817
818         lsn = ((alleaf_t *)hp->h_fn.fn_abd)->al_lsn;
819
820         olsn = 0;
821         level = 1;
822
823 dive:
824         dprintf(("[dive 0x%x] ", lsn));
825         error = bread(hp->h_devvp, dbtodoff(lsn), D_BSIZE, &bp);
826         if (error) {
827                 brelse(bp);
828                 goto done;
829         }
830
831         dp = (struct dirblk *) bp->b_data;
832         if (dp->d_magic != D_MAGIC) {
833                 kprintf("hpfs_readdir: MAGIC DOESN'T MATCH\n");
834                 brelse(bp);
835                 error = EINVAL;
836                 goto done;
837         }
838
839         dep = D_DIRENT(dp);
840
841         if (olsn) {
842                 dprintf(("[restore 0x%x] ", olsn));
843
844                 while(!(dep->de_flag & DE_END) ) {
845                         if((dep->de_flag & DE_DOWN) &&
846                            (olsn == DE_DOWNLSN(dep)))
847                                          break;
848                         dep = (hpfsdirent_t *)((caddr_t)dep + dep->de_reclen);
849                 }
850
851                 if((dep->de_flag & DE_DOWN) && (olsn == DE_DOWNLSN(dep))) {
852                         if (dep->de_flag & DE_END)
853                                 goto blockdone;
854
855                         if (!(dep->de_flag & DE_SPECIAL)) {
856                                 if (num <= cnum) {
857                                         if (hpfs_de_uiomove(&error, hpmp, dep, uio)) {
858                                                 brelse(bp);
859                                                 dprintf(("[resid] "));
860                                                 goto readdone;
861                                         }
862                                         if (error) {
863                                                 brelse (bp);
864                                                 goto done;
865                                         }
866                                         ncookies++;
867                                 }
868                                 cnum++;
869                         }
870
871                         dep = (hpfsdirent_t *)((caddr_t)dep + dep->de_reclen);
872                 } else {
873                         kprintf("hpfs_readdir: ERROR! oLSN not found\n");
874                         brelse(bp);
875                         error = EINVAL;
876                         goto done;
877                 }
878         }
879
880         olsn = 0;
881
882         while(!(dep->de_flag & DE_END)) {
883                 if(dep->de_flag & DE_DOWN) {
884                         lsn = DE_DOWNLSN(dep);
885                         brelse(bp);
886                         level++;
887                         goto dive;
888                 }
889
890                 if (!(dep->de_flag & DE_SPECIAL)) {
891                         if (num <= cnum) {
892                                 if (hpfs_de_uiomove(&error, hpmp, dep, uio)) {
893                                         brelse(bp);
894                                         dprintf(("[resid] "));
895                                         goto readdone;
896                                 }
897                                 if (error) {
898                                         brelse (bp);
899                                         goto done;
900                                 }
901                                 ncookies++;
902                         }
903                         cnum++;
904                 }
905
906                 dep = (hpfsdirent_t *)((caddr_t)dep + dep->de_reclen);
907         }
908
909         if(dep->de_flag & DE_DOWN) {
910                 dprintf(("[enddive] "));
911                 lsn = DE_DOWNLSN(dep);
912                 brelse(bp);
913                 level++;
914                 goto dive;
915         }
916
917 blockdone:
918         dprintf(("[EOB] "));
919         olsn = lsn;
920         lsn = dp->d_parent;
921         brelse(bp);
922         level--;
923
924         dprintf(("[level %d] ", level));
925
926         if (level > 0)
927                 goto dive;      /* undive really */
928
929         if (ap->a_eofflag) {
930             dprintf(("[EOF] "));
931             *ap->a_eofflag = 1;
932         }
933
934 readdone:
935         uio->uio_offset = cnum;
936         dprintf(("[readdone]\n"));
937         if (!error && ap->a_ncookies != NULL) {
938                 off_t *cookies;
939                 off_t *cookiep;
940
941                 dprintf(("%d cookies, ",ncookies));
942                 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
943                         panic("hpfs_readdir: unexpected uio from NFS server");
944                 MALLOC(cookies, off_t *, ncookies * sizeof(off_t),
945                        M_TEMP, M_WAITOK);
946                 for (cookiep = cookies, i=0; i < ncookies; i++)
947                         *cookiep++ = ++num;
948
949                 *ap->a_ncookies = ncookies;
950                 *ap->a_cookies = cookies;
951         }
952
953 done:
954         vn_unlock(ap->a_vp);
955         return (error);
956 }
957
958 /*
959  * hpfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
960  *              struct componentname *a_cnp)
961  */
962 int
963 hpfs_lookup(struct vop_old_lookup_args *ap)
964 {
965         struct vnode *dvp = ap->a_dvp;
966         struct hpfsnode *dhp = VTOHP(dvp);
967         struct hpfsmount *hpmp = dhp->h_hpmp;
968         struct componentname *cnp = ap->a_cnp;
969         struct ucred *cred = cnp->cn_cred;
970         int error;
971         int nameiop = cnp->cn_nameiop;
972         int flags = cnp->cn_flags;
973         int lockparent = flags & CNP_LOCKPARENT;
974 #if HPFS_DEBUG
975         int wantparent = flags & (CNP_LOCKPARENT | CNP_WANTPARENT);
976 #endif
977         dprintf(("hpfs_lookup(0x%x, %s, %ld, %d, %d): \n",
978                 dhp->h_no, cnp->cn_nameptr, cnp->cn_namelen,
979                 lockparent, wantparent));
980
981         if (nameiop != NAMEI_CREATE && nameiop != NAMEI_DELETE && nameiop != NAMEI_LOOKUP) {
982                 kprintf("hpfs_lookup: LOOKUP, DELETE and CREATE are only supported\n");
983                 return (EOPNOTSUPP);
984         }
985
986         error = VOP_EACCESS(dvp, VEXEC, cred);
987         if(error)
988                 return (error);
989
990         if( (cnp->cn_namelen == 1) &&
991             !strncmp(cnp->cn_nameptr,".",1) ) {
992                 dprintf(("hpfs_lookup(0x%x,...): . faked\n",dhp->h_no));
993
994                 vref(dvp);
995                 *ap->a_vpp = dvp;
996
997                 return (0);
998         } else if( (cnp->cn_namelen == 2) &&
999             !strncmp(cnp->cn_nameptr,"..",2) && (flags & CNP_ISDOTDOT) ) {
1000                 dprintf(("hpfs_lookup(0x%x,...): .. faked (0x%x)\n",
1001                         dhp->h_no, dhp->h_fn.fn_parent));
1002
1003                 VOP__UNLOCK(dvp, 0);
1004
1005                 error = VFS_VGET(hpmp->hpm_mp, NULL,
1006                                  dhp->h_fn.fn_parent, ap->a_vpp); 
1007                 if (error) {
1008                         VOP__LOCK(dvp, 0);
1009                         return(error);
1010                 }
1011
1012                 if (lockparent && (error = VOP__LOCK(dvp, 0))) {
1013                         vput( *(ap->a_vpp) );
1014                         return (error);
1015                 }
1016                 return (error);
1017         } else {
1018                 struct buf *bp;
1019                 struct hpfsdirent *dep;
1020                 struct hpfsnode *hp;
1021
1022                 error = hpfs_genlookupbyname(dhp,
1023                                 cnp->cn_nameptr, cnp->cn_namelen, &bp, &dep);
1024                 if (error) {
1025                         if (error == ENOENT && 
1026                             (nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME)) {
1027                                 if(!lockparent)
1028                                         VOP__UNLOCK(dvp, 0);
1029                                 return (EJUSTRETURN);
1030                         }
1031
1032                         return (error);
1033                 }
1034
1035                 dprintf(("hpfs_lookup: fnode: 0x%x, CPID: 0x%x\n",
1036                          dep->de_fnode, dep->de_cpid));
1037
1038                 if (nameiop == NAMEI_DELETE) {
1039                         error = VOP_EACCESS(dvp, VWRITE, cred);
1040                         if (error) {
1041                                 brelse(bp);
1042                                 return (error);
1043                         }
1044                 }
1045
1046                 if (dhp->h_no == dep->de_fnode) {
1047                         brelse(bp);
1048                         vref(dvp);
1049                         *ap->a_vpp = dvp;
1050                         return (0);
1051                 }
1052
1053                 error = VFS_VGET(hpmp->hpm_mp, NULL, dep->de_fnode, ap->a_vpp);
1054                 if (error) {
1055                         kprintf("hpfs_lookup: VFS_VGET FAILED %d\n", error);
1056                         brelse(bp);
1057                         return(error);
1058                 }
1059
1060                 hp = VTOHP(*ap->a_vpp);
1061
1062                 hp->h_mtime = dep->de_mtime;
1063                 hp->h_ctime = dep->de_ctime;
1064                 hp->h_atime = dep->de_atime;
1065                 bcopy(dep->de_name, hp->h_name, dep->de_namelen);
1066                 hp->h_name[dep->de_namelen] = '\0';
1067                 hp->h_namelen = dep->de_namelen;
1068                 hp->h_flag |= H_PARVALID;
1069
1070                 brelse(bp);
1071
1072                 if(!lockparent)
1073                         VOP__UNLOCK(dvp, 0);
1074         }
1075         return (error);
1076 }
1077
1078 /*
1079  * hpfs_remove(struct vnode *a_dvp, struct vnode *a_vp,
1080  *              struct componentname *a_cnp)
1081  */
1082 int
1083 hpfs_remove(struct vop_old_remove_args *ap)
1084 {
1085         int error;
1086
1087         dprintf(("hpfs_remove(0x%x, %s, %ld): \n", VTOHP(ap->a_vp)->h_no,
1088                 ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen));
1089
1090         if (ap->a_vp->v_type == VDIR)
1091                 return (EPERM);
1092
1093         error = hpfs_removefnode (ap->a_dvp, ap->a_vp, ap->a_cnp);
1094         return (error);
1095 }
1096
1097 /*
1098  * hpfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1099  *              struct componentname *a_cnp, struct vattr *a_vap)
1100  */
1101 int
1102 hpfs_create(struct vop_old_create_args *ap)
1103 {
1104         int error;
1105
1106         dprintf(("hpfs_create(0x%x, %s, %ld): \n", VTOHP(ap->a_dvp)->h_no,
1107                 ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen));
1108
1109         error = hpfs_makefnode (ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1110
1111         return (error);
1112 }
1113
1114 /*
1115  * Return POSIX pathconf information applicable to NTFS filesystem
1116  *
1117  * hpfs_pathconf(struct vnode *a_vp, int a_name, t *a_retval)
1118  */
1119 int
1120 hpfs_pathconf(struct vop_pathconf_args *ap)
1121 {
1122         switch (ap->a_name) {
1123         case _PC_LINK_MAX:
1124                 *ap->a_retval = 1;
1125                 return (0);
1126         case _PC_NAME_MAX:
1127                 *ap->a_retval = HPFS_MAXFILENAME;
1128                 return (0);
1129         case _PC_PATH_MAX:
1130                 *ap->a_retval = PATH_MAX;
1131                 return (0);
1132         case _PC_CHOWN_RESTRICTED:
1133                 *ap->a_retval = 1;
1134                 return (0);
1135         case _PC_NO_TRUNC:
1136                 *ap->a_retval = 0;
1137                 return (0);
1138 #if defined(__NetBSD__)
1139         case _PC_SYNC_IO:
1140                 *ap->a_retval = 1;
1141                 return (0);
1142         case _PC_FILESIZEBITS:
1143                 *ap->a_retval = 32;
1144                 return (0);
1145 #endif
1146         default:
1147                 return (EINVAL);
1148         }
1149         /* NOTREACHED */
1150 }
1151
1152
1153 /*
1154  * Global vfs data structures
1155  */
1156
1157 struct vop_ops hpfs_vnode_vops = {
1158         .vop_default =          vop_defaultop,
1159         .vop_getattr =          hpfs_getattr,
1160         .vop_setattr =          hpfs_setattr,
1161         .vop_inactive =         hpfs_inactive,
1162         .vop_reclaim =          hpfs_reclaim,
1163         .vop_print =            hpfs_print,
1164         .vop_old_create =       hpfs_create,
1165         .vop_old_remove =       hpfs_remove,
1166         .vop_old_lookup =       hpfs_lookup,
1167         .vop_access =           hpfs_access,
1168         .vop_readdir =          hpfs_readdir,
1169         .vop_fsync =            hpfs_fsync,
1170         .vop_bmap =             hpfs_bmap,
1171         .vop_getpages =         vop_stdgetpages,
1172         .vop_putpages =         vop_stdputpages,
1173         .vop_strategy =         hpfs_strategy,
1174         .vop_read =             hpfs_read,
1175         .vop_write =            hpfs_write,
1176         .vop_ioctl =            hpfs_ioctl,
1177         .vop_pathconf =         hpfs_pathconf
1178 };
1179