[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.61

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

CVSweb <webmaster@jp.NetBSD.org>