[BACK]Return to ffs_inode.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / ufs / ffs

Annotation of src/sys/ufs/ffs/ffs_inode.c, Revision 1.124

1.124   ! riastrad    1: /*     $NetBSD: ffs_inode.c,v 1.123 2016/11/11 10:50:16 hannken Exp $  */
1.98      simonb      2:
                      3: /*-
                      4:  * Copyright (c) 2008 The NetBSD Foundation, Inc.
                      5:  * All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to The NetBSD Foundation
                      8:  * by Wasabi Systems, Inc.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     20:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     21:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     22:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     23:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     24:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     25:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     26:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     27:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     28:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     29:  * POSSIBILITY OF SUCH DAMAGE.
                     30:  */
1.5       cgd        31:
1.1       mycroft    32: /*
                     33:  * Copyright (c) 1982, 1986, 1989, 1993
                     34:  *     The Regents of the University of California.  All rights reserved.
                     35:  *
                     36:  * Redistribution and use in source and binary forms, with or without
                     37:  * modification, are permitted provided that the following conditions
                     38:  * are met:
                     39:  * 1. Redistributions of source code must retain the above copyright
                     40:  *    notice, this list of conditions and the following disclaimer.
                     41:  * 2. Redistributions in binary form must reproduce the above copyright
                     42:  *    notice, this list of conditions and the following disclaimer in the
                     43:  *    documentation and/or other materials provided with the distribution.
1.60      agc        44:  * 3. Neither the name of the University nor the names of its contributors
1.1       mycroft    45:  *    may be used to endorse or promote products derived from this software
                     46:  *    without specific prior written permission.
                     47:  *
                     48:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     49:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     50:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     51:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     52:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     53:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     54:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     55:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     56:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     57:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     58:  * SUCH DAMAGE.
                     59:  *
1.18      fvdl       60:  *     @(#)ffs_inode.c 8.13 (Berkeley) 4/21/95
1.1       mycroft    61:  */
1.46      lukem      62:
                     63: #include <sys/cdefs.h>
1.124   ! riastrad   64: __KERNEL_RCSID(0, "$NetBSD: ffs_inode.c,v 1.123 2016/11/11 10:50:16 hannken Exp $");
1.17      mrg        65:
1.41      mrg        66: #if defined(_KERNEL_OPT)
1.25      thorpej    67: #include "opt_ffs.h"
1.20      scottr     68: #include "opt_quota.h"
1.21      scottr     69: #endif
1.1       mycroft    70:
                     71: #include <sys/param.h>
                     72: #include <sys/systm.h>
1.98      simonb     73: #include <sys/buf.h>
1.1       mycroft    74: #include <sys/file.h>
1.98      simonb     75: #include <sys/fstrans.h>
                     76: #include <sys/kauth.h>
1.1       mycroft    77: #include <sys/kernel.h>
1.109     para       78: #include <sys/kmem.h>
1.98      simonb     79: #include <sys/mount.h>
                     80: #include <sys/proc.h>
                     81: #include <sys/resourcevar.h>
1.1       mycroft    82: #include <sys/trace.h>
1.98      simonb     83: #include <sys/vnode.h>
                     84: #include <sys/wapbl.h>
1.16      mrg        85:
1.1       mycroft    86: #include <ufs/ufs/quota.h>
                     87: #include <ufs/ufs/inode.h>
                     88: #include <ufs/ufs/ufsmount.h>
                     89: #include <ufs/ufs/ufs_extern.h>
1.19      bouyer     90: #include <ufs/ufs/ufs_bswap.h>
1.98      simonb     91: #include <ufs/ufs/ufs_wapbl.h>
1.1       mycroft    92:
                     93: #include <ufs/ffs/fs.h>
                     94: #include <ufs/ffs/ffs_extern.h>
                     95:
1.72      thorpej    96: static int ffs_indirtrunc(struct inode *, daddr_t, daddr_t, daddr_t, int,
                     97:                          int64_t *);
1.1       mycroft    98:
                     99: /*
1.13      tls       100:  * Update the access, modified, and inode change times as specified
                    101:  * by the IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively.
                    102:  * The IN_MODIFIED flag is used to specify that the inode needs to be
                    103:  * updated but that the times have already been set. The access
                    104:  * and modified times are taken from the second and third parameters;
                    105:  * the inode change time is always taken from the current time. If
1.103     ad        106:  * UPDATE_WAIT flag is set, or UPDATE_DIROP is set then wait for the
                    107:  * disk write of the inode to complete.
1.1       mycroft   108:  */
1.13      tls       109:
1.1       mycroft   110: int
1.77      yamt      111: ffs_update(struct vnode *vp, const struct timespec *acc,
                    112:     const struct timespec *mod, int updflags)
