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