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