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