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 * 4. 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.
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
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
42 #include <sys/types.h>
44 #include <sys/vnode.h>
45 #include <sys/mount.h>
46 #include <sys/namei.h>
47 #include <sys/malloc.h>
50 #include <sys/dirent.h>
53 #include <vm/vm_param.h>
54 #include <vm/vm_page.h>
55 #include <vm/vm_object.h>
56 #include <vm/vm_pager.h>
57 #include <vm/vnode_pager.h>
58 #include <vm/vm_extern.h>
60 #include <sys/sysctl.h>
62 /*#define NTFS_DEBUG 1*/
63 #include <fs/ntfs/ntfs.h>
64 #include <fs/ntfs/ntfs_inode.h>
65 #include <fs/ntfs/ntfs_subr.h>
67 #include <sys/unistd.h> /* for pathconf(2) constants */
69 static vop_read_t ntfs_read;
70 static vop_write_t ntfs_write;
71 static vop_getattr_t ntfs_getattr;
72 static vop_inactive_t ntfs_inactive;
73 static vop_reclaim_t ntfs_reclaim;
74 static vop_bmap_t ntfs_bmap;
75 static vop_strategy_t ntfs_strategy;
76 static vop_access_t ntfs_access;
77 static vop_open_t ntfs_open;
78 static vop_close_t ntfs_close;
79 static vop_readdir_t ntfs_readdir;
80 static vop_cachedlookup_t ntfs_lookup;
81 static vop_fsync_t ntfs_fsync;
82 static vop_pathconf_t ntfs_pathconf;
83 static vop_vptofh_t ntfs_vptofh;
85 int ntfs_prtactive = 1; /* 1 => print out reclaim of active vnodes */
88 * This is a noop, simply returning what one has been given.
92 struct vop_bmap_args /* {
95 struct bufobj **a_bop;
101 struct vnode *vp = ap->a_vp;
103 dprintf(("ntfs_bmap: vn: %p, blk: %d\n", ap->a_vp,(u_int32_t)ap->a_bn));
104 if (ap->a_bop != NULL)
105 *ap->a_bop = &vp->v_bufobj;
106 if (ap->a_bnp != NULL)
107 *ap->a_bnp = ap->a_bn;
108 if (ap->a_runp != NULL)
110 if (ap->a_runb != NULL)
117 struct vop_read_args /* {
121 struct ucred *a_cred;
124 register struct vnode *vp = ap->a_vp;
125 register struct fnode *fp = VTOF(vp);
126 register struct ntnode *ip = FTONT(fp);
127 struct uio *uio = ap->a_uio;
128 struct ntfsmount *ntmp = ip->i_mp;
131 int resid, off, toread;
134 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));
136 dprintf(("ntfs_read: filesize: %d",(u_int32_t)fp->f_size));
138 /* don't allow reading after end of file */
139 if (uio->uio_offset > fp->f_size)
142 resid = MIN(uio->uio_resid, fp->f_size - uio->uio_offset);
144 dprintf((", resid: %d\n", resid));
148 cn = ntfs_btocn(uio->uio_offset);
149 off = ntfs_btocnoff(uio->uio_offset);
151 toread = MIN(off + resid, ntfs_cntob(1));
153 error = bread(vp, cn, ntfs_cntob(1), NOCRED, &bp);
159 error = uiomove(bp->b_data + off, toread - off, uio);
166 resid -= toread - off;
174 struct vop_getattr_args /* {
177 struct ucred *a_cred;
181 register struct vnode *vp = ap->a_vp;
182 register struct fnode *fp = VTOF(vp);
183 register struct ntnode *ip = FTONT(fp);
184 register struct vattr *vap = ap->a_vap;
186 dprintf(("ntfs_getattr: %d, flags: %d\n",ip->i_number,ip->i_flag));
188 vap->va_fsid = dev2udev(ip->i_dev);
189 vap->va_fileid = ip->i_number;
190 vap->va_mode = ip->i_mp->ntm_mode;
191 vap->va_nlink = (ip->i_nlink || ip->i_flag & IN_LOADED ? ip->i_nlink : 1);
192 vap->va_uid = ip->i_mp->ntm_uid;
193 vap->va_gid = ip->i_mp->ntm_gid;
194 vap->va_rdev = NODEV;
195 vap->va_size = fp->f_size;
196 vap->va_bytes = fp->f_allocated;
197 vap->va_atime = ntfs_nttimetounix(fp->f_times.t_access);
198 vap->va_mtime = ntfs_nttimetounix(fp->f_times.t_write);
199 vap->va_ctime = ntfs_nttimetounix(fp->f_times.t_create);
200 vap->va_flags = ip->i_flag;
202 vap->va_blocksize = ip->i_mp->ntm_spc * ip->i_mp->ntm_bps;
203 vap->va_type = vp->v_type;
209 * Last reference to an ntnode. If necessary, write or delete it.
213 struct vop_inactive_args /* {
217 register struct vnode *vp = ap->a_vp;
219 register struct ntnode *ip = VTONT(vp);
222 dprintf(("ntfs_inactive: vnode: %p, ntnode: %d\n", vp, ip->i_number));
224 if (ntfs_prtactive && vrefcnt(vp) != 0)
225 vprint("ntfs_inactive: pushing active", vp);
227 /* XXX since we don't support any filesystem changes
228 * right now, nothing more needs to be done
234 * Reclaim an fnode/ntnode so that it can be used for other purposes.
238 struct vop_reclaim_args /* {
242 register struct vnode *vp = ap->a_vp;
243 register struct fnode *fp = VTOF(vp);
244 register struct ntnode *ip = FTONT(fp);
247 dprintf(("ntfs_reclaim: vnode: %p, ntnode: %d\n", vp, ip->i_number));
249 if (ntfs_prtactive && vrefcnt(vp) != 0)
250 vprint("ntfs_reclaim: pushing active", vp);
253 * Destroy the vm object and flush associated pages.
255 vnode_destroy_vobject(vp);
257 if ((error = ntfs_ntget(ip)) != 0)
260 /* Purge old data structures associated with the inode. */
269 * Calculate the logical to physical mapping if not done already,
270 * then call the device strategy routine.
274 struct vop_strategy_args /* {
278 register struct buf *bp = ap->a_bp;
279 register struct vnode *vp = ap->a_vp;
280 register struct fnode *fp = VTOF(vp);
281 register struct ntnode *ip = FTONT(fp);
282 struct ntfsmount *ntmp = ip->i_mp;
285 dprintf(("ntfs_strategy: offset: %d, blkno: %d, lblkno: %d\n",
286 (u_int32_t)bp->b_offset,(u_int32_t)bp->b_blkno,
287 (u_int32_t)bp->b_lblkno));
289 dprintf(("strategy: bcount: %d flags: 0x%x\n",
290 (u_int32_t)bp->b_bcount,bp->b_flags));
292 if (bp->b_iocmd == BIO_READ) {
295 if (ntfs_cntob(bp->b_blkno) >= fp->f_size) {
299 toread = MIN(bp->b_bcount,
300 fp->f_size-ntfs_cntob(bp->b_blkno));
301 dprintf(("ntfs_strategy: toread: %d, fsize: %d\n",
302 toread,(u_int32_t)fp->f_size));
304 error = ntfs_readattr(ntmp, ip, fp->f_attrtype,
305 fp->f_attrname, ntfs_cntob(bp->b_blkno),
306 toread, bp->b_data, NULL);
309 printf("ntfs_strategy: ntfs_readattr failed\n");
311 bp->b_ioflags |= BIO_ERROR;
314 bzero(bp->b_data + toread, bp->b_bcount - toread);
320 if (ntfs_cntob(bp->b_blkno) + bp->b_bcount >= fp->f_size) {
321 printf("ntfs_strategy: CAN'T EXTEND FILE\n");
322 bp->b_error = error = EFBIG;
323 bp->b_ioflags |= BIO_ERROR;
325 towrite = MIN(bp->b_bcount,
326 fp->f_size-ntfs_cntob(bp->b_blkno));
327 dprintf(("ntfs_strategy: towrite: %d, fsize: %d\n",
328 towrite,(u_int32_t)fp->f_size));
330 error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype,
331 fp->f_attrname, ntfs_cntob(bp->b_blkno),towrite,
332 bp->b_data, &tmp, NULL);
335 printf("ntfs_strategy: ntfs_writeattr fail\n");
337 bp->b_ioflags |= BIO_ERROR;
347 struct vop_write_args /* {
351 struct ucred *a_cred;
354 register struct vnode *vp = ap->a_vp;
355 register struct fnode *fp = VTOF(vp);
356 register struct ntnode *ip = FTONT(fp);
357 struct uio *uio = ap->a_uio;
358 struct ntfsmount *ntmp = ip->i_mp;
363 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));
364 dprintf(("ntfs_write: filesize: %d",(u_int32_t)fp->f_size));
366 if (uio->uio_resid + uio->uio_offset > fp->f_size) {
367 printf("ntfs_write: CAN'T WRITE BEYOND END OF FILE\n");
371 towrite = MIN(uio->uio_resid, fp->f_size - uio->uio_offset);
373 dprintf((", towrite: %d\n",(u_int32_t)towrite));
375 error = ntfs_writeattr_plain(ntmp, ip, fp->f_attrtype,
376 fp->f_attrname, uio->uio_offset, towrite, NULL, &written, uio);
379 printf("ntfs_write: ntfs_writeattr failed: %d\n", error);
387 struct vop_access_args /* {
390 struct ucred *a_cred;
394 struct vnode *vp = ap->a_vp;
395 struct ntnode *ip = VTONT(vp);
396 mode_t mode = ap->a_mode;
401 dprintf(("ntfs_access: %d\n",ip->i_number));
404 * Disallow write attempts on read-only filesystems;
405 * unless the file is a socket, fifo, or a block or
406 * character device resident on the filesystem.
409 switch ((int)vp->v_type) {
413 if (vp->v_mount->mnt_flag & MNT_RDONLY)
416 if (error = getinoquota(ip))
423 return (vaccess(vp->v_type, ip->i_mp->ntm_mode, ip->i_mp->ntm_uid,
424 ip->i_mp->ntm_gid, ap->a_mode, ap->a_cred, NULL));
435 struct vop_open_args /* {
438 struct ucred *a_cred;
443 register struct vnode *vp = ap->a_vp;
444 register struct ntnode *ip = VTONT(vp);
446 printf("ntfs_open: %d\n",ip->i_number);
449 vnode_create_vobject(ap->a_vp, VTOF(ap->a_vp)->f_size, ap->a_td);
452 * Files marked append-only must be opened for appending.
461 * Update the times on the inode.
466 struct vop_close_args /* {
469 struct ucred *a_cred;
474 register struct vnode *vp = ap->a_vp;
475 register struct ntnode *ip = VTONT(vp);
477 printf("ntfs_close: %d\n",ip->i_number);
485 struct vop_readdir_args /* {
488 struct ucred *a_cred;
493 register struct vnode *vp = ap->a_vp;
494 register struct fnode *fp = VTOF(vp);
495 register struct ntnode *ip = FTONT(fp);
496 struct uio *uio = ap->a_uio;
497 struct ntfsmount *ntmp = ip->i_mp;
500 u_int32_t faked = 0, num;
505 dprintf(("ntfs_readdir %d off: %d resid: %d\n",ip->i_number,(u_int32_t)uio->uio_offset,uio->uio_resid));
507 off = uio->uio_offset;
509 /* Simulate . in every dir except ROOT */
510 if( ip->i_number != NTFS_ROOTINO ) {
511 struct dirent dot = { NTFS_ROOTINO,
512 sizeof(struct dirent), DT_DIR, 1, "." };
514 if( uio->uio_offset < sizeof(struct dirent) ) {
515 dot.d_fileno = ip->i_number;
516 error = uiomove((char *)&dot,sizeof(struct dirent),uio);
524 /* Simulate .. in every dir including ROOT */
525 if( uio->uio_offset < 2 * sizeof(struct dirent) ) {
526 struct dirent dotdot = { NTFS_ROOTINO,
527 sizeof(struct dirent), DT_DIR, 2, ".." };
529 error = uiomove((char *)&dotdot,sizeof(struct dirent),uio);
536 faked = (ip->i_number == NTFS_ROOTINO) ? 1 : 2;
537 num = uio->uio_offset / sizeof(struct dirent) - faked;
539 while( uio->uio_resid >= sizeof(struct dirent) ) {
540 struct attr_indexentry *iep;
542 error = ntfs_ntreaddir(ntmp, fp, num, &iep);
550 for(; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (uio->uio_resid >= sizeof(struct dirent));
551 iep = NTFS_NEXTREC(iep, struct attr_indexentry *))
553 if(!ntfs_isnamepermitted(ntmp,iep))
556 for(i=0, j=0; i<iep->ie_fnamelen; i++, j++) {
557 c = NTFS_U28(iep->ie_fname[i]);
559 cde.d_name[j++] = (char)(c>>8);
560 cde.d_name[j] = (char)c&0xFF;
562 cde.d_name[j] = '\0';
563 dprintf(("ntfs_readdir: elem: %d, fname:[%s] type: %d, flag: %d, ",
564 num, cde.d_name, iep->ie_fnametype,
567 cde.d_fileno = iep->ie_number;
568 cde.d_type = (iep->ie_fflag & NTFS_FFLAG_DIR) ? DT_DIR : DT_REG;
569 cde.d_reclen = sizeof(struct dirent);
570 dprintf(("%s\n", (cde.d_type == DT_DIR) ? "dir":"reg"));
572 error = uiomove((char *)&cde, sizeof(struct dirent), uio);
581 dprintf(("ntfs_readdir: %d entries (%d bytes) read\n",
582 ncookies,(u_int)(uio->uio_offset - off)));
583 dprintf(("ntfs_readdir: off: %d resid: %d\n",
584 (u_int32_t)uio->uio_offset,uio->uio_resid));
586 if (!error && ap->a_ncookies != NULL) {
587 struct dirent* dpStart;
592 ddprintf(("ntfs_readdir: %d cookies\n",ncookies));
593 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
594 panic("ntfs_readdir: unexpected uio from NFS server");
595 dpStart = (struct dirent *)
596 ((caddr_t)uio->uio_iov->iov_base -
597 (uio->uio_offset - off));
598 MALLOC(cookies, u_long *, ncookies * sizeof(u_long),
600 for (dp = dpStart, cookiep = cookies, i=0;
602 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen), i++) {
604 *cookiep++ = (u_int) off;
606 *ap->a_ncookies = ncookies;
607 *ap->a_cookies = cookies;
611 *ap->a_eofflag = VTONT(ap->a_vp)->i_size <= uio->uio_offset;
618 struct vop_cachedlookup_args /* {
620 struct vnode **a_vpp;
621 struct componentname *a_cnp;
624 register struct vnode *dvp = ap->a_dvp;
625 register struct ntnode *dip = VTONT(dvp);
626 struct ntfsmount *ntmp = dip->i_mp;
627 struct componentname *cnp = ap->a_cnp;
628 struct ucred *cred = cnp->cn_cred;
630 dprintf(("ntfs_lookup: \"%.*s\" (%ld bytes) in %d\n",
631 (int)cnp->cn_namelen, cnp->cn_nameptr, cnp->cn_namelen,
634 error = VOP_ACCESS(dvp, VEXEC, cred, cnp->cn_thread);
638 if ((cnp->cn_flags & ISLASTCN) &&
639 (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
640 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
643 if(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
644 dprintf(("ntfs_lookup: faking . directory in %d\n",
650 } else if (cnp->cn_flags & ISDOTDOT) {
653 dprintf(("ntfs_lookup: faking .. directory in %d\n",
656 error = ntfs_ntvattrget(ntmp, dip, NTFS_A_NAME, NULL, 0, &vap);
660 VOP_UNLOCK(dvp,0,cnp->cn_thread);
661 dprintf(("ntfs_lookup: parentdir: %d\n",
662 vap->va_a_name->n_pnumber));
663 error = VFS_VGET(ntmp->ntm_mountp, vap->va_a_name->n_pnumber,
664 LK_EXCLUSIVE, ap->a_vpp);
665 ntfs_ntvattrrele(vap);
667 vn_lock(dvp,LK_EXCLUSIVE|LK_RETRY,cnp->cn_thread);
671 error = ntfs_ntlookupfile(ntmp, dvp, cnp, ap->a_vpp);
673 dprintf(("ntfs_ntlookupfile: returned %d\n", error));
677 dprintf(("ntfs_lookup: found ino: %d\n",
678 VTONT(*ap->a_vpp)->i_number));
681 if (cnp->cn_flags & MAKEENTRY)
682 cache_enter(dvp, *ap->a_vpp, cnp);
688 * Flush the blocks of a file to disk.
690 * This function is worthless for vnodes that represent directories. Maybe we
691 * could just do a sync if they try an fsync on a directory file.
695 struct vop_fsync_args /* {
697 struct ucred *a_cred;
706 * Return POSIX pathconf information applicable to NTFS filesystem
710 struct vop_pathconf_args *ap;
713 switch (ap->a_name) {
718 *ap->a_retval = NTFS_MAXFILENAME;
721 *ap->a_retval = PATH_MAX;
723 case _PC_CHOWN_RESTRICTED:
737 struct vop_vptofh_args /* {
742 register struct ntnode *ntp;
743 register struct ntfid *ntfhp;
745 ddprintf(("ntfs_fhtovp(): %p\n", ap->a_vp));
747 ntp = VTONT(ap->a_vp);
748 ntfhp = (struct ntfid *)ap->a_fhp;
749 ntfhp->ntfid_len = sizeof(struct ntfid);
750 ntfhp->ntfid_ino = ntp->i_number;
751 /* ntfhp->ntfid_gen = ntp->i_gen; */
756 * Global vfs data structures
758 struct vop_vector ntfs_vnodeops = {
759 .vop_default = &default_vnodeops,
761 .vop_access = ntfs_access,
762 .vop_bmap = ntfs_bmap,
763 .vop_cachedlookup = ntfs_lookup,
764 .vop_close = ntfs_close,
765 .vop_fsync = ntfs_fsync,
766 .vop_getattr = ntfs_getattr,
767 .vop_inactive = ntfs_inactive,
768 .vop_lookup = vfs_cache_lookup,
769 .vop_open = ntfs_open,
770 .vop_pathconf = ntfs_pathconf,
771 .vop_read = ntfs_read,
772 .vop_readdir = ntfs_readdir,
773 .vop_reclaim = ntfs_reclaim,
774 .vop_strategy = ntfs_strategy,
775 .vop_write = ntfs_write,
776 .vop_vptofh = ntfs_vptofh,