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