Home | History | Annotate | Line # | Download | only in msdosfs
msdosfs_vnops.c revision 1.52.10.2
      1  1.52.10.2  christos /*	$NetBSD: msdosfs_vnops.c,v 1.52.10.2 2008/11/09 02:05:20 christos Exp $	*/
      2  1.52.10.2  christos 
      3  1.52.10.2  christos /*-
      4  1.52.10.2  christos  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
      5  1.52.10.2  christos  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
      6  1.52.10.2  christos  * All rights reserved.
      7  1.52.10.2  christos  * Original code by Paul Popelka (paulp (at) uts.amdahl.com) (see below).
      8  1.52.10.2  christos  *
      9  1.52.10.2  christos  * Redistribution and use in source and binary forms, with or without
     10  1.52.10.2  christos  * modification, are permitted provided that the following conditions
     11  1.52.10.2  christos  * are met:
     12  1.52.10.2  christos  * 1. Redistributions of source code must retain the above copyright
     13  1.52.10.2  christos  *    notice, this list of conditions and the following disclaimer.
     14  1.52.10.2  christos  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.52.10.2  christos  *    notice, this list of conditions and the following disclaimer in the
     16  1.52.10.2  christos  *    documentation and/or other materials provided with the distribution.
     17  1.52.10.2  christos  * 3. All advertising materials mentioning features or use of this software
     18  1.52.10.2  christos  *    must display the following acknowledgement:
     19  1.52.10.2  christos  *	This product includes software developed by TooLs GmbH.
     20  1.52.10.2  christos  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     21  1.52.10.2  christos  *    derived from this software without specific prior written permission.
     22  1.52.10.2  christos  *
     23  1.52.10.2  christos  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     24  1.52.10.2  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  1.52.10.2  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  1.52.10.2  christos  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     27  1.52.10.2  christos  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     28  1.52.10.2  christos  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     29  1.52.10.2  christos  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     30  1.52.10.2  christos  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     31  1.52.10.2  christos  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     32  1.52.10.2  christos  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  1.52.10.2  christos  */
     34  1.52.10.2  christos /*
     35  1.52.10.2  christos  * Written by Paul Popelka (paulp (at) uts.amdahl.com)
     36  1.52.10.2  christos  *
     37  1.52.10.2  christos  * You can do anything you want with this software, just don't say you wrote
     38  1.52.10.2  christos  * it, and don't remove this notice.
     39  1.52.10.2  christos  *
     40  1.52.10.2  christos  * This software is provided "as is".
     41  1.52.10.2  christos  *
     42  1.52.10.2  christos  * The author supplies this software to be publicly redistributed on the
     43  1.52.10.2  christos  * understanding that the author is not responsible for the correct
     44  1.52.10.2  christos  * functioning of this software in any circumstances and is not liable for
     45  1.52.10.2  christos  * any damages caused by this software.
     46  1.52.10.2  christos  *
     47  1.52.10.2  christos  * October 1992
     48  1.52.10.2  christos  */
     49  1.52.10.2  christos 
     50  1.52.10.2  christos #include <sys/cdefs.h>
     51  1.52.10.2  christos __KERNEL_RCSID(0, "$NetBSD: msdosfs_vnops.c,v 1.52.10.2 2008/11/09 02:05:20 christos Exp $");
     52  1.52.10.2  christos 
     53  1.52.10.2  christos #include <sys/param.h>
     54  1.52.10.2  christos #include <sys/systm.h>
     55  1.52.10.2  christos #include <sys/namei.h>
     56  1.52.10.2  christos #include <sys/resourcevar.h>	/* defines plimit structure in proc struct */
     57  1.52.10.2  christos #include <sys/kernel.h>
     58  1.52.10.2  christos #include <sys/file.h>		/* define FWRITE ... */
     59  1.52.10.2  christos #include <sys/stat.h>
     60  1.52.10.2  christos #include <sys/buf.h>
     61  1.52.10.2  christos #include <sys/proc.h>
     62  1.52.10.2  christos #include <sys/mount.h>
     63  1.52.10.2  christos #include <sys/vnode.h>
     64  1.52.10.2  christos #include <sys/signalvar.h>
     65  1.52.10.2  christos #include <sys/malloc.h>
     66  1.52.10.2  christos #include <sys/dirent.h>
     67  1.52.10.2  christos #include <sys/lockf.h>
     68  1.52.10.2  christos #include <sys/kauth.h>
     69  1.52.10.2  christos 
     70  1.52.10.2  christos #include <miscfs/genfs/genfs.h>
     71  1.52.10.2  christos #include <miscfs/specfs/specdev.h> /* XXX */	/* defines v_rdev */
     72  1.52.10.2  christos 
     73  1.52.10.2  christos #include <uvm/uvm_extern.h>
     74  1.52.10.2  christos 
     75  1.52.10.2  christos #include <fs/msdosfs/bpb.h>
     76  1.52.10.2  christos #include <fs/msdosfs/direntry.h>
     77  1.52.10.2  christos #include <fs/msdosfs/denode.h>
     78  1.52.10.2  christos #include <fs/msdosfs/msdosfsmount.h>
     79  1.52.10.2  christos #include <fs/msdosfs/fat.h>
     80  1.52.10.2  christos 
     81  1.52.10.2  christos /*
     82  1.52.10.2  christos  * Some general notes:
     83  1.52.10.2  christos  *
     84  1.52.10.2  christos  * In the ufs filesystem the inodes, superblocks, and indirect blocks are
     85  1.52.10.2  christos  * read/written using the vnode for the filesystem. Blocks that represent
     86  1.52.10.2  christos  * the contents of a file are read/written using the vnode for the file
     87  1.52.10.2  christos  * (including directories when they are read/written as files). This
     88  1.52.10.2  christos  * presents problems for the dos filesystem because data that should be in
     89  1.52.10.2  christos  * an inode (if dos had them) resides in the directory itself.  Since we
     90  1.52.10.2  christos  * must update directory entries without the benefit of having the vnode
     91  1.52.10.2  christos  * for the directory we must use the vnode for the filesystem.  This means
     92  1.52.10.2  christos  * that when a directory is actually read/written (via read, write, or
     93  1.52.10.2  christos  * readdir, or seek) we must use the vnode for the filesystem instead of
     94  1.52.10.2  christos  * the vnode for the directory as would happen in ufs. This is to insure we
     95  1.52.10.2  christos  * retrieve the correct block from the buffer cache since the hash value is
     96  1.52.10.2  christos  * based upon the vnode address and the desired block number.
     97  1.52.10.2  christos  */
     98  1.52.10.2  christos 
     99  1.52.10.2  christos /*
    100  1.52.10.2  christos  * Create a regular file. On entry the directory to contain the file being
    101  1.52.10.2  christos  * created is locked.  We must release before we return. We must also free
    102  1.52.10.2  christos  * the pathname buffer pointed at by cnp->cn_pnbuf, always on error, or
    103  1.52.10.2  christos  * only if the SAVESTART bit in cn_flags is clear on success.
    104  1.52.10.2  christos  */
    105  1.52.10.2  christos int
    106  1.52.10.2  christos msdosfs_create(v)
    107  1.52.10.2  christos 	void *v;
    108  1.52.10.2  christos {
    109  1.52.10.2  christos 	struct vop_create_args /* {
    110  1.52.10.2  christos 		struct vnode *a_dvp;
    111  1.52.10.2  christos 		struct vnode **a_vpp;
    112  1.52.10.2  christos 		struct componentname *a_cnp;
    113  1.52.10.2  christos 		struct vattr *a_vap;
    114  1.52.10.2  christos 	} */ *ap = v;
    115  1.52.10.2  christos 	struct componentname *cnp = ap->a_cnp;
    116  1.52.10.2  christos 	struct denode ndirent;
    117  1.52.10.2  christos 	struct denode *dep;
    118  1.52.10.2  christos 	struct denode *pdep = VTODE(ap->a_dvp);
    119  1.52.10.2  christos 	int error;
    120  1.52.10.2  christos 
    121  1.52.10.2  christos #ifdef MSDOSFS_DEBUG
    122  1.52.10.2  christos 	printf("msdosfs_create(cnp %p, vap %p\n", cnp, ap->a_vap);
    123  1.52.10.2  christos #endif
    124  1.52.10.2  christos 
    125  1.52.10.2  christos 	/*
    126  1.52.10.2  christos 	 * If this is the root directory and there is no space left we
    127  1.52.10.2  christos 	 * can't do anything.  This is because the root directory can not
    128  1.52.10.2  christos 	 * change size.
    129  1.52.10.2  christos 	 */
    130  1.52.10.2  christos 	if (pdep->de_StartCluster == MSDOSFSROOT
    131  1.52.10.2  christos 	    && pdep->de_fndoffset >= pdep->de_FileSize) {
    132  1.52.10.2  christos 		error = ENOSPC;
    133  1.52.10.2  christos 		goto bad;
    134  1.52.10.2  christos 	}
    135  1.52.10.2  christos 
    136  1.52.10.2  christos 	/*
    137  1.52.10.2  christos 	 * Create a directory entry for the file, then call createde() to
    138  1.52.10.2  christos 	 * have it installed. NOTE: DOS files are always executable.  We
    139  1.52.10.2  christos 	 * use the absence of the owner write bit to make the file
    140  1.52.10.2  christos 	 * readonly.
    141  1.52.10.2  christos 	 */
    142  1.52.10.2  christos #ifdef DIAGNOSTIC
    143  1.52.10.2  christos 	if ((cnp->cn_flags & HASBUF) == 0)
    144  1.52.10.2  christos 		panic("msdosfs_create: no name");
    145  1.52.10.2  christos #endif
    146  1.52.10.2  christos 	memset(&ndirent, 0, sizeof(ndirent));
    147  1.52.10.2  christos 	if ((error = uniqdosname(pdep, cnp, ndirent.de_Name)) != 0)
    148  1.52.10.2  christos 		goto bad;
    149  1.52.10.2  christos 
    150  1.52.10.2  christos 	ndirent.de_Attributes = (ap->a_vap->va_mode & S_IWUSR) ?
    151  1.52.10.2  christos 				ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY;
    152  1.52.10.2  christos 	ndirent.de_StartCluster = 0;
    153  1.52.10.2  christos 	ndirent.de_FileSize = 0;
    154  1.52.10.2  christos 	ndirent.de_dev = pdep->de_dev;
    155  1.52.10.2  christos 	ndirent.de_devvp = pdep->de_devvp;
    156  1.52.10.2  christos 	ndirent.de_pmp = pdep->de_pmp;
    157  1.52.10.2  christos 	ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
    158  1.52.10.2  christos 	DETIMES(&ndirent, NULL, NULL, NULL, pdep->de_pmp->pm_gmtoff);
    159  1.52.10.2  christos 	if ((error = createde(&ndirent, pdep, &dep, cnp)) != 0)
    160  1.52.10.2  christos 		goto bad;
    161  1.52.10.2  christos 	if ((cnp->cn_flags & SAVESTART) == 0)
    162  1.52.10.2  christos 		PNBUF_PUT(cnp->cn_pnbuf);
    163  1.52.10.2  christos 	VN_KNOTE(ap->a_dvp, NOTE_WRITE);
    164  1.52.10.2  christos 	vput(ap->a_dvp);
    165  1.52.10.2  christos 	*ap->a_vpp = DETOV(dep);
    166  1.52.10.2  christos 	return (0);
    167  1.52.10.2  christos 
    168  1.52.10.2  christos bad:
    169  1.52.10.2  christos 	PNBUF_PUT(cnp->cn_pnbuf);
    170  1.52.10.2  christos 	vput(ap->a_dvp);
    171  1.52.10.2  christos 	return (error);
    172  1.52.10.2  christos }
    173  1.52.10.2  christos 
    174  1.52.10.2  christos int
    175  1.52.10.2  christos msdosfs_mknod(v)
    176  1.52.10.2  christos 	void *v;
    177  1.52.10.2  christos {
    178  1.52.10.2  christos 	struct vop_mknod_args /* {
    179  1.52.10.2  christos 		struct vnode *a_dvp;
    180  1.52.10.2  christos 		struct vnode **a_vpp;
    181  1.52.10.2  christos 		struct componentname *a_cnp;
    182  1.52.10.2  christos 		struct vattr *a_vap;
    183  1.52.10.2  christos 	} */ *ap = v;
    184  1.52.10.2  christos 
    185  1.52.10.2  christos 	PNBUF_PUT(ap->a_cnp->cn_pnbuf);
    186  1.52.10.2  christos 	vput(ap->a_dvp);
    187  1.52.10.2  christos 	return (EINVAL);
    188  1.52.10.2  christos }
    189  1.52.10.2  christos 
    190  1.52.10.2  christos int
    191  1.52.10.2  christos msdosfs_open(void *v)
    192  1.52.10.2  christos {
    193  1.52.10.2  christos #if 0
    194  1.52.10.2  christos 	struct vop_open_args /* {
    195  1.52.10.2  christos 		struct vnode *a_vp;
    196  1.52.10.2  christos 		int a_mode;
    197  1.52.10.2  christos 		kauth_cred_t a_cred;
    198  1.52.10.2  christos 	} */ *ap;
    199  1.52.10.2  christos #endif
    200  1.52.10.2  christos 
    201  1.52.10.2  christos 	return (0);
    202  1.52.10.2  christos }
    203  1.52.10.2  christos 
    204  1.52.10.2  christos int
    205  1.52.10.2  christos msdosfs_close(v)
    206  1.52.10.2  christos 	void *v;
    207  1.52.10.2  christos {
    208  1.52.10.2  christos 	struct vop_close_args /* {
    209  1.52.10.2  christos 		struct vnode *a_vp;
    210  1.52.10.2  christos 		int a_fflag;
    211  1.52.10.2  christos 		kauth_cred_t a_cred;
    212  1.52.10.2  christos 	} */ *ap = v;
    213  1.52.10.2  christos 	struct vnode *vp = ap->a_vp;
    214  1.52.10.2  christos 	struct denode *dep = VTODE(vp);
    215  1.52.10.2  christos 
    216  1.52.10.2  christos 	mutex_enter(&vp->v_interlock);
    217  1.52.10.2  christos 	if (vp->v_usecount > 1)
    218  1.52.10.2  christos 		DETIMES(dep, NULL, NULL, NULL, dep->de_pmp->pm_gmtoff);
    219  1.52.10.2  christos 	mutex_exit(&vp->v_interlock);
    220  1.52.10.2  christos 	return (0);
    221  1.52.10.2  christos }
    222  1.52.10.2  christos 
    223  1.52.10.2  christos int
    224  1.52.10.2  christos msdosfs_access(v)
    225  1.52.10.2  christos 	void *v;
    226  1.52.10.2  christos {
    227  1.52.10.2  christos 	struct vop_access_args /* {
    228  1.52.10.2  christos 		struct vnode *a_vp;
    229  1.52.10.2  christos 		int a_mode;
    230  1.52.10.2  christos 		kauth_cred_t a_cred;
    231  1.52.10.2  christos 	} */ *ap = v;
    232  1.52.10.2  christos 	struct vnode *vp = ap->a_vp;
    233  1.52.10.2  christos 	struct denode *dep = VTODE(vp);
    234  1.52.10.2  christos 	struct msdosfsmount *pmp = dep->de_pmp;
    235  1.52.10.2  christos 	mode_t mode = ap->a_mode;
    236  1.52.10.2  christos 
    237  1.52.10.2  christos 	/*
    238  1.52.10.2  christos 	 * Disallow write attempts on read-only file systems;
    239  1.52.10.2  christos 	 * unless the file is a socket, fifo, or a block or
    240  1.52.10.2  christos 	 * character device resident on the file system.
    241  1.52.10.2  christos 	 */
    242  1.52.10.2  christos 	if (mode & VWRITE) {
    243  1.52.10.2  christos 		switch (vp->v_type) {
    244  1.52.10.2  christos 		case VDIR:
    245  1.52.10.2  christos 		case VLNK:
    246  1.52.10.2  christos 		case VREG:
    247  1.52.10.2  christos 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
    248  1.52.10.2  christos 				return (EROFS);
    249  1.52.10.2  christos 		default:
    250  1.52.10.2  christos 			break;
    251  1.52.10.2  christos 		}
    252  1.52.10.2  christos 	}
    253  1.52.10.2  christos 
    254  1.52.10.2  christos 	if ((dep->de_Attributes & ATTR_READONLY) == 0)
    255  1.52.10.2  christos 		mode = S_IRWXU|S_IRWXG|S_IRWXO;
    256  1.52.10.2  christos 	else
    257  1.52.10.2  christos 		mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    258  1.52.10.2  christos 	return (vaccess(ap->a_vp->v_type,
    259  1.52.10.2  christos 	    mode & (vp->v_type == VDIR ? pmp->pm_dirmask : pmp->pm_mask),
    260  1.52.10.2  christos 	    pmp->pm_uid, pmp->pm_gid, ap->a_mode, ap->a_cred));
    261  1.52.10.2  christos }
    262  1.52.10.2  christos 
    263  1.52.10.2  christos int
    264  1.52.10.2  christos msdosfs_getattr(v)
    265  1.52.10.2  christos 	void *v;
    266  1.52.10.2  christos {
    267  1.52.10.2  christos 	struct vop_getattr_args /* {
    268  1.52.10.2  christos 		struct vnode *a_vp;
    269  1.52.10.2  christos 		struct vattr *a_vap;
    270  1.52.10.2  christos 		kauth_cred_t a_cred;
    271  1.52.10.2  christos 	} */ *ap = v;
    272  1.52.10.2  christos 	struct denode *dep = VTODE(ap->a_vp);
    273  1.52.10.2  christos 	struct msdosfsmount *pmp = dep->de_pmp;
    274  1.52.10.2  christos 	struct vattr *vap = ap->a_vap;
    275  1.52.10.2  christos 	mode_t mode;
    276  1.52.10.2  christos 	u_long dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
    277  1.52.10.2  christos 	ino_t fileid;
    278  1.52.10.2  christos 
    279  1.52.10.2  christos 	DETIMES(dep, NULL, NULL, NULL, pmp->pm_gmtoff);
    280  1.52.10.2  christos 	vap->va_fsid = dep->de_dev;
    281  1.52.10.2  christos 	/*
    282  1.52.10.2  christos 	 * The following computation of the fileid must be the same as that
    283  1.52.10.2  christos 	 * used in msdosfs_readdir() to compute d_fileno. If not, pwd
    284  1.52.10.2  christos 	 * doesn't work.
    285  1.52.10.2  christos 	 */
    286  1.52.10.2  christos 	if (dep->de_Attributes & ATTR_DIRECTORY) {
    287  1.52.10.2  christos 		fileid = cntobn(pmp, (ino_t)dep->de_StartCluster) * dirsperblk;
    288  1.52.10.2  christos 		if (dep->de_StartCluster == MSDOSFSROOT)
    289  1.52.10.2  christos 			fileid = 1;
    290  1.52.10.2  christos 	} else {
    291  1.52.10.2  christos 		fileid = cntobn(pmp, (ino_t)dep->de_dirclust) * dirsperblk;
    292  1.52.10.2  christos 		if (dep->de_dirclust == MSDOSFSROOT)
    293  1.52.10.2  christos 			fileid = roottobn(pmp, 0) * dirsperblk;
    294  1.52.10.2  christos 		fileid += dep->de_diroffset / sizeof(struct direntry);
    295  1.52.10.2  christos 	}
    296  1.52.10.2  christos 	vap->va_fileid = fileid;
    297  1.52.10.2  christos 	if ((dep->de_Attributes & ATTR_READONLY) == 0)
    298  1.52.10.2  christos 		mode = S_IRWXU|S_IRWXG|S_IRWXO;
    299  1.52.10.2  christos 	else
    300  1.52.10.2  christos 		mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    301  1.52.10.2  christos 	vap->va_mode =
    302  1.52.10.2  christos 	    mode & (ap->a_vp->v_type == VDIR ? pmp->pm_dirmask : pmp->pm_mask);
    303  1.52.10.2  christos 	vap->va_uid = pmp->pm_uid;
    304  1.52.10.2  christos 	vap->va_gid = pmp->pm_gid;
    305  1.52.10.2  christos 	vap->va_nlink = 1;
    306  1.52.10.2  christos 	vap->va_rdev = 0;
    307  1.52.10.2  christos 	vap->va_size = ap->a_vp->v_size;
    308  1.52.10.2  christos 	dos2unixtime(dep->de_MDate, dep->de_MTime, 0, pmp->pm_gmtoff,
    309  1.52.10.2  christos 	    &vap->va_mtime);
    310  1.52.10.2  christos 	if (dep->de_pmp->pm_flags & MSDOSFSMNT_LONGNAME) {
    311  1.52.10.2  christos 		dos2unixtime(dep->de_ADate, 0, 0, pmp->pm_gmtoff,
    312  1.52.10.2  christos 		    &vap->va_atime);
    313  1.52.10.2  christos 		dos2unixtime(dep->de_CDate, dep->de_CTime, dep->de_CHun,
    314  1.52.10.2  christos 		    pmp->pm_gmtoff, &vap->va_ctime);
    315  1.52.10.2  christos 	} else {
    316  1.52.10.2  christos 		vap->va_atime = vap->va_mtime;
    317  1.52.10.2  christos 		vap->va_ctime = vap->va_mtime;
    318  1.52.10.2  christos 	}
    319  1.52.10.2  christos 	vap->va_flags = 0;
    320  1.52.10.2  christos 	if ((dep->de_Attributes & ATTR_ARCHIVE) == 0)
    321  1.52.10.2  christos 		vap->va_mode  |= S_ARCH1;
    322  1.52.10.2  christos 	vap->va_gen = 0;
    323  1.52.10.2  christos 	vap->va_blocksize = pmp->pm_bpcluster;
    324  1.52.10.2  christos 	vap->va_bytes =
    325  1.52.10.2  christos 	    (dep->de_FileSize + pmp->pm_crbomask) & ~pmp->pm_crbomask;
    326  1.52.10.2  christos 	vap->va_type = ap->a_vp->v_type;
    327  1.52.10.2  christos 	return (0);
    328  1.52.10.2  christos }
    329  1.52.10.2  christos 
    330  1.52.10.2  christos int
    331  1.52.10.2  christos msdosfs_setattr(v)
    332  1.52.10.2  christos 	void *v;
    333  1.52.10.2  christos {
    334  1.52.10.2  christos 	struct vop_setattr_args /* {
    335  1.52.10.2  christos 		struct vnode *a_vp;
    336  1.52.10.2  christos 		struct vattr *a_vap;
    337  1.52.10.2  christos 		kauth_cred_t a_cred;
    338  1.52.10.2  christos 	} */ *ap = v;
    339  1.52.10.2  christos 	int error = 0, de_changed = 0;
    340  1.52.10.2  christos 	struct denode *dep = VTODE(ap->a_vp);
    341  1.52.10.2  christos 	struct msdosfsmount *pmp = dep->de_pmp;
    342  1.52.10.2  christos 	struct vnode *vp  = ap->a_vp;
    343  1.52.10.2  christos 	struct vattr *vap = ap->a_vap;
    344  1.52.10.2  christos 	kauth_cred_t cred = ap->a_cred;
    345  1.52.10.2  christos 
    346  1.52.10.2  christos #ifdef MSDOSFS_DEBUG
    347  1.52.10.2  christos 	printf("msdosfs_setattr(): vp %p, vap %p, cred %p\n",
    348  1.52.10.2  christos 	    ap->a_vp, vap, cred);
    349  1.52.10.2  christos #endif
    350  1.52.10.2  christos 	/*
    351  1.52.10.2  christos 	 * Note we silently ignore uid or gid changes.
    352  1.52.10.2  christos 	 */
    353  1.52.10.2  christos 	if ((vap->va_type != VNON) || (vap->va_nlink != (nlink_t)VNOVAL) ||
    354  1.52.10.2  christos 	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
    355  1.52.10.2  christos 	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
    356  1.52.10.2  christos 	    (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL) ||
    357  1.52.10.2  christos 	    (vap->va_uid != VNOVAL && vap->va_uid != pmp->pm_uid) ||
    358  1.52.10.2  christos 	    (vap->va_gid != VNOVAL && vap->va_gid != pmp->pm_gid)) {
    359  1.52.10.2  christos #ifdef MSDOSFS_DEBUG
    360  1.52.10.2  christos 		printf("msdosfs_setattr(): returning EINVAL\n");
    361  1.52.10.2  christos 		printf("    va_type %d, va_nlink %x, va_fsid %lx, va_fileid %llx\n",
    362  1.52.10.2  christos 		    vap->va_type, vap->va_nlink, vap->va_fsid,
    363  1.52.10.2  christos 		    (unsigned long long)vap->va_fileid);
    364  1.52.10.2  christos 		printf("    va_blocksize %lx, va_rdev %x, va_bytes %qx, va_gen %lx\n",
    365  1.52.10.2  christos 		    vap->va_blocksize, vap->va_rdev, (long long)vap->va_bytes, vap->va_gen);
    366  1.52.10.2  christos #endif
    367  1.52.10.2  christos 		return (EINVAL);
    368  1.52.10.2  christos 	}
    369  1.52.10.2  christos 	/*
    370  1.52.10.2  christos 	 * Silently ignore attributes modifications on directories.
    371  1.52.10.2  christos 	 */
    372  1.52.10.2  christos 	if (ap->a_vp->v_type == VDIR)
    373  1.52.10.2  christos 		return 0;
    374  1.52.10.2  christos 
    375  1.52.10.2  christos 	if (vap->va_size != VNOVAL) {
    376  1.52.10.2  christos 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
    377  1.52.10.2  christos 			return (EROFS);
    378  1.52.10.2  christos 		error = detrunc(dep, (u_long)vap->va_size, 0, cred);
    379  1.52.10.2  christos 		if (error)
    380  1.52.10.2  christos 			return (error);
    381  1.52.10.2  christos 		de_changed = 1;
    382  1.52.10.2  christos 	}
    383  1.52.10.2  christos 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
    384  1.52.10.2  christos 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
    385  1.52.10.2  christos 			return (EROFS);
    386  1.52.10.2  christos 		if (kauth_cred_geteuid(cred) != pmp->pm_uid &&
    387  1.52.10.2  christos 		    (error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
    388  1.52.10.2  christos 		    NULL)) &&
    389  1.52.10.2  christos 		    ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
    390  1.52.10.2  christos 		    (error = VOP_ACCESS(ap->a_vp, VWRITE, cred))))
    391  1.52.10.2  christos 			return (error);
    392  1.52.10.2  christos 		if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0 &&
    393  1.52.10.2  christos 		    vap->va_atime.tv_sec != VNOVAL)
    394  1.52.10.2  christos 			unix2dostime(&vap->va_atime, pmp->pm_gmtoff, &dep->de_ADate, NULL, NULL);
    395  1.52.10.2  christos 		if (vap->va_mtime.tv_sec != VNOVAL)
    396  1.52.10.2  christos 			unix2dostime(&vap->va_mtime, pmp->pm_gmtoff, &dep->de_MDate, &dep->de_MTime, NULL);
    397  1.52.10.2  christos 		dep->de_Attributes |= ATTR_ARCHIVE;
    398  1.52.10.2  christos 		dep->de_flag |= DE_MODIFIED;
    399  1.52.10.2  christos 		de_changed = 1;
    400  1.52.10.2  christos 	}
    401  1.52.10.2  christos 	/*
    402  1.52.10.2  christos 	 * DOS files only have the ability to have their writability
    403  1.52.10.2  christos 	 * attribute set, so we use the owner write bit to set the readonly
    404  1.52.10.2  christos 	 * attribute.
    405  1.52.10.2  christos 	 */
    406  1.52.10.2  christos 	if (vap->va_mode != (mode_t)VNOVAL) {
    407  1.52.10.2  christos 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
    408  1.52.10.2  christos 			return (EROFS);
    409  1.52.10.2  christos 		if (kauth_cred_geteuid(cred) != pmp->pm_uid &&
    410  1.52.10.2  christos 		    (error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
    411  1.52.10.2  christos 		    NULL)))
    412  1.52.10.2  christos 			return (error);
    413  1.52.10.2  christos 		/* We ignore the read and execute bits. */
    414  1.52.10.2  christos 		if (vap->va_mode & S_IWUSR)
    415  1.52.10.2  christos 			dep->de_Attributes &= ~ATTR_READONLY;
    416  1.52.10.2  christos 		else
    417  1.52.10.2  christos 			dep->de_Attributes |= ATTR_READONLY;
    418  1.52.10.2  christos 		dep->de_flag |= DE_MODIFIED;
    419  1.52.10.2  christos 		de_changed = 1;
    420  1.52.10.2  christos 	}
    421  1.52.10.2  christos 	/*
    422  1.52.10.2  christos 	 * Allow the `archived' bit to be toggled.
    423  1.52.10.2  christos 	 */
    424  1.52.10.2  christos 	if (vap->va_flags != VNOVAL) {
    425  1.52.10.2  christos 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
    426  1.52.10.2  christos 			return (EROFS);
    427  1.52.10.2  christos 		if (kauth_cred_geteuid(cred) != pmp->pm_uid &&
    428  1.52.10.2  christos 		    (error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER,
    429  1.52.10.2  christos 		    NULL)))
    430  1.52.10.2  christos 			return (error);
    431  1.52.10.2  christos 		if (vap->va_flags & SF_ARCHIVED)
    432  1.52.10.2  christos 			dep->de_Attributes &= ~ATTR_ARCHIVE;
    433  1.52.10.2  christos 		else
    434  1.52.10.2  christos 			dep->de_Attributes |= ATTR_ARCHIVE;
    435  1.52.10.2  christos 		dep->de_flag |= DE_MODIFIED;
    436  1.52.10.2  christos 		de_changed = 1;
    437  1.52.10.2  christos 	}
    438  1.52.10.2  christos 
    439  1.52.10.2  christos 	if (de_changed) {
    440  1.52.10.2  christos 		VN_KNOTE(vp, NOTE_ATTRIB);
    441  1.52.10.2  christos 		return (deupdat(dep, 1));
    442  1.52.10.2  christos 	} else
    443  1.52.10.2  christos 		return (0);
    444  1.52.10.2  christos }
    445  1.52.10.2  christos 
    446  1.52.10.2  christos int
    447  1.52.10.2  christos msdosfs_read(v)
    448  1.52.10.2  christos 	void *v;
    449  1.52.10.2  christos {
    450  1.52.10.2  christos 	struct vop_read_args /* {
    451  1.52.10.2  christos 		struct vnode *a_vp;
    452  1.52.10.2  christos 		struct uio *a_uio;
    453  1.52.10.2  christos 		int a_ioflag;
    454  1.52.10.2  christos 		kauth_cred_t a_cred;
    455  1.52.10.2  christos 	} */ *ap = v;
    456  1.52.10.2  christos 	int error = 0, flags;
    457  1.52.10.2  christos 	int64_t diff;
    458  1.52.10.2  christos 	int blsize;
    459  1.52.10.2  christos 	long n;
    460  1.52.10.2  christos 	long on;
    461  1.52.10.2  christos 	daddr_t lbn;
    462  1.52.10.2  christos 	void *win;
    463  1.52.10.2  christos 	vsize_t bytelen;
    464  1.52.10.2  christos 	struct buf *bp;
    465  1.52.10.2  christos 	struct vnode *vp = ap->a_vp;
    466  1.52.10.2  christos 	struct denode *dep = VTODE(vp);
    467  1.52.10.2  christos 	struct msdosfsmount *pmp = dep->de_pmp;
    468  1.52.10.2  christos 	struct uio *uio = ap->a_uio;
    469  1.52.10.2  christos 
    470  1.52.10.2  christos 	/*
    471  1.52.10.2  christos 	 * If they didn't ask for any data, then we are done.
    472  1.52.10.2  christos 	 */
    473  1.52.10.2  christos 
    474  1.52.10.2  christos 	if (uio->uio_resid == 0)
    475  1.52.10.2  christos 		return (0);
    476  1.52.10.2  christos 	if (uio->uio_offset < 0)
    477  1.52.10.2  christos 		return (EINVAL);
    478  1.52.10.2  christos 	if (uio->uio_offset >= dep->de_FileSize)
    479  1.52.10.2  christos 		return (0);
    480  1.52.10.2  christos 
    481  1.52.10.2  christos 	if (vp->v_type == VREG) {
    482  1.52.10.2  christos 		const int advice = IO_ADV_DECODE(ap->a_ioflag);
    483  1.52.10.2  christos 
    484  1.52.10.2  christos 		while (uio->uio_resid > 0) {
    485  1.52.10.2  christos 			bytelen = MIN(dep->de_FileSize - uio->uio_offset,
    486  1.52.10.2  christos 				      uio->uio_resid);
    487  1.52.10.2  christos 
    488  1.52.10.2  christos 			if (bytelen == 0)
    489  1.52.10.2  christos 				break;
    490  1.52.10.2  christos 			win = ubc_alloc(&vp->v_uobj, uio->uio_offset,
    491  1.52.10.2  christos 					&bytelen, advice, UBC_READ);
    492  1.52.10.2  christos 			error = uiomove(win, bytelen, uio);
    493  1.52.10.2  christos 			flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
    494  1.52.10.2  christos 			ubc_release(win, flags);
    495  1.52.10.2  christos 			if (error)
    496  1.52.10.2  christos 				break;
    497  1.52.10.2  christos 		}
    498  1.52.10.2  christos 		dep->de_flag |= DE_ACCESS;
    499  1.52.10.2  christos 		goto out;
    500  1.52.10.2  christos 	}
    501  1.52.10.2  christos 
    502  1.52.10.2  christos 	/* this loop is only for directories now */
    503  1.52.10.2  christos 	do {
    504  1.52.10.2  christos 		lbn = de_cluster(pmp, uio->uio_offset);
    505  1.52.10.2  christos 		on = uio->uio_offset & pmp->pm_crbomask;
    506  1.52.10.2  christos 		n = MIN(pmp->pm_bpcluster - on, uio->uio_resid);
    507  1.52.10.2  christos 		if (uio->uio_offset >= dep->de_FileSize)
    508  1.52.10.2  christos 			return (0);
    509  1.52.10.2  christos 		/* file size (and hence diff) may be up to 4GB */
    510  1.52.10.2  christos 		diff = dep->de_FileSize - uio->uio_offset;
    511  1.52.10.2  christos 		if (diff < n)
    512  1.52.10.2  christos 			n = (long) diff;
    513  1.52.10.2  christos 
    514  1.52.10.2  christos 		/* convert cluster # to sector # */
    515  1.52.10.2  christos 		error = pcbmap(dep, lbn, &lbn, 0, &blsize);
    516  1.52.10.2  christos 		if (error)
    517  1.52.10.2  christos 			return (error);
    518  1.52.10.2  christos 
    519  1.52.10.2  christos 		/*
    520  1.52.10.2  christos 		 * If we are operating on a directory file then be sure to
    521  1.52.10.2  christos 		 * do i/o with the vnode for the filesystem instead of the
    522  1.52.10.2  christos 		 * vnode for the directory.
    523  1.52.10.2  christos 		 */
    524  1.52.10.2  christos 		error = bread(pmp->pm_devvp, de_bn2kb(pmp, lbn), blsize,
    525  1.52.10.2  christos 		    NOCRED, 0, &bp);
    526  1.52.10.2  christos 		n = MIN(n, pmp->pm_bpcluster - bp->b_resid);
    527  1.52.10.2  christos 		if (error) {
    528  1.52.10.2  christos 			brelse(bp, 0);
    529  1.52.10.2  christos 			return (error);
    530  1.52.10.2  christos 		}
    531  1.52.10.2  christos 		error = uiomove((char *)bp->b_data + on, (int) n, uio);
    532  1.52.10.2  christos 		brelse(bp, 0);
    533  1.52.10.2  christos 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
    534  1.52.10.2  christos 
    535  1.52.10.2  christos out:
    536  1.52.10.2  christos 	if ((ap->a_ioflag & IO_SYNC) == IO_SYNC)
    537  1.52.10.2  christos 		error = deupdat(dep, 1);
    538  1.52.10.2  christos 	return (error);
    539  1.52.10.2  christos }
    540  1.52.10.2  christos 
    541  1.52.10.2  christos /*
    542  1.52.10.2  christos  * Write data to a file or directory.
    543  1.52.10.2  christos  */
    544  1.52.10.2  christos int
    545  1.52.10.2  christos msdosfs_write(v)
    546  1.52.10.2  christos 	void *v;
    547  1.52.10.2  christos {
    548  1.52.10.2  christos 	struct vop_write_args /* {
    549  1.52.10.2  christos 		struct vnode *a_vp;
    550  1.52.10.2  christos 		struct uio *a_uio;
    551  1.52.10.2  christos 		int a_ioflag;
    552  1.52.10.2  christos 		kauth_cred_t a_cred;
    553  1.52.10.2  christos 	} */ *ap = v;
    554  1.52.10.2  christos 	int resid, flags, extended = 0;
    555  1.52.10.2  christos 	int error = 0;
    556  1.52.10.2  christos 	int ioflag = ap->a_ioflag;
    557  1.52.10.2  christos 	u_long osize;
    558  1.52.10.2  christos 	u_long count;
    559  1.52.10.2  christos 	void *win;
    560  1.52.10.2  christos 	vsize_t bytelen;
    561  1.52.10.2  christos 	off_t oldoff;
    562  1.52.10.2  christos 	struct uio *uio = ap->a_uio;
    563  1.52.10.2  christos 	struct proc *p = curproc;
    564  1.52.10.2  christos 	struct vnode *vp = ap->a_vp;
    565  1.52.10.2  christos 	struct denode *dep = VTODE(vp);
    566  1.52.10.2  christos 	struct msdosfsmount *pmp = dep->de_pmp;
    567  1.52.10.2  christos 	kauth_cred_t cred = ap->a_cred;
    568  1.52.10.2  christos 	bool async;
    569  1.52.10.2  christos 
    570  1.52.10.2  christos #ifdef MSDOSFS_DEBUG
    571  1.52.10.2  christos 	printf("msdosfs_write(vp %p, uio %p, ioflag %x, cred %p\n",
    572  1.52.10.2  christos 	    vp, uio, ioflag, cred);
    573  1.52.10.2  christos 	printf("msdosfs_write(): diroff %lu, dirclust %lu, startcluster %lu\n",
    574  1.52.10.2  christos 	    dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
    575  1.52.10.2  christos #endif
    576  1.52.10.2  christos 
    577  1.52.10.2  christos 	switch (vp->v_type) {
    578  1.52.10.2  christos 	case VREG:
    579  1.52.10.2  christos 		if (ioflag & IO_APPEND)
    580  1.52.10.2  christos 			uio->uio_offset = dep->de_FileSize;
    581  1.52.10.2  christos 		break;
    582  1.52.10.2  christos 	case VDIR:
    583  1.52.10.2  christos 		return EISDIR;
    584  1.52.10.2  christos 	default:
    585  1.52.10.2  christos 		panic("msdosfs_write(): bad file type");
    586  1.52.10.2  christos 	}
    587  1.52.10.2  christos 
    588  1.52.10.2  christos 	if (uio->uio_offset < 0)
    589  1.52.10.2  christos 		return (EINVAL);
    590  1.52.10.2  christos 
    591  1.52.10.2  christos 	if (uio->uio_resid == 0)
    592  1.52.10.2  christos 		return (0);
    593  1.52.10.2  christos 
    594  1.52.10.2  christos 	/* Don't bother to try to write files larger than the fs limit */
    595  1.52.10.2  christos 	if (uio->uio_offset + uio->uio_resid > MSDOSFS_FILESIZE_MAX)
    596  1.52.10.2  christos 		return (EFBIG);
    597  1.52.10.2  christos 
    598  1.52.10.2  christos 	/*
    599  1.52.10.2  christos 	 * If they've exceeded their filesize limit, tell them about it.
    600  1.52.10.2  christos 	 */
    601  1.52.10.2  christos 	if (((uio->uio_offset + uio->uio_resid) >
    602  1.52.10.2  christos 	    p->p_rlimit[RLIMIT_FSIZE].rlim_cur)) {
    603  1.52.10.2  christos 		mutex_enter(proc_lock);
    604  1.52.10.2  christos 		psignal(p, SIGXFSZ);
    605  1.52.10.2  christos 		mutex_exit(proc_lock);
    606  1.52.10.2  christos 		return (EFBIG);
    607  1.52.10.2  christos 	}
    608  1.52.10.2  christos 
    609  1.52.10.2  christos 	/*
    610  1.52.10.2  christos 	 * If the offset we are starting the write at is beyond the end of
    611  1.52.10.2  christos 	 * the file, then they've done a seek.  Unix filesystems allow
    612  1.52.10.2  christos 	 * files with holes in them, DOS doesn't so we must fill the hole
    613  1.52.10.2  christos 	 * with zeroed blocks.
    614  1.52.10.2  christos 	 */
    615  1.52.10.2  christos 	if (uio->uio_offset > dep->de_FileSize) {
    616  1.52.10.2  christos 		if ((error = deextend(dep, uio->uio_offset, cred)) != 0)
    617  1.52.10.2  christos 			return (error);
    618  1.52.10.2  christos 	}
    619  1.52.10.2  christos 
    620  1.52.10.2  christos 	/*
    621  1.52.10.2  christos 	 * Remember some values in case the write fails.
    622  1.52.10.2  christos 	 */
    623  1.52.10.2  christos 	async = vp->v_mount->mnt_flag & MNT_ASYNC;
    624  1.52.10.2  christos 	resid = uio->uio_resid;
    625  1.52.10.2  christos 	osize = dep->de_FileSize;
    626  1.52.10.2  christos 
    627  1.52.10.2  christos 	/*
    628  1.52.10.2  christos 	 * If we write beyond the end of the file, extend it to its ultimate
    629  1.52.10.2  christos 	 * size ahead of the time to hopefully get a contiguous area.
    630  1.52.10.2  christos 	 */
    631  1.52.10.2  christos 	if (uio->uio_offset + resid > osize) {
    632  1.52.10.2  christos 		count = de_clcount(pmp, uio->uio_offset + resid) -
    633  1.52.10.2  christos 			de_clcount(pmp, osize);
    634  1.52.10.2  christos 		if ((error = extendfile(dep, count, NULL, NULL, 0)) &&
    635  1.52.10.2  christos 		    (error != ENOSPC || (ioflag & IO_UNIT)))
    636  1.52.10.2  christos 			goto errexit;
    637  1.52.10.2  christos 
    638  1.52.10.2  christos 		dep->de_FileSize = uio->uio_offset + resid;
    639  1.52.10.2  christos 		/* hint uvm to not read in extended part */
    640  1.52.10.2  christos 		uvm_vnp_setwritesize(vp, dep->de_FileSize);
    641  1.52.10.2  christos 		extended = 1;
    642  1.52.10.2  christos 	}
    643  1.52.10.2  christos 
    644  1.52.10.2  christos 	do {
    645  1.52.10.2  christos 		oldoff = uio->uio_offset;
    646  1.52.10.2  christos 		bytelen = uio->uio_resid;
    647  1.52.10.2  christos 
    648  1.52.10.2  christos 		win = ubc_alloc(&vp->v_uobj, oldoff, &bytelen, UVM_ADV_NORMAL,
    649  1.52.10.2  christos 		    UBC_WRITE);
    650  1.52.10.2  christos 		error = uiomove(win, bytelen, uio);
    651  1.52.10.2  christos 		flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
    652  1.52.10.2  christos 		ubc_release(win, flags);
    653  1.52.10.2  christos 		if (error)
    654  1.52.10.2  christos 			break;
    655  1.52.10.2  christos 
    656  1.52.10.2  christos 		/*
    657  1.52.10.2  christos 		 * flush what we just wrote if necessary.
    658  1.52.10.2  christos 		 * XXXUBC simplistic async flushing.
    659  1.52.10.2  christos 		 */
    660  1.52.10.2  christos 
    661  1.52.10.2  christos 		if (!async && oldoff >> 16 != uio->uio_offset >> 16) {
    662  1.52.10.2  christos 			mutex_enter(&vp->v_interlock);
    663  1.52.10.2  christos 			error = VOP_PUTPAGES(vp, (oldoff >> 16) << 16,
    664  1.52.10.2  christos 			    (uio->uio_offset >> 16) << 16, PGO_CLEANIT);
    665  1.52.10.2  christos 		}
    666  1.52.10.2  christos 	} while (error == 0 && uio->uio_resid > 0);
    667  1.52.10.2  christos 
    668  1.52.10.2  christos 	/* set final size */
    669  1.52.10.2  christos 	uvm_vnp_setsize(vp, dep->de_FileSize);
    670  1.52.10.2  christos 	if (error == 0 && ioflag & IO_SYNC) {
    671  1.52.10.2  christos 		mutex_enter(&vp->v_interlock);
    672  1.52.10.2  christos 		error = VOP_PUTPAGES(vp, trunc_page(oldoff),
    673  1.52.10.2  christos 		    round_page(oldoff + bytelen), PGO_CLEANIT | PGO_SYNCIO);
    674  1.52.10.2  christos 	}
    675  1.52.10.2  christos 	dep->de_flag |= DE_UPDATE;
    676  1.52.10.2  christos 
    677  1.52.10.2  christos 	/*
    678  1.52.10.2  christos 	 * If the write failed and they want us to, truncate the file back
    679  1.52.10.2  christos 	 * to the size it was before the write was attempted.
    680  1.52.10.2  christos 	 */
    681  1.52.10.2  christos errexit:
    682  1.52.10.2  christos 	if (resid > uio->uio_resid)
    683  1.52.10.2  christos 		VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
    684  1.52.10.2  christos 	if (error) {
    685  1.52.10.2  christos 		detrunc(dep, osize, ioflag & IO_SYNC, NOCRED);
    686  1.52.10.2  christos 		uio->uio_offset -= resid - uio->uio_resid;
    687  1.52.10.2  christos 		uio->uio_resid = resid;
    688  1.52.10.2  christos 	} else if ((ioflag & IO_SYNC) == IO_SYNC)
    689  1.52.10.2  christos 		error = deupdat(dep, 1);
    690  1.52.10.2  christos 	KASSERT(vp->v_size == dep->de_FileSize);
    691  1.52.10.2  christos 	return (error);
    692  1.52.10.2  christos }
    693  1.52.10.2  christos 
    694  1.52.10.2  christos int
    695  1.52.10.2  christos msdosfs_update(struct vnode *vp, const struct timespec *acc,
    696  1.52.10.2  christos     const struct timespec *mod, int flags)
    697  1.52.10.2  christos {
    698  1.52.10.2  christos 	struct buf *bp;
    699  1.52.10.2  christos 	struct direntry *dirp;
    700  1.52.10.2  christos 	struct denode *dep;
    701  1.52.10.2  christos 	int error;
    702  1.52.10.2  christos 
    703  1.52.10.2  christos 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    704  1.52.10.2  christos 		return (0);
    705  1.52.10.2  christos 	dep = VTODE(vp);
    706  1.52.10.2  christos 	DETIMES(dep, acc, mod, NULL, dep->de_pmp->pm_gmtoff);
    707  1.52.10.2  christos 	if ((dep->de_flag & DE_MODIFIED) == 0)
    708  1.52.10.2  christos 		return (0);
    709  1.52.10.2  christos 	dep->de_flag &= ~DE_MODIFIED;
    710  1.52.10.2  christos 	if (dep->de_Attributes & ATTR_DIRECTORY)
    711  1.52.10.2  christos 		return (0);
    712  1.52.10.2  christos 	if (dep->de_refcnt <= 0)
    713  1.52.10.2  christos 		return (0);
    714  1.52.10.2  christos 	error = readde(dep, &bp, &dirp);
    715  1.52.10.2  christos 	if (error)
    716  1.52.10.2  christos 		return (error);
    717  1.52.10.2  christos 	DE_EXTERNALIZE(dirp, dep);
    718  1.52.10.2  christos 	if (flags & (UPDATE_WAIT|UPDATE_DIROP))
    719  1.52.10.2  christos 		return (bwrite(bp));
    720  1.52.10.2  christos 	else {
    721  1.52.10.2  christos 		bdwrite(bp);
    722  1.52.10.2  christos 		return (0);
    723  1.52.10.2  christos 	}
    724  1.52.10.2  christos }
    725  1.52.10.2  christos 
    726  1.52.10.2  christos /*
    727  1.52.10.2  christos  * Flush the blocks of a file to disk.
    728  1.52.10.2  christos  *
    729  1.52.10.2  christos  * This function is worthless for vnodes that represent directories. Maybe we
    730  1.52.10.2  christos  * could just do a sync if they try an fsync on a directory file.
    731  1.52.10.2  christos  */
    732  1.52.10.2  christos int
    733  1.52.10.2  christos msdosfs_remove(v)
    734  1.52.10.2  christos 	void *v;
    735  1.52.10.2  christos {
    736  1.52.10.2  christos 	struct vop_remove_args /* {
    737  1.52.10.2  christos 		struct vnode *a_dvp;
    738  1.52.10.2  christos 		struct vnode *a_vp;
    739  1.52.10.2  christos 		struct componentname *a_cnp;
    740  1.52.10.2  christos 	} */ *ap = v;
    741  1.52.10.2  christos 	struct denode *dep = VTODE(ap->a_vp);
    742  1.52.10.2  christos 	struct denode *ddep = VTODE(ap->a_dvp);
    743  1.52.10.2  christos 	int error;
    744  1.52.10.2  christos 
    745  1.52.10.2  christos 	if (ap->a_vp->v_type == VDIR)
    746  1.52.10.2  christos 		error = EPERM;
    747  1.52.10.2  christos 	else
    748  1.52.10.2  christos 		error = removede(ddep, dep);
    749  1.52.10.2  christos #ifdef MSDOSFS_DEBUG
    750  1.52.10.2  christos 	printf("msdosfs_remove(), dep %p, v_usecount %d\n",
    751  1.52.10.2  christos 		dep, ap->a_vp->v_usecount);
    752  1.52.10.2  christos #endif
    753  1.52.10.2  christos 	VN_KNOTE(ap->a_vp, NOTE_DELETE);
    754  1.52.10.2  christos 	VN_KNOTE(ap->a_dvp, NOTE_WRITE);
    755  1.52.10.2  christos 	if (ddep == dep)
    756  1.52.10.2  christos 		vrele(ap->a_vp);
    757  1.52.10.2  christos 	else
    758  1.52.10.2  christos 		vput(ap->a_vp);	/* causes msdosfs_inactive() to be called
    759  1.52.10.2  christos 				 * via vrele() */
    760  1.52.10.2  christos 	vput(ap->a_dvp);
    761  1.52.10.2  christos 	return (error);
    762  1.52.10.2  christos }
    763  1.52.10.2  christos 
    764  1.52.10.2  christos /*
    765  1.52.10.2  christos  * DOS filesystems don't know what links are. But since we already called
    766  1.52.10.2  christos  * msdosfs_lookup() with create and lockparent, the parent is locked so we
    767  1.52.10.2  christos  * have to free it before we return the error.
    768  1.52.10.2  christos  */
    769  1.52.10.2  christos int
    770  1.52.10.2  christos msdosfs_link(v)
    771  1.52.10.2  christos 	void *v;
    772  1.52.10.2  christos {
    773  1.52.10.2  christos 	struct vop_link_args /* {
    774  1.52.10.2  christos 		struct vnode *a_dvp;
    775  1.52.10.2  christos 		struct vnode *a_vp;
    776  1.52.10.2  christos 		struct componentname *a_cnp;
    777  1.52.10.2  christos 	} */ *ap = v;
    778  1.52.10.2  christos 
    779  1.52.10.2  christos 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
    780  1.52.10.2  christos 	vput(ap->a_dvp);
    781  1.52.10.2  christos 	return (EOPNOTSUPP);
    782  1.52.10.2  christos }
    783  1.52.10.2  christos 
    784  1.52.10.2  christos /*
    785  1.52.10.2  christos  * Renames on files require moving the denode to a new hash queue since the
    786  1.52.10.2  christos  * denode's location is used to compute which hash queue to put the file
    787  1.52.10.2  christos  * in. Unless it is a rename in place.  For example "mv a b".
    788  1.52.10.2  christos  *
    789  1.52.10.2  christos  * What follows is the basic algorithm:
    790  1.52.10.2  christos  *
    791  1.52.10.2  christos  * if (file move) {
    792  1.52.10.2  christos  *	if (dest file exists) {
    793  1.52.10.2  christos  *		remove dest file
    794  1.52.10.2  christos  *	}
    795  1.52.10.2  christos  *	if (dest and src in same directory) {
    796  1.52.10.2  christos  *		rewrite name in existing directory slot
    797  1.52.10.2  christos  *	} else {
    798  1.52.10.2  christos  *		write new entry in dest directory
    799  1.52.10.2  christos  *		update offset and dirclust in denode
    800  1.52.10.2  christos  *		move denode to new hash chain
    801  1.52.10.2  christos  *		clear old directory entry
    802  1.52.10.2  christos  *	}
    803  1.52.10.2  christos  * } else {
    804  1.52.10.2  christos  *	directory move
    805  1.52.10.2  christos  *	if (dest directory exists) {
    806  1.52.10.2  christos  *		if (dest is not empty) {
    807  1.52.10.2  christos  *			return ENOTEMPTY
    808  1.52.10.2  christos  *		}
    809  1.52.10.2  christos  *		remove dest directory
    810  1.52.10.2  christos  *	}
    811  1.52.10.2  christos  *	if (dest and src in same directory) {
    812  1.52.10.2  christos  *		rewrite name in existing entry
    813  1.52.10.2  christos  *	} else {
    814  1.52.10.2  christos  *		be sure dest is not a child of src directory
    815  1.52.10.2  christos  *		write entry in dest directory
    816  1.52.10.2  christos  *		update "." and ".." in moved directory
    817  1.52.10.2  christos  *		update offset and dirclust in denode
    818  1.52.10.2  christos  *		move denode to new hash chain
    819  1.52.10.2  christos  *		clear old directory entry for moved directory
    820  1.52.10.2  christos  *	}
    821  1.52.10.2  christos  * }
    822  1.52.10.2  christos  *
    823  1.52.10.2  christos  * On entry:
    824  1.52.10.2  christos  *	source's parent directory is unlocked
    825  1.52.10.2  christos  *	source file or directory is unlocked
    826  1.52.10.2  christos  *	destination's parent directory is locked
    827  1.52.10.2  christos  *	destination file or directory is locked if it exists
    828  1.52.10.2  christos  *
    829  1.52.10.2  christos  * On exit:
    830  1.52.10.2  christos  *	all denodes should be released
    831  1.52.10.2  christos  *
    832  1.52.10.2  christos  * Notes:
    833  1.52.10.2  christos  * I'm not sure how the memory containing the pathnames pointed at by the
    834  1.52.10.2  christos  * componentname structures is freed, there may be some memory bleeding
    835  1.52.10.2  christos  * for each rename done.
    836  1.52.10.2  christos  *
    837  1.52.10.2  christos  * --More-- Notes:
    838  1.52.10.2  christos  * This routine needs help.  badly.
    839  1.52.10.2  christos  */
    840  1.52.10.2  christos int
    841  1.52.10.2  christos msdosfs_rename(v)
    842  1.52.10.2  christos 	void *v;
    843  1.52.10.2  christos {
    844  1.52.10.2  christos 	struct vop_rename_args /* {
    845  1.52.10.2  christos 		struct vnode *a_fdvp;
    846  1.52.10.2  christos 		struct vnode *a_fvp;
    847  1.52.10.2  christos 		struct componentname *a_fcnp;
    848  1.52.10.2  christos 		struct vnode *a_tdvp;
    849  1.52.10.2  christos 		struct vnode *a_tvp;
    850  1.52.10.2  christos 		struct componentname *a_tcnp;
    851  1.52.10.2  christos 	} */ *ap = v;
    852  1.52.10.2  christos 	struct vnode *tvp = ap->a_tvp;
    853  1.52.10.2  christos 	struct vnode *tdvp = ap->a_tdvp;
    854  1.52.10.2  christos 	struct vnode *fvp = ap->a_fvp;
    855  1.52.10.2  christos 	struct vnode *fdvp = ap->a_fdvp;
    856  1.52.10.2  christos 	struct componentname *tcnp = ap->a_tcnp;
    857  1.52.10.2  christos 	struct componentname *fcnp = ap->a_fcnp;
    858  1.52.10.2  christos 	struct denode *ip, *xp, *dp, *zp;
    859  1.52.10.2  christos 	u_char toname[11], oldname[11];
    860  1.52.10.2  christos 	u_long from_diroffset, to_diroffset;
    861  1.52.10.2  christos 	u_char to_count;
    862  1.52.10.2  christos 	int doingdirectory = 0, newparent = 0;
    863  1.52.10.2  christos 	int error;
    864  1.52.10.2  christos 	u_long cn;
    865  1.52.10.2  christos 	daddr_t bn;
    866  1.52.10.2  christos 	struct msdosfsmount *pmp;
    867  1.52.10.2  christos 	struct direntry *dotdotp;
    868  1.52.10.2  christos 	struct buf *bp;
    869  1.52.10.2  christos 	int fdvp_dorele = 0;
    870  1.52.10.2  christos 
    871  1.52.10.2  christos 	pmp = VFSTOMSDOSFS(fdvp->v_mount);
    872  1.52.10.2  christos 
    873  1.52.10.2  christos #ifdef DIAGNOSTIC
    874  1.52.10.2  christos 	if ((tcnp->cn_flags & HASBUF) == 0 ||
    875  1.52.10.2  christos 	    (fcnp->cn_flags & HASBUF) == 0)
    876  1.52.10.2  christos 		panic("msdosfs_rename: no name");
    877  1.52.10.2  christos #endif
    878  1.52.10.2  christos 	/*
    879  1.52.10.2  christos 	 * Check for cross-device rename.
    880  1.52.10.2  christos 	 */
    881  1.52.10.2  christos 	if ((fvp->v_mount != tdvp->v_mount) ||
    882  1.52.10.2  christos 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
    883  1.52.10.2  christos 		error = EXDEV;
    884  1.52.10.2  christos abortit:
    885  1.52.10.2  christos 		VOP_ABORTOP(tdvp, tcnp);
    886  1.52.10.2  christos 		if (tdvp == tvp)
    887  1.52.10.2  christos 			vrele(tdvp);
    888  1.52.10.2  christos 		else
    889  1.52.10.2  christos 			vput(tdvp);
    890  1.52.10.2  christos 		if (tvp)
    891  1.52.10.2  christos 			vput(tvp);
    892  1.52.10.2  christos 		VOP_ABORTOP(fdvp, fcnp);
    893  1.52.10.2  christos 		vrele(fdvp);
    894  1.52.10.2  christos 		vrele(fvp);
    895  1.52.10.2  christos 		return (error);
    896  1.52.10.2  christos 	}
    897  1.52.10.2  christos 
    898  1.52.10.2  christos 	/*
    899  1.52.10.2  christos 	 * If source and dest are the same, do nothing.
    900  1.52.10.2  christos 	 */
    901  1.52.10.2  christos 	if (tvp == fvp) {
    902  1.52.10.2  christos 		error = 0;
    903  1.52.10.2  christos 		goto abortit;
    904  1.52.10.2  christos 	}
    905  1.52.10.2  christos 
    906  1.52.10.2  christos 	/*
    907  1.52.10.2  christos 	 * XXX: This can deadlock since we hold tdvp/tvp locked.
    908  1.52.10.2  christos 	 * But I'm not going to fix it now.
    909  1.52.10.2  christos 	 */
    910  1.52.10.2  christos 	if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
    911  1.52.10.2  christos 		goto abortit;
    912  1.52.10.2  christos 	dp = VTODE(fdvp);
    913  1.52.10.2  christos 	ip = VTODE(fvp);
    914  1.52.10.2  christos 
    915  1.52.10.2  christos 	/*
    916  1.52.10.2  christos 	 * Be sure we are not renaming ".", "..", or an alias of ".". This
    917  1.52.10.2  christos 	 * leads to a crippled directory tree.  It's pretty tough to do a
    918  1.52.10.2  christos 	 * "ls" or "pwd" with the "." directory entry missing, and "cd .."
    919  1.52.10.2  christos 	 * doesn't work if the ".." entry is missing.
    920  1.52.10.2  christos 	 */
    921  1.52.10.2  christos 	if (ip->de_Attributes & ATTR_DIRECTORY) {
    922  1.52.10.2  christos 		/*
    923  1.52.10.2  christos 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
    924  1.52.10.2  christos 		 */
    925  1.52.10.2  christos 		if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
    926  1.52.10.2  christos 		    dp == ip ||
    927  1.52.10.2  christos 		    (fcnp->cn_flags & ISDOTDOT) ||
    928  1.52.10.2  christos 		    (tcnp->cn_flags & ISDOTDOT) ||
    929  1.52.10.2  christos 		    (ip->de_flag & DE_RENAME)) {
    930  1.52.10.2  christos 			VOP_UNLOCK(fvp, 0);
    931  1.52.10.2  christos 			error = EINVAL;
    932  1.52.10.2  christos 			goto abortit;
    933  1.52.10.2  christos 		}
    934  1.52.10.2  christos 		ip->de_flag |= DE_RENAME;
    935  1.52.10.2  christos 		doingdirectory++;
    936  1.52.10.2  christos 	}
    937  1.52.10.2  christos 	VN_KNOTE(fdvp, NOTE_WRITE);		/* XXXLUKEM/XXX: right place? */
    938  1.52.10.2  christos 
    939  1.52.10.2  christos 	/*
    940  1.52.10.2  christos 	 * When the target exists, both the directory
    941  1.52.10.2  christos 	 * and target vnodes are returned locked.
    942  1.52.10.2  christos 	 */
    943  1.52.10.2  christos 	dp = VTODE(tdvp);
    944  1.52.10.2  christos 	xp = tvp ? VTODE(tvp) : NULL;
    945  1.52.10.2  christos 	/*
    946  1.52.10.2  christos 	 * Remember direntry place to use for destination
    947  1.52.10.2  christos 	 */
    948  1.52.10.2  christos 	to_diroffset = dp->de_fndoffset;
    949  1.52.10.2  christos 	to_count = dp->de_fndcnt;
    950  1.52.10.2  christos 
    951  1.52.10.2  christos 	/*
    952  1.52.10.2  christos 	 * If ".." must be changed (ie the directory gets a new
    953  1.52.10.2  christos 	 * parent) then the source directory must not be in the
    954  1.52.10.2  christos 	 * directory hierarchy above the target, as this would
    955  1.52.10.2  christos 	 * orphan everything below the source directory. Also
    956  1.52.10.2  christos 	 * the user must have write permission in the source so
    957  1.52.10.2  christos 	 * as to be able to change "..". We must repeat the call
    958  1.52.10.2  christos 	 * to namei, as the parent directory is unlocked by the
    959  1.52.10.2  christos 	 * call to doscheckpath().
    960  1.52.10.2  christos 	 */
    961  1.52.10.2  christos 	error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred);
    962  1.52.10.2  christos 	VOP_UNLOCK(fvp, 0);
    963  1.52.10.2  christos 	if (VTODE(fdvp)->de_StartCluster != VTODE(tdvp)->de_StartCluster)
    964  1.52.10.2  christos 		newparent = 1;
    965  1.52.10.2  christos 
    966  1.52.10.2  christos 	/*
    967  1.52.10.2  christos 	 * XXX: We can do this here because rename uses SAVEFART and
    968  1.52.10.2  christos 	 * therefore fdvp has at least two references (one doesn't
    969  1.52.10.2  christos 	 * belong to us, though, and that's evil).  We'll get
    970  1.52.10.2  christos 	 * another "extra" reference when we do relookup(), so we
    971  1.52.10.2  christos 	 * need to compensate.  We should *NOT* be doing this, but
    972  1.52.10.2  christos 	 * it works, so whatever.
    973  1.52.10.2  christos 	 */
    974  1.52.10.2  christos 	vrele(fdvp);
    975  1.52.10.2  christos 
    976  1.52.10.2  christos 	if (doingdirectory && newparent) {
    977  1.52.10.2  christos 		if (error)	/* write access check above */
    978  1.52.10.2  christos 			goto tdvpbad;
    979  1.52.10.2  christos 		if (xp != NULL)
    980  1.52.10.2  christos 			vput(tvp);
    981  1.52.10.2  christos 		tvp = NULL;
    982  1.52.10.2  christos 		/*
    983  1.52.10.2  christos 		 * doscheckpath() vput()'s dp,
    984  1.52.10.2  christos 		 * so we have to do a relookup afterwards
    985  1.52.10.2  christos 		 */
    986  1.52.10.2  christos 		if ((error = doscheckpath(ip, dp)) != 0)
    987  1.52.10.2  christos 			goto out;
    988  1.52.10.2  christos 		if ((tcnp->cn_flags & SAVESTART) == 0)
    989  1.52.10.2  christos 			panic("msdosfs_rename: lost to startdir");
    990  1.52.10.2  christos 		vn_lock(tdvp, LK_EXCLUSIVE | LK_RETRY);
    991  1.52.10.2  christos 		if ((error = relookup(tdvp, &tvp, tcnp)) != 0) {
    992  1.52.10.2  christos 			VOP_UNLOCK(tdvp, 0);
    993  1.52.10.2  christos 			goto out;
    994  1.52.10.2  christos 		}
    995  1.52.10.2  christos 		/*
    996  1.52.10.2  christos 		 * XXX: SAVESTART causes us to get a reference, but
    997  1.52.10.2  christos 		 * that's released already above in doscheckpath()
    998  1.52.10.2  christos 		 */
    999  1.52.10.2  christos 		dp = VTODE(tdvp);
   1000  1.52.10.2  christos 		xp = tvp ? VTODE(tvp) : NULL;
   1001  1.52.10.2  christos 	}
   1002  1.52.10.2  christos 
   1003  1.52.10.2  christos 	if (xp != NULL) {
   1004  1.52.10.2  christos 		/*
   1005  1.52.10.2  christos 		 * Target must be empty if a directory and have no links
   1006  1.52.10.2  christos 		 * to it. Also, ensure source and target are compatible
   1007  1.52.10.2  christos 		 * (both directories, or both not directories).
   1008  1.52.10.2  christos 		 */
   1009  1.52.10.2  christos 		if (xp->de_Attributes & ATTR_DIRECTORY) {
   1010  1.52.10.2  christos 			if (!dosdirempty(xp)) {
   1011  1.52.10.2  christos 				error = ENOTEMPTY;
   1012  1.52.10.2  christos 				goto tdvpbad;
   1013  1.52.10.2  christos 			}
   1014  1.52.10.2  christos 			if (!doingdirectory) {
   1015  1.52.10.2  christos 				error = ENOTDIR;
   1016  1.52.10.2  christos 				goto tdvpbad;
   1017  1.52.10.2  christos 			}
   1018  1.52.10.2  christos 		} else if (doingdirectory) {
   1019  1.52.10.2  christos 			error = EISDIR;
   1020  1.52.10.2  christos 			goto tdvpbad;
   1021  1.52.10.2  christos 		}
   1022  1.52.10.2  christos 		if ((error = removede(dp, xp)) != 0)
   1023  1.52.10.2  christos 			goto tdvpbad;
   1024  1.52.10.2  christos 		VN_KNOTE(tdvp, NOTE_WRITE);
   1025  1.52.10.2  christos 		VN_KNOTE(tvp, NOTE_DELETE);
   1026  1.52.10.2  christos 		cache_purge(tvp);
   1027  1.52.10.2  christos 		vput(tvp);
   1028  1.52.10.2  christos 		tvp = NULL;
   1029  1.52.10.2  christos 		xp = NULL;
   1030  1.52.10.2  christos 	}
   1031  1.52.10.2  christos 
   1032  1.52.10.2  christos 	/*
   1033  1.52.10.2  christos 	 * Convert the filename in tcnp into a dos filename. We copy this
   1034  1.52.10.2  christos 	 * into the denode and directory entry for the destination
   1035  1.52.10.2  christos 	 * file/directory.
   1036  1.52.10.2  christos 	 */
   1037  1.52.10.2  christos 	if ((error = uniqdosname(VTODE(tdvp), tcnp, toname)) != 0)
   1038  1.52.10.2  christos 		goto abortit;
   1039  1.52.10.2  christos 
   1040  1.52.10.2  christos 	/*
   1041  1.52.10.2  christos 	 * Since from wasn't locked at various places above,
   1042  1.52.10.2  christos 	 * have to do a relookup here.
   1043  1.52.10.2  christos 	 */
   1044  1.52.10.2  christos 	fcnp->cn_flags &= ~MODMASK;
   1045  1.52.10.2  christos 	fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
   1046  1.52.10.2  christos 	if ((fcnp->cn_flags & SAVESTART) == 0)
   1047  1.52.10.2  christos 		panic("msdosfs_rename: lost from startdir");
   1048  1.52.10.2  christos 	VOP_UNLOCK(tdvp, 0);
   1049  1.52.10.2  christos 	vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
   1050  1.52.10.2  christos 	if ((error = relookup(fdvp, &fvp, fcnp))) {
   1051  1.52.10.2  christos 		VOP_UNLOCK(fdvp, 0);
   1052  1.52.10.2  christos 		vrele(ap->a_fvp);
   1053  1.52.10.2  christos 		vrele(tdvp);
   1054  1.52.10.2  christos 		return (error);
   1055  1.52.10.2  christos 	}
   1056  1.52.10.2  christos 	if (fvp == NULL) {
   1057  1.52.10.2  christos 		/*
   1058  1.52.10.2  christos 		 * From name has disappeared.
   1059  1.52.10.2  christos 		 */
   1060  1.52.10.2  christos 		if (doingdirectory)
   1061  1.52.10.2  christos 			panic("rename: lost dir entry");
   1062  1.52.10.2  christos 		vput(fdvp);
   1063  1.52.10.2  christos 		vrele(ap->a_fvp);
   1064  1.52.10.2  christos 		vrele(tdvp);
   1065  1.52.10.2  christos 		return 0;
   1066  1.52.10.2  christos 	}
   1067  1.52.10.2  christos 	fdvp_dorele = 1;
   1068  1.52.10.2  christos 	VOP_UNLOCK(fdvp, 0);
   1069  1.52.10.2  christos 	xp = VTODE(fvp);
   1070  1.52.10.2  christos 	zp = VTODE(fdvp);
   1071  1.52.10.2  christos 	from_diroffset = zp->de_fndoffset;
   1072  1.52.10.2  christos 
   1073  1.52.10.2  christos 	/*
   1074  1.52.10.2  christos 	 * Ensure that the directory entry still exists and has not
   1075  1.52.10.2  christos 	 * changed till now. If the source is a file the entry may
   1076  1.52.10.2  christos 	 * have been unlinked or renamed. In either case there is
   1077  1.52.10.2  christos 	 * no further work to be done. If the source is a directory
   1078  1.52.10.2  christos 	 * then it cannot have been rmdir'ed or renamed; this is
   1079  1.52.10.2  christos 	 * prohibited by the DE_RENAME flag.
   1080  1.52.10.2  christos 	 */
   1081  1.52.10.2  christos 	if (xp != ip) {
   1082  1.52.10.2  christos 		if (doingdirectory)
   1083  1.52.10.2  christos 			panic("rename: lost dir entry");
   1084  1.52.10.2  christos 		vrele(ap->a_fvp);
   1085  1.52.10.2  christos 		VOP_UNLOCK(fvp, 0);
   1086  1.52.10.2  christos 		xp = NULL;
   1087  1.52.10.2  christos 	} else {
   1088  1.52.10.2  christos 		vrele(fvp);
   1089  1.52.10.2  christos 		xp = NULL;
   1090  1.52.10.2  christos 
   1091  1.52.10.2  christos 		/*
   1092  1.52.10.2  christos 		 * First write a new entry in the destination
   1093  1.52.10.2  christos 		 * directory and mark the entry in the source directory
   1094  1.52.10.2  christos 		 * as deleted.  Then move the denode to the correct hash
   1095  1.52.10.2  christos 		 * chain for its new location in the filesystem.  And, if
   1096  1.52.10.2  christos 		 * we moved a directory, then update its .. entry to point
   1097  1.52.10.2  christos 		 * to the new parent directory.
   1098  1.52.10.2  christos 		 */
   1099  1.52.10.2  christos 		memcpy(oldname, ip->de_Name, 11);
   1100  1.52.10.2  christos 		memcpy(ip->de_Name, toname, 11);	/* update denode */
   1101  1.52.10.2  christos 		dp->de_fndoffset = to_diroffset;
   1102  1.52.10.2  christos 		dp->de_fndcnt = to_count;
   1103  1.52.10.2  christos 		error = createde(ip, dp, (struct denode **)0, tcnp);
   1104  1.52.10.2  christos 		if (error) {
   1105  1.52.10.2  christos 			memcpy(ip->de_Name, oldname, 11);
   1106  1.52.10.2  christos 			VOP_UNLOCK(fvp, 0);
   1107  1.52.10.2  christos 			goto bad;
   1108  1.52.10.2  christos 		}
   1109  1.52.10.2  christos 		ip->de_refcnt++;
   1110  1.52.10.2  christos 		zp->de_fndoffset = from_diroffset;
   1111  1.52.10.2  christos 		if ((error = removede(zp, ip)) != 0) {
   1112  1.52.10.2  christos 			/* XXX should really panic here, fs is corrupt */
   1113  1.52.10.2  christos 			VOP_UNLOCK(fvp, 0);
   1114  1.52.10.2  christos 			goto bad;
   1115  1.52.10.2  christos 		}
   1116  1.52.10.2  christos 		cache_purge(fvp);
   1117  1.52.10.2  christos 		if (!doingdirectory) {
   1118  1.52.10.2  christos 			error = pcbmap(dp, de_cluster(pmp, to_diroffset), 0,
   1119  1.52.10.2  christos 				       &ip->de_dirclust, 0);
   1120  1.52.10.2  christos 			if (error) {
   1121  1.52.10.2  christos 				/* XXX should really panic here, fs is corrupt */
   1122  1.52.10.2  christos 				VOP_UNLOCK(fvp, 0);
   1123  1.52.10.2  christos 				goto bad;
   1124  1.52.10.2  christos 			}
   1125  1.52.10.2  christos 			ip->de_diroffset = to_diroffset;
   1126  1.52.10.2  christos 			if (ip->de_dirclust != MSDOSFSROOT)
   1127  1.52.10.2  christos 				ip->de_diroffset &= pmp->pm_crbomask;
   1128  1.52.10.2  christos 		}
   1129  1.52.10.2  christos 		reinsert(ip);
   1130  1.52.10.2  christos 	}
   1131  1.52.10.2  christos 
   1132  1.52.10.2  christos 	/*
   1133  1.52.10.2  christos 	 * If we moved a directory to a new parent directory, then we must
   1134  1.52.10.2  christos 	 * fixup the ".." entry in the moved directory.
   1135  1.52.10.2  christos 	 */
   1136  1.52.10.2  christos 	if (doingdirectory && newparent) {
   1137  1.52.10.2  christos 		cn = ip->de_StartCluster;
   1138  1.52.10.2  christos 		if (cn == MSDOSFSROOT) {
   1139  1.52.10.2  christos 			/* this should never happen */
   1140  1.52.10.2  christos 			panic("msdosfs_rename: updating .. in root directory?");
   1141  1.52.10.2  christos 		} else
   1142  1.52.10.2  christos 			bn = cntobn(pmp, cn);
   1143  1.52.10.2  christos 		error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn),
   1144  1.52.10.2  christos 		    pmp->pm_bpcluster, NOCRED, B_MODIFY, &bp);
   1145  1.52.10.2  christos 		if (error) {
   1146  1.52.10.2  christos 			/* XXX should really panic here, fs is corrupt */
   1147  1.52.10.2  christos 			brelse(bp, 0);
   1148  1.52.10.2  christos 			VOP_UNLOCK(fvp, 0);
   1149  1.52.10.2  christos 			goto bad;
   1150  1.52.10.2  christos 		}
   1151  1.52.10.2  christos 		dotdotp = (struct direntry *)bp->b_data + 1;
   1152  1.52.10.2  christos 		putushort(dotdotp->deStartCluster, dp->de_StartCluster);
   1153  1.52.10.2  christos 		if (FAT32(pmp)) {
   1154  1.52.10.2  christos 			putushort(dotdotp->deHighClust,
   1155  1.52.10.2  christos 				dp->de_StartCluster >> 16);
   1156  1.52.10.2  christos 		} else {
   1157  1.52.10.2  christos 			putushort(dotdotp->deHighClust, 0);
   1158  1.52.10.2  christos 		}
   1159  1.52.10.2  christos 		if ((error = bwrite(bp)) != 0) {
   1160  1.52.10.2  christos 			/* XXX should really panic here, fs is corrupt */
   1161  1.52.10.2  christos 			VOP_UNLOCK(fvp, 0);
   1162  1.52.10.2  christos 			goto bad;
   1163  1.52.10.2  christos 		}
   1164  1.52.10.2  christos 	}
   1165  1.52.10.2  christos 
   1166  1.52.10.2  christos 	VN_KNOTE(fvp, NOTE_RENAME);
   1167  1.52.10.2  christos 	VOP_UNLOCK(fvp, 0);
   1168  1.52.10.2  christos bad:
   1169  1.52.10.2  christos 	if (tvp)
   1170  1.52.10.2  christos 		vput(tvp);
   1171  1.52.10.2  christos 	vrele(tdvp);
   1172  1.52.10.2  christos out:
   1173  1.52.10.2  christos 	ip->de_flag &= ~DE_RENAME;
   1174  1.52.10.2  christos 	if (fdvp_dorele)
   1175  1.52.10.2  christos 		vrele(fdvp);
   1176  1.52.10.2  christos 	vrele(fvp);
   1177  1.52.10.2  christos 	return (error);
   1178  1.52.10.2  christos 
   1179  1.52.10.2  christos 	/* XXX: uuuh */
   1180  1.52.10.2  christos tdvpbad:
   1181  1.52.10.2  christos 	VOP_UNLOCK(tdvp, 0);
   1182  1.52.10.2  christos 	goto bad;
   1183  1.52.10.2  christos }
   1184  1.52.10.2  christos 
   1185  1.52.10.2  christos static const struct {
   1186  1.52.10.2  christos 	struct direntry dot;
   1187  1.52.10.2  christos 	struct direntry dotdot;
   1188  1.52.10.2  christos } dosdirtemplate = {
   1189  1.52.10.2  christos 	{	".       ", "   ",			/* the . entry */
   1190  1.52.10.2  christos 		ATTR_DIRECTORY,				/* file attribute */
   1191  1.52.10.2  christos 		0,	 				/* reserved */
   1192  1.52.10.2  christos 		0, { 0, 0 }, { 0, 0 },			/* create time & date */
   1193  1.52.10.2  christos 		{ 0, 0 },				/* access date */
   1194  1.52.10.2  christos 		{ 0, 0 },				/* high bits of start cluster */
   1195  1.52.10.2  christos 		{ 210, 4 }, { 210, 4 },			/* modify time & date */
   1196  1.52.10.2  christos 		{ 0, 0 },				/* startcluster */
   1197  1.52.10.2  christos 		{ 0, 0, 0, 0 } 				/* filesize */
   1198  1.52.10.2  christos 	},
   1199  1.52.10.2  christos 	{	"..      ", "   ",			/* the .. entry */
   1200  1.52.10.2  christos 		ATTR_DIRECTORY,				/* file attribute */
   1201  1.52.10.2  christos 		0,	 				/* reserved */
   1202  1.52.10.2  christos 		0, { 0, 0 }, { 0, 0 },			/* create time & date */
   1203  1.52.10.2  christos 		{ 0, 0 },				/* access date */
   1204  1.52.10.2  christos 		{ 0, 0 },				/* high bits of start cluster */
   1205  1.52.10.2  christos 		{ 210, 4 }, { 210, 4 },			/* modify time & date */
   1206  1.52.10.2  christos 		{ 0, 0 },				/* startcluster */
   1207  1.52.10.2  christos 		{ 0, 0, 0, 0 }				/* filesize */
   1208  1.52.10.2  christos 	}
   1209  1.52.10.2  christos };
   1210  1.52.10.2  christos 
   1211  1.52.10.2  christos int
   1212  1.52.10.2  christos msdosfs_mkdir(v)
   1213  1.52.10.2  christos 	void *v;
   1214  1.52.10.2  christos {
   1215  1.52.10.2  christos 	struct vop_mkdir_args /* {
   1216  1.52.10.2  christos 		struct vnode *a_dvp;
   1217  1.52.10.2  christos 		struvt vnode **a_vpp;
   1218  1.52.10.2  christos 		struvt componentname *a_cnp;
   1219  1.52.10.2  christos 		struct vattr *a_vap;
   1220  1.52.10.2  christos 	} */ *ap = v;
   1221  1.52.10.2  christos 	struct componentname *cnp = ap->a_cnp;
   1222  1.52.10.2  christos 	struct denode ndirent;
   1223  1.52.10.2  christos 	struct denode *dep;
   1224  1.52.10.2  christos 	struct denode *pdep = VTODE(ap->a_dvp);
   1225  1.52.10.2  christos 	int error;
   1226  1.52.10.2  christos 	int bn;
   1227  1.52.10.2  christos 	u_long newcluster, pcl;
   1228  1.52.10.2  christos 	daddr_t lbn;
   1229  1.52.10.2  christos 	struct direntry *denp;
   1230  1.52.10.2  christos 	struct msdosfsmount *pmp = pdep->de_pmp;
   1231  1.52.10.2  christos 	struct buf *bp;
   1232  1.52.10.2  christos 	int async = pdep->de_pmp->pm_mountp->mnt_flag & MNT_ASYNC;
   1233  1.52.10.2  christos 
   1234  1.52.10.2  christos 	/*
   1235  1.52.10.2  christos 	 * If this is the root directory and there is no space left we
   1236  1.52.10.2  christos 	 * can't do anything.  This is because the root directory can not
   1237  1.52.10.2  christos 	 * change size.
   1238  1.52.10.2  christos 	 */
   1239  1.52.10.2  christos 	if (pdep->de_StartCluster == MSDOSFSROOT
   1240  1.52.10.2  christos 	    && pdep->de_fndoffset >= pdep->de_FileSize) {
   1241  1.52.10.2  christos 		error = ENOSPC;
   1242  1.52.10.2  christos 		goto bad2;
   1243  1.52.10.2  christos 	}
   1244  1.52.10.2  christos 
   1245  1.52.10.2  christos 	/*
   1246  1.52.10.2  christos 	 * Allocate a cluster to hold the about to be created directory.
   1247  1.52.10.2  christos 	 */
   1248  1.52.10.2  christos 	error = clusteralloc(pmp, 0, 1, &newcluster, NULL);
   1249  1.52.10.2  christos 	if (error)
   1250  1.52.10.2  christos 		goto bad2;
   1251  1.52.10.2  christos 
   1252  1.52.10.2  christos 	memset(&ndirent, 0, sizeof(ndirent));
   1253  1.52.10.2  christos 	ndirent.de_pmp = pmp;
   1254  1.52.10.2  christos 	ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
   1255  1.52.10.2  christos 	DETIMES(&ndirent, NULL, NULL, NULL, pmp->pm_gmtoff);
   1256  1.52.10.2  christos 
   1257  1.52.10.2  christos 	/*
   1258  1.52.10.2  christos 	 * Now fill the cluster with the "." and ".." entries. And write
   1259  1.52.10.2  christos 	 * the cluster to disk.  This way it is there for the parent
   1260  1.52.10.2  christos 	 * directory to be pointing at if there were a crash.
   1261  1.52.10.2  christos 	 */
   1262  1.52.10.2  christos 	bn = cntobn(pmp, newcluster);
   1263  1.52.10.2  christos 	lbn = de_bn2kb(pmp, bn);
   1264  1.52.10.2  christos 	/* always succeeds */
   1265  1.52.10.2  christos 	bp = getblk(pmp->pm_devvp, lbn, pmp->pm_bpcluster, 0, 0);
   1266  1.52.10.2  christos 	memset(bp->b_data, 0, pmp->pm_bpcluster);
   1267  1.52.10.2  christos 	memcpy(bp->b_data, &dosdirtemplate, sizeof dosdirtemplate);
   1268  1.52.10.2  christos 	denp = (struct direntry *)bp->b_data;
   1269  1.52.10.2  christos 	putushort(denp[0].deStartCluster, newcluster);
   1270  1.52.10.2  christos 	putushort(denp[0].deCDate, ndirent.de_CDate);
   1271  1.52.10.2  christos 	putushort(denp[0].deCTime, ndirent.de_CTime);
   1272  1.52.10.2  christos 	denp[0].deCHundredth = ndirent.de_CHun;
   1273  1.52.10.2  christos 	putushort(denp[0].deADate, ndirent.de_ADate);
   1274  1.52.10.2  christos 	putushort(denp[0].deMDate, ndirent.de_MDate);
   1275  1.52.10.2  christos 	putushort(denp[0].deMTime, ndirent.de_MTime);
   1276  1.52.10.2  christos 	pcl = pdep->de_StartCluster;
   1277  1.52.10.2  christos 	if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
   1278  1.52.10.2  christos 		pcl = 0;
   1279  1.52.10.2  christos 	putushort(denp[1].deStartCluster, pcl);
   1280  1.52.10.2  christos 	putushort(denp[1].deCDate, ndirent.de_CDate);
   1281  1.52.10.2  christos 	putushort(denp[1].deCTime, ndirent.de_CTime);
   1282  1.52.10.2  christos 	denp[1].deCHundredth = ndirent.de_CHun;
   1283  1.52.10.2  christos 	putushort(denp[1].deADate, ndirent.de_ADate);
   1284  1.52.10.2  christos 	putushort(denp[1].deMDate, ndirent.de_MDate);
   1285  1.52.10.2  christos 	putushort(denp[1].deMTime, ndirent.de_MTime);
   1286  1.52.10.2  christos 	if (FAT32(pmp)) {
   1287  1.52.10.2  christos 		putushort(denp[0].deHighClust, newcluster >> 16);
   1288  1.52.10.2  christos 		putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
   1289  1.52.10.2  christos 	} else {
   1290  1.52.10.2  christos 		putushort(denp[0].deHighClust, 0);
   1291  1.52.10.2  christos 		putushort(denp[1].deHighClust, 0);
   1292  1.52.10.2  christos 	}
   1293  1.52.10.2  christos 
   1294  1.52.10.2  christos 	if (async)
   1295  1.52.10.2  christos 		bdwrite(bp);
   1296  1.52.10.2  christos 	else if ((error = bwrite(bp)) != 0)
   1297  1.52.10.2  christos 		goto bad;
   1298  1.52.10.2  christos 
   1299  1.52.10.2  christos 	/*
   1300  1.52.10.2  christos 	 * Now build up a directory entry pointing to the newly allocated
   1301  1.52.10.2  christos 	 * cluster.  This will be written to an empty slot in the parent
   1302  1.52.10.2  christos 	 * directory.
   1303  1.52.10.2  christos 	 */
   1304  1.52.10.2  christos #ifdef DIAGNOSTIC
   1305  1.52.10.2  christos 	if ((cnp->cn_flags & HASBUF) == 0)
   1306  1.52.10.2  christos 		panic("msdosfs_mkdir: no name");
   1307  1.52.10.2  christos #endif
   1308  1.52.10.2  christos 	if ((error = uniqdosname(pdep, cnp, ndirent.de_Name)) != 0)
   1309  1.52.10.2  christos 		goto bad;
   1310  1.52.10.2  christos 
   1311  1.52.10.2  christos 	ndirent.de_Attributes = ATTR_DIRECTORY;
   1312  1.52.10.2  christos 	ndirent.de_StartCluster = newcluster;
   1313  1.52.10.2  christos 	ndirent.de_FileSize = 0;
   1314  1.52.10.2  christos 	ndirent.de_dev = pdep->de_dev;
   1315  1.52.10.2  christos 	ndirent.de_devvp = pdep->de_devvp;
   1316  1.52.10.2  christos 	if ((error = createde(&ndirent, pdep, &dep, cnp)) != 0)
   1317  1.52.10.2  christos 		goto bad;
   1318  1.52.10.2  christos 	if ((cnp->cn_flags & SAVESTART) == 0)
   1319  1.52.10.2  christos 		PNBUF_PUT(cnp->cn_pnbuf);
   1320  1.52.10.2  christos 	VN_KNOTE(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
   1321  1.52.10.2  christos 	vput(ap->a_dvp);
   1322  1.52.10.2  christos 	*ap->a_vpp = DETOV(dep);
   1323  1.52.10.2  christos 	return (0);
   1324  1.52.10.2  christos 
   1325  1.52.10.2  christos bad:
   1326  1.52.10.2  christos 	clusterfree(pmp, newcluster, NULL);
   1327  1.52.10.2  christos bad2:
   1328  1.52.10.2  christos 	PNBUF_PUT(cnp->cn_pnbuf);
   1329  1.52.10.2  christos 	vput(ap->a_dvp);
   1330  1.52.10.2  christos 	return (error);
   1331  1.52.10.2  christos }
   1332  1.52.10.2  christos 
   1333  1.52.10.2  christos int
   1334  1.52.10.2  christos msdosfs_rmdir(v)
   1335  1.52.10.2  christos 	void *v;
   1336  1.52.10.2  christos {
   1337  1.52.10.2  christos 	struct vop_rmdir_args /* {
   1338  1.52.10.2  christos 		struct vnode *a_dvp;
   1339  1.52.10.2  christos 		struct vnode *a_vp;
   1340  1.52.10.2  christos 		struct componentname *a_cnp;
   1341  1.52.10.2  christos 	} */ *ap = v;
   1342  1.52.10.2  christos 	struct vnode *vp = ap->a_vp;
   1343  1.52.10.2  christos 	struct vnode *dvp = ap->a_dvp;
   1344  1.52.10.2  christos 	struct componentname *cnp = ap->a_cnp;
   1345  1.52.10.2  christos 	struct denode *ip, *dp;
   1346  1.52.10.2  christos 	int error;
   1347  1.52.10.2  christos 
   1348  1.52.10.2  christos 	ip = VTODE(vp);
   1349  1.52.10.2  christos 	dp = VTODE(dvp);
   1350  1.52.10.2  christos 	/*
   1351  1.52.10.2  christos 	 * No rmdir "." please.
   1352  1.52.10.2  christos 	 */
   1353  1.52.10.2  christos 	if (dp == ip) {
   1354  1.52.10.2  christos 		vrele(dvp);
   1355  1.52.10.2  christos 		vput(vp);
   1356  1.52.10.2  christos 		return (EINVAL);
   1357  1.52.10.2  christos 	}
   1358  1.52.10.2  christos 	/*
   1359  1.52.10.2  christos 	 * Verify the directory is empty (and valid).
   1360  1.52.10.2  christos 	 * (Rmdir ".." won't be valid since
   1361  1.52.10.2  christos 	 *  ".." will contain a reference to
   1362  1.52.10.2  christos 	 *  the current directory and thus be
   1363  1.52.10.2  christos 	 *  non-empty.)
   1364  1.52.10.2  christos 	 */
   1365  1.52.10.2  christos 	error = 0;
   1366  1.52.10.2  christos 	if (!dosdirempty(ip) || ip->de_flag & DE_RENAME) {
   1367  1.52.10.2  christos 		error = ENOTEMPTY;
   1368  1.52.10.2  christos 		goto out;
   1369  1.52.10.2  christos 	}
   1370  1.52.10.2  christos 	/*
   1371  1.52.10.2  christos 	 * Delete the entry from the directory.  For dos filesystems this
   1372  1.52.10.2  christos 	 * gets rid of the directory entry on disk, the in memory copy
   1373  1.52.10.2  christos 	 * still exists but the de_refcnt is <= 0.  This prevents it from
   1374  1.52.10.2  christos 	 * being found by deget().  When the vput() on dep is done we give
   1375  1.52.10.2  christos 	 * up access and eventually msdosfs_reclaim() will be called which
   1376  1.52.10.2  christos 	 * will remove it from the denode cache.
   1377  1.52.10.2  christos 	 */
   1378  1.52.10.2  christos 	if ((error = removede(dp, ip)) != 0)
   1379  1.52.10.2  christos 		goto out;
   1380  1.52.10.2  christos 	/*
   1381  1.52.10.2  christos 	 * This is where we decrement the link count in the parent
   1382  1.52.10.2  christos 	 * directory.  Since dos filesystems don't do this we just purge
   1383  1.52.10.2  christos 	 * the name cache and let go of the parent directory denode.
   1384  1.52.10.2  christos 	 */
   1385  1.52.10.2  christos 	VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
   1386  1.52.10.2  christos 	cache_purge(dvp);
   1387  1.52.10.2  christos 	vput(dvp);
   1388  1.52.10.2  christos 	dvp = NULL;
   1389  1.52.10.2  christos 	/*
   1390  1.52.10.2  christos 	 * Truncate the directory that is being deleted.
   1391  1.52.10.2  christos 	 */
   1392  1.52.10.2  christos 	error = detrunc(ip, (u_long)0, IO_SYNC, cnp->cn_cred);
   1393  1.52.10.2  christos 	cache_purge(vp);
   1394  1.52.10.2  christos out:
   1395  1.52.10.2  christos 	VN_KNOTE(vp, NOTE_DELETE);
   1396  1.52.10.2  christos 	if (dvp)
   1397  1.52.10.2  christos 		vput(dvp);
   1398  1.52.10.2  christos 	vput(vp);
   1399  1.52.10.2  christos 	return (error);
   1400  1.52.10.2  christos }
   1401  1.52.10.2  christos 
   1402  1.52.10.2  christos /*
   1403  1.52.10.2  christos  * DOS filesystems don't know what symlinks are.
   1404  1.52.10.2  christos  */
   1405  1.52.10.2  christos int
   1406  1.52.10.2  christos msdosfs_symlink(v)
   1407  1.52.10.2  christos 	void *v;
   1408  1.52.10.2  christos {
   1409  1.52.10.2  christos 	struct vop_symlink_args /* {
   1410  1.52.10.2  christos 		struct vnode *a_dvp;
   1411  1.52.10.2  christos 		struct vnode **a_vpp;
   1412  1.52.10.2  christos 		struct componentname *a_cnp;
   1413  1.52.10.2  christos 		struct vattr *a_vap;
   1414  1.52.10.2  christos 		char *a_target;
   1415  1.52.10.2  christos 	} */ *ap = v;
   1416  1.52.10.2  christos 
   1417  1.52.10.2  christos 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
   1418  1.52.10.2  christos 	vput(ap->a_dvp);
   1419  1.52.10.2  christos 	return (EOPNOTSUPP);
   1420  1.52.10.2  christos }
   1421  1.52.10.2  christos 
   1422  1.52.10.2  christos int
   1423  1.52.10.2  christos msdosfs_readdir(v)
   1424  1.52.10.2  christos 	void *v;
   1425  1.52.10.2  christos {
   1426  1.52.10.2  christos 	struct vop_readdir_args /* {
   1427  1.52.10.2  christos 		struct vnode *a_vp;
   1428  1.52.10.2  christos 		struct uio *a_uio;
   1429  1.52.10.2  christos 		kauth_cred_t a_cred;
   1430  1.52.10.2  christos 		int *a_eofflag;
   1431  1.52.10.2  christos 		off_t **a_cookies;
   1432  1.52.10.2  christos 		int *a_ncookies;
   1433  1.52.10.2  christos 	} */ *ap = v;
   1434  1.52.10.2  christos 	int error = 0;
   1435  1.52.10.2  christos 	int diff;
   1436  1.52.10.2  christos 	long n;
   1437  1.52.10.2  christos 	int blsize;
   1438  1.52.10.2  christos 	long on;
   1439  1.52.10.2  christos 	long lost;
   1440  1.52.10.2  christos 	long count;
   1441  1.52.10.2  christos 	u_long cn;
   1442  1.52.10.2  christos 	ino_t fileno;
   1443  1.52.10.2  christos 	u_long dirsperblk;
   1444  1.52.10.2  christos 	long bias = 0;
   1445  1.52.10.2  christos 	daddr_t bn, lbn;
   1446  1.52.10.2  christos 	struct buf *bp;
   1447  1.52.10.2  christos 	struct denode *dep = VTODE(ap->a_vp);
   1448  1.52.10.2  christos 	struct msdosfsmount *pmp = dep->de_pmp;
   1449  1.52.10.2  christos 	struct direntry *dentp;
   1450  1.52.10.2  christos 	struct dirent *dirbuf;
   1451  1.52.10.2  christos 	struct uio *uio = ap->a_uio;
   1452  1.52.10.2  christos 	off_t *cookies = NULL;
   1453  1.52.10.2  christos 	int ncookies = 0, nc = 0;
   1454  1.52.10.2  christos 	off_t offset, uio_off;
   1455  1.52.10.2  christos 	int chksum = -1;
   1456  1.52.10.2  christos 
   1457  1.52.10.2  christos #ifdef MSDOSFS_DEBUG
   1458  1.52.10.2  christos 	printf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\n",
   1459  1.52.10.2  christos 	    ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
   1460  1.52.10.2  christos #endif
   1461  1.52.10.2  christos 
   1462  1.52.10.2  christos 	/*
   1463  1.52.10.2  christos 	 * msdosfs_readdir() won't operate properly on regular files since
   1464  1.52.10.2  christos 	 * it does i/o only with the filesystem vnode, and hence can
   1465  1.52.10.2  christos 	 * retrieve the wrong block from the buffer cache for a plain file.
   1466  1.52.10.2  christos 	 * So, fail attempts to readdir() on a plain file.
   1467  1.52.10.2  christos 	 */
   1468  1.52.10.2  christos 	if ((dep->de_Attributes & ATTR_DIRECTORY) == 0)
   1469  1.52.10.2  christos 		return (ENOTDIR);
   1470  1.52.10.2  christos 
   1471  1.52.10.2  christos 	/*
   1472  1.52.10.2  christos 	 * If the user buffer is smaller than the size of one dos directory
   1473  1.52.10.2  christos 	 * entry or the file offset is not a multiple of the size of a
   1474  1.52.10.2  christos 	 * directory entry, then we fail the read.
   1475  1.52.10.2  christos 	 */
   1476  1.52.10.2  christos 	count = uio->uio_resid & ~(sizeof(struct direntry) - 1);
   1477  1.52.10.2  christos 	offset = uio->uio_offset;
   1478  1.52.10.2  christos 	if (count < sizeof(struct direntry) ||
   1479  1.52.10.2  christos 	    (offset & (sizeof(struct direntry) - 1)))
   1480  1.52.10.2  christos 		return (EINVAL);
   1481  1.52.10.2  christos 	lost = uio->uio_resid - count;
   1482  1.52.10.2  christos 	uio->uio_resid = count;
   1483  1.52.10.2  christos 	uio_off = uio->uio_offset;
   1484  1.52.10.2  christos 
   1485  1.52.10.2  christos 	/* Allocate a temporary dirent buffer. */
   1486  1.52.10.2  christos 	dirbuf = malloc(sizeof(struct dirent), M_MSDOSFSTMP, M_WAITOK | M_ZERO);
   1487  1.52.10.2  christos 
   1488  1.52.10.2  christos 	if (ap->a_ncookies) {
   1489  1.52.10.2  christos 		nc = uio->uio_resid / _DIRENT_MINSIZE((struct dirent *)0);
   1490  1.52.10.2  christos 		cookies = malloc(nc * sizeof (off_t), M_TEMP, M_WAITOK);
   1491  1.52.10.2  christos 		*ap->a_cookies = cookies;
   1492  1.52.10.2  christos 	}
   1493  1.52.10.2  christos 
   1494  1.52.10.2  christos 	dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
   1495  1.52.10.2  christos 
   1496  1.52.10.2  christos 	/*
   1497  1.52.10.2  christos 	 * If they are reading from the root directory then, we simulate
   1498  1.52.10.2  christos 	 * the . and .. entries since these don't exist in the root
   1499  1.52.10.2  christos 	 * directory.  We also set the offset bias to make up for having to
   1500  1.52.10.2  christos 	 * simulate these entries. By this I mean that at file offset 64 we
   1501  1.52.10.2  christos 	 * read the first entry in the root directory that lives on disk.
   1502  1.52.10.2  christos 	 */
   1503  1.52.10.2  christos 	if (dep->de_StartCluster == MSDOSFSROOT
   1504  1.52.10.2  christos 	    || (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)) {
   1505  1.52.10.2  christos #if 0
   1506  1.52.10.2  christos 		printf("msdosfs_readdir(): going after . or .. in root dir, "
   1507  1.52.10.2  christos 		    "offset %" PRIu64 "\n", offset);
   1508  1.52.10.2  christos #endif
   1509  1.52.10.2  christos 		bias = 2 * sizeof(struct direntry);
   1510  1.52.10.2  christos 		if (offset < bias) {
   1511  1.52.10.2  christos 			for (n = (int)offset / sizeof(struct direntry);
   1512  1.52.10.2  christos 			     n < 2; n++) {
   1513  1.52.10.2  christos 				if (FAT32(pmp))
   1514  1.52.10.2  christos 					dirbuf->d_fileno = cntobn(pmp,
   1515  1.52.10.2  christos 					     (ino_t)pmp->pm_rootdirblk)
   1516  1.52.10.2  christos 					     * dirsperblk;
   1517  1.52.10.2  christos 				else
   1518  1.52.10.2  christos 					dirbuf->d_fileno = 1;
   1519  1.52.10.2  christos 				dirbuf->d_type = DT_DIR;
   1520  1.52.10.2  christos 				switch (n) {
   1521  1.52.10.2  christos 				case 0:
   1522  1.52.10.2  christos 					dirbuf->d_namlen = 1;
   1523  1.52.10.2  christos 					strlcpy(dirbuf->d_name, ".",
   1524  1.52.10.2  christos 					    sizeof(dirbuf->d_name));
   1525  1.52.10.2  christos 					break;
   1526  1.52.10.2  christos 				case 1:
   1527  1.52.10.2  christos 					dirbuf->d_namlen = 2;
   1528  1.52.10.2  christos 					strlcpy(dirbuf->d_name, "..",
   1529  1.52.10.2  christos 					    sizeof(dirbuf->d_name));
   1530  1.52.10.2  christos 					break;
   1531  1.52.10.2  christos 				}
   1532  1.52.10.2  christos 				dirbuf->d_reclen = _DIRENT_SIZE(dirbuf);
   1533  1.52.10.2  christos 				if (uio->uio_resid < dirbuf->d_reclen)
   1534  1.52.10.2  christos 					goto out;
   1535  1.52.10.2  christos 				error = uiomove(dirbuf, dirbuf->d_reclen, uio);
   1536  1.52.10.2  christos 				if (error)
   1537  1.52.10.2  christos 					goto out;
   1538  1.52.10.2  christos 				offset += sizeof(struct direntry);
   1539  1.52.10.2  christos 				uio_off = offset;
   1540  1.52.10.2  christos 				if (cookies) {
   1541  1.52.10.2  christos 					*cookies++ = offset;
   1542  1.52.10.2  christos 					ncookies++;
   1543  1.52.10.2  christos 					if (ncookies >= nc)
   1544  1.52.10.2  christos 						goto out;
   1545  1.52.10.2  christos 				}
   1546  1.52.10.2  christos 			}
   1547  1.52.10.2  christos 		}
   1548  1.52.10.2  christos 	}
   1549  1.52.10.2  christos 
   1550  1.52.10.2  christos 	while (uio->uio_resid > 0) {
   1551  1.52.10.2  christos 		lbn = de_cluster(pmp, offset - bias);
   1552  1.52.10.2  christos 		on = (offset - bias) & pmp->pm_crbomask;
   1553  1.52.10.2  christos 		n = MIN(pmp->pm_bpcluster - on, uio->uio_resid);
   1554  1.52.10.2  christos 		diff = dep->de_FileSize - (offset - bias);
   1555  1.52.10.2  christos 		if (diff <= 0)
   1556  1.52.10.2  christos 			break;
   1557  1.52.10.2  christos 		n = MIN(n, diff);
   1558  1.52.10.2  christos 		if ((error = pcbmap(dep, lbn, &bn, &cn, &blsize)) != 0)
   1559  1.52.10.2  christos 			break;
   1560  1.52.10.2  christos 		error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
   1561  1.52.10.2  christos 		    NOCRED, 0, &bp);
   1562  1.52.10.2  christos 		if (error) {
   1563  1.52.10.2  christos 			brelse(bp, 0);
   1564  1.52.10.2  christos 			free(dirbuf, M_MSDOSFSTMP);
   1565  1.52.10.2  christos 			return (error);
   1566  1.52.10.2  christos 		}
   1567  1.52.10.2  christos 		n = MIN(n, blsize - bp->b_resid);
   1568  1.52.10.2  christos 
   1569  1.52.10.2  christos 		/*
   1570  1.52.10.2  christos 		 * Convert from dos directory entries to fs-independent
   1571  1.52.10.2  christos 		 * directory entries.
   1572  1.52.10.2  christos 		 */
   1573  1.52.10.2  christos 		for (dentp = (struct direntry *)((char *)bp->b_data + on);
   1574  1.52.10.2  christos 		     (char *)dentp < (char *)bp->b_data + on + n;
   1575  1.52.10.2  christos 		     dentp++, offset += sizeof(struct direntry)) {
   1576  1.52.10.2  christos #if 0
   1577  1.52.10.2  christos 
   1578  1.52.10.2  christos 			printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
   1579  1.52.10.2  christos 			    dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
   1580  1.52.10.2  christos #endif
   1581  1.52.10.2  christos 			/*
   1582  1.52.10.2  christos 			 * If this is an unused entry, we can stop.
   1583  1.52.10.2  christos 			 */
   1584  1.52.10.2  christos 			if (dentp->deName[0] == SLOT_EMPTY) {
   1585  1.52.10.2  christos 				brelse(bp, 0);
   1586  1.52.10.2  christos 				goto out;
   1587  1.52.10.2  christos 			}
   1588  1.52.10.2  christos 			/*
   1589  1.52.10.2  christos 			 * Skip deleted entries.
   1590  1.52.10.2  christos 			 */
   1591  1.52.10.2  christos 			if (dentp->deName[0] == SLOT_DELETED) {
   1592  1.52.10.2  christos 				chksum = -1;
   1593  1.52.10.2  christos 				continue;
   1594  1.52.10.2  christos 			}
   1595  1.52.10.2  christos 
   1596  1.52.10.2  christos 			/*
   1597  1.52.10.2  christos 			 * Handle Win95 long directory entries
   1598  1.52.10.2  christos 			 */
   1599  1.52.10.2  christos 			if (dentp->deAttributes == ATTR_WIN95) {
   1600  1.52.10.2  christos 				if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
   1601  1.52.10.2  christos 					continue;
   1602  1.52.10.2  christos 				chksum = win2unixfn((struct winentry *)dentp,
   1603  1.52.10.2  christos 				    dirbuf, chksum);
   1604  1.52.10.2  christos 				continue;
   1605  1.52.10.2  christos 			}
   1606  1.52.10.2  christos 
   1607  1.52.10.2  christos 			/*
   1608  1.52.10.2  christos 			 * Skip volume labels
   1609  1.52.10.2  christos 			 */
   1610  1.52.10.2  christos 			if (dentp->deAttributes & ATTR_VOLUME) {
   1611  1.52.10.2  christos 				chksum = -1;
   1612  1.52.10.2  christos 				continue;
   1613  1.52.10.2  christos 			}
   1614  1.52.10.2  christos 			/*
   1615  1.52.10.2  christos 			 * This computation of d_fileno must match
   1616  1.52.10.2  christos 			 * the computation of va_fileid in
   1617  1.52.10.2  christos 			 * msdosfs_getattr.
   1618  1.52.10.2  christos 			 */
   1619  1.52.10.2  christos 			if (dentp->deAttributes & ATTR_DIRECTORY) {
   1620  1.52.10.2  christos 				fileno = getushort(dentp->deStartCluster);
   1621  1.52.10.2  christos 				if (FAT32(pmp))
   1622  1.52.10.2  christos 					fileno |= ((ino_t)getushort(dentp->deHighClust)) << 16;
   1623  1.52.10.2  christos 				/* if this is the root directory */
   1624  1.52.10.2  christos 				if (fileno == MSDOSFSROOT)
   1625  1.52.10.2  christos 					if (FAT32(pmp))
   1626  1.52.10.2  christos 						fileno = cntobn(pmp,
   1627  1.52.10.2  christos 						    (ino_t)pmp->pm_rootdirblk)
   1628  1.52.10.2  christos 						    * dirsperblk;
   1629  1.52.10.2  christos 					else
   1630  1.52.10.2  christos 						fileno = 1;
   1631  1.52.10.2  christos 				else
   1632  1.52.10.2  christos 					fileno = cntobn(pmp, fileno) * dirsperblk;
   1633  1.52.10.2  christos 				dirbuf->d_fileno = fileno;
   1634  1.52.10.2  christos 				dirbuf->d_type = DT_DIR;
   1635  1.52.10.2  christos 			} else {
   1636  1.52.10.2  christos 				dirbuf->d_fileno =
   1637  1.52.10.2  christos 				    offset / sizeof(struct direntry);
   1638  1.52.10.2  christos 				dirbuf->d_type = DT_REG;
   1639  1.52.10.2  christos 			}
   1640  1.52.10.2  christos 			if (chksum != winChksum(dentp->deName))
   1641  1.52.10.2  christos 				dirbuf->d_namlen = dos2unixfn(dentp->deName,
   1642  1.52.10.2  christos 				    (u_char *)dirbuf->d_name,
   1643  1.52.10.2  christos 				    pmp->pm_flags & MSDOSFSMNT_SHORTNAME);
   1644  1.52.10.2  christos 			else
   1645  1.52.10.2  christos 				dirbuf->d_name[dirbuf->d_namlen] = 0;
   1646  1.52.10.2  christos 			chksum = -1;
   1647  1.52.10.2  christos 			dirbuf->d_reclen = _DIRENT_SIZE(dirbuf);
   1648  1.52.10.2  christos 			if (uio->uio_resid < dirbuf->d_reclen) {
   1649  1.52.10.2  christos 				brelse(bp, 0);
   1650  1.52.10.2  christos 				goto out;
   1651  1.52.10.2  christos 			}
   1652  1.52.10.2  christos 			error = uiomove(dirbuf, dirbuf->d_reclen, uio);
   1653  1.52.10.2  christos 			if (error) {
   1654  1.52.10.2  christos 				brelse(bp, 0);
   1655  1.52.10.2  christos 				goto out;
   1656  1.52.10.2  christos 			}
   1657  1.52.10.2  christos 			uio_off = offset + sizeof(struct direntry);
   1658  1.52.10.2  christos 			if (cookies) {
   1659  1.52.10.2  christos 				*cookies++ = offset + sizeof(struct direntry);
   1660  1.52.10.2  christos 				ncookies++;
   1661  1.52.10.2  christos 				if (ncookies >= nc) {
   1662  1.52.10.2  christos 					brelse(bp, 0);
   1663  1.52.10.2  christos 					goto out;
   1664  1.52.10.2  christos 				}
   1665  1.52.10.2  christos 			}
   1666  1.52.10.2  christos 		}
   1667  1.52.10.2  christos 		brelse(bp, 0);
   1668  1.52.10.2  christos 	}
   1669  1.52.10.2  christos 
   1670  1.52.10.2  christos out:
   1671  1.52.10.2  christos 	uio->uio_offset = uio_off;
   1672  1.52.10.2  christos 	uio->uio_resid += lost;
   1673  1.52.10.2  christos 	if (dep->de_FileSize - (offset - bias) <= 0)
   1674  1.52.10.2  christos 		*ap->a_eofflag = 1;
   1675  1.52.10.2  christos 	else
   1676  1.52.10.2  christos 		*ap->a_eofflag = 0;
   1677  1.52.10.2  christos 
   1678  1.52.10.2  christos 	if (ap->a_ncookies) {
   1679  1.52.10.2  christos 		if (error) {
   1680  1.52.10.2  christos 			free(*ap->a_cookies, M_TEMP);
   1681  1.52.10.2  christos 			*ap->a_ncookies = 0;
   1682  1.52.10.2  christos 			*ap->a_cookies = NULL;
   1683  1.52.10.2  christos 		} else
   1684  1.52.10.2  christos 			*ap->a_ncookies = ncookies;
   1685  1.52.10.2  christos 	}
   1686  1.52.10.2  christos 	free(dirbuf, M_MSDOSFSTMP);
   1687  1.52.10.2  christos 	return (error);
   1688  1.52.10.2  christos }
   1689  1.52.10.2  christos 
   1690  1.52.10.2  christos /*
   1691  1.52.10.2  christos  * DOS filesystems don't know what symlinks are.
   1692  1.52.10.2  christos  */
   1693  1.52.10.2  christos int
   1694  1.52.10.2  christos msdosfs_readlink(void *v)
   1695  1.52.10.2  christos {
   1696  1.52.10.2  christos #if 0
   1697  1.52.10.2  christos 	struct vop_readlink_args /* {
   1698  1.52.10.2  christos 		struct vnode *a_vp;
   1699  1.52.10.2  christos 		struct uio *a_uio;
   1700  1.52.10.2  christos 		kauth_cred_t a_cred;
   1701  1.52.10.2  christos 	} */ *ap;
   1702  1.52.10.2  christos #endif
   1703  1.52.10.2  christos 
   1704  1.52.10.2  christos 	return (EINVAL);
   1705  1.52.10.2  christos }
   1706  1.52.10.2  christos 
   1707  1.52.10.2  christos /*
   1708  1.52.10.2  christos  * vp  - address of vnode file the file
   1709  1.52.10.2  christos  * bn  - which cluster we are interested in mapping to a filesystem block number.
   1710  1.52.10.2  christos  * vpp - returns the vnode for the block special file holding the filesystem
   1711  1.52.10.2  christos  *	 containing the file of interest
   1712  1.52.10.2  christos  * bnp - address of where to return the filesystem relative block number
   1713  1.52.10.2  christos  */
   1714  1.52.10.2  christos int
   1715  1.52.10.2  christos msdosfs_bmap(v)
   1716  1.52.10.2  christos 	void *v;
   1717  1.52.10.2  christos {
   1718  1.52.10.2  christos 	struct vop_bmap_args /* {
   1719  1.52.10.2  christos 		struct vnode *a_vp;
   1720  1.52.10.2  christos 		daddr_t a_bn;
   1721  1.52.10.2  christos 		struct vnode **a_vpp;
   1722  1.52.10.2  christos 		daddr_t *a_bnp;
   1723  1.52.10.2  christos 		int *a_runp;
   1724  1.52.10.2  christos 	} */ *ap = v;
   1725  1.52.10.2  christos 	struct denode *dep = VTODE(ap->a_vp);
   1726  1.52.10.2  christos 	int run, maxrun;
   1727  1.52.10.2  christos 	daddr_t runbn;
   1728  1.52.10.2  christos 	int status;
   1729  1.52.10.2  christos 
   1730  1.52.10.2  christos 	if (ap->a_vpp != NULL)
   1731  1.52.10.2  christos 		*ap->a_vpp = dep->de_devvp;
   1732  1.52.10.2  christos 	if (ap->a_bnp == NULL)
   1733  1.52.10.2  christos 		return (0);
   1734  1.52.10.2  christos 	status = pcbmap(dep, ap->a_bn, ap->a_bnp, 0, 0);
   1735  1.52.10.2  christos 
   1736  1.52.10.2  christos 	/*
   1737  1.52.10.2  christos 	 * From FreeBSD:
   1738  1.52.10.2  christos 	 * A little kludgy, but we loop calling pcbmap until we
   1739  1.52.10.2  christos 	 * reach the end of the contiguous piece, or reach MAXPHYS.
   1740  1.52.10.2  christos 	 * Since it reduces disk I/Os, the "wasted" CPU is put to
   1741  1.52.10.2  christos 	 * good use (4 to 5 fold sequential read I/O improvement on USB
   1742  1.52.10.2  christos 	 * drives).
   1743  1.52.10.2  christos 	 */
   1744  1.52.10.2  christos 	if (ap->a_runp != NULL) {
   1745  1.52.10.2  christos 		/* taken from ufs_bmap */
   1746  1.52.10.2  christos 		maxrun = ulmin(MAXPHYS / dep->de_pmp->pm_bpcluster - 1,
   1747  1.52.10.2  christos 			       dep->de_pmp->pm_maxcluster - ap->a_bn);
   1748  1.52.10.2  christos 		for (run = 1; run <= maxrun; run++) {
   1749  1.52.10.2  christos 			if (pcbmap(dep, ap->a_bn + run, &runbn, NULL, NULL)
   1750  1.52.10.2  christos 			    != 0 || runbn !=
   1751  1.52.10.2  christos 			            *ap->a_bnp + de_cn2bn(dep->de_pmp, run))
   1752  1.52.10.2  christos 				break;
   1753  1.52.10.2  christos 		}
   1754  1.52.10.2  christos 		*ap->a_runp = run - 1;
   1755  1.52.10.2  christos 	}
   1756  1.52.10.2  christos 
   1757  1.52.10.2  christos 	/*
   1758  1.52.10.2  christos 	 * We need to scale *ap->a_bnp by sector_size/DEV_BSIZE
   1759  1.52.10.2  christos 	 */
   1760  1.52.10.2  christos 	*ap->a_bnp = de_bn2kb(dep->de_pmp, *ap->a_bnp);
   1761  1.52.10.2  christos 	return status;
   1762  1.52.10.2  christos }
   1763  1.52.10.2  christos 
   1764  1.52.10.2  christos int
   1765  1.52.10.2  christos msdosfs_strategy(v)
   1766  1.52.10.2  christos 	void *v;
   1767  1.52.10.2  christos {
   1768  1.52.10.2  christos 	struct vop_strategy_args /* {
   1769  1.52.10.2  christos 		struct vnode *a_vp;
   1770  1.52.10.2  christos 		struct buf *a_bp;
   1771  1.52.10.2  christos 	} */ *ap = v;
   1772  1.52.10.2  christos 	struct vnode *vp = ap->a_vp;
   1773  1.52.10.2  christos 	struct buf *bp = ap->a_bp;
   1774  1.52.10.2  christos 	struct denode *dep = VTODE(bp->b_vp);
   1775  1.52.10.2  christos 	int error = 0;
   1776  1.52.10.2  christos 
   1777  1.52.10.2  christos 	if (vp->v_type == VBLK || vp->v_type == VCHR)
   1778  1.52.10.2  christos 		panic("msdosfs_strategy: spec");
   1779  1.52.10.2  christos 	/*
   1780  1.52.10.2  christos 	 * If we don't already know the filesystem relative block number
   1781  1.52.10.2  christos 	 * then get it using pcbmap().  If pcbmap() returns the block
   1782  1.52.10.2  christos 	 * number as -1 then we've got a hole in the file.  DOS filesystems
   1783  1.52.10.2  christos 	 * don't allow files with holes, so we shouldn't ever see this.
   1784  1.52.10.2  christos 	 */
   1785  1.52.10.2  christos 	if (bp->b_blkno == bp->b_lblkno) {
   1786  1.52.10.2  christos 		error = pcbmap(dep, de_bn2cn(dep->de_pmp, bp->b_lblkno),
   1787  1.52.10.2  christos 			       &bp->b_blkno, 0, 0);
   1788  1.52.10.2  christos 		if (error)
   1789  1.52.10.2  christos 			bp->b_blkno = -1;
   1790  1.52.10.2  christos 		if (bp->b_blkno == -1)
   1791  1.52.10.2  christos 			clrbuf(bp);
   1792  1.52.10.2  christos 		else
   1793  1.52.10.2  christos 			bp->b_blkno = de_bn2kb(dep->de_pmp, bp->b_blkno);
   1794  1.52.10.2  christos 	}
   1795  1.52.10.2  christos 	if (bp->b_blkno == -1) {
   1796  1.52.10.2  christos 		biodone(bp);
   1797  1.52.10.2  christos 		return (error);
   1798  1.52.10.2  christos 	}
   1799  1.52.10.2  christos 
   1800  1.52.10.2  christos 	/*
   1801  1.52.10.2  christos 	 * Read/write the block from/to the disk that contains the desired
   1802  1.52.10.2  christos 	 * file block.
   1803  1.52.10.2  christos 	 */
   1804  1.52.10.2  christos 
   1805  1.52.10.2  christos 	vp = dep->de_devvp;
   1806  1.52.10.2  christos 	return (VOP_STRATEGY(vp, bp));
   1807  1.52.10.2  christos }
   1808  1.52.10.2  christos 
   1809  1.52.10.2  christos int
   1810  1.52.10.2  christos msdosfs_print(v)
   1811  1.52.10.2  christos 	void *v;
   1812  1.52.10.2  christos {
   1813  1.52.10.2  christos 	struct vop_print_args /* {
   1814  1.52.10.2  christos 		struct vnode *vp;
   1815  1.52.10.2  christos 	} */ *ap = v;
   1816  1.52.10.2  christos 	struct denode *dep = VTODE(ap->a_vp);
   1817  1.52.10.2  christos 
   1818  1.52.10.2  christos 	printf(
   1819  1.52.10.2  christos 	    "tag VT_MSDOSFS, startcluster %ld, dircluster %ld, diroffset %ld ",
   1820  1.52.10.2  christos 	    dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset);
   1821  1.52.10.2  christos 	printf(" dev %llu, %llu ", (unsigned long long)major(dep->de_dev),
   1822  1.52.10.2  christos 	    (unsigned long long)minor(dep->de_dev));
   1823  1.52.10.2  christos 	printf("\n");
   1824  1.52.10.2  christos 	return (0);
   1825  1.52.10.2  christos }
   1826  1.52.10.2  christos 
   1827  1.52.10.2  christos int
   1828  1.52.10.2  christos msdosfs_advlock(v)
   1829  1.52.10.2  christos 	void *v;
   1830  1.52.10.2  christos {
   1831  1.52.10.2  christos 	struct vop_advlock_args /* {
   1832  1.52.10.2  christos 		struct vnode *a_vp;
   1833  1.52.10.2  christos 		void *a_id;
   1834  1.52.10.2  christos 		int a_op;
   1835  1.52.10.2  christos 		struct flock *a_fl;
   1836  1.52.10.2  christos 		int a_flags;
   1837  1.52.10.2  christos 	} */ *ap = v;
   1838  1.52.10.2  christos 	struct denode *dep = VTODE(ap->a_vp);
   1839  1.52.10.2  christos 
   1840  1.52.10.2  christos 	return lf_advlock(ap, &dep->de_lockf, dep->de_FileSize);
   1841  1.52.10.2  christos }
   1842  1.52.10.2  christos 
   1843  1.52.10.2  christos int
   1844  1.52.10.2  christos msdosfs_pathconf(v)
   1845  1.52.10.2  christos 	void *v;
   1846  1.52.10.2  christos {
   1847  1.52.10.2  christos 	struct vop_pathconf_args /* {
   1848  1.52.10.2  christos 		struct vnode *a_vp;
   1849  1.52.10.2  christos 		int a_name;
   1850  1.52.10.2  christos 		register_t *a_retval;
   1851  1.52.10.2  christos 	} */ *ap = v;
   1852  1.52.10.2  christos 
   1853  1.52.10.2  christos 	switch (ap->a_name) {
   1854  1.52.10.2  christos 	case _PC_LINK_MAX:
   1855  1.52.10.2  christos 		*ap->a_retval = 1;
   1856  1.52.10.2  christos 		return (0);
   1857  1.52.10.2  christos 	case _PC_NAME_MAX:
   1858  1.52.10.2  christos 		*ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_namemax;
   1859  1.52.10.2  christos 		return (0);
   1860  1.52.10.2  christos 	case _PC_PATH_MAX:
   1861  1.52.10.2  christos 		*ap->a_retval = PATH_MAX;
   1862  1.52.10.2  christos 		return (0);
   1863  1.52.10.2  christos 	case _PC_CHOWN_RESTRICTED:
   1864  1.52.10.2  christos 		*ap->a_retval = 1;
   1865  1.52.10.2  christos 		return (0);
   1866  1.52.10.2  christos 	case _PC_NO_TRUNC:
   1867  1.52.10.2  christos 		*ap->a_retval = 0;
   1868  1.52.10.2  christos 		return (0);
   1869  1.52.10.2  christos 	case _PC_SYNC_IO:
   1870  1.52.10.2  christos 		*ap->a_retval = 1;
   1871  1.52.10.2  christos 		return (0);
   1872  1.52.10.2  christos 	case _PC_FILESIZEBITS:
   1873  1.52.10.2  christos 		*ap->a_retval = 32;
   1874  1.52.10.2  christos 		return (0);
   1875  1.52.10.2  christos 	default:
   1876  1.52.10.2  christos 		return (EINVAL);
   1877  1.52.10.2  christos 	}
   1878  1.52.10.2  christos 	/* NOTREACHED */
   1879  1.52.10.2  christos }
   1880  1.52.10.2  christos 
   1881  1.52.10.2  christos int
   1882  1.52.10.2  christos msdosfs_fsync(v)
   1883  1.52.10.2  christos 	void *v;
   1884  1.52.10.2  christos {
   1885  1.52.10.2  christos 	struct vop_fsync_args /* {
   1886  1.52.10.2  christos 		struct vnode *a_vp;
   1887  1.52.10.2  christos 		kauth_cred_t a_cred;
   1888  1.52.10.2  christos 		int a_flags;
   1889  1.52.10.2  christos 		off_t offlo;
   1890  1.52.10.2  christos 		off_t offhi;
   1891  1.52.10.2  christos 	} */ *ap = v;
   1892  1.52.10.2  christos 	struct vnode *vp = ap->a_vp;
   1893  1.52.10.2  christos 	int wait;
   1894  1.52.10.2  christos 	int error;
   1895  1.52.10.2  christos 
   1896  1.52.10.2  christos 	wait = (ap->a_flags & FSYNC_WAIT) != 0;
   1897  1.52.10.2  christos 	vflushbuf(vp, wait);
   1898  1.52.10.2  christos 	if ((ap->a_flags & FSYNC_DATAONLY) != 0)
   1899  1.52.10.2  christos 		error = 0;
   1900  1.52.10.2  christos 	else
   1901  1.52.10.2  christos 		error = msdosfs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0);
   1902  1.52.10.2  christos 
   1903  1.52.10.2  christos 	if (error == 0 && ap->a_flags & FSYNC_CACHE) {
   1904  1.52.10.2  christos 		struct denode *dep = VTODE(vp);
   1905  1.52.10.2  christos 		struct vnode *devvp = dep->de_devvp;
   1906  1.52.10.2  christos 
   1907  1.52.10.2  christos 		int l = 0;
   1908  1.52.10.2  christos 		error = VOP_IOCTL(devvp, DIOCCACHESYNC, &l, FWRITE,
   1909  1.52.10.2  christos 					  curlwp->l_cred);
   1910  1.52.10.2  christos 	}
   1911  1.52.10.2  christos 
   1912  1.52.10.2  christos 	return (error);
   1913  1.52.10.2  christos }
   1914  1.52.10.2  christos 
   1915  1.52.10.2  christos void
   1916  1.52.10.2  christos msdosfs_detimes(struct denode *dep, const struct timespec *acc,
   1917  1.52.10.2  christos     const struct timespec *mod, const struct timespec *cre, int gmtoff)
   1918  1.52.10.2  christos {
   1919  1.52.10.2  christos 	struct timespec *ts = NULL, tsb;
   1920  1.52.10.2  christos 
   1921  1.52.10.2  christos 	KASSERT(dep->de_flag & (DE_UPDATE | DE_CREATE | DE_ACCESS));
   1922  1.52.10.2  christos 	/* XXX just call getnanotime early and use result if needed? */
   1923  1.52.10.2  christos 	dep->de_flag |= DE_MODIFIED;
   1924  1.52.10.2  christos 	if (dep->de_flag & DE_UPDATE) {
   1925  1.52.10.2  christos 		if (mod == NULL) {
   1926  1.52.10.2  christos 			getnanotime(&tsb);
   1927  1.52.10.2  christos 			mod = ts = &tsb;
   1928  1.52.10.2  christos 		}
   1929  1.52.10.2  christos 		unix2dostime(mod, gmtoff, &dep->de_MDate, &dep->de_MTime, NULL);
   1930  1.52.10.2  christos 		dep->de_Attributes |= ATTR_ARCHIVE;
   1931  1.52.10.2  christos 	}
   1932  1.52.10.2  christos 	if ((dep->de_pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0) {
   1933  1.52.10.2  christos 		if (dep->de_flag & DE_ACCESS)  {
   1934  1.52.10.2  christos 			if (acc == NULL)
   1935  1.52.10.2  christos 				acc = ts == NULL ?
   1936  1.52.10.2  christos 				    (getnanotime(&tsb), ts = &tsb) : ts;
   1937  1.52.10.2  christos 			unix2dostime(acc, gmtoff, &dep->de_ADate, NULL, NULL);
   1938  1.52.10.2  christos 		}
   1939  1.52.10.2  christos 		if (dep->de_flag & DE_CREATE) {
   1940  1.52.10.2  christos 			if (cre == NULL)
   1941  1.52.10.2  christos 				cre = ts == NULL ?
   1942  1.52.10.2  christos 				    (getnanotime(&tsb), ts = &tsb) : ts;
   1943  1.52.10.2  christos 			unix2dostime(cre, gmtoff, &dep->de_CDate,
   1944  1.52.10.2  christos 			    &dep->de_CTime, &dep->de_CHun);
   1945  1.52.10.2  christos 		}
   1946  1.52.10.2  christos 	}
   1947  1.52.10.2  christos 
   1948  1.52.10.2  christos 	dep->de_flag &= ~(DE_UPDATE | DE_CREATE | DE_ACCESS);
   1949  1.52.10.2  christos }
   1950  1.52.10.2  christos 
   1951  1.52.10.2  christos /* Global vfs data structures for msdosfs */
   1952  1.52.10.2  christos int (**msdosfs_vnodeop_p)(void *);
   1953  1.52.10.2  christos const struct vnodeopv_entry_desc msdosfs_vnodeop_entries[] = {
   1954  1.52.10.2  christos 	{ &vop_default_desc, vn_default_error },
   1955  1.52.10.2  christos 	{ &vop_lookup_desc, msdosfs_lookup },		/* lookup */
   1956  1.52.10.2  christos 	{ &vop_create_desc, msdosfs_create },		/* create */
   1957  1.52.10.2  christos 	{ &vop_mknod_desc, msdosfs_mknod },		/* mknod */
   1958  1.52.10.2  christos 	{ &vop_open_desc, msdosfs_open },		/* open */
   1959  1.52.10.2  christos 	{ &vop_close_desc, msdosfs_close },		/* close */
   1960  1.52.10.2  christos 	{ &vop_access_desc, msdosfs_access },		/* access */
   1961  1.52.10.2  christos 	{ &vop_getattr_desc, msdosfs_getattr },		/* getattr */
   1962  1.52.10.2  christos 	{ &vop_setattr_desc, msdosfs_setattr },		/* setattr */
   1963  1.52.10.2  christos 	{ &vop_read_desc, msdosfs_read },		/* read */
   1964  1.52.10.2  christos 	{ &vop_write_desc, msdosfs_write },		/* write */
   1965  1.52.10.2  christos 	{ &vop_fcntl_desc, genfs_fcntl },		/* fcntl */
   1966  1.52.10.2  christos 	{ &vop_ioctl_desc, msdosfs_ioctl },		/* ioctl */
   1967  1.52.10.2  christos 	{ &vop_poll_desc, msdosfs_poll },		/* poll */
   1968  1.52.10.2  christos 	{ &vop_kqfilter_desc, genfs_kqfilter },		/* kqfilter */
   1969  1.52.10.2  christos 	{ &vop_revoke_desc, msdosfs_revoke },		/* revoke */
   1970  1.52.10.2  christos 	{ &vop_mmap_desc, msdosfs_mmap },		/* mmap */
   1971  1.52.10.2  christos 	{ &vop_fsync_desc, msdosfs_fsync },		/* fsync */
   1972  1.52.10.2  christos 	{ &vop_seek_desc, msdosfs_seek },		/* seek */
   1973  1.52.10.2  christos 	{ &vop_remove_desc, msdosfs_remove },		/* remove */
   1974  1.52.10.2  christos 	{ &vop_link_desc, msdosfs_link },		/* link */
   1975  1.52.10.2  christos 	{ &vop_rename_desc, msdosfs_rename },		/* rename */
   1976  1.52.10.2  christos 	{ &vop_mkdir_desc, msdosfs_mkdir },		/* mkdir */
   1977  1.52.10.2  christos 	{ &vop_rmdir_desc, msdosfs_rmdir },		/* rmdir */
   1978  1.52.10.2  christos 	{ &vop_symlink_desc, msdosfs_symlink },		/* symlink */
   1979  1.52.10.2  christos 	{ &vop_readdir_desc, msdosfs_readdir },		/* readdir */
   1980  1.52.10.2  christos 	{ &vop_readlink_desc, msdosfs_readlink },	/* readlink */
   1981  1.52.10.2  christos 	{ &vop_abortop_desc, msdosfs_abortop },		/* abortop */
   1982  1.52.10.2  christos 	{ &vop_inactive_desc, msdosfs_inactive },	/* inactive */
   1983  1.52.10.2  christos 	{ &vop_reclaim_desc, msdosfs_reclaim },		/* reclaim */
   1984  1.52.10.2  christos 	{ &vop_lock_desc, genfs_lock },			/* lock */
   1985  1.52.10.2  christos 	{ &vop_unlock_desc, genfs_unlock },		/* unlock */
   1986  1.52.10.2  christos 	{ &vop_bmap_desc, msdosfs_bmap },		/* bmap */
   1987  1.52.10.2  christos 	{ &vop_strategy_desc, msdosfs_strategy },	/* strategy */
   1988  1.52.10.2  christos 	{ &vop_print_desc, msdosfs_print },		/* print */
   1989  1.52.10.2  christos 	{ &vop_islocked_desc, genfs_islocked },		/* islocked */
   1990  1.52.10.2  christos 	{ &vop_pathconf_desc, msdosfs_pathconf },	/* pathconf */
   1991  1.52.10.2  christos 	{ &vop_advlock_desc, msdosfs_advlock },		/* advlock */
   1992  1.52.10.2  christos 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
   1993  1.52.10.2  christos 	{ &vop_getpages_desc, genfs_getpages },		/* getpages */
   1994  1.52.10.2  christos 	{ &vop_putpages_desc, genfs_putpages },		/* putpages */
   1995  1.52.10.2  christos 	{ NULL, NULL }
   1996  1.52.10.2  christos };
   1997  1.52.10.2  christos const struct vnodeopv_desc msdosfs_vnodeop_opv_desc =
   1998  1.52.10.2  christos 	{ &msdosfs_vnodeop_p, msdosfs_vnodeop_entries };
   1999