1.9       christos  113: {
1.30      augustss  114:        struct fs *fs;
1.1       mycroft   115:        struct buf *bp;
                    116:        struct inode *ip;
                    117:        int error;
1.86      christos  118:        void *cp;
1.34      mycroft   119:        int waitfor, flags;
1.1       mycroft   120:
1.77      yamt      121:        if (vp->v_mount->mnt_flag & MNT_RDONLY)
1.11      mycroft   122:                return (0);
1.77      yamt      123:        ip = VTOI(vp);
                    124:        FFS_ITIMES(ip, acc, mod, NULL);
                    125:        if (updflags & UPDATE_CLOSE)
1.65      mycroft   126:                flags = ip->i_flag & (IN_MODIFIED | IN_ACCESSED);
                    127:        else
                    128:                flags = ip->i_flag & IN_MODIFIED;
1.34      mycroft   129:        if (flags == 0)
1.1       mycroft   130:                return (0);
                    131:        fs = ip->i_fs;
1.31      perseant  132:
1.35      mycroft   133:        if ((flags & IN_MODIFIED) != 0 &&
1.77      yamt      134:            (vp->v_mount->mnt_flag & MNT_ASYNC) == 0) {
                    135:                waitfor = updflags & UPDATE_WAIT;
1.103     ad        136:                if ((updflags & UPDATE_DIROP) != 0)
1.35      mycroft   137:                        waitfor |= UPDATE_WAIT;
                    138:        } else
                    139:                waitfor = 0;
1.31      perseant  140:
1.1       mycroft   141:        /*
                    142:         * Ensure that uid and gid are correct. This is a temporary
                    143:         * fix until fsck has been changed to do the update.
                    144:         */
1.55      fvdl      145:        if (fs->fs_magic == FS_UFS1_MAGIC &&                    /* XXX */
                    146:            fs->fs_old_inodefmt < FS_44INODEFMT) {              /* XXX */
                    147:                ip->i_ffs1_ouid = ip->i_uid;    /* XXX */
                    148:                ip->i_ffs1_ogid = ip->i_gid;    /* XXX */
1.18      fvdl      149:        }                                                       /* XXX */
1.9       christos  150:        error = bread(ip->i_devvp,
1.114     dholland  151:                      FFS_FSBTODB(fs, ino_to_fsba(fs, ip->i_number)),
1.117     maxv      152:                      (int)fs->fs_bsize, B_MODIFY, &bp);
1.9       christos  153:        if (error) {
1.1       mycroft   154:                return (error);
                    155:        }
1.35      mycroft   156:        ip->i_flag &= ~(IN_MODIFIED | IN_ACCESSED);
1.98      simonb    157:        /* Keep unlinked inode list up to date */
1.110     matt      158:        KDASSERTMSG(DIP(ip, nlink) == ip->i_nlink,
                    159:            "DIP(ip, nlink) [%d] == ip->i_nlink [%d]",
                    160:            DIP(ip, nlink), ip->i_nlink);
1.98      simonb    161:        if (ip->i_mode) {
                    162:                if (ip->i_nlink > 0) {
                    163:                        UFS_WAPBL_UNREGISTER_INODE(ip->i_ump->um_mountp,
                    164:                            ip->i_number, ip->i_mode);
                    165:                } else {
                    166:                        UFS_WAPBL_REGISTER_INODE(ip->i_ump->um_mountp,
                    167:                            ip->i_number, ip->i_mode);
                    168:                }
                    169:        }
1.55      fvdl      170:        if (fs->fs_magic == FS_UFS1_MAGIC) {
1.86      christos  171:                cp = (char *)bp->b_data +
1.55      fvdl      172:                    (ino_to_fsbo(fs, ip->i_number) * DINODE1_SIZE);
1.23      christos  173: #ifdef FFS_EI
1.55      fvdl      174:                if (UFS_FSNEEDSWAP(fs))
                    175:                        ffs_dinode1_swap(ip->i_din.ffs1_din,
                    176:                            (struct ufs1_dinode *)cp);
                    177:                else
                    178: #endif
                    179:                        memcpy(cp, ip->i_din.ffs1_din, DINODE1_SIZE);
                    180:        } else {
1.86      christos  181:                cp = (char *)bp->b_data +
1.55      fvdl      182:                    (ino_to_fsbo(fs, ip->i_number) * DINODE2_SIZE);
                    183: #ifdef FFS_EI
                    184:                if (UFS_FSNEEDSWAP(fs))
                    185:                        ffs_dinode2_swap(ip->i_din.ffs2_din,
                    186:                            (struct ufs2_dinode *)cp);
                    187:                else
1.23      christos  188: #endif
1.55      fvdl      189:                        memcpy(cp, ip->i_din.ffs2_din, DINODE2_SIZE);
                    190:        }
1.35      mycroft   191:        if (waitfor) {
1.1       mycroft   192:                return (bwrite(bp));
1.29      fvdl      193:        } else {
1.1       mycroft   194:                bdwrite(bp);
                    195:                return (0);
                    196:        }
                    197: }
                    198:
                    199: #define        SINGLE  0       /* index of single indirect block */
                    200: #define        DOUBLE  1       /* index of double indirect block */
                    201: #define        TRIPLE  2       /* index of triple indirect block */
                    202: /*
                    203:  * Truncate the inode oip to at most length size, freeing the
                    204:  * disk blocks.
                    205:  */
