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

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

CVSweb <webmaster@jp.NetBSD.org>