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