1.9       christos  206: int
1.92      pooka     207: ffs_truncate(struct vnode *ovp, off_t length, int ioflag, kauth_cred_t cred)
1.9       christos  208: {
1.53      fvdl      209:        daddr_t lastblock;
1.68      mycroft   210:        struct inode *oip = VTOI(ovp);
1.112     dholland  211:        daddr_t bn, lastiblock[UFS_NIADDR], indir_lbn[UFS_NIADDR];
                    212:        daddr_t blks[UFS_NDADDR + UFS_NIADDR];
1.30      augustss  213:        struct fs *fs;
1.85      yamt      214:        int offset, pgoffset, level;
1.118     jdolecek  215:        int64_t blocksreleased = 0;
1.77      yamt      216:        int i, aflag, nblocks;
1.38      chs       217:        int error, allerror = 0;
1.1       mycroft   218:        off_t osize;
1.67      mycroft   219:        int sync;
1.68      mycroft   220:        struct ufsmount *ump = oip->i_ump;
1.122     jdolecek  221:        void *dcookie;
1.1       mycroft   222:
1.118     jdolecek  223:        UFS_WAPBL_JLOCK_ASSERT(ip->i_ump->um_mountp);
                    224:
1.78      yamt      225:        if (ovp->v_type == VCHR || ovp->v_type == VBLK ||
                    226:            ovp->v_type == VFIFO || ovp->v_type == VSOCK) {
                    227:                KASSERT(oip->i_size == 0);
                    228:                return 0;
                    229:        }
                    230:
1.2       pk        231:        if (length < 0)
1.3       mycroft   232:                return (EINVAL);
1.68      mycroft   233:
1.1       mycroft   234:        if (ovp->v_type == VLNK &&
1.68      mycroft   235:            (oip->i_size < ump->um_maxsymlinklen ||
                    236:             (ump->um_maxsymlinklen == 0 && DIP(oip, blocks) == 0))) {
1.38      chs       237:                KDASSERT(length == 0);
1.55      fvdl      238:                memset(SHORTLINK(oip), 0, (size_t)oip->i_size);
                    239:                oip->i_size = 0;
1.57      kristerw  240:                DIP_ASSIGN(oip, size, 0);
1.1       mycroft   241:                oip->i_flag |= IN_CHANGE | IN_UPDATE;
1.77      yamt      242:                return (ffs_update(ovp, NULL, NULL, 0));
1.1       mycroft   243:        }
1.55      fvdl      244:        if (oip->i_size == length) {
1.104     bouyer    245:                /* still do a uvm_vnp_setsize() as writesize may be larger */
                    246:                uvm_vnp_setsize(ovp, length);
1.1       mycroft   247:                oip->i_flag |= IN_CHANGE | IN_UPDATE;
1.77      yamt      248:                return (ffs_update(ovp, NULL, NULL, 0));
1.1       mycroft   249:        }
                    250:        fs = oip->i_fs;
1.68      mycroft   251:        if (length > ump->um_maxfilesize)
1.38      chs       252:                return (EFBIG);
                    253:
1.63      hannken   254:        if ((oip->i_flags & SF_SNAPSHOT) != 0)
                    255:                ffs_snapremove(ovp);
                    256:
1.55      fvdl      257:        osize = oip->i_size;
1.50      chs       258:        aflag = ioflag & IO_SYNC ? B_SYNC : 0;
1.29      fvdl      259:
1.38      chs       260:        /*
                    261:         * Lengthen the size of the file. We must ensure that the
                    262:         * last byte of the file is allocated. Since the smallest
                    263:         * value of osize is 0, length will be at least 1.
                    264:         */
                    265:
                    266:        if (osize < length) {
1.115     dholland  267:                if (ffs_lblkno(fs, osize) < UFS_NDADDR &&
                    268:                    ffs_lblkno(fs, osize) != ffs_lblkno(fs, length) &&
                    269:                    ffs_blkroundup(fs, osize) != osize) {
1.69      mycroft   270:                        off_t eob;
                    271:
1.115     dholland  272:                        eob = ffs_blkroundup(fs, osize);
1.87      yamt      273:                        uvm_vnp_setwritesize(ovp, eob);
1.69      mycroft   274:                        error = ufs_balloc_range(ovp, osize, eob - osize,
1.77      yamt      275:                            cred, aflag);
1.108     bouyer    276:                        if (error) {
                    277:                                (void) ffs_truncate(ovp, osize,
                    278:                                    ioflag & IO_SYNC, cred);
1.48      chs       279:                                return error;
1.108     bouyer    280:                        }
1.48      chs       281:                        if (ioflag & IO_SYNC) {
1.106     rmind     282:                                mutex_enter(ovp->v_interlock);
1.49      chs       283:                                VOP_PUTPAGES(ovp,
1.69      mycroft   284:                                    trunc_page(osize & fs->fs_bmask),
1.99      hannken   285:                                    round_page(eob), PGO_CLEANIT | PGO_SYNCIO |
                    286:                                    PGO_JOURNALLOCKED);
1.48      chs       287:                        }
                    288:                }
1.87      yamt      289:                uvm_vnp_setwritesize(ovp, length);
1.77      yamt      290:                error = ufs_balloc_range(ovp, length - 1, 1, cred, aflag);
1.45      chs       291:                if (error) {
1.92      pooka     292:                        (void) ffs_truncate(ovp, osize, ioflag & IO_SYNC, cred);
1.68      mycroft   293:                        return (error);
1.45      chs       294:                }
1.43      chs       295:                uvm_vnp_setsize(ovp, length);
1.38      chs       296:                oip->i_flag |= IN_CHANGE | IN_UPDATE;
1.55      fvdl      297:                KASSERT(ovp->v_size == oip->i_size);
1.77      yamt      298:                return (ffs_update(ovp, NULL, NULL, 0));
1.38      chs       299:        }
                    300:
                    301:        /*
                    302:         * When truncating a regular file down to a non-block-aligned size,
                    303:         * we must zero the part of last block which is past the new EOF.
                    304:         * We must synchronously flush the zeroed pages to disk
                    305:         * since the new pages will be invalidated as soon as we
                    306:         * inform the VM system of the new, smaller size.
1.48      chs       307:         * We must do this before acquiring the GLOCK, since fetching
1.38      chs       308:         * the pages will acquire the GLOCK internally.
                    309:         * So there is a window where another thread could see a whole
                    310:         * zeroed page past EOF, but that's life.
                    311:         */
                    312:
1.113     dholland  313:        offset = ffs_blkoff(fs, length);
1.85      yamt      314:        pgoffset = length & PAGE_MASK;
                    315:        if (ovp->v_type == VREG && (pgoffset != 0 || offset != 0) &&
                    316:            osize > length) {
1.70      mycroft   317:                daddr_t lbn;
1.38      chs       318:                voff_t eoz;
1.85      yamt      319:                int size;
1.38      chs       320:
1.85      yamt      321:                if (offset != 0) {
                    322:                        error = ufs_balloc_range(ovp, length - 1, 1, cred,
                    323:                            aflag);
                    324:                        if (error)
                    325:                                return error;
                    326:                }
1.115     dholland  327:                lbn = ffs_lblkno(fs, length);
1.113     dholland  328:                size = ffs_blksize(fs, oip, lbn);
1.115     dholland  329:                eoz = MIN(MAX(ffs_lblktosize(fs, lbn) + size, round_page(pgoffset)),
1.85      yamt      330:                    osize);
1.107     hannken   331:                ubc_zerorange(&ovp->v_uobj, length, eoz - length,
                    332:                    UBC_UNMAP_FLAG(ovp));
1.70      mycroft   333:                if (round_page(eoz) > round_page(length)) {
1.106     rmind     334:                        mutex_enter(ovp->v_interlock);
1.70      mycroft   335:                        error = VOP_PUTPAGES(ovp, round_page(length),
                    336:                            round_page(eoz),
1.99      hannken   337:                            PGO_CLEANIT | PGO_DEACTIVATE | PGO_JOURNALLOCKED |
1.71      mycroft   338:                            ((ioflag & IO_SYNC) ? PGO_SYNCIO : 0));
1.70      mycroft   339:                        if (error)
                    340:                                return error;
                    341:                }
1.38      chs       342:        }
                    343:
1.84      yamt      344:        genfs_node_wrlock(ovp);
1.55      fvdl      345:        oip->i_size = length;
1.57      kristerw  346:        DIP_ASSIGN(oip, size, length);
1.18      fvdl      347:        uvm_vnp_setsize(ovp, length);
1.1       mycroft   348:        /*
                    349:         * Calculate index into inode's block list of
                    350:         * last direct and indirect blocks (if any)
                    351:         * which we want to keep.  Lastblock is -1 when
                    352:         * the file is truncated to 0.
                    353:         */
1.115     dholland  354:        lastblock = ffs_lblkno(fs, length + fs->fs_bsize - 1) - 1;
1.112     dholland  355:        lastiblock[SINGLE] = lastblock - UFS_NDADDR;
1.113     dholland  356:        lastiblock[DOUBLE] = lastiblock[SINGLE] - FFS_NINDIR(fs);
                    357:        lastiblock[TRIPLE] = lastiblock[DOUBLE] - FFS_NINDIR(fs) * FFS_NINDIR(fs);
1.1       mycroft   358:        nblocks = btodb(fs->fs_bsize);
                    359:        /*
                    360:         * Update file and block pointers on disk before we start freeing
                    361:         * blocks.  If we crash before free'ing blocks below, the blocks
                    362:         * will be returned to the free list.  lastiblock values are also
                    363:         * normalized to -1 for calls to ffs_indirtrunc below.
                    364:         */
1.67      mycroft   365:        sync = 0;
1.55      fvdl      366:        for (level = TRIPLE; level >= SINGLE; level--) {
1.112     dholland  367:                blks[UFS_NDADDR + level] = DIP(oip, ib[level]);
                    368:                if (lastiblock[level] < 0 && blks[UFS_NDADDR + level] != 0) {
1.67      mycroft   369:                        sync = 1;
1.57      kristerw  370:                        DIP_ASSIGN(oip, ib[level], 0);
1.1       mycroft   371:                        lastiblock[level] = -1;
                    372:                }
1.55      fvdl      373:        }
1.112     dholland  374:        for (i = 0; i < UFS_NDADDR; i++) {
1.64      hannken   375:                blks[i] = DIP(oip, db[i]);
1.67      mycroft   376:                if (i > lastblock && blks[i] != 0) {
                    377:                        sync = 1;
1.57      kristerw  378:                        DIP_ASSIGN(oip, db[i], 0);
1.67      mycroft   379:                }
                    380:        }
1.68      mycroft   381:        oip->i_flag |= IN_CHANGE | IN_UPDATE;
1.67      mycroft   382:        if (sync) {
1.77      yamt      383:                error = ffs_update(ovp, NULL, NULL, UPDATE_WAIT);
1.67      mycroft   384:                if (error && !allerror)
                    385:                        allerror = error;
1.55      fvdl      386:        }
1.32      mycroft   387:
1.1       mycroft   388:        /*
                    389:         * Having written the new inode to disk, save its new configuration
                    390:         * and put back the old block pointers long enough to process them.
                    391:         * Note that we save the new block configuration so we can check it
                    392:         * when we are done.
                    393:         */
1.112     dholland  394:        for (i = 0; i < UFS_NDADDR; i++) {
1.64      hannken   395:                bn = DIP(oip, db[i]);
                    396:                DIP_ASSIGN(oip, db[i], blks[i]);
                    397:                blks[i] = bn;
1.55      fvdl      398:        }
1.112     dholland  399:        for (i = 0; i < UFS_NIADDR; i++) {
1.64      hannken   400:                bn = DIP(oip, ib[i]);
1.112     dholland  401:                DIP_ASSIGN(oip, ib[i], blks[UFS_NDADDR + i]);
                    402:                blks[UFS_NDADDR + i] = bn;
1.55      fvdl      403:        }
                    404:
                    405:        oip->i_size = osize;
1.57      kristerw  406:        DIP_ASSIGN(oip, size, osize);
1.32      mycroft   407:        error = vtruncbuf(ovp, lastblock + 1, 0, 0);
                    408:        if (error && !allerror)
                    409:                allerror = error;
1.1       mycroft   410:
                    411:        /*
                    412:         * Indirect blocks first.
                    413:         */
1.112     dholland  414:        indir_lbn[SINGLE] = -UFS_NDADDR;
1.113     dholland  415:        indir_lbn[DOUBLE] = indir_lbn[SINGLE] - FFS_NINDIR(fs) - 1;
                    416:        indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - FFS_NINDIR(fs) * FFS_NINDIR(fs) - 1;
1.1       mycroft   417:        for (level = TRIPLE; level >= SINGLE; level--) {
1.55      fvdl      418:                if (oip->i_ump->um_fstype == UFS1)
                    419:                        bn = ufs_rw32(oip->i_ffs1_ib[level],UFS_FSNEEDSWAP(fs));
                    420:                else
                    421:                        bn = ufs_rw64(oip->i_ffs2_ib[level],UFS_FSNEEDSWAP(fs));
1.1       mycroft   422:                if (bn != 0) {
1.122     jdolecek  423:                        if (lastiblock[level] < 0 &&
                    424:                            oip->i_ump->um_mountp->mnt_wapbl) {
                    425:                                error = UFS_WAPBL_REGISTER_DEALLOCATION(
                    426:                                    oip->i_ump->um_mountp,
                    427:                                    FFS_FSBTODB(fs, bn), fs->fs_bsize,
                    428:                                    &dcookie);
                    429:                                if (error)
                    430:                                        goto out;
                    431:                        } else {
                    432:                                dcookie = NULL;
                    433:                        }
                    434:
1.1       mycroft   435:                        error = ffs_indirtrunc(oip, indir_lbn[level],
1.118     jdolecek  436:                            FFS_FSBTODB(fs, bn), lastiblock[level], level,
                    437:                            &blocksreleased);
1.122     jdolecek  438:                        if (error) {
                    439:                                if (dcookie) {
                    440:                                        UFS_WAPBL_UNREGISTER_DEALLOCATION(
                    441:                                            oip->i_ump->um_mountp, dcookie);
                    442:                                }
1.118     jdolecek  443:                                goto out;
1.122     jdolecek  444:                        }
1.118     jdolecek  445:
1.1       mycroft   446:                        if (lastiblock[level] < 0) {
1.122     jdolecek  447:                                if (!dcookie)
1.98      simonb    448:                                        ffs_blkfree(fs, oip->i_devvp, bn,
                    449:                                            fs->fs_bsize, oip->i_number);
1.118     jdolecek  450:                                DIP_ASSIGN(oip, ib[level], 0);
1.1       mycroft   451:                                blocksreleased += nblocks;
                    452:                        }
                    453:                }
                    454:                if (lastiblock[level] >= 0)
                    455:                        goto done;
                    456:        }
                    457:
                    458:        /*
                    459:         * All whole direct blocks or frags.
                    460:         */
1.112     dholland  461:        for (i = UFS_NDADDR - 1; i > lastblock; i--) {
1.30      augustss  462:                long bsize;
1.1       mycroft   463:
1.55      fvdl      464:                if (oip->i_ump->um_fstype == UFS1)
                    465:                        bn = ufs_rw32(oip->i_ffs1_db[i], UFS_FSNEEDSWAP(fs));
                    466:                else
                    467:                        bn = ufs_rw64(oip->i_ffs2_db[i], UFS_FSNEEDSWAP(fs));
1.1       mycroft   468:                if (bn == 0)
                    469:                        continue;
1.118     jdolecek  470:
1.113     dholland  471:                bsize = ffs_blksize(fs, oip, i);
1.98      simonb    472:                if ((oip->i_ump->um_mountp->mnt_wapbl) &&
                    473:                    (ovp->v_type != VREG)) {
1.118     jdolecek  474:                        error = UFS_WAPBL_REGISTER_DEALLOCATION(
                    475:                            oip->i_ump->um_mountp,
1.122     jdolecek  476:                            FFS_FSBTODB(fs, bn), bsize, NULL);
1.118     jdolecek  477:                        if (error)
                    478:                                goto out;
1.98      simonb    479:                } else
                    480:                        ffs_blkfree(fs, oip->i_devvp, bn, bsize, oip->i_number);
1.118     jdolecek  481:                DIP_ASSIGN(oip, db[i], 0);
1.1       mycroft   482:                blocksreleased += btodb(bsize);
                    483:        }
                    484:        if (lastblock < 0)
                    485:                goto done;
                    486:
                    487:        /*
                    488:         * Finally, look for a change in size of the
                    489:         * last direct block; release any frags.
                    490:         */
1.55      fvdl      491:        if (oip->i_ump->um_fstype == UFS1)
                    492:                bn = ufs_rw32(oip->i_ffs1_db[lastblock], UFS_FSNEEDSWAP(fs));
                    493:        else
                    494:                bn = ufs_rw64(oip->i_ffs2_db[lastblock], UFS_FSNEEDSWAP(fs));
1.1       mycroft   495:        if (bn != 0) {
                    496:                long oldspace, newspace;
                    497:
                    498:                /*
                    499:                 * Calculate amount of space we're giving
                    500:                 * back as old block size minus new block size.
                    501:                 */
1.113     dholland  502:                oldspace = ffs_blksize(fs, oip, lastblock);
1.55      fvdl      503:                oip->i_size = length;
1.57      kristerw  504:                DIP_ASSIGN(oip, size, length);
1.113     dholland  505:                newspace = ffs_blksize(fs, oip, lastblock);
1.1       mycroft   506:                if (newspace == 0)
                    507:                        panic("itrunc: newspace");
                    508:                if (oldspace - newspace > 0) {
                    509:                        /*
                    510:                         * Block number of space to be free'd is
                    511:                         * the old block # plus the number of frags
                    512:                         * required for the storage we're keeping.
                    513:                         */
1.115     dholland  514:                        bn += ffs_numfrags(fs, newspace);
1.98      simonb    515:                        if ((oip->i_ump->um_mountp->mnt_wapbl) &&
                    516:                            (ovp->v_type != VREG)) {
1.118     jdolecek  517:                                error = UFS_WAPBL_REGISTER_DEALLOCATION(
1.114     dholland  518:                                    oip->i_ump->um_mountp, FFS_FSBTODB(fs, bn),
1.122     jdolecek  519:                                    oldspace - newspace, NULL);
1.118     jdolecek  520:                                if (error)
                    521:                                        goto out;
1.98      simonb    522:                        } else
                    523:                                ffs_blkfree(fs, oip->i_devvp, bn,
                    524:                                    oldspace - newspace, oip->i_number);
1.1       mycroft   525:                        blocksreleased += btodb(oldspace - newspace);
                    526:                }
                    527:        }
1.32      mycroft   528:
1.1       mycroft   529: done:
                    530:        for (level = SINGLE; level <= TRIPLE; level++)
1.124   ! riastrad  531:                KASSERTMSG((blks[UFS_NDADDR + level] == DIP(oip, ib[level])),
        !           532:                    "itrunc1 blk mismatch: %jx != %jx",
        !           533:                    (uintmax_t)blks[UFS_NDADDR + level],
        !           534:                    (uintmax_t)DIP(oip, ib[level]));
1.112     dholland  535:        for (i = 0; i < UFS_NDADDR; i++)
1.124   ! riastrad  536:                KASSERTMSG((blks[i] == DIP(oip, db[i])),
        !           537:                    "itrunc2 blk mismatch: %jx != %jx",
        !           538:                    (uintmax_t)blks[i], (uintmax_t)DIP(oip, db[i]));
        !           539:        KASSERTMSG((length != 0 || LIST_EMPTY(&ovp->v_cleanblkhd)),
        !           540:            "itrunc3: zero length and nonempty cleanblkhd");
        !           541:        KASSERTMSG((length != 0 || LIST_EMPTY(&ovp->v_dirtyblkhd)),
        !           542:            "itrunc3: zero length and nonempty dirtyblkhd");
1.118     jdolecek  543:
                    544: out:
                    545:        /*
                    546:         * Set length back to old size if deallocation failed. Some indirect
                    547:         * blocks were deallocated creating a hole, but that is okay.
                    548:         */
                    549:        if (error == EAGAIN) {
1.121     jdolecek  550:                if (!allerror)
                    551:                        allerror = error;
1.118     jdolecek  552:                length = osize;
                    553:                uvm_vnp_setsize(ovp, length);
                    554:        }
                    555:
1.1       mycroft   556:        /*
                    557:         * Put back the real size.
                    558:         */
1.55      fvdl      559:        oip->i_size = length;
1.57      kristerw  560:        DIP_ASSIGN(oip, size, length);
                    561:        DIP_ADD(oip, blocks, -blocksreleased);
1.84      yamt      562:        genfs_node_unlock(ovp);
1.1       mycroft   563:        oip->i_flag |= IN_CHANGE;
1.98      simonb    564:        UFS_WAPBL_UPDATE(ovp, NULL, NULL, 0);
1.105     bouyer    565: #if defined(QUOTA) || defined(QUOTA2)
1.1       mycroft   566:        (void) chkdq(oip, -blocksreleased, NOCRED, 0);
                    567: #endif
1.55      fvdl      568:        KASSERT(ovp->v_type != VREG || ovp->v_size == oip->i_size);
1.1       mycroft   569:        return (allerror);
                    570: }
                    571:
                    572: /*
                    573:  * Release blocks associated with the inode ip and stored in the indirect
                    574:  * block bn.  Blocks are free'd in LIFO order up to (but not including)
                    575:  * lastbn.  If level is greater than SINGLE, the block is an indirect block
                    576:  * and recursive calls to indirtrunc must be used to cleanse other indirect
                    577:  * blocks.
                    578:  *
                    579:  * NB: triple indirect blocks are untested.
                    580:  */
                    581: static int
