1 /* $NetBSD: ntfs_vnops.c,v 1.23 1999/10/31 19:45:27 jdolecek Exp $ */
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * John Heidemann of the UCLA Ficus project.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
38 * $FreeBSD: src/sys/ntfs/ntfs_vnops.c,v 1.9.2.4 2002/08/06 19:35:18 semenu Exp $
39 * $DragonFly: src/sys/vfs/ntfs/ntfs_vnops.c,v 1.44 2007/11/20 21:03:50 dillon Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
47 #include <sys/types.h>
49 #include <sys/vnode.h>
50 #include <sys/mount.h>
52 #include <sys/namei.h>
53 #include <sys/malloc.h>
55 #include <sys/dirent.h>
56 #include <machine/limits.h>
59 #include <vm/vm_param.h>
60 #if defined(__NetBSD__)
61 #include <vm/vm_prot.h>
63 #include <vm/vm_page.h>
64 #include <vm/vm_object.h>
65 #include <vm/vm_pager.h>
66 #if defined(__DragonFly__)
67 #include <vm/vnode_pager.h>
69 #include <vm/vm_extern.h>
71 #include <sys/sysctl.h>
75 /*#define NTFS_DEBUG 1*/
77 #include "ntfs_inode.h"
78 #include "ntfs_subr.h"
79 #if defined(__NetBSD__)
80 #include <miscfs/specfs/specdev.h>
81 #include <miscfs/genfs/genfs.h>
84 #include <sys/unistd.h> /* for pathconf(2) constants */
86 static int ntfs_read (struct vop_read_args *);
87 static int ntfs_write (struct vop_write_args *ap);
88 static int ntfs_getattr (struct vop_getattr_args *ap);
89 static int ntfs_inactive (struct vop_inactive_args *ap);
90 static int ntfs_print (struct vop_print_args *ap);
91 static int ntfs_reclaim (struct vop_reclaim_args *ap);
92 static int ntfs_strategy (struct vop_strategy_args *ap);
93 static int ntfs_access (struct vop_access_args *ap);
94 static int ntfs_open (struct vop_open_args *ap);
95 static int ntfs_close (struct vop_close_args *ap);
96 static int ntfs_readdir (struct vop_readdir_args *ap);
97 static int ntfs_lookup (struct vop_old_lookup_args *ap);
98 static int ntfs_bmap (struct vop_bmap_args *ap);
99 #if defined(__DragonFly__)
100 static int ntfs_fsync (struct vop_fsync_args *ap);
102 static int ntfs_bypass (struct vop_generic_args *);
104 static int ntfs_pathconf (struct vop_pathconf_args *);
106 int ntfs_prtactive = 1; /* 1 => print out reclaim of active vnodes */
109 * This is a noop, simply returning what one has been given.
111 * ntfs_bmap(struct vnode *a_vp, off_t a_loffset,
112 * daddr_t *a_doffsetp, int *a_runp, int *a_runb)
115 ntfs_bmap(struct vop_bmap_args *ap)
117 dprintf(("ntfs_bmap: vn: %p, blk: %d\n", ap->a_vp,(u_int32_t)ap->a_bn));
118 if (ap->a_doffsetp != NULL)
119 *ap->a_doffsetp = ap->a_loffset;
120 if (ap->a_runp != NULL)
122 #if !defined(__NetBSD__)
123 if (ap->a_runb != NULL)
130 * ntfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
131 * struct ucred *a_cred)
134 ntfs_read(struct vop_read_args *ap)
136 struct vnode *vp = ap->a_vp;
137 struct fnode *fp = VTOF(vp);
138 struct ntnode *ip = FTONT(fp);
139 struct uio *uio = ap->a_uio;
140 struct ntfsmount *ntmp = ip->i_mp;
143 int resid, off, toread;
146 dprintf(("ntfs_read: ino: %d, off: %d resid: %ld, segflg: %d\n",
147 ip->i_number, (u_int32_t)uio->uio_offset,
148 uio->uio_resid, uio->uio_segflg));
150 dprintf(("ntfs_read: filesize: %d",(u_int32_t)fp->f_size));
152 /* don't allow reading after end of file */
153 if (uio->uio_offset > fp->f_size)
156 resid = (int)szmin(uio->uio_resid, fp->f_size - uio->uio_offset);
158 dprintf((", resid: %d\n", resid));
162 cn = ntfs_btocn(uio->uio_offset);
163 off = ntfs_btocnoff(uio->uio_offset);
165 toread = min(off + resid, ntfs_cntob(1));
167 error = bread(vp, ntfs_cntodoff(cn), ntfs_cntob(1), &bp);
173 error = uiomove(bp->b_data + off, toread - off, uio);
180 resid -= toread - off;
186 #if !defined(__DragonFly__)
192 ntfs_bypass(struct vop_generic_args *ap)
195 dprintf(("ntfs_bypass: %s\n", ap->a_desc->sd_name));
202 * ntfs_getattr(struct vnode *a_vp, struct vattr *a_vap)
205 ntfs_getattr(struct vop_getattr_args *ap)
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;
212 dprintf(("ntfs_getattr: %d, flags: %d\n",ip->i_number,ip->i_flag));
214 #if defined(__DragonFly__)
215 vap->va_fsid = dev2udev(ip->i_dev);
217 vap->va_fsid = ip->i_dev;
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;
224 vap->va_rmajor = VNOVAL;
225 vap->va_rminor = VNOVAL;
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;
233 vap->va_blocksize = ip->i_mp->ntm_spc * ip->i_mp->ntm_bps;
234 vap->va_type = vp->v_type;
241 * Last reference to an ntnode. If necessary, write or delete it.
243 * ntfs_inactive(struct vnode *a_vp)
246 ntfs_inactive(struct vop_inactive_args *ap)
248 struct vnode *vp = ap->a_vp;
250 struct ntnode *ip = VTONT(vp);
253 dprintf(("ntfs_inactive: vnode: %p, ntnode: %d\n", vp, ip->i_number));
255 if (ntfs_prtactive && vp->v_sysref.refcnt > 1)
256 vprint("ntfs_inactive: pushing active", vp);
259 * XXX since we don't support any filesystem changes
260 * right now, nothing more needs to be done
266 * Reclaim an fnode/ntnode so that it can be used for other purposes.
268 * ntfs_reclaim(struct vnode *a_vp)
271 ntfs_reclaim(struct vop_reclaim_args *ap)
273 struct vnode *vp = ap->a_vp;
274 struct fnode *fp = VTOF(vp);
275 struct ntnode *ip = FTONT(fp);
278 dprintf(("ntfs_reclaim: vnode: %p, ntnode: %d\n", vp, ip->i_number));
280 if (ntfs_prtactive && vp->v_sysref.refcnt > 1)
281 vprint("ntfs_reclaim: pushing active", vp);
283 if ((error = ntfs_ntget(ip)) != 0)
294 * ntfs_print(struct vnode *a_vp)
297 ntfs_print(struct vop_print_args *ap)
303 * Calculate the logical to physical mapping if not done already,
304 * then call the device strategy routine.
306 * ntfs_strategy(struct vnode *a_vp, struct bio *a_bio)
309 ntfs_strategy(struct vop_strategy_args *ap)
311 struct bio *bio = ap->a_bio;
312 struct buf *bp = bio->bio_buf;
313 struct vnode *vp = ap->a_vp;
314 struct fnode *fp = VTOF(vp);
315 struct ntnode *ip = FTONT(fp);
316 struct ntfsmount *ntmp = ip->i_mp;
322 dprintf(("ntfs_strategy: loffset: %lld, doffset: %lld\n",
323 bp->b_loffset, bio->bio_offset));
325 dprintf(("strategy: bcount: %d flags: 0x%lx\n",
326 (u_int32_t)bp->b_bcount,bp->b_flags));
332 if (bio->bio_offset >= fp->f_size) {
336 toread = min(bp->b_bcount,
337 fp->f_size - bio->bio_offset);
338 dprintf(("ntfs_strategy: toread: %d, fsize: %d\n",
339 toread,(u_int32_t)fp->f_size));
341 error = ntfs_readattr(ntmp, ip, fp->f_attrtype,
342 fp->f_attrname, bio->bio_offset,
343 toread, bp->b_data, NULL);
346 kprintf("ntfs_strategy: ntfs_readattr failed\n");
348 bp->b_flags |= B_ERROR;
351 bzero(bp->b_data + toread, bp->b_bcount - toread);
355 if (bio->bio_offset + bp->b_bcount >= fp->f_size) {
356 kprintf("ntfs_strategy: CAN'T EXTEND FILE\n");
357 bp->b_error = error = EFBIG;
358 bp->b_flags |= B_ERROR;
360 towrite = min(bp->b_bcount,
361 fp->f_size - bio->bio_offset);
362 dprintf(("ntfs_strategy: towrite: %d, fsize: %d\n",
363 towrite,(u_int32_t)fp->f_size));
365 error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype,
366 fp->f_attrname, bio->bio_offset,towrite,
367 bp->b_data, &tmp, NULL);
370 kprintf("ntfs_strategy: ntfs_writeattr fail\n");
372 bp->b_flags |= B_ERROR;
377 panic("ntfs: bad b_cmd %d\n", bp->b_cmd);
384 * ntfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
385 * struct ucred *a_cred)
388 ntfs_write(struct vop_write_args *ap)
390 struct vnode *vp = ap->a_vp;
391 struct fnode *fp = VTOF(vp);
392 struct ntnode *ip = FTONT(fp);
393 struct uio *uio = ap->a_uio;
394 struct ntfsmount *ntmp = ip->i_mp;
399 dprintf(("ntfs_write: ino: %d, off: %d resid: %ld, segflg: %d\n",
400 ip->i_number, (u_int32_t)uio->uio_offset,
401 uio->uio_resid, uio->uio_segflg));
402 dprintf(("ntfs_write: filesize: %d",(u_int32_t)fp->f_size));
404 if (uio->uio_resid + uio->uio_offset > fp->f_size) {
405 kprintf("ntfs_write: CAN'T WRITE BEYOND END OF FILE\n");
408 if (uio->uio_offset > fp->f_size)
411 towrite = szmin(uio->uio_resid, fp->f_size - uio->uio_offset);
413 dprintf((", towrite: %ld\n", towrite));
415 error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype,
416 fp->f_attrname, uio->uio_offset, towrite, NULL, &written, uio);
419 kprintf("ntfs_write: ntfs_writeattr failed: %d\n", error);
426 * ntfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
429 ntfs_access(struct vop_access_args *ap)
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;
441 dprintf(("ntfs_access: %d\n",ip->i_number));
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.
449 switch ((int)vp->v_type) {
453 if (vp->v_mount->mnt_flag & MNT_RDONLY)
456 if (error = getinoquota(ip))
463 /* Otherwise, user id 0 always gets access. */
464 if (cred->cr_uid == 0)
469 /* Otherwise, check the owner. */
470 if (cred->cr_uid == ip->i_mp->ntm_uid) {
477 return ((ip->i_mp->ntm_mode & mask) == mask ? 0 : EACCES);
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) {
489 return ((ip->i_mp->ntm_mode&mask) == mask ? 0 : EACCES);
492 /* Otherwise, check everyone else. */
499 return ((ip->i_mp->ntm_mode & mask) == mask ? 0 : EACCES);
507 * ntfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
512 ntfs_open(struct vop_open_args *ap)
514 return (vop_stdopen(ap));
520 * Update the times on the inode.
522 * ntfs_close(struct vnode *a_vp, int a_fflag)
526 ntfs_close(struct vop_close_args *ap)
529 struct vnode *vp = ap->a_vp;
530 struct ntnode *ip = VTONT(vp);
532 kprintf("ntfs_close: %d\n",ip->i_number);
535 return (vop_stdclose(ap));
539 * ntfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
540 * int *a_ncookies, off_t **cookies)
543 ntfs_readdir(struct vop_readdir_args *ap)
545 struct vnode *vp = ap->a_vp;
546 struct fnode *fp = VTOF(vp);
547 struct ntnode *ip = FTONT(fp);
548 struct uio *uio = ap->a_uio;
549 struct ntfsmount *ntmp = ip->i_mp;
552 u_int32_t faked = 0, num, off;
554 char convname[NTFS_MAXFILENAME + 1];
556 dprintf(("ntfs_readdir %d off: %d resid: %ld\n",
557 ip->i_number, (u_int32_t)uio->uio_offset,
560 if (uio->uio_offset < 0 || uio->uio_offset > INT_MAX)
562 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0)
566 * uio->uio_offset carries the number of the entry
567 * where we should start returning dirents.
569 * We fake up "." if we're not reading the FS root
570 * and we always fake up "..".
572 * off contains the entry we are starting at,
573 * num increments while we are reading.
576 off = num = uio->uio_offset;
577 faked = (ip->i_number == NTFS_ROOTINO) ? 1 : 2;
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,
591 /* Simulate .. in every dir including ROOT */
592 if (num == faked - 1) {
593 /* XXX NTFS_ROOTINO seems to be wrong here */
594 if (vop_write_dirent(&error, uio, NTFS_ROOTINO,
605 struct attr_indexentry *iep;
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
613 error = ntfs_ntreaddir(ntmp, fp, num - faked, &iep);
621 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST);
622 iep = NTFS_NEXTREC(iep, struct attr_indexentry *))
624 if(!ntfs_isnamepermitted(ntmp,iep))
626 for(i=0, j=0; i < iep->ie_fnamelen; i++, j++) {
627 c = NTFS_U28(iep->ie_fname[i]);
629 convname[j++] = (char)(c>>8);
630 convname[j] = (char)c&0xFF;
633 if (vop_write_dirent(&error, uio, iep->ie_number,
634 (iep->ie_fflag & NTFS_FFLAG_DIR) ? DT_DIR : DT_REG,
638 dprintf(("ntfs_readdir: elem: %d, fname:[%s] type: %d, "
640 ncookies, convname, iep->ie_fnametype,
642 (iep->ie_fflag & NTFS_FFLAG_DIR) ?
654 uio->uio_offset = num;
656 dprintf(("ntfs_readdir: %d entries (%d bytes) read\n",
657 ncookies,(u_int)(uio->uio_offset - off)));
658 dprintf(("ntfs_readdir: off: %d resid: %ld\n",
659 (u_int32_t)uio->uio_offset, uio->uio_resid));
661 if (!error && ap->a_ncookies != NULL) {
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");
668 MALLOC(cookies, off_t *, ncookies * sizeof(off_t),
674 *ap->a_ncookies = ncookies;
675 *ap->a_cookies = cookies;
679 *ap->a_eofflag = VTONT(vp)->i_size <= uio->uio_offset;
687 * ntfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
688 * struct componentname *a_cnp)
691 ntfs_lookup(struct vop_old_lookup_args *ap)
693 struct vnode *dvp = ap->a_dvp;
694 struct ntnode *dip = VTONT(dvp);
695 struct ntfsmount *ntmp = dip->i_mp;
696 struct componentname *cnp = ap->a_cnp;
698 int lockparent = cnp->cn_flags & CNP_LOCKPARENT;
700 int wantparent = cnp->cn_flags & (CNP_LOCKPARENT | CNP_WANTPARENT);
702 dprintf(("ntfs_lookup: \"%.*s\" (%ld bytes) in %d, lp: %d, wp: %d \n",
703 (int)cnp->cn_namelen, cnp->cn_nameptr, cnp->cn_namelen,
704 dip->i_number, lockparent, wantparent));
708 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
709 dprintf(("ntfs_lookup: faking . directory in %d\n",
715 } else if (cnp->cn_flags & CNP_ISDOTDOT) {
718 dprintf(("ntfs_lookup: faking .. directory in %d\n",
721 error = ntfs_ntvattrget(ntmp, dip, NTFS_A_NAME, NULL, 0, &vap);
726 cnp->cn_flags |= CNP_PDIRUNLOCK;
728 dprintf(("ntfs_lookup: parentdir: %d\n",
729 vap->va_a_name->n_pnumber));
730 error = VFS_VGET(ntmp->ntm_mountp, NULL,
731 vap->va_a_name->n_pnumber,ap->a_vpp);
732 ntfs_ntvattrrele(vap);
734 if (VN_LOCK(dvp, LK_EXCLUSIVE | LK_RETRY) == 0)
735 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
740 error = VN_LOCK(dvp, LK_EXCLUSIVE);
746 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
749 error = ntfs_ntlookupfile(ntmp, dvp, cnp, ap->a_vpp);
751 dprintf(("ntfs_ntlookupfile: returned %d\n", error));
755 dprintf(("ntfs_lookup: found ino: %d\n",
756 VTONT(*ap->a_vpp)->i_number));
764 #if defined(__DragonFly__)
766 * Flush the blocks of a file to disk.
768 * This function is worthless for vnodes that represent directories. Maybe we
769 * could just do a sync if they try an fsync on a directory file.
771 * ntfs_fsync(struct vnode *a_vp, int a_waitfor)
774 ntfs_fsync(struct vop_fsync_args *ap)
781 * Return POSIX pathconf information applicable to NTFS filesystem
784 ntfs_pathconf(struct vop_pathconf_args *ap)
786 switch (ap->a_name) {
791 *ap->a_retval = NTFS_MAXFILENAME;
794 *ap->a_retval = PATH_MAX;
796 case _PC_CHOWN_RESTRICTED:
802 #if defined(__NetBSD__)
806 case _PC_FILESIZEBITS:
817 * Global vfs data structures
819 struct vop_ops ntfs_vnode_vops = {
820 .vop_default = vop_defaultop,
821 .vop_getattr = ntfs_getattr,
822 .vop_inactive = ntfs_inactive,
823 .vop_reclaim = ntfs_reclaim,
824 .vop_print = ntfs_print,
825 .vop_pathconf = ntfs_pathconf,
826 .vop_old_lookup = ntfs_lookup,
827 .vop_access = ntfs_access,
828 .vop_close = ntfs_close,
829 .vop_open = ntfs_open,
830 .vop_readdir = ntfs_readdir,
831 .vop_fsync = ntfs_fsync,
832 .vop_bmap = ntfs_bmap,
833 .vop_getpages = vop_stdgetpages,
834 .vop_putpages = vop_stdputpages,
835 .vop_strategy = ntfs_strategy,
836 .vop_read = ntfs_read,
837 .vop_write = ntfs_write