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