1.72      thorpej   582: ffs_indirtrunc(struct inode *ip, daddr_t lbn, daddr_t dbn, daddr_t lastbn,
                    583:     int level, int64_t *countp)
1.1       mycroft   584: {
1.30      augustss  585:        int i;
1.1       mycroft   586:        struct buf *bp;
1.30      augustss  587:        struct fs *fs = ip->i_fs;
1.55      fvdl      588:        int32_t *bap1 = NULL;
                    589:        int64_t *bap2 = NULL;
1.1       mycroft   590:        struct vnode *vp;
1.53      fvdl      591:        daddr_t nb, nlbn, last;
1.121     jdolecek  592:        char *copy = NULL;
1.118     jdolecek  593:        int64_t factor;
                    594:        int64_t nblocks;
1.121     jdolecek  595:        int error = 0, allerror = 0;
1.55      fvdl      596:        const int needswap = UFS_FSNEEDSWAP(fs);
1.121     jdolecek  597:        const int wapbl = (ip->i_ump->um_mountp->mnt_wapbl != NULL);
1.122     jdolecek  598:        void *dcookie;
1.118     jdolecek  599:
1.55      fvdl      600: #define RBAP(ip, i) (((ip)->i_ump->um_fstype == UFS1) ? \
                    601:            ufs_rw32(bap1[i], needswap) : ufs_rw64(bap2[i], needswap))
