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 $
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/types.h>
48 #include <sys/vnode.h>
49 #include <sys/mount.h>
51 #include <sys/namei.h>
52 #include <sys/malloc.h>
54 #include <sys/dirent.h>
55 #include <machine/limits.h>
58 #include <vm/vm_param.h>
59 #if defined(__NetBSD__)
60 #include <vm/vm_prot.h>
62 #include <vm/vm_page.h>
63 #include <vm/vm_object.h>
64 #include <vm/vm_pager.h>
65 #if defined(__DragonFly__)
66 #include <vm/vnode_pager.h>
68 #include <vm/vm_extern.h>
70 #include <sys/sysctl.h>
74 /*#define NTFS_DEBUG 1*/
76 #include "ntfs_inode.h"
77 #include "ntfs_subr.h"
78 #if defined(__NetBSD__)
79 #include <miscfs/specfs/specdev.h>
80 #include <miscfs/genfs/genfs.h>
83 #include <sys/unistd.h> /* for pathconf(2) constants */
85 static int ntfs_read (struct vop_read_args *);
86 static int ntfs_write (struct vop_write_args *ap);
87 static int ntfs_getattr (struct vop_getattr_args *ap);
88 static int ntfs_inactive (struct vop_inactive_args *ap);
89 static int ntfs_print (struct vop_print_args *ap);
90 static int ntfs_reclaim (struct vop_reclaim_args *ap);
91 static int ntfs_strategy (struct vop_strategy_args *ap);
92 static int ntfs_access (struct vop_access_args *ap);
93 static int ntfs_open (struct vop_open_args *ap);
94 static int ntfs_close (struct vop_close_args *ap);
95 static int ntfs_readdir (struct vop_readdir_args *ap);
96 static int ntfs_lookup (struct vop_old_lookup_args *ap);
97 static int ntfs_bmap (struct vop_bmap_args *ap);
98 #if defined(__DragonFly__)
99 static int ntfs_fsync (struct vop_fsync_args *ap);
101 static int ntfs_bypass (struct vop_generic_args *);
103 static int ntfs_pathconf (struct vop_pathconf_args *);
105 int ntfs_prtactive = 1; /* 1 => print out reclaim of active vnodes */
108 * This is a noop, simply returning what one has been given.
110 * ntfs_bmap(struct vnode *a_vp, off_t a_loffset,
111 * daddr_t *a_doffsetp, int *a_runp, int *a_runb)
114 ntfs_bmap(struct vop_bmap_args *ap)
116 dprintf(("ntfs_bmap: vn: %p, blk: %d\n", ap->a_vp,(u_int32_t)ap->a_bn));
117 if (ap->a_doffsetp != NULL)
118 *ap->a_doffsetp = ap->a_loffset;
119 if (ap->a_runp != NULL)
121 #if !defined(__NetBSD__)
122 if (ap->a_runb != NULL)
129 * ntfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
130 * struct ucred *a_cred)
133 ntfs_read(struct vop_read_args *ap)
135 struct vnode *vp = ap->a_vp;
136 struct fnode *fp = VTOF(vp);
137 struct ntnode *ip = FTONT(fp);
138 struct uio *uio = ap->a_uio;
139 struct ntfsmount *ntmp = ip->i_mp;
142 int resid, off, toread;
145 dprintf(("ntfs_read: ino: %d, off: %d resid: %ld, segflg: %d\n",
146 ip->i_number, (u_int32_t)uio->uio_offset,
147 uio->uio_resid, uio->uio_segflg));
149 dprintf(("ntfs_read: filesize: %d",(u_int32_t)fp->f_size));
151 /* don't allow reading after end of file */
152 if (uio->uio_offset > fp->f_size)
155 resid = (int)szmin(uio->uio_resid, fp->f_size - uio->uio_offset);
157 dprintf((", resid: %d\n", resid));
161 cn = ntfs_btocn(uio->uio_offset);
162 off = ntfs_btocnoff(uio->uio_offset);
164 toread = min(off + resid, ntfs_cntob(1));
166 error = bread(vp, ntfs_cntodoff(cn), ntfs_cntob(1), &bp);
172 error = uiomove(bp->b_data + off, toread - off, uio);
179 resid -= toread - off;
185 #if !defined(__DragonFly__)
191 ntfs_bypass(struct vop_generic_args *ap)
194 dprintf(("ntfs_bypass: %s\n", ap->a_desc->sd_name));
201 * ntfs_getattr(struct vnode *a_vp, struct vattr *a_vap)
204 ntfs_getattr(struct vop_getattr_args *ap)
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;
211 dprintf(("ntfs_getattr: %d, flags: %d\n",ip->i_number,ip->i_flag));
213 #if defined(__DragonFly__)
214 vap->va_fsid = dev2udev(ip->i_dev);
216 vap->va_fsid = ip->i_dev;
218 vap->va_fileid = ip->i_number;
219 vap->va_mode = ip->i_mp->ntm_mode;
220 vap->va_nlink = ip->i_nlink;
221 vap->va_uid = ip->i_mp->ntm_uid;
222 vap->va_gid = ip->i_mp->ntm_gid;
223 vap->va_rmajor = VNOVAL;
224 vap->va_rminor = VNOVAL;
225 vap->va_size = fp->f_size;
226 vap->va_bytes = fp->f_allocated;
227 vap->va_atime = ntfs_nttimetounix(fp->f_times.t_access);
228 vap->va_mtime = ntfs_nttimetounix(fp->f_times.t_write);
229 vap->va_ctime = ntfs_nttimetounix(fp->f_times.t_create);
230 vap->va_flags = ip->i_flag;
232 vap->va_blocksize = ip->i_mp->ntm_spc * ip->i_mp->ntm_bps;
233 vap->va_type = vp->v_type;
240 * Last reference to an ntnode. If necessary, write or delete it.
242 * ntfs_inactive(struct vnode *a_vp)
245 ntfs_inactive(struct vop_inactive_args *ap)
247 struct vnode *vp = ap->a_vp;
249 struct ntnode *ip = VTONT(vp);
252 dprintf(("ntfs_inactive: vnode: %p, ntnode: %d\n", vp, ip->i_number));
254 if (ntfs_prtactive && vp->v_sysref.refcnt > 1)
255 vprint("ntfs_inactive: pushing active", vp);
258 * XXX since we don't support any filesystem changes
259 * right now, nothing more needs to be done
265 * Reclaim an fnode/ntnode so that it can be used for other purposes.
267 * ntfs_reclaim(struct vnode *a_vp)
270 ntfs_reclaim(struct vop_reclaim_args *ap)
272 struct vnode *vp = ap->a_vp;
273 struct fnode *fp = VTOF(vp);
274 struct ntnode *ip = FTONT(fp);
277 dprintf(("ntfs_reclaim: vnode: %p, ntnode: %d\n", vp, ip->i_number));
279 if (ntfs_prtactive && vp->v_sysref.refcnt > 1)
280 vprint("ntfs_reclaim: pushing active", vp);
282 if ((error = ntfs_ntget(ip)) != 0)
293 * ntfs_print(struct vnode *a_vp)
296 ntfs_print(struct vop_print_args *ap)
302 * Calculate the logical to physical mapping if not done already,
303 * then call the device strategy routine.
305 * ntfs_strategy(struct vnode *a_vp, struct bio *a_bio)
308 ntfs_strategy(struct vop_strategy_args *ap)
310 struct bio *bio = ap->a_bio;
311 struct buf *bp = bio->bio_buf;
312 struct vnode *vp = ap->a_vp;
313 struct fnode *fp = VTOF(vp);
314 struct ntnode *ip = FTONT(fp);
315 struct ntfsmount *ntmp = ip->i_mp;
321 dprintf(("ntfs_strategy: loffset: %lld, doffset: %lld\n",
322 bp->b_loffset, bio->bio_offset));
324 dprintf(("strategy: bcount: %d flags: 0x%lx\n",
325 (u_int32_t)bp->b_bcount,bp->b_flags));
331 if (bio->bio_offset >= fp->f_size) {
335 toread = min(bp->b_bcount,
336 fp->f_size - bio->bio_offset);
337 dprintf(("ntfs_strategy: toread: %d, fsize: %d\n",
338 toread,(u_int32_t)fp->f_size));
340 error = ntfs_readattr(ntmp, ip, fp->f_attrtype,
341 fp->f_attrname, bio->bio_offset,
342 toread, bp->b_data, NULL);
345 kprintf("ntfs_strategy: ntfs_readattr failed\n");
347 bp->b_flags |= B_ERROR;
350 bzero(bp->b_data + toread, bp->b_bcount - toread);
354 if (bio->bio_offset + bp->b_bcount >= fp->f_size) {
355 kprintf("ntfs_strategy: CAN'T EXTEND FILE\n");
356 bp->b_error = error = EFBIG;
357 bp->b_flags |= B_ERROR;
359 towrite = min(bp->b_bcount,
360 fp->f_size - bio->bio_offset);
361 dprintf(("ntfs_strategy: towrite: %d, fsize: %d\n",
362 towrite,(u_int32_t)fp->f_size));
364 error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype,
365 fp->f_attrname, bio->bio_offset,towrite,
366 bp->b_data, &tmp, NULL);
369 kprintf("ntfs_strategy: ntfs_writeattr fail\n");
371 bp->b_flags |= B_ERROR;
376 panic("ntfs: bad b_cmd %d\n", bp->b_cmd);
383 * ntfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
384 * struct ucred *a_cred)
387 ntfs_write(struct vop_write_args *ap)
389 struct vnode *vp = ap->a_vp;
390 struct fnode *fp = VTOF(vp);
391 struct ntnode *ip = FTONT(fp);
392 struct uio *uio = ap->a_uio;
393 struct ntfsmount *ntmp = ip->i_mp;
398 dprintf(("ntfs_write: ino: %d, off: %d resid: %ld, segflg: %d\n",
399 ip->i_number, (u_int32_t)uio->uio_offset,
400 uio->uio_resid, uio->uio_segflg));
401 dprintf(("ntfs_write: filesize: %d",(u_int32_t)fp->f_size));
403 if (uio->uio_resid + uio->uio_offset > fp->f_size) {
404 kprintf("ntfs_write: CAN'T WRITE BEYOND END OF FILE\n");
407 if (uio->uio_offset > fp->f_size)
410 towrite = szmin(uio->uio_resid, fp->f_size - uio->uio_offset);
412 dprintf((", towrite: %ld\n", towrite));
414 error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype,
415 fp->f_attrname, uio->uio_offset, towrite, NULL, &written, uio);
418 kprintf("ntfs_write: ntfs_writeattr failed: %d\n", error);
425 * ntfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
428 ntfs_access(struct vop_access_args *ap)
430 struct vnode *vp = ap->a_vp;
431 struct ntnode *ip = VTONT(vp);
432 struct ucred *cred = ap->a_cred;
433 mode_t mask, mode = ap->a_mode;
440 dprintf(("ntfs_access: %d\n",ip->i_number));
443 * Disallow write attempts on read-only file systems;
444 * unless the file is a socket, fifo, or a block or
445 * character device resident on the file system.
448 switch ((int)vp->v_type) {
452 if (vp->v_mount->mnt_flag & MNT_RDONLY)
455 if (error = getinoquota(ip))
462 /* Otherwise, user id 0 always gets access. */
463 if (cred->cr_uid == 0)
468 /* Otherwise, check the owner. */
469 if (cred->cr_uid == ip->i_mp->ntm_uid) {
476 return ((ip->i_mp->ntm_mode & mask) == mask ? 0 : EACCES);
479 /* Otherwise, check the groups. */
480 for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
481 if (ip->i_mp->ntm_gid == *gp) {
488 return ((ip->i_mp->ntm_mode&mask) == mask ? 0 : EACCES);
491 /* Otherwise, check everyone else. */
498 return ((ip->i_mp->ntm_mode & mask) == mask ? 0 : EACCES);
506 * ntfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
511 ntfs_open(struct vop_open_args *ap)
513 return (vop_stdopen(ap));
519 * Update the times on the inode.
521 * ntfs_close(struct vnode *a_vp, int a_fflag)
525 ntfs_close(struct vop_close_args *ap)
528 struct vnode *vp = ap->a_vp;
529 struct ntnode *ip = VTONT(vp);
531 kprintf("ntfs_close: %d\n",ip->i_number);
534 return (vop_stdclose(ap));
538 * ntfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
539 * int *a_ncookies, off_t **cookies)
542 ntfs_readdir(struct vop_readdir_args *ap)
544 struct vnode *vp = ap->a_vp;
545 struct fnode *fp = VTOF(vp);
546 struct ntnode *ip = FTONT(fp);
547 struct uio *uio = ap->a_uio;
548 struct ntfsmount *ntmp = ip->i_mp;
551 u_int32_t faked = 0, num, off;
553 char convname[NTFS_MAXFILENAME + 1];
555 dprintf(("ntfs_readdir %d off: %d resid: %ld\n",
556 ip->i_number, (u_int32_t)uio->uio_offset,
559 if (uio->uio_offset < 0 || uio->uio_offset > INT_MAX)
561 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0)
565 * uio->uio_offset carries the number of the entry
566 * where we should start returning dirents.
568 * We fake up "." if we're not reading the FS root
569 * and we always fake up "..".
571 * off contains the entry we are starting at,
572 * num increments while we are reading.
575 off = num = uio->uio_offset;
576 faked = (ip->i_number == NTFS_ROOTINO) ? 1 : 2;
578 /* Simulate . in every dir except ROOT */
579 if (ip->i_number != NTFS_ROOTINO && num == 0) {
580 if (vop_write_dirent(&error, uio, ip->i_number,
590 /* Simulate .. in every dir including ROOT */
591 if (num == faked - 1) {
592 /* XXX NTFS_ROOTINO seems to be wrong here */
593 if (vop_write_dirent(&error, uio, NTFS_ROOTINO,
604 struct attr_indexentry *iep;
607 * num is the number of the entry we will return,
608 * but ntfs_ntreaddir takes the entry number of the
609 * ntfs directory listing, so subtract the faked
612 error = ntfs_ntreaddir(ntmp, fp, num - faked, &iep);
620 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST);
621 iep = NTFS_NEXTREC(iep, struct attr_indexentry *))
623 if(!ntfs_isnamepermitted(ntmp,iep))
625 for(i=0, j=0; i < iep->ie_fnamelen; i++, j++) {
626 c = NTFS_U28(iep->ie_fname[i]);
628 convname[j++] = (char)(c>>8);
629 convname[j] = (char)c&0xFF;
632 if (vop_write_dirent(&error, uio, iep->ie_number,
633 (iep->ie_fflag & NTFS_FFLAG_DIR) ? DT_DIR : DT_REG,
637 dprintf(("ntfs_readdir: elem: %d, fname:[%s] type: %d, "
639 ncookies, convname, iep->ie_fnametype,
641 (iep->ie_fflag & NTFS_FFLAG_DIR) ?
653 uio->uio_offset = num;
655 dprintf(("ntfs_readdir: %d entries (%d bytes) read\n",
656 ncookies,(u_int)(uio->uio_offset - off)));
657 dprintf(("ntfs_readdir: off: %d resid: %ld\n",
658 (u_int32_t)uio->uio_offset, uio->uio_resid));
660 if (!error && ap->a_ncookies != NULL) {
664 ddprintf(("ntfs_readdir: %d cookies\n",ncookies));
665 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
666 panic("ntfs_readdir: unexpected uio from NFS server");
667 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
672 *ap->a_ncookies = ncookies;
673 *ap->a_cookies = cookies;
677 *ap->a_eofflag = VTONT(vp)->i_size <= uio->uio_offset;
685 * ntfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
686 * struct componentname *a_cnp)
689 ntfs_lookup(struct vop_old_lookup_args *ap)
691 struct vnode *dvp = ap->a_dvp;
692 struct ntnode *dip = VTONT(dvp);
693 struct ntfsmount *ntmp = dip->i_mp;
694 struct componentname *cnp = ap->a_cnp;
696 int lockparent = cnp->cn_flags & CNP_LOCKPARENT;
698 int wantparent = cnp->cn_flags & (CNP_LOCKPARENT | CNP_WANTPARENT);
700 dprintf(("ntfs_lookup: \"%.*s\" (%ld bytes) in %d, lp: %d, wp: %d \n",
701 (int)cnp->cn_namelen, cnp->cn_nameptr, cnp->cn_namelen,
702 dip->i_number, lockparent, wantparent));
706 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
707 dprintf(("ntfs_lookup: faking . directory in %d\n",
713 } else if (cnp->cn_flags & CNP_ISDOTDOT) {
716 dprintf(("ntfs_lookup: faking .. directory in %d\n",
719 error = ntfs_ntvattrget(ntmp, dip, NTFS_A_NAME, NULL, 0, &vap);
724 cnp->cn_flags |= CNP_PDIRUNLOCK;
726 dprintf(("ntfs_lookup: parentdir: %d\n",
727 vap->va_a_name->n_pnumber));
728 error = VFS_VGET(ntmp->ntm_mountp, NULL,
729 vap->va_a_name->n_pnumber,ap->a_vpp);
730 ntfs_ntvattrrele(vap);
732 if (VN_LOCK(dvp, LK_EXCLUSIVE | LK_RETRY) == 0)
733 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
738 error = VN_LOCK(dvp, LK_EXCLUSIVE);
744 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
747 error = ntfs_ntlookupfile(ntmp, dvp, cnp, ap->a_vpp);
749 dprintf(("ntfs_ntlookupfile: returned %d\n", error));
753 dprintf(("ntfs_lookup: found ino: %d\n",
754 VTONT(*ap->a_vpp)->i_number));
762 #if defined(__DragonFly__)
764 * Flush the blocks of a file to disk.
766 * This function is worthless for vnodes that represent directories. Maybe we
767 * could just do a sync if they try an fsync on a directory file.
769 * ntfs_fsync(struct vnode *a_vp, int a_waitfor)
772 ntfs_fsync(struct vop_fsync_args *ap)
779 * Return POSIX pathconf information applicable to NTFS filesystem
782 ntfs_pathconf(struct vop_pathconf_args *ap)
784 switch (ap->a_name) {
789 *ap->a_retval = NTFS_MAXFILENAME;
792 *ap->a_retval = PATH_MAX;
794 case _PC_CHOWN_RESTRICTED:
800 #if defined(__NetBSD__)
804 case _PC_FILESIZEBITS:
815 * Global vfs data structures
817 struct vop_ops ntfs_vnode_vops = {
818 .vop_default = vop_defaultop,
819 .vop_getattr = ntfs_getattr,
820 .vop_inactive = ntfs_inactive,
821 .vop_reclaim = ntfs_reclaim,
822 .vop_print = ntfs_print,
823 .vop_pathconf = ntfs_pathconf,
824 .vop_old_lookup = ntfs_lookup,
825 .vop_access = ntfs_access,
826 .vop_close = ntfs_close,
827 .vop_open = ntfs_open,
828 .vop_readdir = ntfs_readdir,
829 .vop_fsync = ntfs_fsync,
830 .vop_bmap = ntfs_bmap,
831 .vop_getpages = vop_stdgetpages,
832 .vop_putpages = vop_stdputpages,
833 .vop_strategy = ntfs_strategy,
834 .vop_read = ntfs_read,
835 .vop_write = ntfs_write