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