1.57      kristerw  602: #define BAP_ASSIGN(ip, i, value)                                       \
                    603:        do {                                                            \
                    604:                if ((ip)->i_ump->um_fstype == UFS1)                     \
                    605:                        bap1[i] = (value);                              \
                    606:                else                                                    \
                    607:                        bap2[i] = (value);                              \
                    608:        } while(0)
1.1       mycroft   609:
                    610:        /*
                    611:         * Calculate index in current block of last
                    612:         * block to be kept.  -1 indicates the entire
                    613:         * block so we need not calculate the index.
                    614:         */
                    615:        factor = 1;
                    616:        for (i = SINGLE; i < level; i++)
1.113     dholland  617:                factor *= FFS_NINDIR(fs);
1.1       mycroft   618:        last = lastbn;
                    619:        if (lastbn > 0)
                    620:                last /= factor;
                    621:        nblocks = btodb(fs->fs_bsize);
                    622:        /*
                    623:         * Get buffer of block pointers, zero those entries corresponding
1.121     jdolecek  624:         * to blocks to be free'd, and update on disk copy first.  Since
1.1       mycroft   625:         * double(triple) indirect before single(double) indirect, calls
                    626:         * to bmap on these blocks will fail.  However, we already have
                    627:         * the on disk address, so we have to set the b_blkno field
                    628:         * explicitly instead of letting bread do everything for us.
                    629:         */
                    630:        vp = ITOV(ip);
