kernel - Adjust UFS and HAMMER to use uiomovebp()
[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 $
39 *
40 */
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/kernel.h>
45#include <sys/time.h>
46#include <sys/types.h>
47#include <sys/stat.h>
48#include <sys/vnode.h>
49#include <sys/mount.h>
dadab5e9 50#include <sys/proc.h>
984263bc
MD
51#include <sys/namei.h>
52#include <sys/malloc.h>
53#include <sys/buf.h>
54#include <sys/dirent.h>
7bf50a33 55#include <machine/limits.h>
984263bc
MD
56
57#include <vm/vm.h>
58#include <vm/vm_param.h>
59#if defined(__NetBSD__)
60#include <vm/vm_prot.h>
61#endif
62#include <vm/vm_page.h>
63#include <vm/vm_object.h>
64#include <vm/vm_pager.h>
08c23b99 65#if defined(__DragonFly__)
984263bc
MD
66#include <vm/vnode_pager.h>
67#endif
68#include <vm/vm_extern.h>
69
70#include <sys/sysctl.h>
71
54341a3b
MD
72#include <sys/buf2.h>
73
984263bc 74/*#define NTFS_DEBUG 1*/
1f2de5d4
MD
75#include "ntfs.h"
76#include "ntfs_inode.h"
77#include "ntfs_subr.h"
984263bc
MD
78#if defined(__NetBSD__)
79#include <miscfs/specfs/specdev.h>
80#include <miscfs/genfs/genfs.h>
81#endif
82
83#include <sys/unistd.h> /* for pathconf(2) constants */
84
a6ee311a
RG
85static int ntfs_read (struct vop_read_args *);
86static int ntfs_write (struct vop_write_args *ap);
87static int ntfs_getattr (struct vop_getattr_args *ap);
88static int ntfs_inactive (struct vop_inactive_args *ap);
89static int ntfs_print (struct vop_print_args *ap);
90static int ntfs_reclaim (struct vop_reclaim_args *ap);
91static int ntfs_strategy (struct vop_strategy_args *ap);
92static int ntfs_access (struct vop_access_args *ap);
93static int ntfs_open (struct vop_open_args *ap);
94static int ntfs_close (struct vop_close_args *ap);
95static int ntfs_readdir (struct vop_readdir_args *ap);
e62afb5f 96static int ntfs_lookup (struct vop_old_lookup_args *ap);
a6ee311a 97static int ntfs_bmap (struct vop_bmap_args *ap);
08c23b99 98#if defined(__DragonFly__)
a6ee311a 99static int ntfs_fsync (struct vop_fsync_args *ap);
984263bc 100#else
2d3e977e 101static int ntfs_bypass (struct vop_generic_args *);
984263bc 102#endif
66a1ddf5 103static int ntfs_pathconf (struct vop_pathconf_args *);
984263bc
MD
104
105int ntfs_prtactive = 1; /* 1 => print out reclaim of active vnodes */
106
984263bc
MD
107/*
108 * This is a noop, simply returning what one has been given.
cd0a65a2 109 *
08daea96 110 * ntfs_bmap(struct vnode *a_vp, off_t a_loffset,
54078292 111 * daddr_t *a_doffsetp, int *a_runp, int *a_runb)
984263bc
MD
112 */
113int
cd0a65a2 114ntfs_bmap(struct vop_bmap_args *ap)
984263bc
MD
115{
116 dprintf(("ntfs_bmap: vn: %p, blk: %d\n", ap->a_vp,(u_int32_t)ap->a_bn));
54078292
MD
117 if (ap->a_doffsetp != NULL)
118 *ap->a_doffsetp = ap->a_loffset;
984263bc
MD
119 if (ap->a_runp != NULL)
120 *ap->a_runp = 0;
121#if !defined(__NetBSD__)
122 if (ap->a_runb != NULL)
123 *ap->a_runb = 0;
124#endif
125 return (0);
126}
127
cd0a65a2
CP
128/*
129 * ntfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
130 * struct ucred *a_cred)
131 */
984263bc 132static int
cd0a65a2 133ntfs_read(struct vop_read_args *ap)
984263bc 134{
34ee9825
RG
135 struct vnode *vp = ap->a_vp;
136 struct fnode *fp = VTOF(vp);
137 struct ntnode *ip = FTONT(fp);
984263bc
MD
138 struct uio *uio = ap->a_uio;
139 struct ntfsmount *ntmp = ip->i_mp;
140 struct buf *bp;
141 daddr_t cn;
142 int resid, off, toread;
143 int error;
144
e54488bb
MD
145 dprintf(("ntfs_read: ino: %d, off: %d resid: %ld, segflg: %d\n",
146 ip->i_number, (u_int32_t)uio->uio_offset,
147 uio->uio_resid, uio->uio_segflg));
984263bc
MD
148
149 dprintf(("ntfs_read: filesize: %d",(u_int32_t)fp->f_size));
150
151 /* don't allow reading after end of file */
152 if (uio->uio_offset > fp->f_size)
153 return (0);
154
e54488bb 155 resid = (int)szmin(uio->uio_resid, fp->f_size - uio->uio_offset);
984263bc
MD
156
157 dprintf((", resid: %d\n", resid));
158
159 error = 0;
160 while (resid) {
161 cn = ntfs_btocn(uio->uio_offset);
162 off = ntfs_btocnoff(uio->uio_offset);
163
164 toread = min(off + resid, ntfs_cntob(1));
165
54078292 166 error = bread(vp, ntfs_cntodoff(cn), ntfs_cntob(1), &bp);
984263bc
MD
167 if (error) {
168 brelse(bp);
169 break;
170 }
171
68cdd773 172 error = uiomovebp(bp, bp->b_data + off, toread - off, uio);
984263bc
MD
173 if(error) {
174 brelse(bp);
175 break;
176 }
177 brelse(bp);
178
179 resid -= toread - off;
180 }
181
182 return (error);
183}
184
08c23b99 185#if !defined(__DragonFly__)
984263bc 186
cd0a65a2 187/*
31bd717a 188 * ntfs_bypass()
cd0a65a2 189 */
984263bc 190static int
cd0a65a2 191ntfs_bypass(struct vop_generic_args *ap)
984263bc
MD
192{
193 int error = ENOTTY;
31bd717a 194 dprintf(("ntfs_bypass: %s\n", ap->a_desc->sd_name));
984263bc
MD
195 return (error);
196}
197
198#endif
199
cd0a65a2 200/*
b478fdce 201 * ntfs_getattr(struct vnode *a_vp, struct vattr *a_vap)
cd0a65a2 202 */
984263bc 203static int
cd0a65a2 204ntfs_getattr(struct vop_getattr_args *ap)
984263bc 205{
34ee9825
RG
206 struct vnode *vp = ap->a_vp;
207 struct fnode *fp = VTOF(vp);
208 struct ntnode *ip = FTONT(fp);
209 struct vattr *vap = ap->a_vap;
984263bc
MD
210
211 dprintf(("ntfs_getattr: %d, flags: %d\n",ip->i_number,ip->i_flag));
212
08c23b99 213#if defined(__DragonFly__)
984263bc
MD
214 vap->va_fsid = dev2udev(ip->i_dev);
215#else /* NetBSD */
216 vap->va_fsid = ip->i_dev;
217#endif
218 vap->va_fileid = ip->i_number;
219 vap->va_mode = ip->i_mp->ntm_mode;
220 vap->va_nlink = ip->i_nlink;
221 vap->va_uid = ip->i_mp->ntm_uid;
222 vap->va_gid = ip->i_mp->ntm_gid;
0e9b9130
MD
223 vap->va_rmajor = VNOVAL;
224 vap->va_rminor = VNOVAL;
984263bc
MD
225 vap->va_size = fp->f_size;
226 vap->va_bytes = fp->f_allocated;
227 vap->va_atime = ntfs_nttimetounix(fp->f_times.t_access);
228 vap->va_mtime = ntfs_nttimetounix(fp->f_times.t_write);
229 vap->va_ctime = ntfs_nttimetounix(fp->f_times.t_create);
230 vap->va_flags = ip->i_flag;
231 vap->va_gen = 0;
232 vap->va_blocksize = ip->i_mp->ntm_spc * ip->i_mp->ntm_bps;
233 vap->va_type = vp->v_type;
234 vap->va_filerev = 0;
235 return (0);
236}
237
238
239/*
240 * Last reference to an ntnode. If necessary, write or delete it.
cd0a65a2
CP
241 *
242 * ntfs_inactive(struct vnode *a_vp)
984263bc
MD
243 */
244int
cd0a65a2 245ntfs_inactive(struct vop_inactive_args *ap)
984263bc 246{
34ee9825 247 struct vnode *vp = ap->a_vp;
984263bc 248#ifdef NTFS_DEBUG
34ee9825 249 struct ntnode *ip = VTONT(vp);
984263bc
MD
250#endif
251
252 dprintf(("ntfs_inactive: vnode: %p, ntnode: %d\n", vp, ip->i_number));
253
3c37c940 254 if (ntfs_prtactive && vp->v_sysref.refcnt > 1)
984263bc
MD
255 vprint("ntfs_inactive: pushing active", vp);
256
5fd012e0
MD
257 /*
258 * XXX since we don't support any filesystem changes
984263bc
MD
259 * right now, nothing more needs to be done
260 */
261 return (0);
262}
263
264/*
265 * Reclaim an fnode/ntnode so that it can be used for other purposes.
cd0a65a2
CP
266 *
267 * ntfs_reclaim(struct vnode *a_vp)
984263bc
MD
268 */
269int
cd0a65a2 270ntfs_reclaim(struct vop_reclaim_args *ap)
984263bc 271{
34ee9825
RG
272 struct vnode *vp = ap->a_vp;
273 struct fnode *fp = VTOF(vp);
274 struct ntnode *ip = FTONT(fp);
984263bc
MD
275 int error;
276
277 dprintf(("ntfs_reclaim: vnode: %p, ntnode: %d\n", vp, ip->i_number));
278
3c37c940 279 if (ntfs_prtactive && vp->v_sysref.refcnt > 1)
984263bc
MD
280 vprint("ntfs_reclaim: pushing active", vp);
281
282 if ((error = ntfs_ntget(ip)) != 0)
283 return (error);
284
984263bc
MD
285 ntfs_frele(fp);
286 ntfs_ntput(ip);
287 vp->v_data = NULL;
288
289 return (0);
290}
291
cd0a65a2
CP
292/*
293 * ntfs_print(struct vnode *a_vp)
294 */
984263bc 295static int
cd0a65a2 296ntfs_print(struct vop_print_args *ap)
984263bc
MD
297{
298 return (0);
299}
300
301/*
302 * Calculate the logical to physical mapping if not done already,
303 * then call the device strategy routine.
cd0a65a2 304 *
81b5c339 305 * ntfs_strategy(struct vnode *a_vp, struct bio *a_bio)
984263bc
MD
306 */
307int
cd0a65a2 308ntfs_strategy(struct vop_strategy_args *ap)
984263bc 309{
81b5c339
MD
310 struct bio *bio = ap->a_bio;
311 struct buf *bp = bio->bio_buf;
312 struct vnode *vp = ap->a_vp;
34ee9825
RG
313 struct fnode *fp = VTOF(vp);
314 struct ntnode *ip = FTONT(fp);
984263bc 315 struct ntfsmount *ntmp = ip->i_mp;
10f3fee5
MD
316 u_int32_t toread;
317 u_int32_t towrite;
318 size_t tmp;
984263bc
MD
319 int error;
320
54078292
MD
321 dprintf(("ntfs_strategy: loffset: %lld, doffset: %lld\n",
322 bp->b_loffset, bio->bio_offset));
984263bc
MD
323
324 dprintf(("strategy: bcount: %d flags: 0x%lx\n",
325 (u_int32_t)bp->b_bcount,bp->b_flags));
326
10f3fee5 327 bp->b_error = 0;
984263bc 328
10f3fee5
MD
329 switch(bp->b_cmd) {
330 case BUF_CMD_READ:
54078292 331 if (bio->bio_offset >= fp->f_size) {
984263bc
MD
332 clrbuf(bp);
333 error = 0;
334 } else {
335 toread = min(bp->b_bcount,
54078292 336 fp->f_size - bio->bio_offset);
984263bc
MD
337 dprintf(("ntfs_strategy: toread: %d, fsize: %d\n",
338 toread,(u_int32_t)fp->f_size));
339
340 error = ntfs_readattr(ntmp, ip, fp->f_attrtype,
54078292 341 fp->f_attrname, bio->bio_offset,
984263bc
MD
342 toread, bp->b_data, NULL);
343
344 if (error) {
086c1d7e 345 kprintf("ntfs_strategy: ntfs_readattr failed\n");
984263bc
MD
346 bp->b_error = error;
347 bp->b_flags |= B_ERROR;
348 }
349
350 bzero(bp->b_data + toread, bp->b_bcount - toread);
351 }
10f3fee5
MD
352 break;
353 case BUF_CMD_WRITE:
54078292 354 if (bio->bio_offset + bp->b_bcount >= fp->f_size) {
086c1d7e 355 kprintf("ntfs_strategy: CAN'T EXTEND FILE\n");
984263bc
MD
356 bp->b_error = error = EFBIG;
357 bp->b_flags |= B_ERROR;
358 } else {
359 towrite = min(bp->b_bcount,
54078292 360 fp->f_size - bio->bio_offset);
984263bc
MD
361 dprintf(("ntfs_strategy: towrite: %d, fsize: %d\n",
362 towrite,(u_int32_t)fp->f_size));
363
364 error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype,
54078292 365 fp->f_attrname, bio->bio_offset,towrite,
984263bc
MD
366 bp->b_data, &tmp, NULL);
367
368 if (error) {
086c1d7e 369 kprintf("ntfs_strategy: ntfs_writeattr fail\n");
984263bc
MD
370 bp->b_error = error;
371 bp->b_flags |= B_ERROR;
372 }
373 }
10f3fee5
MD
374 break;
375 default:
376 panic("ntfs: bad b_cmd %d\n", bp->b_cmd);
984263bc 377 }
81b5c339 378 biodone(bio);
984263bc
MD
379 return (error);
380}
381
cd0a65a2
CP
382/*
383 * ntfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
384 * struct ucred *a_cred)
385 */
984263bc 386static int
cd0a65a2 387ntfs_write(struct vop_write_args *ap)
984263bc 388{
34ee9825
RG
389 struct vnode *vp = ap->a_vp;
390 struct fnode *fp = VTOF(vp);
391 struct ntnode *ip = FTONT(fp);
984263bc
MD
392 struct uio *uio = ap->a_uio;
393 struct ntfsmount *ntmp = ip->i_mp;
e54488bb 394 size_t towrite;
984263bc
MD
395 size_t written;
396 int error;
397
e54488bb
MD
398 dprintf(("ntfs_write: ino: %d, off: %d resid: %ld, segflg: %d\n",
399 ip->i_number, (u_int32_t)uio->uio_offset,
400 uio->uio_resid, uio->uio_segflg));
984263bc
MD
401 dprintf(("ntfs_write: filesize: %d",(u_int32_t)fp->f_size));
402
403 if (uio->uio_resid + uio->uio_offset > fp->f_size) {
086c1d7e 404 kprintf("ntfs_write: CAN'T WRITE BEYOND END OF FILE\n");
984263bc
MD
405 return (EFBIG);
406 }
e54488bb
MD
407 if (uio->uio_offset > fp->f_size)
408 return (EFBIG);
984263bc 409
e54488bb 410 towrite = szmin(uio->uio_resid, fp->f_size - uio->uio_offset);
984263bc 411
e54488bb 412 dprintf((", towrite: %ld\n", towrite));
984263bc
MD
413
414 error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype,
415 fp->f_attrname, uio->uio_offset, towrite, NULL, &written, uio);
416#ifdef NTFS_DEBUG
417 if (error)
086c1d7e 418 kprintf("ntfs_write: ntfs_writeattr failed: %d\n", error);
984263bc
MD
419#endif
420
421 return (error);
422}
423
cd0a65a2 424/*
b478fdce 425 * ntfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
cd0a65a2 426 */
984263bc 427int
cd0a65a2 428ntfs_access(struct vop_access_args *ap)
984263bc
MD
429{
430 struct vnode *vp = ap->a_vp;
431 struct ntnode *ip = VTONT(vp);
432 struct ucred *cred = ap->a_cred;
433 mode_t mask, mode = ap->a_mode;
34ee9825 434 gid_t *gp;
984263bc
MD
435 int i;
436#ifdef QUOTA
437 int error;
438#endif
439
440 dprintf(("ntfs_access: %d\n",ip->i_number));
441
442 /*
443 * Disallow write attempts on read-only file systems;
444 * unless the file is a socket, fifo, or a block or
445 * character device resident on the file system.
446 */
447 if (mode & VWRITE) {
448 switch ((int)vp->v_type) {
449 case VDIR:
450 case VLNK:
451 case VREG:
452 if (vp->v_mount->mnt_flag & MNT_RDONLY)
453 return (EROFS);
454#ifdef QUOTA
455 if (error = getinoquota(ip))
456 return (error);
457#endif
458 break;
459 }
460 }
461
462 /* Otherwise, user id 0 always gets access. */
463 if (cred->cr_uid == 0)
464 return (0);
465
466 mask = 0;
467
468 /* Otherwise, check the owner. */
469 if (cred->cr_uid == ip->i_mp->ntm_uid) {
470 if (mode & VEXEC)
471 mask |= S_IXUSR;
472 if (mode & VREAD)
473 mask |= S_IRUSR;
474 if (mode & VWRITE)
475 mask |= S_IWUSR;
476 return ((ip->i_mp->ntm_mode & mask) == mask ? 0 : EACCES);
477 }
478
479 /* Otherwise, check the groups. */
480 for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
481 if (ip->i_mp->ntm_gid == *gp) {
482 if (mode & VEXEC)
483 mask |= S_IXGRP;
484 if (mode & VREAD)
485 mask |= S_IRGRP;
486 if (mode & VWRITE)
487 mask |= S_IWGRP;
488 return ((ip->i_mp->ntm_mode&mask) == mask ? 0 : EACCES);
489 }
490
491 /* Otherwise, check everyone else. */
492 if (mode & VEXEC)
493 mask |= S_IXOTH;
494 if (mode & VREAD)
495 mask |= S_IROTH;
496 if (mode & VWRITE)
497 mask |= S_IWOTH;
498 return ((ip->i_mp->ntm_mode & mask) == mask ? 0 : EACCES);
499}
500
501/*
502 * Open called.
503 *
504 * Nothing to do.
cd0a65a2
CP
505 *
506 * ntfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
b478fdce 507 * struct file *a_fp)
984263bc
MD
508 */
509/* ARGSUSED */
510static int
cd0a65a2 511ntfs_open(struct vop_open_args *ap)
984263bc 512{
8ddc6004 513 return (vop_stdopen(ap));
984263bc
MD
514}
515
516/*
517 * Close called.
518 *
519 * Update the times on the inode.
cd0a65a2 520 *
b478fdce 521 * ntfs_close(struct vnode *a_vp, int a_fflag)
984263bc
MD
522 */
523/* ARGSUSED */
524static int
cd0a65a2 525ntfs_close(struct vop_close_args *ap)
984263bc
MD
526{
527#if NTFS_DEBUG
34ee9825
RG
528 struct vnode *vp = ap->a_vp;
529 struct ntnode *ip = VTONT(vp);
984263bc 530
086c1d7e 531 kprintf("ntfs_close: %d\n",ip->i_number);
984263bc
MD
532#endif
533
8ddc6004 534 return (vop_stdclose(ap));
984263bc
MD
535}
536
cd0a65a2
CP
537/*
538 * ntfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
84009d92 539 * int *a_ncookies, off_t **cookies)
cd0a65a2 540 */
984263bc 541int
cd0a65a2 542ntfs_readdir(struct vop_readdir_args *ap)
984263bc 543{
34ee9825
RG
544 struct vnode *vp = ap->a_vp;
545 struct fnode *fp = VTOF(vp);
546 struct ntnode *ip = FTONT(fp);
984263bc
MD
547 struct uio *uio = ap->a_uio;
548 struct ntfsmount *ntmp = ip->i_mp;
23731001
AP
549 int i, j, error = 0;
550 wchar c;
7bf50a33 551 u_int32_t faked = 0, num, off;
984263bc 552 int ncookies = 0;
7bf50a33 553 char convname[NTFS_MAXFILENAME + 1];
984263bc 554
e54488bb
MD
555 dprintf(("ntfs_readdir %d off: %d resid: %ld\n",
556 ip->i_number, (u_int32_t)uio->uio_offset,
557 uio->uio_resid));
984263bc 558
7bf50a33
SS
559 if (uio->uio_offset < 0 || uio->uio_offset > INT_MAX)
560 return (EINVAL);
885ecb13
MD
561 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0)
562 return (error);
984263bc 563
7bf50a33
SS
564 /*
565 * uio->uio_offset carries the number of the entry
566 * where we should start returning dirents.
567 *
568 * We fake up "." if we're not reading the FS root
569 * and we always fake up "..".
570 *
571 * off contains the entry we are starting at,
572 * num increments while we are reading.
573 */
984263bc 574
7bf50a33
SS
575 off = num = uio->uio_offset;
576 faked = (ip->i_number == NTFS_ROOTINO) ? 1 : 2;
984263bc 577
7bf50a33
SS
578 /* Simulate . in every dir except ROOT */
579 if (ip->i_number != NTFS_ROOTINO && num == 0) {
580 if (vop_write_dirent(&error, uio, ip->i_number,
581 DT_DIR, 1, "."))
885ecb13 582 goto done;
7bf50a33 583 if (error)
885ecb13 584 goto done;
7bf50a33
SS
585
586 num++;
587 ncookies++;
984263bc
MD
588 }
589
590 /* Simulate .. in every dir including ROOT */
7bf50a33
SS
591 if (num == faked - 1) {
592 /* XXX NTFS_ROOTINO seems to be wrong here */
593 if (vop_write_dirent(&error, uio, NTFS_ROOTINO,
594 DT_DIR, 2, ".."))
595 goto readdone;
885ecb13
MD
596 if (error)
597 goto done;
984263bc 598
7bf50a33
SS
599 num++;
600 ncookies++;
984263bc
MD
601 }
602
7bf50a33 603 for (;;) {
984263bc
MD
604 struct attr_indexentry *iep;
605
7bf50a33
SS
606 /*
607 * num is the number of the entry we will return,
608 * but ntfs_ntreaddir takes the entry number of the
609 * ntfs directory listing, so subtract the faked
610 * . and .. entries.
611 */
612 error = ntfs_ntreaddir(ntmp, fp, num - faked, &iep);
984263bc 613
885ecb13
MD
614 if (error)
615 goto done;
984263bc
MD
616
617 if( NULL == iep )
618 break;
619
7bf50a33 620 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST);
984263bc
MD
621 iep = NTFS_NEXTREC(iep, struct attr_indexentry *))
622 {
623 if(!ntfs_isnamepermitted(ntmp,iep))
624 continue;
23731001
AP
625 for(i=0, j=0; i < iep->ie_fnamelen; i++, j++) {
626 c = NTFS_U28(iep->ie_fname[i]);
627 if (c&0xFF00)
628 convname[j++] = (char)(c>>8);
629 convname[j] = (char)c&0xFF;
630 }
631 convname[j] = '\0';
7bf50a33
SS
632 if (vop_write_dirent(&error, uio, iep->ie_number,
633 (iep->ie_fflag & NTFS_FFLAG_DIR) ? DT_DIR : DT_REG,
23731001 634 j, convname))
7bf50a33
SS
635 goto readdone;
636
637 dprintf(("ntfs_readdir: elem: %d, fname:[%s] type: %d, "
638 "flag: %d, %s\n",
639 ncookies, convname, iep->ie_fnametype,
640 iep->ie_flag,
641 (iep->ie_fflag & NTFS_FFLAG_DIR) ?
642 "dir" : "reg"));
643
885ecb13
MD
644 if (error)
645 goto done;
984263bc
MD
646
647 ncookies++;
648 num++;
649 }
650 }
651
7bf50a33
SS
652readdone:
653 uio->uio_offset = num;
654
984263bc
MD
655 dprintf(("ntfs_readdir: %d entries (%d bytes) read\n",
656 ncookies,(u_int)(uio->uio_offset - off)));
e54488bb
MD
657 dprintf(("ntfs_readdir: off: %d resid: %ld\n",
658 (u_int32_t)uio->uio_offset, uio->uio_resid));
984263bc
MD
659
660 if (!error && ap->a_ncookies != NULL) {
984263bc
MD
661 off_t *cookies;
662 off_t *cookiep;
984263bc
MD
663
664 ddprintf(("ntfs_readdir: %d cookies\n",ncookies));
665 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
666 panic("ntfs_readdir: unexpected uio from NFS server");
884717e1 667 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
7bf50a33
SS
668 cookiep = cookies;
669 while (off < num)
670 *cookiep++ = ++off;
671
984263bc
MD
672 *ap->a_ncookies = ncookies;
673 *ap->a_cookies = cookies;
674 }
675/*
676 if (ap->a_eofflag)
885ecb13 677 *ap->a_eofflag = VTONT(vp)->i_size <= uio->uio_offset;
984263bc 678*/
885ecb13
MD
679done:
680 vn_unlock(vp);
984263bc
MD
681 return (error);
682}
683
cd0a65a2
CP
684/*
685 * ntfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
686 * struct componentname *a_cnp)
687 */
984263bc 688int
e62afb5f 689ntfs_lookup(struct vop_old_lookup_args *ap)
984263bc 690{
34ee9825
RG
691 struct vnode *dvp = ap->a_dvp;
692 struct ntnode *dip = VTONT(dvp);
984263bc
MD
693 struct ntfsmount *ntmp = dip->i_mp;
694 struct componentname *cnp = ap->a_cnp;
984263bc 695 int error;
2b69e610 696 int lockparent = cnp->cn_flags & CNP_LOCKPARENT;
984263bc 697#if NTFS_DEBUG
2b69e610 698 int wantparent = cnp->cn_flags & (CNP_LOCKPARENT | CNP_WANTPARENT);
984263bc
MD
699#endif
700 dprintf(("ntfs_lookup: \"%.*s\" (%ld bytes) in %d, lp: %d, wp: %d \n",
701 (int)cnp->cn_namelen, cnp->cn_nameptr, cnp->cn_namelen,
702 dip->i_number, lockparent, wantparent));
703
9df3a184
MD
704 *ap->a_vpp = NULL;
705
fad57d0e 706 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
984263bc
MD
707 dprintf(("ntfs_lookup: faking . directory in %d\n",
708 dip->i_number));
709
597aea93 710 vref(dvp);
984263bc
MD
711 *ap->a_vpp = dvp;
712 error = 0;
2b69e610 713 } else if (cnp->cn_flags & CNP_ISDOTDOT) {
984263bc
MD
714 struct ntvattr *vap;
715
716 dprintf(("ntfs_lookup: faking .. directory in %d\n",
717 dip->i_number));
718
719 error = ntfs_ntvattrget(ntmp, dip, NTFS_A_NAME, NULL, 0, &vap);
720 if(error)
721 return (error);
722
acde96db 723 VOP__UNLOCK(dvp, 0);
2b69e610 724 cnp->cn_flags |= CNP_PDIRUNLOCK;
984263bc
MD
725
726 dprintf(("ntfs_lookup: parentdir: %d\n",
727 vap->va_a_name->n_pnumber));
b9b0a6d0 728 error = VFS_VGET(ntmp->ntm_mountp, NULL,
984263bc
MD
729 vap->va_a_name->n_pnumber,ap->a_vpp);
730 ntfs_ntvattrrele(vap);
731 if (error) {
acde96db 732 if (VN_LOCK(dvp, LK_EXCLUSIVE | LK_RETRY) == 0)
2b69e610 733 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
984263bc
MD
734 return (error);
735 }
736
fad57d0e 737 if (lockparent) {
acde96db 738 error = VN_LOCK(dvp, LK_EXCLUSIVE);
984263bc 739 if (error) {
9df3a184
MD
740 vput(*ap->a_vpp);
741 *ap->a_vpp = NULL;
984263bc
MD
742 return (error);
743 }
2b69e610 744 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
984263bc
MD
745 }
746 } else {
747 error = ntfs_ntlookupfile(ntmp, dvp, cnp, ap->a_vpp);
748 if (error) {
749 dprintf(("ntfs_ntlookupfile: returned %d\n", error));
750 return (error);
751 }
752
753 dprintf(("ntfs_lookup: found ino: %d\n",
754 VTONT(*ap->a_vpp)->i_number));
755
09eac123 756 if (!lockparent) {
acde96db 757 VOP__UNLOCK(dvp, 0);
09eac123
AHJ
758 cnp->cn_flags |= CNP_PDIRUNLOCK;
759 }
984263bc 760 }
984263bc
MD
761 return (error);
762}
763
08c23b99 764#if defined(__DragonFly__)
984263bc
MD
765/*
766 * Flush the blocks of a file to disk.
767 *
768 * This function is worthless for vnodes that represent directories. Maybe we
769 * could just do a sync if they try an fsync on a directory file.
cd0a65a2 770 *
b478fdce 771 * ntfs_fsync(struct vnode *a_vp, int a_waitfor)
984263bc
MD
772 */
773static int
cd0a65a2 774ntfs_fsync(struct vop_fsync_args *ap)
984263bc
MD
775{
776 return (0);
777}
778#endif
779
780/*
781 * Return POSIX pathconf information applicable to NTFS filesystem
782 */
783int
66a1ddf5 784ntfs_pathconf(struct vop_pathconf_args *ap)
984263bc 785{
984263bc
MD
786 switch (ap->a_name) {
787 case _PC_LINK_MAX:
788 *ap->a_retval = 1;
789 return (0);
790 case _PC_NAME_MAX:
791 *ap->a_retval = NTFS_MAXFILENAME;
792 return (0);
793 case _PC_PATH_MAX:
794 *ap->a_retval = PATH_MAX;
795 return (0);
796 case _PC_CHOWN_RESTRICTED:
797 *ap->a_retval = 1;
798 return (0);
799 case _PC_NO_TRUNC:
800 *ap->a_retval = 0;
801 return (0);
802#if defined(__NetBSD__)
803 case _PC_SYNC_IO:
804 *ap->a_retval = 1;
805 return (0);
806 case _PC_FILESIZEBITS:
807 *ap->a_retval = 64;
808 return (0);
809#endif
810 default:
811 return (EINVAL);
812 }
813 /* NOTREACHED */
814}
815
816/*
817 * Global vfs data structures
818 */
66a1ddf5
MD
819struct vop_ops ntfs_vnode_vops = {
820 .vop_default = vop_defaultop,
821 .vop_getattr = ntfs_getattr,
822 .vop_inactive = ntfs_inactive,
823 .vop_reclaim = ntfs_reclaim,
824 .vop_print = ntfs_print,
825 .vop_pathconf = ntfs_pathconf,
66a1ddf5
MD
826 .vop_old_lookup = ntfs_lookup,
827 .vop_access = ntfs_access,
828 .vop_close = ntfs_close,
829 .vop_open = ntfs_open,
830 .vop_readdir = ntfs_readdir,
831 .vop_fsync = ntfs_fsync,
832 .vop_bmap = ntfs_bmap,
1787385d
MD
833 .vop_getpages = vop_stdgetpages,
834 .vop_putpages = vop_stdputpages,
66a1ddf5
MD
835 .vop_strategy = ntfs_strategy,
836 .vop_read = ntfs_read,
837 .vop_write = ntfs_write
984263bc
MD
838};
839