ntfs_vnops.c revision 1.37 1 /* $NetBSD: ntfs_vnops.c,v 1.37 2008/01/17 10:39:14 ad 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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * Id: ntfs_vnops.c,v 1.5 1999/05/12 09:43:06 semenu Exp
35 *
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: ntfs_vnops.c,v 1.37 2008/01/17 10:39:14 ad Exp $");
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/time.h>
45 #include <sys/stat.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
48 #include <sys/namei.h>
49 #include <sys/malloc.h>
50 #include <sys/buf.h>
51 #include <sys/dirent.h>
52 #include <sys/kauth.h>
53
54 #if !defined(__NetBSD__)
55 #include <vm/vm.h>
56 #endif
57
58 #if defined(__FreeBSD__)
59 #include <vm/vnode_pager.h>
60 #endif
61
62 #include <sys/sysctl.h>
63
64
65 #include <fs/ntfs/ntfs.h>
66 #include <fs/ntfs/ntfs_inode.h>
67 #include <fs/ntfs/ntfs_subr.h>
68 #include <miscfs/specfs/specdev.h>
69 #include <miscfs/genfs/genfs.h>
70
71 #include <sys/unistd.h> /* for pathconf(2) constants */
72
73 static int ntfs_bypass(void *);
74 static int ntfs_read(void *);
75 static int ntfs_write(void *);
76 static int ntfs_getattr(void *);
77 static int ntfs_inactive(void *);
78 static int ntfs_print(void *);
79 static int ntfs_reclaim(void *);
80 static int ntfs_strategy(void *);
81 static int ntfs_access(void *);
82 static int ntfs_open(void *);
83 static int ntfs_close(void *);
84 static int ntfs_readdir(void *);
85 static int ntfs_lookup(void *);
86 static int ntfs_bmap(void *);
87 #if defined(__FreeBSD__)
88 static int ntfs_getpages(struct vop_getpages_args *);
89 static int ntfs_putpages(struct vop_putpages_args *);
90 #endif
91 static int ntfs_fsync(void *);
92 static int ntfs_pathconf(void *);
93
94 extern int prtactive;
95
96 #if defined(__FreeBSD__)
97 int
98 ntfs_getpages(ap)
99 struct vop_getpages_args *ap;
100 {
101 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
102 ap->a_reqpage);
103 }
104
105 int
106 ntfs_putpages(ap)
107 struct vop_putpages_args *ap;
108 {
109 return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
110 ap->a_sync, ap->a_rtvals);
111 }
112 #endif
113
114 /*
115 * This is a noop, simply returning what one has been given.
116 */
117 int
118 ntfs_bmap(void *v)
119 {
120 struct vop_bmap_args /* {
121 struct vnode *a_vp;
122 daddr_t a_bn;
123 struct vnode **a_vpp;
124 daddr_t *a_bnp;
125 int *a_runp;
126 int *a_runb;
127 } */ *ap = v;
128 dprintf(("ntfs_bmap: vn: %p, blk: %d\n", ap->a_vp,(u_int32_t)ap->a_bn));
129 if (ap->a_vpp != NULL)
130 *ap->a_vpp = ap->a_vp;
131 if (ap->a_bnp != NULL)
132 *ap->a_bnp = ap->a_bn;
133 if (ap->a_runp != NULL)
134 *ap->a_runp = 0;
135 #if !defined(__NetBSD__)
136 if (ap->a_runb != NULL)
137 *ap->a_runb = 0;
138 #endif
139 return (0);
140 }
141
142 static int
143 ntfs_read(void *v)
144 {
145 struct vop_read_args /* {
146 struct vnode *a_vp;
147 struct uio *a_uio;
148 int a_ioflag;
149 kauth_cred_t a_cred;
150 } */ *ap = v;
151 struct vnode *vp = ap->a_vp;
152 struct fnode *fp = VTOF(vp);
153 struct ntnode *ip = FTONT(fp);
154 struct uio *uio = ap->a_uio;
155 struct ntfsmount *ntmp = ip->i_mp;
156 u_int64_t toread;
157 int error;
158
159 dprintf(("ntfs_read: ino: %llu, off: %qd resid: %qd\n",
160 (unsigned long long)ip->i_number, (long long)uio->uio_offset,
161 (long long)uio->uio_resid));
162
163 dprintf(("ntfs_read: filesize: %qu",(long long)fp->f_size));
164
165 /* don't allow reading after end of file */
166 if (uio->uio_offset > fp->f_size)
167 toread = 0;
168 else
169 toread = MIN(uio->uio_resid, fp->f_size - uio->uio_offset );
170
171 dprintf((", toread: %qu\n",(long long)toread));
172
173 if (toread == 0)
174 return (0);
175
176 error = ntfs_readattr(ntmp, ip, fp->f_attrtype,
177 fp->f_attrname, uio->uio_offset, toread, NULL, uio);
178 if (error) {
179 printf("ntfs_read: ntfs_readattr failed: %d\n",error);
180 return (error);
181 }
182
183 return (0);
184 }
185
186 static int
187 ntfs_bypass(void *v)
188 {
189 struct vop_generic_args /* {
190 struct vnodeop_desc *a_desc;
191 <other random data follows, presumably>
192 } */ *ap __unused = v;
193 int error = ENOTTY;
194 dprintf(("ntfs_bypass: %s\n", ap->a_desc->vdesc_name));
195 return (error);
196 }
197
198
199 static int
200 ntfs_getattr(void *v)
201 {
202 struct vop_getattr_args /* {
203 struct vnode *a_vp;
204 struct vattr *a_vap;
205 kauth_cred_t a_cred;
206 } */ *ap = v;
207 struct vnode *vp = ap->a_vp;
208 struct fnode *fp = VTOF(vp);
209 struct ntnode *ip = FTONT(fp);
210 struct vattr *vap = ap->a_vap;
211
212 dprintf(("ntfs_getattr: %llu, flags: %d\n",
213 (unsigned long long)ip->i_number, ip->i_flag));
214
215 #if defined(__FreeBSD__)
216 vap->va_fsid = dev2udev(ip->i_dev);
217 #else /* NetBSD */
218 vap->va_fsid = ip->i_dev;
219 #endif
220 vap->va_fileid = ip->i_number;
221 vap->va_mode = ip->i_mp->ntm_mode;
222 vap->va_nlink = ip->i_nlink;
223 vap->va_uid = ip->i_mp->ntm_uid;
224 vap->va_gid = ip->i_mp->ntm_gid;
225 vap->va_rdev = 0; /* XXX UNODEV ? */
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.
242 */
243 int
244 ntfs_inactive(void *v)
245 {
246 struct vop_inactive_args /* {
247 struct vnode *a_vp;
248 } */ *ap = v;
249 struct vnode *vp = ap->a_vp;
250 #ifdef NTFS_DEBUG
251 struct ntnode *ip = VTONT(vp);
252 #endif
253
254 dprintf(("ntfs_inactive: vnode: %p, ntnode: %llu\n", vp,
255 (unsigned long long)ip->i_number));
256
257 VOP_UNLOCK(vp, 0);
258
259 /* XXX since we don't support any filesystem changes
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.
267 */
268 int
269 ntfs_reclaim(void *v)
270 {
271 struct vop_reclaim_args /* {
272 struct vnode *a_vp;
273 } */ *ap = v;
274 struct vnode *vp = ap->a_vp;
275 struct fnode *fp = VTOF(vp);
276 struct ntnode *ip = FTONT(fp);
277 int error;
278
279 dprintf(("ntfs_reclaim: vnode: %p, ntnode: %llu\n", vp,
280 (unsigned long long)ip->i_number));
281
282 if (prtactive && vp->v_usecount > 1)
283 vprint("ntfs_reclaim: pushing active", vp);
284
285 if ((error = ntfs_ntget(ip)) != 0)
286 return (error);
287
288 /* Purge old data structures associated with the inode. */
289 cache_purge(vp);
290 if (ip->i_devvp) {
291 vrele(ip->i_devvp);
292 ip->i_devvp = NULL;
293 }
294
295 genfs_node_destroy(vp);
296 ntfs_frele(fp);
297 ntfs_ntput(ip);
298 vp->v_data = NULL;
299
300 return (0);
301 }
302
303 static int
304 ntfs_print(void *v)
305 {
306 struct vop_print_args /* {
307 struct vnode *a_vp;
308 } */ *ap = v;
309 struct ntnode *ip = VTONT(ap->a_vp);
310
311 printf("tag VT_NTFS, ino %llu, flag %#x, usecount %d, nlink %ld\n",
312 (unsigned long long)ip->i_number, ip->i_flag, ip->i_usecount,
313 ip->i_nlink);
314 printf(" ");
315 lockmgr_printinfo(ap->a_vp->v_vnlock);
316 printf("\n");
317 return (0);
318 }
319
320 /*
321 * Calculate the logical to physical mapping if not done already,
322 * then call the device strategy routine.
323 */
324 int
325 ntfs_strategy(void *v)
326 {
327 struct vop_strategy_args /* {
328 struct vnode *a_vp;
329 struct buf *a_bp;
330 } */ *ap = v;
331 struct buf *bp = ap->a_bp;
332 struct vnode *vp = ap->a_vp;
333 struct fnode *fp = VTOF(vp);
334 struct ntnode *ip = FTONT(fp);
335 struct ntfsmount *ntmp = ip->i_mp;
336 int error;
337
338 #ifdef __FreeBSD__
339 dprintf(("ntfs_strategy: offset: %d, blkno: %d, lblkno: %d\n",
340 (u_int32_t)bp->b_offset,(u_int32_t)bp->b_blkno,
341 (u_int32_t)bp->b_lblkno));
342 #else
343 dprintf(("ntfs_strategy: blkno: %d, lblkno: %d\n",
344 (u_int32_t)bp->b_blkno,
345 (u_int32_t)bp->b_lblkno));
346 #endif
347
348 dprintf(("strategy: bcount: %u flags: 0x%x\n",
349 (u_int32_t)bp->b_bcount,bp->b_flags));
350
351 if (bp->b_flags & B_READ) {
352 u_int32_t toread;
353
354 if (ntfs_cntob(bp->b_blkno) >= fp->f_size) {
355 clrbuf(bp);
356 error = 0;
357 } else {
358 toread = MIN(bp->b_bcount,
359 fp->f_size - ntfs_cntob(bp->b_blkno));
360 dprintf(("ntfs_strategy: toread: %d, fsize: %d\n",
361 toread,(u_int32_t)fp->f_size));
362
363 error = ntfs_readattr(ntmp, ip, fp->f_attrtype,
364 fp->f_attrname, ntfs_cntob(bp->b_blkno),
365 toread, bp->b_data, NULL);
366
367 if (error) {
368 printf("ntfs_strategy: ntfs_readattr failed\n");
369 bp->b_error = error;
370 }
371
372 memset((char *)bp->b_data + toread, 0,
373 bp->b_bcount - toread);
374 }
375 } else {
376 size_t tmp;
377 u_int32_t towrite;
378
379 if (ntfs_cntob(bp->b_blkno) + bp->b_bcount >= fp->f_size) {
380 printf("ntfs_strategy: CAN'T EXTEND FILE\n");
381 bp->b_error = error = EFBIG;
382 } else {
383 towrite = MIN(bp->b_bcount,
384 fp->f_size - ntfs_cntob(bp->b_blkno));
385 dprintf(("ntfs_strategy: towrite: %d, fsize: %d\n",
386 towrite,(u_int32_t)fp->f_size));
387
388 error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype,
389 fp->f_attrname, ntfs_cntob(bp->b_blkno),towrite,
390 bp->b_data, &tmp, NULL);
391
392 if (error) {
393 printf("ntfs_strategy: ntfs_writeattr fail\n");
394 bp->b_error = error;
395 }
396 }
397 }
398 biodone(bp);
399 return (error);
400 }
401
402 static int
403 ntfs_write(void *v)
404 {
405 struct vop_write_args /* {
406 struct vnode *a_vp;
407 struct uio *a_uio;
408 int a_ioflag;
409 kauth_cred_t a_cred;
410 } */ *ap = v;
411 struct vnode *vp = ap->a_vp;
412 struct fnode *fp = VTOF(vp);
413 struct ntnode *ip = FTONT(fp);
414 struct uio *uio = ap->a_uio;
415 struct ntfsmount *ntmp = ip->i_mp;
416 u_int64_t towrite;
417 size_t written;
418 int error;
419
420 dprintf(("ntfs_write: ino: %llu, off: %qd resid: %qd\n",
421 (unsigned long long)ip->i_number, (long long)uio->uio_offset,
422 (long long)uio->uio_resid));
423 dprintf(("ntfs_write: filesize: %qu",(long long)fp->f_size));
424
425 if (uio->uio_resid + uio->uio_offset > fp->f_size) {
426 printf("ntfs_write: CAN'T WRITE BEYOND END OF FILE\n");
427 return (EFBIG);
428 }
429
430 towrite = MIN(uio->uio_resid, fp->f_size - uio->uio_offset);
431
432 dprintf((", towrite: %qu\n",(long long)towrite));
433
434 error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype,
435 fp->f_attrname, uio->uio_offset, towrite, NULL, &written, uio);
436 #ifdef NTFS_DEBUG
437 if (error)
438 printf("ntfs_write: ntfs_writeattr failed: %d\n", error);
439 #endif
440
441 return (error);
442 }
443
444 int
445 ntfs_access(void *v)
446 {
447 struct vop_access_args /* {
448 struct vnode *a_vp;
449 int a_mode;
450 kauth_cred_t a_cred;
451 } */ *ap = v;
452 struct vnode *vp = ap->a_vp;
453 struct ntnode *ip = VTONT(vp);
454 kauth_cred_t cred = ap->a_cred;
455 mode_t mask, mode = ap->a_mode;
456 gid_t grp;
457 int i;
458 uint16_t ngroups;
459
460 dprintf(("ntfs_access: %llu\n", (unsigned long long)ip->i_number));
461
462 /*
463 * Disallow write attempts on read-only file systems;
464 * unless the file is a socket, fifo, or a block or
465 * character device resident on the file system.
466 */
467 if (mode & VWRITE) {
468 switch ((int)vp->v_type) {
469 case VDIR:
470 case VLNK:
471 case VREG:
472 if (vp->v_mount->mnt_flag & MNT_RDONLY)
473 return (EROFS);
474 break;
475 }
476 }
477
478 /* Otherwise, user id 0 always gets access. */
479 if (kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL) == 0)
480 return (0);
481
482 mask = 0;
483
484 /* Otherwise, check the owner. */
485 if (kauth_cred_geteuid(cred) == ip->i_mp->ntm_uid) {
486 if (mode & VEXEC)
487 mask |= S_IXUSR;
488 if (mode & VREAD)
489 mask |= S_IRUSR;
490 if (mode & VWRITE)
491 mask |= S_IWUSR;
492 return ((ip->i_mp->ntm_mode & mask) == mask ? 0 : EACCES);
493 }
494
495 /* Otherwise, check the groups. */
496 ngroups = kauth_cred_ngroups(cred);
497 for (i = 0; i < ngroups; i++) {
498 grp = kauth_cred_group(cred, i);
499 if (ip->i_mp->ntm_gid == grp) {
500 if (mode & VEXEC)
501 mask |= S_IXGRP;
502 if (mode & VREAD)
503 mask |= S_IRGRP;
504 if (mode & VWRITE)
505 mask |= S_IWGRP;
506 return ((ip->i_mp->ntm_mode&mask) == mask ? 0 : EACCES);
507 }
508 }
509
510 /* Otherwise, check everyone else. */
511 if (mode & VEXEC)
512 mask |= S_IXOTH;
513 if (mode & VREAD)
514 mask |= S_IROTH;
515 if (mode & VWRITE)
516 mask |= S_IWOTH;
517 return ((ip->i_mp->ntm_mode & mask) == mask ? 0 : EACCES);
518 }
519
520 /*
521 * Open called.
522 *
523 * Nothing to do.
524 */
525 /* ARGSUSED */
526 static int
527 ntfs_open(void *v)
528 {
529 struct vop_open_args /* {
530 struct vnode *a_vp;
531 int a_mode;
532 kauth_cred_t a_cred;
533 } */ *ap __unused = v;
534 #ifdef NTFS_DEBUG
535 struct vnode *vp = ap->a_vp;
536 struct ntnode *ip = VTONT(vp);
537
538 printf("ntfs_open: %llu\n", (unsigned long long)ip->i_number);
539 #endif
540
541 /*
542 * Files marked append-only must be opened for appending.
543 */
544
545 return (0);
546 }
547
548 /*
549 * Close called.
550 *
551 * Update the times on the inode.
552 */
553 /* ARGSUSED */
554 static int
555 ntfs_close(void *v)
556 {
557 struct vop_close_args /* {
558 struct vnode *a_vp;
559 int a_fflag;
560 kauth_cred_t a_cred;
561 } */ *ap __unused = v;
562 #ifdef NTFS_DEBUG
563 struct vnode *vp = ap->a_vp;
564 struct ntnode *ip = VTONT(vp);
565
566 printf("ntfs_close: %llu\n", (unsigned long long)ip->i_number);
567 #endif
568
569 return (0);
570 }
571
572 int
573 ntfs_readdir(void *v)
574 {
575 struct vop_readdir_args /* {
576 struct vnode *a_vp;
577 struct uio *a_uio;
578 kauth_cred_t a_cred;
579 int *a_ncookies;
580 u_int **cookies;
581 } */ *ap = v;
582 struct vnode *vp = ap->a_vp;
583 struct fnode *fp = VTOF(vp);
584 struct ntnode *ip = FTONT(fp);
585 struct uio *uio = ap->a_uio;
586 struct ntfsmount *ntmp = ip->i_mp;
587 int i, error = 0;
588 u_int32_t faked = 0, num;
589 int ncookies = 0;
590 struct dirent *cde;
591 off_t off;
592
593 dprintf(("ntfs_readdir %llu off: %qd resid: %qd\n",
594 (unsigned long long)ip->i_number, (long long)uio->uio_offset,
595 (long long)uio->uio_resid));
596
597 off = uio->uio_offset;
598
599 MALLOC(cde, struct dirent *, sizeof(struct dirent), M_TEMP, M_WAITOK);
600
601 /* Simulate . in every dir except ROOT */
602 if (ip->i_number != NTFS_ROOTINO
603 && uio->uio_offset < sizeof(struct dirent)) {
604 cde->d_fileno = ip->i_number;
605 cde->d_reclen = sizeof(struct dirent);
606 cde->d_type = DT_DIR;
607 cde->d_namlen = 1;
608 strncpy(cde->d_name, ".", 2);
609 error = uiomove((void *)cde, sizeof(struct dirent), uio);
610 if (error)
611 goto out;
612
613 ncookies++;
614 }
615
616 /* Simulate .. in every dir including ROOT */
617 if (uio->uio_offset < 2 * sizeof(struct dirent)) {
618 cde->d_fileno = NTFS_ROOTINO; /* XXX */
619 cde->d_reclen = sizeof(struct dirent);
620 cde->d_type = DT_DIR;
621 cde->d_namlen = 2;
622 strncpy(cde->d_name, "..", 3);
623
624 error = uiomove((void *) cde, sizeof(struct dirent), uio);
625 if (error)
626 goto out;
627
628 ncookies++;
629 }
630
631 faked = (ip->i_number == NTFS_ROOTINO) ? 1 : 2;
632 num = uio->uio_offset / sizeof(struct dirent) - faked;
633
634 while (uio->uio_resid >= sizeof(struct dirent)) {
635 struct attr_indexentry *iep;
636 char *fname;
637 size_t remains;
638 int sz;
639
640 error = ntfs_ntreaddir(ntmp, fp, num, &iep);
641 if (error)
642 goto out;
643
644 if (NULL == iep)
645 break;
646
647 for(; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (uio->uio_resid >= sizeof(struct dirent));
648 iep = NTFS_NEXTREC(iep, struct attr_indexentry *))
649 {
650 if(!ntfs_isnamepermitted(ntmp,iep))
651 continue;
652
653 remains = sizeof(cde->d_name) - 1;
654 fname = cde->d_name;
655 for(i=0; i<iep->ie_fnamelen; i++) {
656 sz = (*ntmp->ntm_wput)(fname, remains,
657 iep->ie_fname[i]);
658 fname += sz;
659 remains -= sz;
660 }
661 *fname = '\0';
662 dprintf(("ntfs_readdir: elem: %d, fname:[%s] type: %d, flag: %d, ",
663 num, cde->d_name, iep->ie_fnametype,
664 iep->ie_flag));
665 cde->d_namlen = fname - (char *) cde->d_name;
666 cde->d_fileno = iep->ie_number;
667 cde->d_type = (iep->ie_fflag & NTFS_FFLAG_DIR) ? DT_DIR : DT_REG;
668 cde->d_reclen = sizeof(struct dirent);
669 dprintf(("%s\n", (cde->d_type == DT_DIR) ? "dir":"reg"));
670
671 error = uiomove((void *)cde, sizeof(struct dirent), uio);
672 if (error)
673 goto out;
674
675 ncookies++;
676 num++;
677 }
678 }
679
680 dprintf(("ntfs_readdir: %d entries (%d bytes) read\n",
681 ncookies,(u_int)(uio->uio_offset - off)));
682 dprintf(("ntfs_readdir: off: %qd resid: %qu\n",
683 (long long)uio->uio_offset,(long long)uio->uio_resid));
684
685 if (!error && ap->a_ncookies != NULL) {
686 struct dirent* dpStart;
687 struct dirent* dp;
688 #if defined(__FreeBSD__)
689 u_long *cookies;
690 u_long *cookiep;
691 #else /* defined(__NetBSD__) */
692 off_t *cookies;
693 off_t *cookiep;
694 #endif
695
696 dprintf(("ntfs_readdir: %d cookies\n",ncookies));
697 dpStart = (struct dirent *)
698 ((char *)uio->uio_iov->iov_base -
699 (uio->uio_offset - off));
700 #if defined(__FreeBSD__)
701 MALLOC(cookies, u_long *, ncookies * sizeof(u_long),
702 M_TEMP, M_WAITOK);
703 #else /* defined(__NetBSD__) */
704 cookies = malloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
705 #endif
706 for (dp = dpStart, cookiep = cookies, i=0;
707 i < ncookies;
708 dp = (struct dirent *)((char *) dp + dp->d_reclen), i++) {
709 off += dp->d_reclen;
710 *cookiep++ = (u_int) off;
711 }
712 *ap->a_ncookies = ncookies;
713 *ap->a_cookies = cookies;
714 }
715 /*
716 if (ap->a_eofflag)
717 *ap->a_eofflag = VTONT(ap->a_vp)->i_size <= uio->uio_offset;
718 */
719 out:
720 FREE(cde, M_TEMP);
721 return (error);
722 }
723
724 int
725 ntfs_lookup(void *v)
726 {
727 struct vop_lookup_args /* {
728 struct vnode *a_dvp;
729 struct vnode **a_vpp;
730 struct componentname *a_cnp;
731 } */ *ap = v;
732 struct vnode *dvp = ap->a_dvp;
733 struct ntnode *dip = VTONT(dvp);
734 struct ntfsmount *ntmp = dip->i_mp;
735 struct componentname *cnp = ap->a_cnp;
736 kauth_cred_t cred = cnp->cn_cred;
737 int error;
738
739 dprintf(("ntfs_lookup: \"%.*s\" (%ld bytes) in %llu\n",
740 (int)cnp->cn_namelen, cnp->cn_nameptr, cnp->cn_namelen,
741 (unsigned long long)dip->i_number));
742
743 error = VOP_ACCESS(dvp, VEXEC, cred);
744 if(error)
745 return (error);
746
747 if ((cnp->cn_flags & ISLASTCN) &&
748 (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
749 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
750 return (EROFS);
751
752 #ifdef __NetBSD__
753 /*
754 * We now have a segment name to search for, and a directory
755 * to search.
756 *
757 * Before tediously performing a linear scan of the directory,
758 * check the name cache to see if the directory/name pair
759 * we are looking for is known already.
760 */
761 if ((error = cache_lookup(ap->a_dvp, ap->a_vpp, cnp)) >= 0)
762 return (error);
763 #endif
764
765 if(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
766 dprintf(("ntfs_lookup: faking . directory in %llu\n",
767 (unsigned long long)dip->i_number));
768
769 VREF(dvp);
770 *ap->a_vpp = dvp;
771 error = 0;
772 } else if (cnp->cn_flags & ISDOTDOT) {
773 struct ntvattr *vap;
774
775 dprintf(("ntfs_lookup: faking .. directory in %llu\n",
776 (unsigned long long)dip->i_number));
777
778 VOP_UNLOCK(dvp, 0);
779 error = ntfs_ntvattrget(ntmp, dip, NTFS_A_NAME, NULL, 0, &vap);
780 if (error) {
781 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
782 return (error);
783 }
784
785 dprintf(("ntfs_lookup: parentdir: %d\n",
786 vap->va_a_name->n_pnumber));
787 error = VFS_VGET(ntmp->ntm_mountp,
788 vap->va_a_name->n_pnumber,ap->a_vpp);
789 ntfs_ntvattrrele(vap);
790 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
791 if (error) {
792 return (error);
793 }
794 } else {
795 error = ntfs_ntlookupfile(ntmp, dvp, cnp, ap->a_vpp);
796 if (error) {
797 dprintf(("ntfs_ntlookupfile: returned %d\n", error));
798 return (error);
799 }
800
801 dprintf(("ntfs_lookup: found ino: %llu\n",
802 (unsigned long long)VTONT(*ap->a_vpp)->i_number));
803 }
804
805 if (cnp->cn_flags & MAKEENTRY)
806 cache_enter(dvp, *ap->a_vpp, cnp);
807
808 return (error);
809 }
810
811 /*
812 * Flush the blocks of a file to disk.
813 *
814 * This function is worthless for vnodes that represent directories. Maybe we
815 * could just do a sync if they try an fsync on a directory file.
816 */
817 static int
818 ntfs_fsync(void *v)
819 {
820 struct vop_fsync_args /* {
821 struct vnode *a_vp;
822 kauth_cred_t a_cred;
823 int a_flags;
824 off_t offlo;
825 off_t offhi;
826 } */ *ap = v;
827 struct vnode *vp = ap->a_vp;
828 int wait;
829
830 if (ap->a_flags & FSYNC_CACHE) {
831 return EOPNOTSUPP;
832 }
833
834 wait = (ap->a_flags & FSYNC_WAIT) != 0;
835 vflushbuf(vp, wait);
836
837 return 0;
838 }
839
840 /*
841 * Return POSIX pathconf information applicable to NTFS filesystem
842 */
843 static int
844 ntfs_pathconf(void *v)
845 {
846 struct vop_pathconf_args /* {
847 struct vnode *a_vp;
848 int a_name;
849 register_t *a_retval;
850 } */ *ap = v;
851
852 switch (ap->a_name) {
853 case _PC_LINK_MAX:
854 *ap->a_retval = 1;
855 return (0);
856 case _PC_NAME_MAX:
857 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_namemax;
858 return (0);
859 case _PC_PATH_MAX:
860 *ap->a_retval = PATH_MAX;
861 return (0);
862 case _PC_CHOWN_RESTRICTED:
863 *ap->a_retval = 1;
864 return (0);
865 case _PC_NO_TRUNC:
866 *ap->a_retval = 0;
867 return (0);
868 case _PC_SYNC_IO:
869 *ap->a_retval = 1;
870 return (0);
871 case _PC_FILESIZEBITS:
872 *ap->a_retval = 64;
873 return (0);
874 default:
875 return (EINVAL);
876 }
877 /* NOTREACHED */
878 }
879
880 /*
881 * Global vfs data structures
882 */
883 vop_t **ntfs_vnodeop_p;
884 #if defined(__FreeBSD__)
885 static
886 struct vnodeopv_entry_desc ntfs_vnodeop_entries[] = {
887 { &vop_default_desc, (vop_t *)ntfs_bypass },
888
889 { &vop_getattr_desc, (vop_t *)ntfs_getattr },
890 { &vop_inactive_desc, (vop_t *)ntfs_inactive },
891 { &vop_reclaim_desc, (vop_t *)ntfs_reclaim },
892 { &vop_print_desc, (vop_t *)ntfs_print },
893 { &vop_pathconf_desc, ntfs_pathconf },
894
895 { &vop_islocked_desc, (vop_t *)vop_stdislocked },
896 { &vop_unlock_desc, (vop_t *)vop_stdunlock },
897 { &vop_lock_desc, (vop_t *)vop_stdlock },
898 { &vop_cachedlookup_desc, (vop_t *)ntfs_lookup },
899 { &vop_lookup_desc, (vop_t *)vfs_cache_lookup },
900
901 { &vop_access_desc, (vop_t *)ntfs_access },
902 { &vop_close_desc, (vop_t *)ntfs_close },
903 { &vop_open_desc, (vop_t *)ntfs_open },
904 { &vop_readdir_desc, (vop_t *)ntfs_readdir },
905 { &vop_fsync_desc, (vop_t *)ntfs_fsync },
906
907 { &vop_bmap_desc, (vop_t *)ntfs_bmap },
908 { &vop_getpages_desc, (vop_t *) ntfs_getpages },
909 { &vop_putpages_desc, (vop_t *) ntfs_putpages },
910 { &vop_strategy_desc, (vop_t *)ntfs_strategy },
911 { &vop_bwrite_desc, (vop_t *)vop_stdbwrite },
912 { &vop_read_desc, (vop_t *)ntfs_read },
913 { &vop_write_desc, (vop_t *)ntfs_write },
914
915 { NULL, NULL }
916 };
917
918 static
919 struct vnodeopv_desc ntfs_vnodeop_opv_desc =
920 { &ntfs_vnodeop_p, ntfs_vnodeop_entries };
921
922 VNODEOP_SET(ntfs_vnodeop_opv_desc);
923
924 #else /* !FreeBSD */
925
926 const struct vnodeopv_entry_desc ntfs_vnodeop_entries[] = {
927 { &vop_default_desc, (vop_t *) ntfs_bypass },
928 { &vop_lookup_desc, (vop_t *) ntfs_lookup }, /* lookup */
929 { &vop_create_desc, genfs_eopnotsupp }, /* create */
930 { &vop_mknod_desc, genfs_eopnotsupp }, /* mknod */
931 { &vop_open_desc, (vop_t *) ntfs_open }, /* open */
932 { &vop_close_desc,(vop_t *) ntfs_close }, /* close */
933 { &vop_access_desc, (vop_t *) ntfs_access }, /* access */
934 { &vop_getattr_desc, (vop_t *) ntfs_getattr }, /* getattr */
935 { &vop_setattr_desc, genfs_eopnotsupp }, /* setattr */
936 { &vop_read_desc, (vop_t *) ntfs_read }, /* read */
937 { &vop_write_desc, (vop_t *) ntfs_write }, /* write */
938 { &vop_lease_desc, genfs_lease_check }, /* lease */
939 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */
940 { &vop_ioctl_desc, genfs_enoioctl }, /* ioctl */
941 { &vop_poll_desc, genfs_poll }, /* poll */
942 { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */
943 { &vop_revoke_desc, genfs_revoke }, /* revoke */
944 { &vop_mmap_desc, genfs_mmap }, /* mmap */
945 { &vop_fsync_desc, (vop_t *) ntfs_fsync }, /* fsync */
946 { &vop_seek_desc, genfs_seek }, /* seek */
947 { &vop_remove_desc, genfs_eopnotsupp }, /* remove */
948 { &vop_link_desc, genfs_eopnotsupp }, /* link */
949 { &vop_rename_desc, genfs_eopnotsupp }, /* rename */
950 { &vop_mkdir_desc, genfs_eopnotsupp }, /* mkdir */
951 { &vop_rmdir_desc, genfs_eopnotsupp }, /* rmdir */
952 { &vop_symlink_desc, genfs_eopnotsupp }, /* symlink */
953 { &vop_readdir_desc, (vop_t *) ntfs_readdir }, /* readdir */
954 { &vop_readlink_desc, genfs_eopnotsupp }, /* readlink */
955 { &vop_abortop_desc, genfs_abortop }, /* abortop */
956 { &vop_inactive_desc, (vop_t *) ntfs_inactive }, /* inactive */
957 { &vop_reclaim_desc, (vop_t *) ntfs_reclaim }, /* reclaim */
958 { &vop_lock_desc, genfs_lock }, /* lock */
959 { &vop_unlock_desc, genfs_unlock }, /* unlock */
960 { &vop_bmap_desc, (vop_t *) ntfs_bmap }, /* bmap */
961 { &vop_strategy_desc, (vop_t *) ntfs_strategy }, /* strategy */
962 { &vop_print_desc, (vop_t *) ntfs_print }, /* print */
963 { &vop_islocked_desc, genfs_islocked }, /* islocked */
964 { &vop_pathconf_desc, ntfs_pathconf }, /* pathconf */
965 { &vop_advlock_desc, genfs_nullop }, /* advlock */
966 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
967 { &vop_getpages_desc, genfs_compat_getpages }, /* getpages */
968 { &vop_putpages_desc, genfs_putpages }, /* putpages */
969 { NULL, NULL }
970 };
971 const struct vnodeopv_desc ntfs_vnodeop_opv_desc =
972 { &ntfs_vnodeop_p, ntfs_vnodeop_entries };
973
974 #endif
975