1.97      hannken   631:        error = ffs_getblk(vp, lbn, FFS_NOBLK, fs->fs_bsize, false, &bp);
1.118     jdolecek  632:        if (error)
1.97      hannken   633:                return error;
1.118     jdolecek  634:
1.93      ad        635:        if (bp->b_oflags & (BO_DONE | BO_DELWRI)) {
1.1       mycroft   636:                /* Braces must be here in case trace evaluates to nothing. */
                    637:                trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
                    638:        } else {
                    639:                trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
1.95      ad        640:                curlwp->l_ru.ru_inblock++;      /* pay for read */
1.1       mycroft   641:                bp->b_flags |= B_READ;
1.97      hannken   642:                bp->b_flags &= ~B_COWDONE;      /* we change blkno below */
1.1       mycroft   643:                if (bp->b_bcount > bp->b_bufsize)
                    644:                        panic("ffs_indirtrunc: bad buffer size");
                    645:                bp->b_blkno = dbn;
1.61      yamt      646:                BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
1.62      hannken   647:                VOP_STRATEGY(vp, bp);
1.1       mycroft   648:                error = biowait(bp);
1.97      hannken   649:                if (error == 0)
                    650:                        error = fscow_run(bp, true);
1.1       mycroft   651:        }
                    652:        if (error) {
1.89      ad        653:                brelse(bp, 0);
1.118     jdolecek  654:                return error;
1.1       mycroft   655:        }
                    656:
