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

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

CVSweb <webmaster@jp.NetBSD.org>