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