1.121     jdolecek  657:        /*
                    658:         * Clear reference to blocks to be removed on disk, before actually
                    659:         * reclaiming them, so that fsck is more likely to be able to recover
                    660:         * the filesystem if system goes down during the truncate process.
                    661:         * This assumes the truncate process would not fail, contrary
                    662:         * to the wapbl case.
                    663:         */
1.123     hannken   664:        if (ip->i_ump->um_fstype == UFS1)
                    665:                bap1 = (int32_t *)bp->b_data;
                    666:        else
                    667:                bap2 = (int64_t *)bp->b_data;
1.121     jdolecek  668:        if (lastbn >= 0 && !wapbl) {
                    669:                copy = kmem_alloc(fs->fs_bsize, KM_SLEEP);
                    670:                memcpy((void *)copy, bp->b_data, (u_int)fs->fs_bsize);
                    671:                for (i = last + 1; i < FFS_NINDIR(fs); i++)
                    672:                        BAP_ASSIGN(ip, i, 0);
                    673:                error = bwrite(bp);
                    674:                if (error)
                    675:                        allerror = error;
                    676:
                    677:                if (ip->i_ump->um_fstype == UFS1)
                    678:                        bap1 = (int32_t *)copy;
                    679:                else
                    680:                        bap2 = (int64_t *)copy;
                    681:        }
