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