Add missing commit for the VM load heuristic and page allocation rate
[dragonfly.git] / sys / vfs / ntfs / ntfs_vnops.c
CommitLineData
984263bc
MD
1/* $NetBSD: ntfs_vnops.c,v 1.23 1999/10/31 19:45:27 jdolecek Exp $ */
2
3/*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * John Heidemann of the UCLA Ficus project.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * $FreeBSD: src/sys/ntfs/ntfs_vnops.c,v 1.9.2.4 2002/08/06 19:35:18 semenu Exp $
81b5c339 39 * $DragonFly: src/sys/vfs/ntfs/ntfs_vnops.c,v 1.25 2006/02/17 19:18:07 dillon Exp $
984263bc
MD
40 *
41 */
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/kernel.h>
46#include <sys/time.h>
47#include <sys/types.h>
48#include <sys/stat.h>
49#include <sys/vnode.h>
50#include <sys/mount.h>
dadab5e9 51#include <sys/proc.h>
984263bc
MD
52#include <sys/namei.h>
53#include <sys/malloc.h>
54#include <sys/buf.h>
55#include <sys/dirent.h>
7bf50a33 56#include <machine/limits.h>
984263bc
MD
57
58#include <vm/vm.h>
59#include <vm/vm_param.h>
60#if defined(__NetBSD__)
61#include <vm/vm_prot.h>
62#endif
63#include <vm/vm_page.h>
64#include <vm/vm_object.h>
65#include <vm/vm_pager.h>
08c23b99 66#if defined(__DragonFly__)
984263bc
MD
67#include <vm/vnode_pager.h>
68#endif
69#include <vm/vm_extern.h>
70
71#include <sys/sysctl.h>
72
73/*#define NTFS_DEBUG 1*/
1f2de5d4
MD
74#include "ntfs.h"
75#include "ntfs_inode.h"
76#include "ntfs_subr.h"
984263bc
MD
77#if defined(__NetBSD__)
78#include <miscfs/specfs/specdev.h>
79#include <miscfs/genfs/genfs.h>
80#endif
81
82#include <sys/unistd.h> /* for pathconf(2) constants */
83
a6ee311a
RG
84static int ntfs_read (struct vop_read_args *);
85static int ntfs_write (struct vop_write_args *ap);
86static int ntfs_getattr (struct vop_getattr_args *ap);
87static int ntfs_inactive (struct vop_inactive_args *ap);
88static int ntfs_print (struct vop_print_args *ap);
89static int ntfs_reclaim (struct vop_reclaim_args *ap);
90static int ntfs_strategy (struct vop_strategy_args *ap);
91static int ntfs_access (struct vop_access_args *ap);
92static int ntfs_open (struct vop_open_args *ap);
93static int ntfs_close (struct vop_close_args *ap);
94static int ntfs_readdir (struct vop_readdir_args *ap);
e62afb5f 95static int ntfs_lookup (struct vop_old_lookup_args *ap);
a6ee311a 96static int ntfs_bmap (struct vop_bmap_args *ap);
08c23b99 97#if defined(__DragonFly__)
a6ee311a
RG
98static int ntfs_getpages (struct vop_getpages_args *ap);
99static int ntfs_putpages (struct vop_putpages_args *);
100static int ntfs_fsync (struct vop_fsync_args *ap);
984263bc 101#else
2d3e977e 102static int ntfs_bypass (struct vop_generic_args *);
984263bc 103#endif
a6ee311a 104static int ntfs_pathconf (void *);
984263bc
MD
105
106int ntfs_prtactive = 1; /* 1 => print out reclaim of active vnodes */
107
08c23b99 108#if defined(__DragonFly__)
984263bc 109int
cd0a65a2 110ntfs_getpages(struct vop_getpages_args *ap)
984263bc
MD
111{
112 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
113 ap->a_reqpage);
114}
115
116int
cd0a65a2 117ntfs_putpages(struct vop_putpages_args *ap)
984263bc
MD
118{
119 return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
120 ap->a_sync, ap->a_rtvals);
121}
122#endif
123
124/*
125 * This is a noop, simply returning what one has been given.
cd0a65a2
CP
126 *
127 * ntfs_bmap(struct vnode *a_vp, daddr_t a_bn, struct vnode **a_vpp,
128 * daddr_t *a_bnp, int *a_runp, int *a_runb)
984263bc
MD
129 */
130int
cd0a65a2 131ntfs_bmap(struct vop_bmap_args *ap)
984263bc
MD
132{
133 dprintf(("ntfs_bmap: vn: %p, blk: %d\n", ap->a_vp,(u_int32_t)ap->a_bn));
134 if (ap->a_vpp != NULL)
135 *ap->a_vpp = ap->a_vp;
136 if (ap->a_bnp != NULL)
137 *ap->a_bnp = ap->a_bn;
138 if (ap->a_runp != NULL)
139 *ap->a_runp = 0;
140#if !defined(__NetBSD__)
141 if (ap->a_runb != NULL)
142 *ap->a_runb = 0;
143#endif
144 return (0);
145}
146
cd0a65a2
CP
147/*
148 * ntfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
149 * struct ucred *a_cred)
150 */
984263bc 151static int
cd0a65a2 152ntfs_read(struct vop_read_args *ap)
984263bc 153{
34ee9825
RG
154 struct vnode *vp = ap->a_vp;
155 struct fnode *fp = VTOF(vp);
156 struct ntnode *ip = FTONT(fp);
984263bc
MD
157 struct uio *uio = ap->a_uio;
158 struct ntfsmount *ntmp = ip->i_mp;
159 struct buf *bp;
160 daddr_t cn;
161 int resid, off, toread;
162 int error;
163
164 dprintf(("ntfs_read: ino: %d, off: %d resid: %d, segflg: %d\n",ip->i_number,(u_int32_t)uio->uio_offset,uio->uio_resid,uio->uio_segflg));
165
166 dprintf(("ntfs_read: filesize: %d",(u_int32_t)fp->f_size));
167
168 /* don't allow reading after end of file */
169 if (uio->uio_offset > fp->f_size)
170 return (0);
171
172 resid = min(uio->uio_resid, fp->f_size - uio->uio_offset);
173
174 dprintf((", resid: %d\n", resid));
175
176 error = 0;
177 while (resid) {
178 cn = ntfs_btocn(uio->uio_offset);
179 off = ntfs_btocnoff(uio->uio_offset);
180
181 toread = min(off + resid, ntfs_cntob(1));
182
3b568787 183 error = bread(vp, cn, ntfs_cntob(1), &bp);
984263bc
MD
184 if (error) {
185 brelse(bp);
186 break;
187 }
188
189 error = uiomove(bp->b_data + off, toread - off, uio);
190 if(error) {
191 brelse(bp);
192 break;
193 }
194 brelse(bp);
195
196 resid -= toread - off;
197 }
198
199 return (error);
200}
201
08c23b99 202#if !defined(__DragonFly__)
984263bc 203
cd0a65a2
CP
204/*
205 * ntfs_bypass(struct vnodeop_desc *a_desc, ...)
206 */
984263bc 207static int
cd0a65a2 208ntfs_bypass(struct vop_generic_args *ap)
984263bc
MD
209{
210 int error = ENOTTY;
211 dprintf(("ntfs_bypass: %s\n", ap->a_desc->vdesc_name));
212 return (error);
213}
214
215#endif
216
cd0a65a2
CP
217/*
218 * ntfs_getattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred,
219 * struct thread *a_td)
220 */
984263bc 221static int
cd0a65a2 222ntfs_getattr(struct vop_getattr_args *ap)
984263bc 223{
34ee9825
RG
224 struct vnode *vp = ap->a_vp;
225 struct fnode *fp = VTOF(vp);
226 struct ntnode *ip = FTONT(fp);
227 struct vattr *vap = ap->a_vap;
984263bc
MD
228
229 dprintf(("ntfs_getattr: %d, flags: %d\n",ip->i_number,ip->i_flag));
230
08c23b99 231#if defined(__DragonFly__)
984263bc
MD
232 vap->va_fsid = dev2udev(ip->i_dev);
233#else /* NetBSD */
234 vap->va_fsid = ip->i_dev;
235#endif
236 vap->va_fileid = ip->i_number;
237 vap->va_mode = ip->i_mp->ntm_mode;
238 vap->va_nlink = ip->i_nlink;
239 vap->va_uid = ip->i_mp->ntm_uid;
240 vap->va_gid = ip->i_mp->ntm_gid;
241 vap->va_rdev = 0; /* XXX UNODEV ? */
242 vap->va_size = fp->f_size;
243 vap->va_bytes = fp->f_allocated;
244 vap->va_atime = ntfs_nttimetounix(fp->f_times.t_access);
245 vap->va_mtime = ntfs_nttimetounix(fp->f_times.t_write);
246 vap->va_ctime = ntfs_nttimetounix(fp->f_times.t_create);
247 vap->va_flags = ip->i_flag;
248 vap->va_gen = 0;
249 vap->va_blocksize = ip->i_mp->ntm_spc * ip->i_mp->ntm_bps;
250 vap->va_type = vp->v_type;
251 vap->va_filerev = 0;
252 return (0);
253}
254
255
256/*
257 * Last reference to an ntnode. If necessary, write or delete it.
cd0a65a2
CP
258 *
259 * ntfs_inactive(struct vnode *a_vp)
984263bc
MD
260 */
261int
cd0a65a2 262ntfs_inactive(struct vop_inactive_args *ap)
984263bc 263{
34ee9825 264 struct vnode *vp = ap->a_vp;
984263bc 265#ifdef NTFS_DEBUG
34ee9825 266 struct ntnode *ip = VTONT(vp);
984263bc
MD
267#endif
268
269 dprintf(("ntfs_inactive: vnode: %p, ntnode: %d\n", vp, ip->i_number));
270
271 if (ntfs_prtactive && vp->v_usecount != 0)
272 vprint("ntfs_inactive: pushing active", vp);
273
5fd012e0
MD
274 /*
275 * XXX since we don't support any filesystem changes
984263bc
MD
276 * right now, nothing more needs to be done
277 */
278 return (0);
279}
280
281/*
282 * Reclaim an fnode/ntnode so that it can be used for other purposes.
cd0a65a2
CP
283 *
284 * ntfs_reclaim(struct vnode *a_vp)
984263bc
MD
285 */
286int
cd0a65a2 287ntfs_reclaim(struct vop_reclaim_args *ap)
984263bc 288{
34ee9825
RG
289 struct vnode *vp = ap->a_vp;
290 struct fnode *fp = VTOF(vp);
291 struct ntnode *ip = FTONT(fp);
984263bc
MD
292 int error;
293
294 dprintf(("ntfs_reclaim: vnode: %p, ntnode: %d\n", vp, ip->i_number));
295
296 if (ntfs_prtactive && vp->v_usecount != 0)
297 vprint("ntfs_reclaim: pushing active", vp);
298
299 if ((error = ntfs_ntget(ip)) != 0)
300 return (error);
301
984263bc
MD
302 ntfs_frele(fp);
303 ntfs_ntput(ip);
304 vp->v_data = NULL;
305
306 return (0);
307}
308
cd0a65a2
CP
309/*
310 * ntfs_print(struct vnode *a_vp)
311 */
984263bc 312static int
cd0a65a2 313ntfs_print(struct vop_print_args *ap)
984263bc
MD
314{
315 return (0);
316}
317
318/*
319 * Calculate the logical to physical mapping if not done already,
320 * then call the device strategy routine.
cd0a65a2 321 *
81b5c339 322 * ntfs_strategy(struct vnode *a_vp, struct bio *a_bio)
984263bc
MD
323 */
324int
cd0a65a2 325ntfs_strategy(struct vop_strategy_args *ap)
984263bc 326{
81b5c339
MD
327 struct bio *bio = ap->a_bio;
328 struct buf *bp = bio->bio_buf;
329 struct vnode *vp = ap->a_vp;
34ee9825
RG
330 struct fnode *fp = VTOF(vp);
331 struct ntnode *ip = FTONT(fp);
984263bc
MD
332 struct ntfsmount *ntmp = ip->i_mp;
333 int error;
334
08c23b99 335#ifdef __DragonFly__
984263bc 336 dprintf(("ntfs_strategy: offset: %d, blkno: %d, lblkno: %d\n",
81b5c339 337 (u_int32_t)bp->b_loffset,(u_int32_t)bio->bio_blkno,
984263bc
MD
338 (u_int32_t)bp->b_lblkno));
339#else
340 dprintf(("ntfs_strategy: blkno: %d, lblkno: %d\n",
81b5c339 341 (u_int32_t)bio->bio_blkno,
984263bc
MD
342 (u_int32_t)bp->b_lblkno));
343#endif
344
345 dprintf(("strategy: bcount: %d flags: 0x%lx\n",
346 (u_int32_t)bp->b_bcount,bp->b_flags));
347
348 if (bp->b_flags & B_READ) {
349 u_int32_t toread;
350
81b5c339 351 if (ntfs_cntob(bio->bio_blkno) >= fp->f_size) {
984263bc
MD
352 clrbuf(bp);
353 error = 0;
354 } else {
355 toread = min(bp->b_bcount,
81b5c339 356 fp->f_size-ntfs_cntob(bio->bio_blkno));
984263bc
MD
357 dprintf(("ntfs_strategy: toread: %d, fsize: %d\n",
358 toread,(u_int32_t)fp->f_size));
359
360 error = ntfs_readattr(ntmp, ip, fp->f_attrtype,
81b5c339 361 fp->f_attrname, ntfs_cntob(bio->bio_blkno),
984263bc
MD
362 toread, bp->b_data, NULL);
363
364 if (error) {
365 printf("ntfs_strategy: ntfs_readattr failed\n");
366 bp->b_error = error;
367 bp->b_flags |= B_ERROR;
368 }
369
370 bzero(bp->b_data + toread, bp->b_bcount - toread);
371 }
372 } else {
373 size_t tmp;
374 u_int32_t towrite;
375
81b5c339 376 if (ntfs_cntob(bio->bio_blkno) + bp->b_bcount >= fp->f_size) {
984263bc
MD
377 printf("ntfs_strategy: CAN'T EXTEND FILE\n");
378 bp->b_error = error = EFBIG;
379 bp->b_flags |= B_ERROR;
380 } else {
381 towrite = min(bp->b_bcount,
81b5c339 382 fp->f_size-ntfs_cntob(bio->bio_blkno));
984263bc
MD
383 dprintf(("ntfs_strategy: towrite: %d, fsize: %d\n",
384 towrite,(u_int32_t)fp->f_size));
385
386 error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype,
81b5c339 387 fp->f_attrname, ntfs_cntob(bio->bio_blkno),towrite,
984263bc
MD
388 bp->b_data, &tmp, NULL);
389
390 if (error) {
391 printf("ntfs_strategy: ntfs_writeattr fail\n");
392 bp->b_error = error;
393 bp->b_flags |= B_ERROR;
394 }
395 }
396 }
81b5c339 397 biodone(bio);
984263bc
MD
398 return (error);
399}
400
cd0a65a2
CP
401/*
402 * ntfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
403 * struct ucred *a_cred)
404 */
984263bc 405static int
cd0a65a2 406ntfs_write(struct vop_write_args *ap)
984263bc 407{
34ee9825
RG
408 struct vnode *vp = ap->a_vp;
409 struct fnode *fp = VTOF(vp);
410 struct ntnode *ip = FTONT(fp);
984263bc
MD
411 struct uio *uio = ap->a_uio;
412 struct ntfsmount *ntmp = ip->i_mp;
413 u_int64_t towrite;
414 size_t written;
415 int error;
416
417 dprintf(("ntfs_write: ino: %d, off: %d resid: %d, segflg: %d\n",ip->i_number,(u_int32_t)uio->uio_offset,uio->uio_resid,uio->uio_segflg));
418 dprintf(("ntfs_write: filesize: %d",(u_int32_t)fp->f_size));
419
420 if (uio->uio_resid + uio->uio_offset > fp->f_size) {
421 printf("ntfs_write: CAN'T WRITE BEYOND END OF FILE\n");
422 return (EFBIG);
423 }
424
425 towrite = min(uio->uio_resid, fp->f_size - uio->uio_offset);
426
427 dprintf((", towrite: %d\n",(u_int32_t)towrite));
428
429 error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype,
430 fp->f_attrname, uio->uio_offset, towrite, NULL, &written, uio);
431#ifdef NTFS_DEBUG
432 if (error)
433 printf("ntfs_write: ntfs_writeattr failed: %d\n", error);
434#endif
435
436 return (error);
437}
438
cd0a65a2
CP
439/*
440 * ntfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
441 * struct thread *a_td)
442 */
984263bc 443int
cd0a65a2 444ntfs_access(struct vop_access_args *ap)
984263bc
MD
445{
446 struct vnode *vp = ap->a_vp;
447 struct ntnode *ip = VTONT(vp);
448 struct ucred *cred = ap->a_cred;
449 mode_t mask, mode = ap->a_mode;
34ee9825 450 gid_t *gp;
984263bc
MD
451 int i;
452#ifdef QUOTA
453 int error;
454#endif
455
456 dprintf(("ntfs_access: %d\n",ip->i_number));
457
458 /*
459 * Disallow write attempts on read-only file systems;
460 * unless the file is a socket, fifo, or a block or
461 * character device resident on the file system.
462 */
463 if (mode & VWRITE) {
464 switch ((int)vp->v_type) {
465 case VDIR:
466 case VLNK:
467 case VREG:
468 if (vp->v_mount->mnt_flag & MNT_RDONLY)
469 return (EROFS);
470#ifdef QUOTA
471 if (error = getinoquota(ip))
472 return (error);
473#endif
474 break;
475 }
476 }
477
478 /* Otherwise, user id 0 always gets access. */
479 if (cred->cr_uid == 0)
480 return (0);
481
482 mask = 0;
483
484 /* Otherwise, check the owner. */
485 if (cred->cr_uid == ip->i_mp->ntm_uid) {
486 if (mode & VEXEC)
487 mask |= S_IXUSR;
488 if (mode & VREAD)
489 mask |= S_IRUSR;
490 if (mode & VWRITE)
491 mask |= S_IWUSR;
492 return ((ip->i_mp->ntm_mode & mask) == mask ? 0 : EACCES);
493 }
494
495 /* Otherwise, check the groups. */
496 for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
497 if (ip->i_mp->ntm_gid == *gp) {
498 if (mode & VEXEC)
499 mask |= S_IXGRP;
500 if (mode & VREAD)
501 mask |= S_IRGRP;
502 if (mode & VWRITE)
503 mask |= S_IWGRP;
504 return ((ip->i_mp->ntm_mode&mask) == mask ? 0 : EACCES);
505 }
506
507 /* Otherwise, check everyone else. */
508 if (mode & VEXEC)
509 mask |= S_IXOTH;
510 if (mode & VREAD)
511 mask |= S_IROTH;
512 if (mode & VWRITE)
513 mask |= S_IWOTH;
514 return ((ip->i_mp->ntm_mode & mask) == mask ? 0 : EACCES);
515}
516
517/*
518 * Open called.
519 *
520 * Nothing to do.
cd0a65a2
CP
521 *
522 * ntfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
523 * struct thread *a_td)
984263bc
MD
524 */
525/* ARGSUSED */
526static int
cd0a65a2 527ntfs_open(struct vop_open_args *ap)
984263bc
MD
528{
529#if NTFS_DEBUG
34ee9825
RG
530 struct vnode *vp = ap->a_vp;
531 struct ntnode *ip = VTONT(vp);
984263bc
MD
532
533 printf("ntfs_open: %d\n",ip->i_number);
534#endif
535
536 /*
537 * Files marked append-only must be opened for appending.
538 */
539
540 return (0);
541}
542
543/*
544 * Close called.
545 *
546 * Update the times on the inode.
cd0a65a2
CP
547 *
548 * ntfs_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
549 * struct thread *a_td)
984263bc
MD
550 */
551/* ARGSUSED */
552static int
cd0a65a2 553ntfs_close(struct vop_close_args *ap)
984263bc
MD
554{
555#if NTFS_DEBUG
34ee9825
RG
556 struct vnode *vp = ap->a_vp;
557 struct ntnode *ip = VTONT(vp);
984263bc
MD
558
559 printf("ntfs_close: %d\n",ip->i_number);
560#endif
561
562 return (0);
563}
564
cd0a65a2
CP
565/*
566 * ntfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
567 * int *a_ncookies, u_int **cookies)
568 */
984263bc 569int
cd0a65a2 570ntfs_readdir(struct vop_readdir_args *ap)
984263bc 571{
34ee9825
RG
572 struct vnode *vp = ap->a_vp;
573 struct fnode *fp = VTOF(vp);
574 struct ntnode *ip = FTONT(fp);
984263bc
MD
575 struct uio *uio = ap->a_uio;
576 struct ntfsmount *ntmp = ip->i_mp;
577 int i, error = 0;
7bf50a33 578 u_int32_t faked = 0, num, off;
984263bc 579 int ncookies = 0;
7bf50a33 580 char convname[NTFS_MAXFILENAME + 1];
984263bc
MD
581
582 dprintf(("ntfs_readdir %d off: %d resid: %d\n",ip->i_number,(u_int32_t)uio->uio_offset,uio->uio_resid));
583
7bf50a33
SS
584 if (uio->uio_offset < 0 || uio->uio_offset > INT_MAX)
585 return (EINVAL);
984263bc 586
7bf50a33
SS
587 /*
588 * uio->uio_offset carries the number of the entry
589 * where we should start returning dirents.
590 *
591 * We fake up "." if we're not reading the FS root
592 * and we always fake up "..".
593 *
594 * off contains the entry we are starting at,
595 * num increments while we are reading.
596 */
984263bc 597
7bf50a33
SS
598 off = num = uio->uio_offset;
599 faked = (ip->i_number == NTFS_ROOTINO) ? 1 : 2;
984263bc 600
7bf50a33
SS
601 /* Simulate . in every dir except ROOT */
602 if (ip->i_number != NTFS_ROOTINO && num == 0) {
603 if (vop_write_dirent(&error, uio, ip->i_number,
604 DT_DIR, 1, "."))
605 return (0);
606 if (error)
607 return (error);
608
609 num++;
610 ncookies++;
984263bc
MD
611 }
612
613 /* Simulate .. in every dir including ROOT */
7bf50a33
SS
614 if (num == faked - 1) {
615 /* XXX NTFS_ROOTINO seems to be wrong here */
616 if (vop_write_dirent(&error, uio, NTFS_ROOTINO,
617 DT_DIR, 2, ".."))
618 goto readdone;
984263bc
MD
619 if(error)
620 return (error);
621
7bf50a33
SS
622 num++;
623 ncookies++;
984263bc
MD
624 }
625
7bf50a33 626 for (;;) {
984263bc
MD
627 struct attr_indexentry *iep;
628
7bf50a33
SS
629 /*
630 * num is the number of the entry we will return,
631 * but ntfs_ntreaddir takes the entry number of the
632 * ntfs directory listing, so subtract the faked
633 * . and .. entries.
634 */
635 error = ntfs_ntreaddir(ntmp, fp, num - faked, &iep);
984263bc
MD
636
637 if(error)
638 return (error);
639
640 if( NULL == iep )
641 break;
642
7bf50a33 643 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST);
984263bc
MD
644 iep = NTFS_NEXTREC(iep, struct attr_indexentry *))
645 {
646 if(!ntfs_isnamepermitted(ntmp,iep))
647 continue;
648
7bf50a33
SS
649 for (i=0; i < iep->ie_fnamelen; i++)
650 convname[i] = NTFS_U28(iep->ie_fname[i]);
651 convname[i] = '\0';
652
653 if (vop_write_dirent(&error, uio, iep->ie_number,
654 (iep->ie_fflag & NTFS_FFLAG_DIR) ? DT_DIR : DT_REG,
655 iep->ie_fnamelen, convname))
656 goto readdone;
657
658 dprintf(("ntfs_readdir: elem: %d, fname:[%s] type: %d, "
659 "flag: %d, %s\n",
660 ncookies, convname, iep->ie_fnametype,
661 iep->ie_flag,
662 (iep->ie_fflag & NTFS_FFLAG_DIR) ?
663 "dir" : "reg"));
664
984263bc
MD
665 if(error)
666 return (error);
667
668 ncookies++;
669 num++;
670 }
671 }
672
7bf50a33
SS
673readdone:
674 uio->uio_offset = num;
675
984263bc
MD
676 dprintf(("ntfs_readdir: %d entries (%d bytes) read\n",
677 ncookies,(u_int)(uio->uio_offset - off)));
678 dprintf(("ntfs_readdir: off: %d resid: %d\n",
679 (u_int32_t)uio->uio_offset,uio->uio_resid));
680
681 if (!error && ap->a_ncookies != NULL) {
08c23b99 682#if defined(__DragonFly__)
984263bc
MD
683 u_long *cookies;
684 u_long *cookiep;
685#else /* defined(__NetBSD__) */
686 off_t *cookies;
687 off_t *cookiep;
688#endif
689
690 ddprintf(("ntfs_readdir: %d cookies\n",ncookies));
691 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
692 panic("ntfs_readdir: unexpected uio from NFS server");
08c23b99 693#if defined(__DragonFly__)
984263bc
MD
694 MALLOC(cookies, u_long *, ncookies * sizeof(u_long),
695 M_TEMP, M_WAITOK);
696#else /* defined(__NetBSD__) */
697 MALLOC(cookies, off_t *, ncookies * sizeof(off_t),
698 M_TEMP, M_WAITOK);
699#endif
7bf50a33
SS
700 cookiep = cookies;
701 while (off < num)
702 *cookiep++ = ++off;
703
984263bc
MD
704 *ap->a_ncookies = ncookies;
705 *ap->a_cookies = cookies;
706 }
707/*
708 if (ap->a_eofflag)
709 *ap->a_eofflag = VTONT(ap->a_vp)->i_size <= uio->uio_offset;
710*/
711 return (error);
712}
713
cd0a65a2
CP
714/*
715 * ntfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
716 * struct componentname *a_cnp)
717 */
984263bc 718int
e62afb5f 719ntfs_lookup(struct vop_old_lookup_args *ap)
984263bc 720{
34ee9825
RG
721 struct vnode *dvp = ap->a_dvp;
722 struct ntnode *dip = VTONT(dvp);
984263bc
MD
723 struct ntfsmount *ntmp = dip->i_mp;
724 struct componentname *cnp = ap->a_cnp;
984263bc 725 int error;
2b69e610 726 int lockparent = cnp->cn_flags & CNP_LOCKPARENT;
984263bc 727#if NTFS_DEBUG
2b69e610 728 int wantparent = cnp->cn_flags & (CNP_LOCKPARENT | CNP_WANTPARENT);
984263bc
MD
729#endif
730 dprintf(("ntfs_lookup: \"%.*s\" (%ld bytes) in %d, lp: %d, wp: %d \n",
731 (int)cnp->cn_namelen, cnp->cn_nameptr, cnp->cn_namelen,
732 dip->i_number, lockparent, wantparent));
733
9df3a184
MD
734 *ap->a_vpp = NULL;
735
fad57d0e 736 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
984263bc
MD
737 dprintf(("ntfs_lookup: faking . directory in %d\n",
738 dip->i_number));
739
597aea93 740 vref(dvp);
984263bc
MD
741 *ap->a_vpp = dvp;
742 error = 0;
2b69e610 743 } else if (cnp->cn_flags & CNP_ISDOTDOT) {
984263bc
MD
744 struct ntvattr *vap;
745
746 dprintf(("ntfs_lookup: faking .. directory in %d\n",
747 dip->i_number));
748
749 error = ntfs_ntvattrget(ntmp, dip, NTFS_A_NAME, NULL, 0, &vap);
750 if(error)
751 return (error);
752
dadab5e9 753 VOP__UNLOCK(dvp,0,cnp->cn_td);
2b69e610 754 cnp->cn_flags |= CNP_PDIRUNLOCK;
984263bc
MD
755
756 dprintf(("ntfs_lookup: parentdir: %d\n",
757 vap->va_a_name->n_pnumber));
758 error = VFS_VGET(ntmp->ntm_mountp,
759 vap->va_a_name->n_pnumber,ap->a_vpp);
760 ntfs_ntvattrrele(vap);
761 if (error) {
dadab5e9 762 if (VN_LOCK(dvp,LK_EXCLUSIVE|LK_RETRY,cnp->cn_td)==0)
2b69e610 763 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
984263bc
MD
764 return (error);
765 }
766
fad57d0e 767 if (lockparent) {
dadab5e9 768 error = VN_LOCK(dvp, LK_EXCLUSIVE, cnp->cn_td);
984263bc 769 if (error) {
9df3a184
MD
770 vput(*ap->a_vpp);
771 *ap->a_vpp = NULL;
984263bc
MD
772 return (error);
773 }
2b69e610 774 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
984263bc
MD
775 }
776 } else {
777 error = ntfs_ntlookupfile(ntmp, dvp, cnp, ap->a_vpp);
778 if (error) {
779 dprintf(("ntfs_ntlookupfile: returned %d\n", error));
780 return (error);
781 }
782
783 dprintf(("ntfs_lookup: found ino: %d\n",
784 VTONT(*ap->a_vpp)->i_number));
785
fad57d0e 786 if (!lockparent)
dadab5e9 787 VOP__UNLOCK(dvp, 0, cnp->cn_td);
984263bc 788 }
984263bc
MD
789 return (error);
790}
791
08c23b99 792#if defined(__DragonFly__)
984263bc
MD
793/*
794 * Flush the blocks of a file to disk.
795 *
796 * This function is worthless for vnodes that represent directories. Maybe we
797 * could just do a sync if they try an fsync on a directory file.
cd0a65a2
CP
798 *
799 * ntfs_fsync(struct vnode *a_vp, struct ucred *a_cred, int a_waitfor,
800 * struct thread *a_td)
984263bc
MD
801 */
802static int
cd0a65a2 803ntfs_fsync(struct vop_fsync_args *ap)
984263bc
MD
804{
805 return (0);
806}
807#endif
808
809/*
810 * Return POSIX pathconf information applicable to NTFS filesystem
811 */
812int
cd0a65a2 813ntfs_pathconf(void *v)
984263bc
MD
814{
815 struct vop_pathconf_args /* {
816 struct vnode *a_vp;
817 int a_name;
818 register_t *a_retval;
819 } */ *ap = v;
820
821 switch (ap->a_name) {
822 case _PC_LINK_MAX:
823 *ap->a_retval = 1;
824 return (0);
825 case _PC_NAME_MAX:
826 *ap->a_retval = NTFS_MAXFILENAME;
827 return (0);
828 case _PC_PATH_MAX:
829 *ap->a_retval = PATH_MAX;
830 return (0);
831 case _PC_CHOWN_RESTRICTED:
832 *ap->a_retval = 1;
833 return (0);
834 case _PC_NO_TRUNC:
835 *ap->a_retval = 0;
836 return (0);
837#if defined(__NetBSD__)
838 case _PC_SYNC_IO:
839 *ap->a_retval = 1;
840 return (0);
841 case _PC_FILESIZEBITS:
842 *ap->a_retval = 64;
843 return (0);
844#endif
845 default:
846 return (EINVAL);
847 }
848 /* NOTREACHED */
849}
850
851/*
852 * Global vfs data structures
853 */
984263bc 854struct vnodeopv_entry_desc ntfs_vnodeop_entries[] = {
2d3e977e
MD
855 { &vop_default_desc, vop_defaultop },
856
625ddaba
JS
857 { &vop_getattr_desc, (vnodeopv_entry_t)ntfs_getattr },
858 { &vop_inactive_desc, (vnodeopv_entry_t)ntfs_inactive },
859 { &vop_reclaim_desc, (vnodeopv_entry_t)ntfs_reclaim },
860 { &vop_print_desc, (vnodeopv_entry_t)ntfs_print },
861 { &vop_pathconf_desc, (vnodeopv_entry_t)ntfs_pathconf },
862
863 { &vop_islocked_desc, (vnodeopv_entry_t)vop_stdislocked },
864 { &vop_unlock_desc, (vnodeopv_entry_t)vop_stdunlock },
865 { &vop_lock_desc, (vnodeopv_entry_t)vop_stdlock },
e62afb5f 866 { &vop_old_lookup_desc, (vnodeopv_entry_t)ntfs_lookup },
625ddaba
JS
867
868 { &vop_access_desc, (vnodeopv_entry_t)ntfs_access },
869 { &vop_close_desc, (vnodeopv_entry_t)ntfs_close },
870 { &vop_open_desc, (vnodeopv_entry_t)ntfs_open },
871 { &vop_readdir_desc, (vnodeopv_entry_t)ntfs_readdir },
872 { &vop_fsync_desc, (vnodeopv_entry_t)ntfs_fsync },
873
874 { &vop_bmap_desc, (vnodeopv_entry_t)ntfs_bmap },
875 { &vop_getpages_desc, (vnodeopv_entry_t)ntfs_getpages },
876 { &vop_putpages_desc, (vnodeopv_entry_t)ntfs_putpages },
877 { &vop_strategy_desc, (vnodeopv_entry_t)ntfs_strategy },
878 { &vop_bwrite_desc, (vnodeopv_entry_t)vop_stdbwrite },
879 { &vop_read_desc, (vnodeopv_entry_t)ntfs_read },
880 { &vop_write_desc, (vnodeopv_entry_t)ntfs_write },
984263bc
MD
881
882 { NULL, NULL }
883};
884