[BACK]Return to msdosfs_vnops.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / fs / msdosfs

Annotation of src/sys/fs/msdosfs/msdosfs_vnops.c, Revision 1.2

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

CVSweb <webmaster@jp.NetBSD.org>