1.1       mycroft   682:
                    683:        /*
1.121     jdolecek  684:         * Recursively free totally unused blocks.
1.1       mycroft   685:         */
1.113     dholland  686:        for (i = FFS_NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
1.1       mycroft   687:            i--, nlbn += factor) {
1.55      fvdl      688:                nb = RBAP(ip, i);
1.1       mycroft   689:                if (nb == 0)
                    690:                        continue;
1.118     jdolecek  691:
1.122     jdolecek  692:                if ((ip->i_ump->um_mountp->mnt_wapbl) &&
                    693:                    ((level > SINGLE) || (ITOV(ip)->v_type != VREG))) {
                    694:                        error = UFS_WAPBL_REGISTER_DEALLOCATION(
                    695:                            ip->i_ump->um_mountp,
                    696:                            FFS_FSBTODB(fs, nb), fs->fs_bsize,
                    697:                            &dcookie);
                    698:                        if (error)
                    699:                                goto out;
                    700:                } else {
                    701:                        dcookie = NULL;
                    702:                }
                    703:
1.1       mycroft   704:                if (level > SINGLE) {
1.114     dholland  705:                        error = ffs_indirtrunc(ip, nlbn, FFS_FSBTODB(fs, nb),
1.118     jdolecek  706:                                               (daddr_t)-1, level - 1, countp);
1.122     jdolecek  707:                        if (error) {
                    708:                                if (dcookie) {
                    709:                                        UFS_WAPBL_UNREGISTER_DEALLOCATION(
                    710:                                            ip->i_ump->um_mountp, dcookie);
                    711:                                }
                    712:
1.118     jdolecek  713:                                goto out;
1.122     jdolecek  714:                        }
1.1       mycroft   715:                }
1.118     jdolecek  716:
1.122     jdolecek  717:                if (!dcookie)
1.98      simonb    718:                        ffs_blkfree(fs, ip->i_devvp, nb, fs->fs_bsize,
                    719:                            ip->i_number);
1.118     jdolecek  720:
                    721:                BAP_ASSIGN(ip, i, 0);
                    722:                *countp += nblocks;
1.1       mycroft   723:        }
                    724:
                    725:        /*
1.118     jdolecek  726:         * Recursively free blocks on the now last partial indirect block.
1.1       mycroft   727:         */
                    728:        if (level > SINGLE && lastbn >= 0) {
1.119     jdolecek  729:                last = lastbn % factor;
                    730:                nb = RBAP(ip, i);
1.1       mycroft   731:                if (nb != 0) {
1.114     dholland  732:                        error = ffs_indirtrunc(ip, nlbn, FFS_FSBTODB(fs, nb),
1.119     jdolecek  733:                                               last, level - 1, countp);
1.9       christos  734:                        if (error)
1.118     jdolecek  735:                                goto out;
1.1       mycroft   736:                }
                    737:        }
1.12      thorpej   738:
1.118     jdolecek  739: out:
1.121     jdolecek  740:        if (error && !allerror)
                    741:                allerror = error;
                    742:
                    743:        if (copy != NULL) {
                    744:                kmem_free(copy, fs->fs_bsize);
                    745:        } else if (lastbn < 0 && error == 0) {
1.118     jdolecek  746:                /* all freed, release without writing back */
                    747:                brelse(bp, BC_INVAL);
1.121     jdolecek  748:        } else if (wapbl) {
                    749:                /* only partially freed, write the updated block */
                    750:                error = bwrite(bp);
                    751:                if (!allerror)
                    752:                        allerror = error;
1.12      thorpej   753:        }
                    754:
1.121     jdolecek  755:        return (allerror);
1.1       mycroft   756: }
1.74      drochner  757:
                    758: void
                    759: ffs_itimes(struct inode *ip, const struct timespec *acc,
                    760:     const struct timespec *mod, const struct timespec *cre)
                    761: {
1.82      kardel    762:        struct timespec now;
1.75      christos  763:
1.76      yamt      764:        if (!(ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY))) {
                    765:                return;
                    766:        }
1.75      christos  767:
1.83      yamt      768:        vfs_timestamp(&now);
1.74      drochner  769:        if (ip->i_flag & IN_ACCESS) {
                    770:                if (acc == NULL)
1.82      kardel    771:                        acc = &now;
1.74      drochner  772:                DIP_ASSIGN(ip, atime, acc->tv_sec);
                    773:                DIP_ASSIGN(ip, atimensec, acc->tv_nsec);
                    774:        }
                    775:        if (ip->i_flag & (IN_UPDATE | IN_MODIFY)) {
                    776:                if ((ip->i_flags & SF_SNAPSHOT) == 0) {
                    777:                        if (mod == NULL)
1.82      kardel    778:                                mod = &now;
1.74      drochner  779:                        DIP_ASSIGN(ip, mtime, mod->tv_sec);
                    780:                        DIP_ASSIGN(ip, mtimensec, mod->tv_nsec);
                    781:                }
                    782:                ip->i_modrev++;
                    783:        }
                    784:        if (ip->i_flag & (IN_CHANGE | IN_MODIFY)) {
                    785:                if (cre == NULL)
1.82      kardel    786:                        cre = &now;
1.74      drochner  787:                DIP_ASSIGN(ip, ctime, cre->tv_sec);
                    788:                DIP_ASSIGN(ip, ctimensec, cre->tv_nsec);
                    789:        }
                    790:        if (ip->i_flag & (IN_ACCESS | IN_MODIFY))
                    791:                ip->i_flag |= IN_ACCESSED;
                    792:        if (ip->i_flag & (IN_UPDATE | IN_CHANGE))
                    793:                ip->i_flag |= IN_MODIFIED;
                    794:        ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY);
                    795: }

CVSweb <webmaster@jp.NetBSD.org>