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