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

Annotation of src/sys/ufs/ffs/ffs_snapshot.c, Revision 1.24.8.3

1.24.8.3! elad        1: /*     $NetBSD: ffs_snapshot.c,v 1.24.8.2 2006/03/10 14:21:11 elad Exp $       */
1.18      thorpej     2:
1.1       hannken     3: /*
                      4:  * Copyright 2000 Marshall Kirk McKusick. All Rights Reserved.
                      5:  *
                      6:  * Further information about snapshots can be obtained from:
                      7:  *
                      8:  *     Marshall Kirk McKusick          http://www.mckusick.com/softdep/
                      9:  *     1614 Oxford Street              mckusick@mckusick.com
                     10:  *     Berkeley, CA 94709-1608         +1-510-843-9542
                     11:  *     USA
                     12:  *
                     13:  * Redistribution and use in source and binary forms, with or without
                     14:  * modification, are permitted provided that the following conditions
                     15:  * are met:
                     16:  *
                     17:  * 1. Redistributions of source code must retain the above copyright
                     18:  *    notice, this list of conditions and the following disclaimer.
                     19:  * 2. Redistributions in binary form must reproduce the above copyright
                     20:  *    notice, this list of conditions and the following disclaimer in the
                     21:  *    documentation and/or other materials provided with the distribution.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY MARSHALL KIRK MCKUSICK ``AS IS'' AND ANY
                     24:  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
                     25:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
                     26:  * DISCLAIMED.  IN NO EVENT SHALL MARSHALL KIRK MCKUSICK BE LIABLE FOR
                     27:  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  *
                     35:  *     @(#)ffs_snapshot.c      8.11 (McKusick) 7/23/00
                     36:  *
                     37:  *     from FreeBSD: ffs_snapshot.c,v 1.79 2004/02/13 02:02:06 kuriyama Exp
                     38:  */
                     39:
                     40: #include <sys/cdefs.h>
1.24.8.3! elad       41: __KERNEL_RCSID(0, "$NetBSD: ffs_snapshot.c,v 1.24.8.2 2006/03/10 14:21:11 elad Exp $");
1.8       hannken    42:
                     43: #if defined(_KERNEL_OPT)
                     44: #include "opt_ffs.h"
                     45: #endif
1.1       hannken    46:
                     47: #include <sys/param.h>
                     48: #include <sys/kernel.h>
                     49: #include <sys/systm.h>
                     50: #include <sys/conf.h>
                     51: #include <sys/buf.h>
                     52: #include <sys/proc.h>
                     53: #include <sys/namei.h>
                     54: #include <sys/sched.h>
                     55: #include <sys/stat.h>
                     56: #include <sys/malloc.h>
                     57: #include <sys/mount.h>
                     58: #include <sys/resource.h>
                     59: #include <sys/resourcevar.h>
                     60: #include <sys/vnode.h>
                     61:
                     62: #include <miscfs/specfs/specdev.h>
                     63:
                     64: #include <ufs/ufs/quota.h>
                     65: #include <ufs/ufs/ufsmount.h>
                     66: #include <ufs/ufs/inode.h>
                     67: #include <ufs/ufs/ufs_extern.h>
                     68: #include <ufs/ufs/ufs_bswap.h>
                     69:
                     70: #include <ufs/ffs/fs.h>
                     71: #include <ufs/ffs/ffs_extern.h>
                     72:
                     73: /* FreeBSD -> NetBSD conversion */
1.24.8.1  elad       74: #define KERNCRED       proc0.p_cred
1.1       hannken    75: #define ufs1_daddr_t   int32_t
                     76: #define ufs2_daddr_t   int64_t
                     77: #define ufs_lbn_t      daddr_t
                     78: #define VI_MTX(v)      (&(v)->v_interlock)
                     79: #define VI_LOCK(v)     simple_lock(&(v)->v_interlock)
                     80: #define VI_UNLOCK(v)   simple_unlock(&(v)->v_interlock)
                     81: #define MNT_ILOCK(v)   simple_lock(&mntvnode_slock)
                     82: #define MNT_IUNLOCK(v) simple_unlock(&mntvnode_slock)
                     83:
1.10      hannken    84: #if !defined(FFS_NO_SNAPSHOT)
1.1       hannken    85: static int cgaccount(int, struct vnode *, caddr_t, int);
                     86: static int expunge_ufs1(struct vnode *, struct inode *, struct fs *,
                     87:     int (*)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *, struct fs *,
                     88:     ufs_lbn_t, int), int);
                     89: static int indiracct_ufs1(struct vnode *, struct vnode *, int,
                     90:     ufs1_daddr_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, struct fs *,
                     91:     int (*)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *, struct fs *,
                     92:     ufs_lbn_t, int), int);
                     93: static int fullacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
                     94:     struct fs *, ufs_lbn_t, int);
                     95: static int snapacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
                     96:     struct fs *, ufs_lbn_t, int);
                     97: static int mapacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
                     98:     struct fs *, ufs_lbn_t, int);
                     99: static int expunge_ufs2(struct vnode *, struct inode *, struct fs *,
                    100:     int (*)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *, struct fs *,
                    101:     ufs_lbn_t, int), int);
                    102: static int indiracct_ufs2(struct vnode *, struct vnode *, int,
                    103:     ufs2_daddr_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, struct fs *,
                    104:     int (*)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *, struct fs *,
                    105:     ufs_lbn_t, int), int);
                    106: static int fullacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
                    107:     struct fs *, ufs_lbn_t, int);
                    108: static int snapacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
                    109:     struct fs *, ufs_lbn_t, int);
                    110: static int mapacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
                    111:     struct fs *, ufs_lbn_t, int);
1.10      hannken   112: #endif /* !defined(FFS_NO_SNAPSHOT) */
                    113:
1.1       hannken   114: static int ffs_copyonwrite(void *, struct buf *);
                    115: static int readfsblk(struct vnode *, caddr_t, ufs2_daddr_t);
1.10      hannken   116: static int __unused readvnblk(struct vnode *, caddr_t, ufs2_daddr_t);
1.1       hannken   117: static int writevnblk(struct vnode *, caddr_t, ufs2_daddr_t);
1.4       hannken   118: static inline int cow_enter(void);
                    119: static inline void cow_leave(int);
1.1       hannken   120: static inline ufs2_daddr_t db_get(struct inode *, int);
                    121: static inline void db_assign(struct inode *, int, ufs2_daddr_t);
                    122: static inline ufs2_daddr_t idb_get(struct inode *, caddr_t, int);
                    123: static inline void idb_assign(struct inode *, caddr_t, int, ufs2_daddr_t);
                    124:
                    125: #ifdef DEBUG
                    126: static int snapdebug = 0;
                    127: #endif
                    128:
                    129: /*
                    130:  * Create a snapshot file and initialize it for the filesystem.
1.4       hannken   131:  * Vnode is locked on entry and return.
1.1       hannken   132:  */
                    133: int
1.18      thorpej   134: ffs_snapshot(struct mount *mp, struct vnode *vp, struct timespec *ctime)
1.1       hannken   135: {
1.10      hannken   136: #if defined(FFS_NO_SNAPSHOT)
                    137:        return EOPNOTSUPP;
                    138: }
                    139: #else /* defined(FFS_NO_SNAPSHOT) */
1.1       hannken   140:        ufs2_daddr_t numblks, blkno, *blkp, snaplistsize = 0, *snapblklist;
                    141:        int error, ns, cg, snaploc;
1.15      hannken   142:        int i, s, size, len, loc;
1.1       hannken   143:        int flag = mp->mnt_flag;
                    144:        struct timeval starttime;
                    145: #ifdef DEBUG
                    146:        struct timeval endtime;
                    147: #endif
                    148:        struct timespec ts;
                    149:        long redo = 0;
                    150:        int32_t *lp;
                    151:        void *space;
1.16      hannken   152:        caddr_t sbbuf = NULL;
1.1       hannken   153:        struct ufsmount *ump = VFSTOUFS(mp);
                    154:        struct fs *copy_fs = NULL, *fs = ump->um_fs;
1.23      christos  155:        struct lwp *l = curlwp;
1.1       hannken   156:        struct inode *ip, *xp;
1.15      hannken   157:        struct buf *bp, *ibp, *nbp;
1.1       hannken   158:        struct vattr vat;
                    159:        struct vnode *xvp, *nvp, *devvp;
                    160:
                    161:        ns = UFS_FSNEEDSWAP(fs);
                    162:        /*
                    163:         * Need to serialize access to snapshot code per filesystem.
                    164:         */
                    165:        /*
                    166:         * If the vnode already is a snapshot, return.
                    167:         */
                    168:        if (VTOI(vp)->i_flags & SF_SNAPSHOT) {
                    169:                if (ctime) {
                    170:                        ctime->tv_sec = DIP(VTOI(vp), mtime);
                    171:                        ctime->tv_nsec = DIP(VTOI(vp), mtimensec);
                    172:                }
                    173:                return 0;
                    174:        }
                    175:        /*
1.9       hannken   176:         * Check mount, exclusive reference and owner.
1.1       hannken   177:         */
1.4       hannken   178:        if (vp->v_mount != mp)
1.1       hannken   179:                return EXDEV;
1.4       hannken   180:        if (vp->v_usecount != 1 || vp->v_writecount != 0)
1.1       hannken   181:                return EBUSY;
1.24.8.2  elad      182:        if (kauth_authorize_generic(l->l_proc->p_cred, KAUTH_GENERIC_ISSUSER,
1.24.8.1  elad      183:                              &l->l_proc->p_acflag) != 0 &&
                    184:            VTOI(vp)->i_uid != kauth_cred_geteuid(l->l_proc->p_cred))
1.9       hannken   185:                return EACCES;
                    186:
1.1       hannken   187:        if (vp->v_size != 0) {
1.23      christos  188:                error = ffs_truncate(vp, 0, 0, NOCRED, l);
1.4       hannken   189:                if (error)
1.1       hannken   190:                        return error;
                    191:        }
                    192:        /*
                    193:         * Assign a snapshot slot in the superblock.
                    194:         */
                    195:        for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
                    196:                if (fs->fs_snapinum[snaploc] == 0)
                    197:                        break;
                    198:        if (snaploc == FSMAXSNAP)
                    199:                return (ENOSPC);
                    200:        ip = VTOI(vp);
                    201:        devvp = ip->i_devvp;
                    202:        /*
1.16      hannken   203:         * Write an empty list of preallocated blocks to the end of
                    204:         * the snapshot to set size to at least that of the filesystem.
1.1       hannken   205:         */
                    206:        numblks = howmany(fs->fs_size, fs->fs_frag);
1.16      hannken   207:        blkno = 1;
                    208:        blkno = ufs_rw64(blkno, ns);
1.1       hannken   209:        error = vn_rdwr(UIO_WRITE, vp,
1.16      hannken   210:            (caddr_t)&blkno, sizeof(blkno), lblktosize(fs, (off_t)numblks),
1.24.8.1  elad      211:            UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, l->l_proc->p_cred, NULL, NULL);
1.1       hannken   212:        if (error)
                    213:                goto out;
                    214:        /*
                    215:         * Preallocate critical data structures so that we can copy
                    216:         * them in without further allocation after we suspend all
                    217:         * operations on the filesystem. We would like to just release
                    218:         * the allocated buffers without writing them since they will
                    219:         * be filled in below once we are ready to go, but this upsets
                    220:         * the soft update code, so we go ahead and write the new buffers.
                    221:         *
                    222:         * Allocate all indirect blocks and mark all of them as not
                    223:         * needing to be copied.
                    224:         */
                    225:        for (blkno = NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
1.22      yamt      226:                error = ffs_balloc(vp, lblktosize(fs, (off_t)blkno),
1.24.8.1  elad      227:                    fs->fs_bsize, l->l_proc->p_cred, B_METAONLY, &ibp);
1.1       hannken   228:                if (error)
                    229:                        goto out;
1.15      hannken   230:                bawrite(ibp);
1.1       hannken   231:        }
                    232:        /*
                    233:         * Allocate copies for the superblock and its summary information.
                    234:         */
1.22      yamt      235:        error = ffs_balloc(vp, fs->fs_sblockloc, fs->fs_sbsize, KERNCRED,
1.15      hannken   236:            0, &nbp);
                    237:        if (error)
                    238:                goto out;
                    239:        bawrite(nbp);
1.1       hannken   240:        blkno = fragstoblks(fs, fs->fs_csaddr);
1.15      hannken   241:        len = howmany(fs->fs_cssize, fs->fs_bsize);
                    242:        for (loc = 0; loc < len; loc++) {
1.22      yamt      243:                error = ffs_balloc(vp, lblktosize(fs, (off_t)(blkno + loc)),
1.15      hannken   244:                    fs->fs_bsize, KERNCRED, 0, &nbp);
                    245:                if (error)
1.1       hannken   246:                        goto out;
1.15      hannken   247:                bawrite(nbp);
                    248:        }
1.1       hannken   249:        /*
                    250:         * Copy all the cylinder group maps. Although the
                    251:         * filesystem is still active, we hope that only a few
                    252:         * cylinder groups will change between now and when we
                    253:         * suspend operations. Thus, we will be able to quickly
                    254:         * touch up the few cylinder groups that changed during
                    255:         * the suspension period.
                    256:         */
                    257:        len = howmany(fs->fs_ncg, NBBY);
1.24.8.3! elad      258:        fs->fs_active = malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
1.1       hannken   259:        for (cg = 0; cg < fs->fs_ncg; cg++) {
1.22      yamt      260:                if ((error = ffs_balloc(vp, lfragtosize(fs, cgtod(fs, cg)),
1.15      hannken   261:                    fs->fs_bsize, KERNCRED, 0, &nbp)) != 0)
1.1       hannken   262:                        goto out;
1.15      hannken   263:                error = cgaccount(cg, vp, nbp->b_data, 1);
                    264:                bawrite(nbp);
                    265:                if (error)
1.1       hannken   266:                        goto out;
                    267:        }
                    268:        /*
                    269:         * Change inode to snapshot type file.
                    270:         */
                    271:        ip->i_flags |= SF_SNAPSHOT;
                    272:        DIP_ASSIGN(ip, flags, ip->i_flags);
                    273:        ip->i_flag |= IN_CHANGE | IN_UPDATE;
                    274:        /*
                    275:         * Ensure that the snapshot is completely on disk.
                    276:         * Since we have marked it as a snapshot it is safe to
                    277:         * unlock it as no process will be allowed to write to it.
                    278:         */
1.23      christos  279:        if ((error = VOP_FSYNC(vp, KERNCRED, FSYNC_WAIT, 0, 0, l)) != 0)
1.1       hannken   280:                goto out;
                    281:        VOP_UNLOCK(vp, 0);
                    282:        /*
                    283:         * All allocations are done, so we can now snapshot the system.
                    284:         *
                    285:         * Suspend operation on filesystem.
                    286:         */
                    287:        if ((error = vfs_write_suspend(vp->v_mount, PUSER|PCATCH, 0)) != 0) {
                    288:                vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
                    289:                goto out;
                    290:        }
                    291:        vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
                    292:        microtime(&starttime);
                    293:        /*
                    294:         * First, copy all the cylinder group maps that have changed.
                    295:         */
                    296:        for (cg = 0; cg < fs->fs_ncg; cg++) {
                    297:                if (ACTIVECG_ISSET(fs, cg))
                    298:                        continue;
                    299:                redo++;
1.22      yamt      300:                if ((error = ffs_balloc(vp, lfragtosize(fs, cgtod(fs, cg)),
1.15      hannken   301:                    fs->fs_bsize, KERNCRED, 0, &nbp)) != 0)
1.1       hannken   302:                        goto out1;
1.15      hannken   303:                error = cgaccount(cg, vp, nbp->b_data, 2);
                    304:                bawrite(nbp);
                    305:                if (error)
1.1       hannken   306:                        goto out1;
                    307:        }
                    308:        /*
                    309:         * Grab a copy of the superblock and its summary information.
                    310:         * We delay writing it until the suspension is released below.
                    311:         */
1.16      hannken   312:        sbbuf = malloc(fs->fs_bsize, M_UFSMNT, M_WAITOK);
1.1       hannken   313:        loc = blkoff(fs, fs->fs_sblockloc);
                    314:        if (loc > 0)
1.16      hannken   315:                bzero(&sbbuf[0], loc);
                    316:        copy_fs = (struct fs *)(sbbuf + loc);
1.1       hannken   317:        bcopy(fs, copy_fs, fs->fs_sbsize);
                    318:        size = fs->fs_bsize < SBLOCKSIZE ? fs->fs_bsize : SBLOCKSIZE;
                    319:        if (fs->fs_sbsize < size)
1.16      hannken   320:                bzero(&sbbuf[loc + fs->fs_sbsize], size - fs->fs_sbsize);
1.1       hannken   321:        size = blkroundup(fs, fs->fs_cssize);
                    322:        if (fs->fs_contigsumsize > 0)
                    323:                size += fs->fs_ncg * sizeof(int32_t);
                    324:        space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
                    325:        copy_fs->fs_csp = space;
                    326:        bcopy(fs->fs_csp, copy_fs->fs_csp, fs->fs_cssize);
1.12      yamt      327:        space = (char *)space + fs->fs_cssize;
1.1       hannken   328:        loc = howmany(fs->fs_cssize, fs->fs_fsize);
                    329:        i = fs->fs_frag - loc % fs->fs_frag;
                    330:        len = (i == fs->fs_frag) ? 0 : i * fs->fs_fsize;
                    331:        if (len > 0) {
                    332:                if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + loc),
                    333:                    len, KERNCRED, &bp)) != 0) {
                    334:                        brelse(bp);
                    335:                        free(copy_fs->fs_csp, M_UFSMNT);
                    336:                        goto out1;
                    337:                }
                    338:                bcopy(bp->b_data, space, (u_int)len);
1.12      yamt      339:                space = (char *)space + len;
1.1       hannken   340:                bp->b_flags |= B_INVAL | B_NOCACHE;
                    341:                brelse(bp);
                    342:        }
                    343:        if (fs->fs_contigsumsize > 0) {
                    344:                copy_fs->fs_maxcluster = lp = space;
                    345:                for (i = 0; i < fs->fs_ncg; i++)
                    346:                        *lp++ = fs->fs_contigsumsize;
                    347:        }
                    348:        /*
                    349:         * We must check for active files that have been unlinked
                    350:         * (e.g., with a zero link count). We have to expunge all
                    351:         * trace of these files from the snapshot so that they are
                    352:         * not reclaimed prematurely by fsck or unnecessarily dumped.
                    353:         * We turn off the MNTK_SUSPENDED flag to avoid a panic from
                    354:         * spec_strategy about writing on a suspended filesystem.
                    355:         * Note that we skip unlinked snapshot files as they will
                    356:         * be handled separately below.
                    357:         *
                    358:         * We also calculate the needed size for the snapshot list.
                    359:         */
                    360:        snaplistsize = fs->fs_ncg + howmany(fs->fs_cssize, fs->fs_bsize) +
                    361:            FSMAXSNAP + 1 /* superblock */ + 1 /* last block */ + 1 /* size */;
                    362:        MNT_ILOCK(mp);
                    363: loop:
                    364:        for (xvp = LIST_FIRST(&mp->mnt_vnodelist); xvp; xvp = nvp) {
                    365:                /*
                    366:                 * Make sure this vnode wasn't reclaimed in getnewvnode().
                    367:                 * Start over if it has (it won't be on the list anymore).
                    368:                 */
                    369:                if (xvp->v_mount != mp)
                    370:                        goto loop;
                    371:                nvp = LIST_NEXT(xvp, v_mntvnodes);
                    372:                VI_LOCK(xvp);
                    373:                MNT_IUNLOCK(mp);
                    374:                if ((xvp->v_flag & VXLOCK) ||
                    375:                    xvp->v_usecount == 0 || xvp->v_type == VNON ||
                    376:                    (VTOI(xvp)->i_flags & SF_SNAPSHOT)) {
                    377:                        VI_UNLOCK(xvp);
                    378:                        MNT_ILOCK(mp);
                    379:                        continue;
                    380:                }
                    381:                if (vn_lock(xvp, LK_EXCLUSIVE | LK_INTERLOCK) != 0) {
                    382:                        MNT_ILOCK(mp);
                    383:                        goto loop;
                    384:                }
                    385: #ifdef DEBUG
                    386:                if (snapdebug)
                    387:                        vprint("ffs_snapshot: busy vnode", xvp);
                    388: #endif
1.24.8.1  elad      389:                if (VOP_GETATTR(xvp, &vat, l->l_proc->p_cred, l) == 0 &&
1.1       hannken   390:                    vat.va_nlink > 0) {
                    391:                        VOP_UNLOCK(xvp, 0);
                    392:                        MNT_ILOCK(mp);
                    393:                        continue;
                    394:                }
                    395:                xp = VTOI(xvp);
                    396:                if (ffs_checkfreefile(copy_fs, vp, xp->i_number)) {
                    397:                        VOP_UNLOCK(xvp, 0);
                    398:                        MNT_ILOCK(mp);
                    399:                        continue;
                    400:                }
                    401:                /*
                    402:                 * If there is a fragment, clear it here.
                    403:                 */
                    404:                blkno = 0;
                    405:                loc = howmany(xp->i_size, fs->fs_bsize) - 1;
                    406:                if (loc < NDADDR) {
                    407:                        len = fragroundup(fs, blkoff(fs, xp->i_size));
1.5       hannken   408:                        if (len > 0 && len < fs->fs_bsize) {
1.1       hannken   409:                                ffs_blkfree(copy_fs, vp, db_get(xp, loc),
                    410:                                    len, xp->i_number);
                    411:                                blkno = db_get(xp, loc);
                    412:                                db_assign(xp, loc, 0);
                    413:                        }
                    414:                }
                    415:                snaplistsize += 1;
                    416:                if (xp->i_ump->um_fstype == UFS1)
                    417:                        error = expunge_ufs1(vp, xp, copy_fs, fullacct_ufs1,
                    418:                            BLK_NOCOPY);
                    419:                else
                    420:                        error = expunge_ufs2(vp, xp, copy_fs, fullacct_ufs2,
                    421:                            BLK_NOCOPY);
                    422:                if (blkno)
                    423:                        db_assign(xp, loc, blkno);
1.6       hannken   424:                if (!error)
                    425:                        error = ffs_freefile(copy_fs, vp, xp->i_number,
                    426:                            xp->i_mode);
1.1       hannken   427:                VOP_UNLOCK(xvp, 0);
                    428:                if (error) {
                    429:                        free(copy_fs->fs_csp, M_UFSMNT);
                    430:                        goto out1;
                    431:                }
                    432:                MNT_ILOCK(mp);
                    433:        }
                    434:        MNT_IUNLOCK(mp);
                    435:        /*
                    436:         * If there already exist snapshots on this filesystem, grab a
                    437:         * reference to their shared lock. If this is the first snapshot
                    438:         * on this filesystem, we need to allocate a lock for the snapshots
                    439:         * to share. In either case, acquire the snapshot lock and give
                    440:         * up our original private lock.
                    441:         */
                    442:        VI_LOCK(devvp);
                    443:        if ((xp = TAILQ_FIRST(&ump->um_snapshots)) != NULL) {
                    444:                struct lock *lkp;
                    445:
                    446:                lkp = ITOV(xp)->v_vnlock;
                    447:                VI_UNLOCK(devvp);
                    448:                VI_LOCK(vp);
                    449:                vp->v_vnlock = lkp;
                    450:        } else {
                    451:                struct lock *lkp;
                    452:
                    453:                VI_UNLOCK(devvp);
                    454:                MALLOC(lkp, struct lock *, sizeof(struct lock), M_UFSMNT,
                    455:                    M_WAITOK);
                    456:                lockinit(lkp, PVFS, "snaplk", 0, LK_CANRECURSE);
                    457:                VI_LOCK(vp);
                    458:                vp->v_vnlock = lkp;
                    459:        }
                    460:        vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY);
                    461:        transferlockers(&vp->v_lock, vp->v_vnlock);
                    462:        lockmgr(&vp->v_lock, LK_RELEASE, NULL);
                    463:        /*
                    464:         * If this is the first snapshot on this filesystem, then we need
                    465:         * to allocate the space for the list of preallocated snapshot blocks.
                    466:         * This list will be refined below, but this preliminary one will
                    467:         * keep us out of deadlock until the full one is ready.
                    468:         */
                    469:        if (xp == NULL) {
1.24.8.3! elad      470:                snapblklist = malloc(
1.1       hannken   471:                    snaplistsize * sizeof(ufs2_daddr_t), M_UFSMNT, M_WAITOK);
                    472:                blkp = &snapblklist[1];
1.15      hannken   473:                *blkp++ = lblkno(fs, fs->fs_sblockloc);
1.1       hannken   474:                blkno = fragstoblks(fs, fs->fs_csaddr);
                    475:                for (cg = 0; cg < fs->fs_ncg; cg++) {
1.15      hannken   476:                        if (fragstoblks(fs, cgtod(fs, cg)) > blkno)
1.1       hannken   477:                                break;
1.15      hannken   478:                        *blkp++ = fragstoblks(fs, cgtod(fs, cg));
1.1       hannken   479:                }
                    480:                len = howmany(fs->fs_cssize, fs->fs_bsize);
                    481:                for (loc = 0; loc < len; loc++)
1.15      hannken   482:                        *blkp++ = blkno + loc;
1.1       hannken   483:                for (; cg < fs->fs_ncg; cg++)
1.15      hannken   484:                        *blkp++ = fragstoblks(fs, cgtod(fs, cg));
                    485:                snapblklist[0] = blkp - snapblklist;
1.1       hannken   486:                VI_LOCK(devvp);
                    487:                if (ump->um_snapblklist != NULL)
                    488:                        panic("ffs_snapshot: non-empty list");
                    489:                ump->um_snapblklist = snapblklist;
                    490:                VI_UNLOCK(devvp);
                    491:        }
                    492:        /*
                    493:         * Record snapshot inode. Since this is the newest snapshot,
                    494:         * it must be placed at the end of the list.
                    495:         */
                    496:        VI_LOCK(devvp);
                    497:        fs->fs_snapinum[snaploc] = ip->i_number;
                    498:        if (ip->i_nextsnap.tqe_prev != 0)
1.19      christos  499:                panic("ffs_snapshot: %llu already on list",
                    500:                    (unsigned long long)ip->i_number);
1.1       hannken   501:        TAILQ_INSERT_TAIL(&ump->um_snapshots, ip, i_nextsnap);
                    502:        VI_UNLOCK(devvp);
                    503:        if (xp == NULL)
                    504:                vn_cow_establish(devvp, ffs_copyonwrite, devvp);
                    505:        vp->v_flag |= VSYSTEM;
                    506: out1:
                    507:        /*
                    508:         * Resume operation on filesystem.
                    509:         */
                    510:        vfs_write_resume(vp->v_mount);
                    511:        /*
                    512:         * Set the mtime to the time the snapshot has been taken.
                    513:         */
1.21      yamt      514:        TIMEVAL_TO_TIMESPEC(&starttime, &ts);
1.1       hannken   515:        if (ctime)
                    516:                *ctime = ts;
                    517:        DIP_ASSIGN(ip, mtime, ts.tv_sec);
                    518:        DIP_ASSIGN(ip, mtimensec, ts.tv_nsec);
                    519:        ip->i_flag |= IN_CHANGE | IN_UPDATE;
                    520:
                    521: #ifdef DEBUG
                    522:        if (starttime.tv_sec > 0) {
                    523:                microtime(&endtime);
                    524:                timersub(&endtime, &starttime, &endtime);
                    525:                printf("%s: suspended %ld.%03ld sec, redo %ld of %d\n",
                    526:                    vp->v_mount->mnt_stat.f_mntonname, (long)endtime.tv_sec,
                    527:                    endtime.tv_usec / 1000, redo, fs->fs_ncg);
                    528:        }
                    529: #endif
                    530:        if (error)
                    531:                goto out;
                    532:        /*
                    533:         * Copy allocation information from all the snapshots in
                    534:         * this snapshot and then expunge them from its view.
                    535:         */
                    536:        TAILQ_FOREACH(xp, &ump->um_snapshots, i_nextsnap) {
                    537:                if (xp == ip)
                    538:                        break;
                    539:                if (xp->i_ump->um_fstype == UFS1)
                    540:                        error = expunge_ufs1(vp, xp, fs, snapacct_ufs1,
                    541:                            BLK_SNAP);
                    542:                else
                    543:                        error = expunge_ufs2(vp, xp, fs, snapacct_ufs2,
                    544:                            BLK_SNAP);
                    545:                if (error) {
                    546:                        fs->fs_snapinum[snaploc] = 0;
                    547:                        goto done;
                    548:                }
                    549:        }
                    550:        /*
                    551:         * Allocate space for the full list of preallocated snapshot blocks.
                    552:         */
1.24.8.3! elad      553:        snapblklist = malloc(snaplistsize * sizeof(ufs2_daddr_t),
1.1       hannken   554:            M_UFSMNT, M_WAITOK);
                    555:        ip->i_snapblklist = &snapblklist[1];
                    556:        /*
                    557:         * Expunge the blocks used by the snapshots from the set of
                    558:         * blocks marked as used in the snapshot bitmaps. Also, collect
                    559:         * the list of allocated blocks in i_snapblklist.
                    560:         */
                    561:        if (ip->i_ump->um_fstype == UFS1)
                    562:                error = expunge_ufs1(vp, ip, copy_fs, mapacct_ufs1, BLK_SNAP);
                    563:        else
                    564:                error = expunge_ufs2(vp, ip, copy_fs, mapacct_ufs2, BLK_SNAP);
                    565:        if (error) {
                    566:                fs->fs_snapinum[snaploc] = 0;
                    567:                FREE(snapblklist, M_UFSMNT);
                    568:                goto done;
                    569:        }
                    570:        if (snaplistsize < ip->i_snapblklist - snapblklist)
                    571:                panic("ffs_snapshot: list too small");
                    572:        snaplistsize = ip->i_snapblklist - snapblklist;
1.15      hannken   573:        snapblklist[0] = snaplistsize;
                    574:        ip->i_snapblklist = &snapblklist[0];
1.1       hannken   575:        /*
                    576:         * Write out the list of allocated blocks to the end of the snapshot.
                    577:         */
1.15      hannken   578:        for (i = 0; i < snaplistsize; i++)
                    579:                snapblklist[i] = ufs_rw64(snapblklist[i], ns);
1.16      hannken   580:        error = vn_rdwr(UIO_WRITE, vp, (caddr_t)snapblklist,
                    581:            snaplistsize*sizeof(ufs2_daddr_t), lblktosize(fs, (off_t)numblks),
1.24.8.1  elad      582:            UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, l->l_proc->p_cred, NULL, NULL);
1.15      hannken   583:        for (i = 0; i < snaplistsize; i++)
                    584:                snapblklist[i] = ufs_rw64(snapblklist[i], ns);
1.1       hannken   585:        if (error) {
                    586:                fs->fs_snapinum[snaploc] = 0;
                    587:                FREE(snapblklist, M_UFSMNT);
                    588:                goto done;
                    589:        }
                    590:        /*
                    591:         * Write the superblock and its summary information
                    592:         * to the snapshot.
                    593:         */
                    594:        blkno = fragstoblks(fs, fs->fs_csaddr);
                    595:        len = howmany(fs->fs_cssize, fs->fs_bsize);
                    596:        space = copy_fs->fs_csp;
1.8       hannken   597: #ifdef FFS_EI
1.1       hannken   598:        if (ns) {
                    599:                ffs_sb_swap(copy_fs, copy_fs);
                    600:                ffs_csum_swap(space, space, fs->fs_cssize);
                    601:        }
1.8       hannken   602: #endif
1.1       hannken   603:        for (loc = 0; loc < len; loc++) {
1.15      hannken   604:                error = bread(vp, blkno + loc, fs->fs_bsize, KERNCRED, &nbp);
                    605:                if (error) {
                    606:                        brelse(nbp);
1.1       hannken   607:                        fs->fs_snapinum[snaploc] = 0;
                    608:                        FREE(snapblklist, M_UFSMNT);
                    609:                        goto done;
                    610:                }
1.15      hannken   611:                bcopy(space, nbp->b_data, fs->fs_bsize);
1.1       hannken   612:                space = (char *)space + fs->fs_bsize;
1.15      hannken   613:                bawrite(nbp);
1.1       hannken   614:        }
                    615:        /*
                    616:         * As this is the newest list, it is the most inclusive, so
1.15      hannken   617:         * should replace the previous list. If this is the first snapshot
                    618:         * free the preliminary list.
1.1       hannken   619:         */
                    620:        VI_LOCK(devvp);
                    621:        space = ump->um_snapblklist;
                    622:        ump->um_snapblklist = snapblklist;
                    623:        VI_UNLOCK(devvp);
1.15      hannken   624:        if (TAILQ_FIRST(&ump->um_snapshots) == ip)
1.1       hannken   625:                FREE(space, M_UFSMNT);
                    626: done:
                    627:        free(copy_fs->fs_csp, M_UFSMNT);
1.15      hannken   628:        if (!error) {
                    629:                error = bread(vp, lblkno(fs, fs->fs_sblockloc), fs->fs_bsize,
                    630:                    KERNCRED, &nbp);
                    631:                if (error) {
                    632:                        brelse(nbp);
                    633:                        fs->fs_snapinum[snaploc] = 0;
                    634:                }
1.16      hannken   635:                bcopy(sbbuf, nbp->b_data, fs->fs_bsize);
1.15      hannken   636:                bawrite(nbp);
                    637:        }
1.1       hannken   638: out:
1.4       hannken   639:        /*
1.15      hannken   640:         * Invalidate and free all pages on the snapshot vnode.
                    641:         * All metadata has been written through the buffer cache.
                    642:         * Clean all dirty buffers now to avoid UBC inconsistencies.
1.4       hannken   643:         */
                    644:        if (!error) {
                    645:                simple_lock(&vp->v_interlock);
                    646:                error = VOP_PUTPAGES(vp, 0, 0,
                    647:                    PGO_ALLPAGES|PGO_CLEANIT|PGO_SYNCIO|PGO_FREE);
                    648:        }
1.15      hannken   649:        if (!error) {
                    650:                s = splbio();
                    651:                for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
                    652:                        nbp = LIST_NEXT(bp, b_vnbufs);
                    653:                        simple_lock(&bp->b_interlock);
                    654:                        splx(s);
                    655:                        if ((bp->b_flags & (B_DELWRI|B_BUSY)) != B_DELWRI)
                    656:                                panic("ffs_snapshot: not dirty or busy, bp %p",
                    657:                                    bp);
                    658:                        bp->b_flags |= B_BUSY|B_VFLUSH;
                    659:                        if (LIST_FIRST(&bp->b_dep) == NULL)
                    660:                                bp->b_flags |= B_NOCACHE;
                    661:                        simple_unlock(&bp->b_interlock);
                    662:                        bwrite(bp);
                    663:                        s = splbio();
                    664:                }
                    665:                simple_lock(&global_v_numoutput_slock);
                    666:                while (vp->v_numoutput) {
                    667:                        vp->v_flag |= VBWAIT;
                    668:                        ltsleep((caddr_t)&vp->v_numoutput, PRIBIO+1,
                    669:                            "snapflushbuf", 0, &global_v_numoutput_slock);
                    670:                }
                    671:                simple_unlock(&global_v_numoutput_slock);
                    672:                splx(s);
                    673:        }
1.16      hannken   674:        if (sbbuf)
                    675:                free(sbbuf, M_UFSMNT);
1.1       hannken   676:        if (fs->fs_active != 0) {
                    677:                FREE(fs->fs_active, M_DEVBUF);
                    678:                fs->fs_active = 0;
                    679:        }
                    680:        mp->mnt_flag = flag;
                    681:        if (error)
1.23      christos  682:                (void) ffs_truncate(vp, (off_t)0, 0, NOCRED, l);
1.1       hannken   683:        else
                    684:                vref(vp);
                    685:        return (error);
                    686: }
                    687:
                    688: /*
                    689:  * Copy a cylinder group map. All the unallocated blocks are marked
                    690:  * BLK_NOCOPY so that the snapshot knows that it need not copy them
                    691:  * if they are later written. If passno is one, then this is a first
                    692:  * pass, so only setting needs to be done. If passno is 2, then this
                    693:  * is a revision to a previous pass which must be undone as the
                    694:  * replacement pass is done.
                    695:  */
                    696: static int
1.18      thorpej   697: cgaccount(int cg, struct vnode *vp, caddr_t data, int passno)
1.1       hannken   698: {
                    699:        struct buf *bp, *ibp;
                    700:        struct inode *ip;
                    701:        struct cg *cgp;
                    702:        struct fs *fs;
                    703:        ufs2_daddr_t base, numblks;
                    704:        int error, len, loc, ns, indiroff;
                    705:
                    706:        ip = VTOI(vp);
                    707:        fs = ip->i_fs;
                    708:        ns = UFS_FSNEEDSWAP(fs);
                    709:        error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
                    710:                (int)fs->fs_cgsize, KERNCRED, &bp);
                    711:        if (error) {
                    712:                brelse(bp);
                    713:                return (error);
                    714:        }
                    715:        cgp = (struct cg *)bp->b_data;
                    716:        if (!cg_chkmagic(cgp, ns)) {
                    717:                brelse(bp);
                    718:                return (EIO);
                    719:        }
                    720:        ACTIVECG_SET(fs, cg);
                    721:
                    722:        bcopy(bp->b_data, data, fs->fs_cgsize);
                    723:        brelse(bp);
                    724:        if (fs->fs_cgsize < fs->fs_bsize)
                    725:                bzero(&data[fs->fs_cgsize],
                    726:                    fs->fs_bsize - fs->fs_cgsize);
                    727:        numblks = howmany(fs->fs_size, fs->fs_frag);
                    728:        len = howmany(fs->fs_fpg, fs->fs_frag);
                    729:        base = cg * fs->fs_fpg / fs->fs_frag;
                    730:        if (base + len >= numblks)
                    731:                len = numblks - base - 1;
                    732:        loc = 0;
                    733:        if (base < NDADDR) {
                    734:                for ( ; loc < NDADDR; loc++) {
                    735:                        if (ffs_isblock(fs, cg_blksfree(cgp, ns), loc))
                    736:                                db_assign(ip, loc, BLK_NOCOPY);
                    737:                        else if (db_get(ip, loc) == BLK_NOCOPY) {
                    738:                                if (passno == 2)
                    739:                                        db_assign(ip, loc, 0);
                    740:                                else if (passno == 1)
                    741:                                        panic("ffs_snapshot: lost direct block");
                    742:                        }
                    743:                }
                    744:        }
1.22      yamt      745:        if ((error = ffs_balloc(vp, lblktosize(fs, (off_t)(base + loc)),
1.11      perry     746:            fs->fs_bsize, KERNCRED, B_METAONLY, &ibp)) != 0)
1.1       hannken   747:                return (error);
                    748:        indiroff = (base + loc - NDADDR) % NINDIR(fs);
                    749:        for ( ; loc < len; loc++, indiroff++) {
                    750:                if (indiroff >= NINDIR(fs)) {
1.15      hannken   751:                        bawrite(ibp);
1.22      yamt      752:                        if ((error = ffs_balloc(vp,
1.1       hannken   753:                            lblktosize(fs, (off_t)(base + loc)),
                    754:                            fs->fs_bsize, KERNCRED, B_METAONLY, &ibp)) != 0)
                    755:                                return (error);
                    756:                        indiroff = 0;
                    757:                }
                    758:                if (ffs_isblock(fs, cg_blksfree(cgp, ns), loc))
                    759:                        idb_assign(ip, ibp->b_data, indiroff, BLK_NOCOPY);
                    760:                else if (idb_get(ip, ibp->b_data, indiroff) == BLK_NOCOPY) {
                    761:                        if (passno == 2)
                    762:                                idb_assign(ip, ibp->b_data, indiroff, 0);
                    763:                        else if (passno == 1)
                    764:                                panic("ffs_snapshot: lost indirect block");
                    765:                }
                    766:        }
1.15      hannken   767:        bdwrite(ibp);
1.1       hannken   768:        return (0);
                    769: }
                    770:
                    771: /*
                    772:  * Before expunging a snapshot inode, note all the
                    773:  * blocks that it claims with BLK_SNAP so that fsck will
                    774:  * be able to account for those blocks properly and so
                    775:  * that this snapshot knows that it need not copy them
                    776:  * if the other snapshot holding them is freed. This code
                    777:  * is reproduced once each for UFS1 and UFS2.
                    778:  */
                    779: static int
1.18      thorpej   780: expunge_ufs1(struct vnode *snapvp, struct inode *cancelip, struct fs *fs,
                    781:     int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
                    782:                    struct fs *, ufs_lbn_t, int),
                    783:     int expungetype)
1.1       hannken   784: {
1.4       hannken   785:        int i, s, error, ns, indiroff;
1.1       hannken   786:        ufs_lbn_t lbn, rlbn;
                    787:        ufs2_daddr_t len, blkno, numblks, blksperindir;
                    788:        struct ufs1_dinode *dip;
                    789:        struct buf *bp;
1.17      christos  790:        caddr_t bf;
1.1       hannken   791:
                    792:        ns = UFS_FSNEEDSWAP(fs);
                    793:        /*
                    794:         * Prepare to expunge the inode. If its inode block has not
                    795:         * yet been copied, then allocate and fill the copy.
                    796:         */
                    797:        lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
                    798:        blkno = 0;
                    799:        if (lbn < NDADDR) {
                    800:                blkno = db_get(VTOI(snapvp), lbn);
                    801:        } else {
1.4       hannken   802:                s = cow_enter();
1.22      yamt      803:                error = ffs_balloc(snapvp, lblktosize(fs, (off_t)lbn),
1.1       hannken   804:                   fs->fs_bsize, KERNCRED, B_METAONLY, &bp);
1.4       hannken   805:                cow_leave(s);
1.1       hannken   806:                if (error)
                    807:                        return (error);
                    808:                indiroff = (lbn - NDADDR) % NINDIR(fs);
                    809:                blkno = idb_get(VTOI(snapvp), bp->b_data, indiroff);
                    810:                brelse(bp);
                    811:        }
1.17      christos  812:        bf = malloc(fs->fs_bsize, M_UFSMNT, M_WAITOK);
1.1       hannken   813:        if (blkno != 0)
1.17      christos  814:                error = readvnblk(snapvp, bf, lbn);
1.1       hannken   815:        else
1.17      christos  816:                error = readfsblk(snapvp, bf, lbn);
1.1       hannken   817:        if (error) {
1.17      christos  818:                free(bf, M_UFSMNT);
1.1       hannken   819:                return error;
                    820:        }
                    821:        /*
                    822:         * Set a snapshot inode to be a zero length file, regular files
                    823:         * to be completely unallocated.
                    824:         */
1.17      christos  825:        dip = (struct ufs1_dinode *)bf + ino_to_fsbo(fs, cancelip->i_number);
1.1       hannken   826:        if (expungetype == BLK_NOCOPY)
                    827:                dip->di_mode = 0;
                    828:        dip->di_size = 0;
                    829:        dip->di_blocks = 0;
                    830:        dip->di_flags =
                    831:            ufs_rw32(ufs_rw32(dip->di_flags, ns) & ~SF_SNAPSHOT, ns);
                    832:        bzero(&dip->di_db[0], (NDADDR + NIADDR) * sizeof(ufs1_daddr_t));
1.17      christos  833:        error = writevnblk(snapvp, bf, lbn);
                    834:        free(bf, M_UFSMNT);
1.1       hannken   835:        if (error)
                    836:                return error;
                    837:        /*
                    838:         * Now go through and expunge all the blocks in the file
                    839:         * using the function requested.
                    840:         */
                    841:        numblks = howmany(cancelip->i_size, fs->fs_bsize);
                    842:        if ((error = (*acctfunc)(snapvp, &cancelip->i_ffs1_db[0],
                    843:            &cancelip->i_ffs1_db[NDADDR], fs, 0, expungetype)))
                    844:                return (error);
                    845:        if ((error = (*acctfunc)(snapvp, &cancelip->i_ffs1_ib[0],
                    846:            &cancelip->i_ffs1_ib[NIADDR], fs, -1, expungetype)))
                    847:                return (error);
                    848:        blksperindir = 1;
                    849:        lbn = -NDADDR;
                    850:        len = numblks - NDADDR;
                    851:        rlbn = NDADDR;
                    852:        for (i = 0; len > 0 && i < NIADDR; i++) {
                    853:                error = indiracct_ufs1(snapvp, ITOV(cancelip), i,
                    854:                    ufs_rw32(cancelip->i_ffs1_ib[i], ns), lbn, rlbn, len,
                    855:                    blksperindir, fs, acctfunc, expungetype);
                    856:                if (error)
                    857:                        return (error);
                    858:                blksperindir *= NINDIR(fs);
                    859:                lbn -= blksperindir + 1;
                    860:                len -= blksperindir;
                    861:                rlbn += blksperindir;
                    862:        }
                    863:        return (0);
                    864: }
                    865:
                    866: /*
                    867:  * Descend an indirect block chain for vnode cancelvp accounting for all
                    868:  * its indirect blocks in snapvp.
1.11      perry     869:  */
1.1       hannken   870: static int
1.18      thorpej   871: indiracct_ufs1(struct vnode *snapvp, struct vnode *cancelvp, int level,
                    872:     ufs1_daddr_t blkno, ufs_lbn_t lbn, ufs_lbn_t rlbn, ufs_lbn_t remblks,
                    873:     ufs_lbn_t blksperindir, struct fs *fs,
                    874:     int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
                    875:                    struct fs *, ufs_lbn_t, int),
                    876:     int expungetype)
1.1       hannken   877: {
                    878:        int error, ns, num, i;
                    879:        ufs_lbn_t subblksperindir;
                    880:        struct indir indirs[NIADDR + 2];
                    881:        ufs1_daddr_t last, *bap;
                    882:        struct buf *bp;
                    883:
                    884:        ns = UFS_FSNEEDSWAP(fs);
                    885:
                    886:        if (blkno == 0) {
                    887:                if (expungetype == BLK_NOCOPY)
                    888:                        return (0);
                    889:                panic("indiracct_ufs1: missing indir");
                    890:        }
                    891:        if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
                    892:                return (error);
                    893:        if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
                    894:                panic("indiracct_ufs1: botched params");
                    895:        /*
                    896:         * We have to expand bread here since it will deadlock looking
                    897:         * up the block number for any blocks that are not in the cache.
                    898:         */
                    899:        bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0);
                    900:        bp->b_blkno = fsbtodb(fs, blkno);
                    901:        if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0 &&
                    902:            (error = readfsblk(bp->b_vp, bp->b_data, fragstoblks(fs, blkno)))) {
                    903:                brelse(bp);
                    904:                return (error);
                    905:        }
                    906:        /*
                    907:         * Account for the block pointers in this indirect block.
                    908:         */
                    909:        last = howmany(remblks, blksperindir);
                    910:        if (last > NINDIR(fs))
                    911:                last = NINDIR(fs);
1.24.8.3! elad      912:        bap = malloc(fs->fs_bsize, M_DEVBUF, M_WAITOK);
1.1       hannken   913:        bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
                    914:        brelse(bp);
                    915:        error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
                    916:            level == 0 ? rlbn : -1, expungetype);
                    917:        if (error || level == 0)
                    918:                goto out;
                    919:        /*
                    920:         * Account for the block pointers in each of the indirect blocks
                    921:         * in the levels below us.
                    922:         */
                    923:        subblksperindir = blksperindir / NINDIR(fs);
                    924:        for (lbn++, level--, i = 0; i < last; i++) {
                    925:                error = indiracct_ufs1(snapvp, cancelvp, level,
                    926:                    ufs_rw32(bap[i], ns), lbn, rlbn, remblks, subblksperindir,
                    927:                    fs, acctfunc, expungetype);
                    928:                if (error)
                    929:                        goto out;
                    930:                rlbn += blksperindir;
                    931:                lbn -= blksperindir;
                    932:                remblks -= blksperindir;
                    933:        }
                    934: out:
                    935:        FREE(bap, M_DEVBUF);
                    936:        return (error);
                    937: }
                    938:
                    939: /*
                    940:  * Do both snap accounting and map accounting.
                    941:  */
                    942: static int
1.18      thorpej   943: fullacct_ufs1(struct vnode *vp, ufs1_daddr_t *oldblkp, ufs1_daddr_t *lastblkp,
                    944:     struct fs *fs, ufs_lbn_t lblkno,
                    945:     int exptype /* BLK_SNAP or BLK_NOCOPY */)
1.1       hannken   946: {
                    947:        int error;
                    948:
                    949:        if ((error = snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
                    950:                return (error);
                    951:        return (mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype));
                    952: }
                    953:
                    954: /*
                    955:  * Identify a set of blocks allocated in a snapshot inode.
                    956:  */
                    957: static int
1.18      thorpej   958: snapacct_ufs1(struct vnode *vp, ufs1_daddr_t *oldblkp, ufs1_daddr_t *lastblkp,
                    959:     struct fs *fs, ufs_lbn_t lblkno,
                    960:     int expungetype /* BLK_SNAP or BLK_NOCOPY */)
1.1       hannken   961: {
                    962:        struct inode *ip = VTOI(vp);
                    963:        ufs1_daddr_t blkno, *blkp;
                    964:        ufs_lbn_t lbn;
                    965:        struct buf *ibp;
                    966:        int error, ns;
                    967:
                    968:        ns = UFS_FSNEEDSWAP(fs);
                    969:
                    970:        for ( ; oldblkp < lastblkp; oldblkp++) {
                    971:                blkno = ufs_rw32(*oldblkp, ns);
                    972:                if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
                    973:                        continue;
                    974:                lbn = fragstoblks(fs, blkno);
                    975:                if (lbn < NDADDR) {
                    976:                        blkp = &ip->i_ffs1_db[lbn];
                    977:                        ip->i_flag |= IN_CHANGE | IN_UPDATE;
                    978:                } else {
1.22      yamt      979:                        error = ffs_balloc(vp, lblktosize(fs, (off_t)lbn),
1.1       hannken   980:                            fs->fs_bsize, KERNCRED, B_METAONLY, &ibp);
                    981:                        if (error)
                    982:                                return (error);
                    983:                        blkp = &((ufs1_daddr_t *)(ibp->b_data))
                    984:                            [(lbn - NDADDR) % NINDIR(fs)];
                    985:                }
                    986:                /*
                    987:                 * If we are expunging a snapshot vnode and we
                    988:                 * find a block marked BLK_NOCOPY, then it is
                    989:                 * one that has been allocated to this snapshot after
                    990:                 * we took our current snapshot and can be ignored.
                    991:                 */
                    992:                blkno = ufs_rw32(*blkp, ns);
                    993:                if (expungetype == BLK_SNAP && blkno == BLK_NOCOPY) {
                    994:                        if (lbn >= NDADDR)
                    995:                                brelse(ibp);
                    996:                } else {
                    997:                        if (blkno != 0)
                    998:                                panic("snapacct_ufs1: bad block");
                    999:                        *blkp = ufs_rw32(expungetype, ns);
                   1000:                        if (lbn >= NDADDR)
1.15      hannken  1001:                                bdwrite(ibp);
1.1       hannken  1002:                }
                   1003:        }
                   1004:        return (0);
                   1005: }
                   1006:
                   1007: /*
                   1008:  * Account for a set of blocks allocated in a snapshot inode.
                   1009:  */
                   1010: static int
1.18      thorpej  1011: mapacct_ufs1(struct vnode *vp, ufs1_daddr_t *oldblkp, ufs1_daddr_t *lastblkp,
                   1012:     struct fs *fs, ufs_lbn_t lblkno, int expungetype)
1.1       hannken  1013: {
                   1014:        ufs1_daddr_t blkno;
                   1015:        struct inode *ip;
                   1016:        ino_t inum;
                   1017:        int acctit, ns;
                   1018:
                   1019:        ns = UFS_FSNEEDSWAP(fs);
                   1020:        ip = VTOI(vp);
                   1021:        inum = ip->i_number;
                   1022:        if (lblkno == -1)
                   1023:                acctit = 0;
                   1024:        else
                   1025:                acctit = 1;
                   1026:        for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
                   1027:                blkno = ufs_rw32(*oldblkp, ns);
                   1028:                if (blkno == 0 || blkno == BLK_NOCOPY)
                   1029:                        continue;
                   1030:                if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
1.15      hannken  1031:                        *ip->i_snapblklist++ = lblkno;
1.1       hannken  1032:                if (blkno == BLK_SNAP)
                   1033:                        blkno = blkstofrags(fs, lblkno);
                   1034:                ffs_blkfree(fs, vp, blkno, fs->fs_bsize, inum);
                   1035:        }
                   1036:        return (0);
                   1037: }
                   1038:
                   1039: /*
                   1040:  * Before expunging a snapshot inode, note all the
                   1041:  * blocks that it claims with BLK_SNAP so that fsck will
                   1042:  * be able to account for those blocks properly and so
                   1043:  * that this snapshot knows that it need not copy them
                   1044:  * if the other snapshot holding them is freed. This code
                   1045:  * is reproduced once each for UFS1 and UFS2.
                   1046:  */
                   1047: static int
1.18      thorpej  1048: expunge_ufs2(struct vnode *snapvp, struct inode *cancelip, struct fs *fs,
                   1049:     int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
                   1050:                    struct fs *, ufs_lbn_t, int),
                   1051:     int expungetype)
1.1       hannken  1052: {
1.4       hannken  1053:        int i, s, error, ns, indiroff;
1.1       hannken  1054:        ufs_lbn_t lbn, rlbn;
                   1055:        ufs2_daddr_t len, blkno, numblks, blksperindir;
                   1056:        struct ufs2_dinode *dip;
                   1057:        struct buf *bp;
1.17      christos 1058:        caddr_t bf;
1.1       hannken  1059:
                   1060:        ns = UFS_FSNEEDSWAP(fs);
                   1061:        /*
                   1062:         * Prepare to expunge the inode. If its inode block has not
                   1063:         * yet been copied, then allocate and fill the copy.
                   1064:         */
                   1065:        lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
                   1066:        blkno = 0;
                   1067:        if (lbn < NDADDR) {
                   1068:                blkno = db_get(VTOI(snapvp), lbn);
                   1069:        } else {
1.4       hannken  1070:                s = cow_enter();
1.22      yamt     1071:                error = ffs_balloc(snapvp, lblktosize(fs, (off_t)lbn),
1.1       hannken  1072:                   fs->fs_bsize, KERNCRED, B_METAONLY, &bp);
1.4       hannken  1073:                cow_leave(s);
1.1       hannken  1074:                if (error)
                   1075:                        return (error);
                   1076:                indiroff = (lbn - NDADDR) % NINDIR(fs);
                   1077:                blkno = idb_get(VTOI(snapvp), bp->b_data, indiroff);
                   1078:                brelse(bp);
                   1079:        }
1.17      christos 1080:        bf = malloc(fs->fs_bsize, M_UFSMNT, M_WAITOK);
1.1       hannken  1081:        if (blkno != 0)
1.17      christos 1082:                error = readvnblk(snapvp, bf, lbn);
1.1       hannken  1083:        else
1.17      christos 1084:                error = readfsblk(snapvp, bf, lbn);
1.1       hannken  1085:        if (error) {
1.17      christos 1086:                free(bf, M_UFSMNT);
1.1       hannken  1087:                return error;
                   1088:        }
                   1089:        /*
                   1090:         * Set a snapshot inode to be a zero length file, regular files
                   1091:         * to be completely unallocated.
                   1092:         */
1.17      christos 1093:        dip = (struct ufs2_dinode *)bf + ino_to_fsbo(fs, cancelip->i_number);
1.1       hannken  1094:        if (expungetype == BLK_NOCOPY)
                   1095:                dip->di_mode = 0;
                   1096:        dip->di_size = 0;
                   1097:        dip->di_blocks = 0;
                   1098:        dip->di_flags =
                   1099:            ufs_rw32(ufs_rw32(dip->di_flags, ns) & ~SF_SNAPSHOT, ns);
                   1100:        bzero(&dip->di_db[0], (NDADDR + NIADDR) * sizeof(ufs2_daddr_t));
1.17      christos 1101:        error = writevnblk(snapvp, bf, lbn);
                   1102:        free(bf, M_UFSMNT);
1.1       hannken  1103:        if (error)
                   1104:                return error;
                   1105:        /*
                   1106:         * Now go through and expunge all the blocks in the file
                   1107:         * using the function requested.
                   1108:         */
                   1109:        numblks = howmany(cancelip->i_size, fs->fs_bsize);
                   1110:        if ((error = (*acctfunc)(snapvp, &cancelip->i_ffs2_db[0],
                   1111:            &cancelip->i_ffs2_db[NDADDR], fs, 0, expungetype)))
                   1112:                return (error);
                   1113:        if ((error = (*acctfunc)(snapvp, &cancelip->i_ffs2_ib[0],
                   1114:            &cancelip->i_ffs2_ib[NIADDR], fs, -1, expungetype)))
                   1115:                return (error);
                   1116:        blksperindir = 1;
                   1117:        lbn = -NDADDR;
                   1118:        len = numblks - NDADDR;
                   1119:        rlbn = NDADDR;
                   1120:        for (i = 0; len > 0 && i < NIADDR; i++) {
                   1121:                error = indiracct_ufs2(snapvp, ITOV(cancelip), i,
                   1122:                    ufs_rw64(cancelip->i_ffs2_ib[i], ns), lbn, rlbn, len,
                   1123:                    blksperindir, fs, acctfunc, expungetype);
                   1124:                if (error)
                   1125:                        return (error);
                   1126:                blksperindir *= NINDIR(fs);
                   1127:                lbn -= blksperindir + 1;
                   1128:                len -= blksperindir;
                   1129:                rlbn += blksperindir;
                   1130:        }
                   1131:        return (0);
                   1132: }
                   1133:
                   1134: /*
                   1135:  * Descend an indirect block chain for vnode cancelvp accounting for all
                   1136:  * its indirect blocks in snapvp.
1.11      perry    1137:  */
1.1       hannken  1138: static int
1.18      thorpej  1139: indiracct_ufs2(struct vnode *snapvp, struct vnode *cancelvp, int level,
                   1140:     ufs2_daddr_t blkno, ufs_lbn_t lbn, ufs_lbn_t rlbn, ufs_lbn_t remblks,
                   1141:     ufs_lbn_t blksperindir, struct fs *fs,
                   1142:     int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
                   1143:                    struct fs *, ufs_lbn_t, int),
                   1144:     int expungetype)
1.1       hannken  1145: {
                   1146:        int error, ns, num, i;
                   1147:        ufs_lbn_t subblksperindir;
                   1148:        struct indir indirs[NIADDR + 2];
                   1149:        ufs2_daddr_t last, *bap;
                   1150:        struct buf *bp;
                   1151:
                   1152:        ns = UFS_FSNEEDSWAP(fs);
                   1153:
                   1154:        if (blkno == 0) {
                   1155:                if (expungetype == BLK_NOCOPY)
                   1156:                        return (0);
                   1157:                panic("indiracct_ufs2: missing indir");
                   1158:        }
                   1159:        if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
                   1160:                return (error);
                   1161:        if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
                   1162:                panic("indiracct_ufs2: botched params");
                   1163:        /*
                   1164:         * We have to expand bread here since it will deadlock looking
                   1165:         * up the block number for any blocks that are not in the cache.
                   1166:         */
                   1167:        bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0);
                   1168:        bp->b_blkno = fsbtodb(fs, blkno);
                   1169:        if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0 &&
                   1170:            (error = readfsblk(bp->b_vp, bp->b_data, fragstoblks(fs, blkno)))) {
                   1171:                brelse(bp);
                   1172:                return (error);
                   1173:        }
                   1174:        /*
                   1175:         * Account for the block pointers in this indirect block.
                   1176:         */
                   1177:        last = howmany(remblks, blksperindir);
                   1178:        if (last > NINDIR(fs))
                   1179:                last = NINDIR(fs);
1.24.8.3! elad     1180:        bap = malloc(fs->fs_bsize, M_DEVBUF, M_WAITOK);
1.1       hannken  1181:        bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
                   1182:        brelse(bp);
                   1183:        error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
                   1184:            level == 0 ? rlbn : -1, expungetype);
                   1185:        if (error || level == 0)
                   1186:                goto out;
                   1187:        /*
                   1188:         * Account for the block pointers in each of the indirect blocks
                   1189:         * in the levels below us.
                   1190:         */
                   1191:        subblksperindir = blksperindir / NINDIR(fs);
                   1192:        for (lbn++, level--, i = 0; i < last; i++) {
                   1193:                error = indiracct_ufs2(snapvp, cancelvp, level,
                   1194:                    ufs_rw64(bap[i], ns), lbn, rlbn, remblks, subblksperindir,
                   1195:                    fs, acctfunc, expungetype);
                   1196:                if (error)
                   1197:                        goto out;
                   1198:                rlbn += blksperindir;
                   1199:                lbn -= blksperindir;
                   1200:                remblks -= blksperindir;
                   1201:        }
                   1202: out:
                   1203:        FREE(bap, M_DEVBUF);
                   1204:        return (error);
                   1205: }
                   1206:
                   1207: /*
                   1208:  * Do both snap accounting and map accounting.
                   1209:  */
                   1210: static int
1.18      thorpej  1211: fullacct_ufs2(struct vnode *vp, ufs2_daddr_t *oldblkp, ufs2_daddr_t *lastblkp,
                   1212:     struct fs *fs, ufs_lbn_t lblkno,
                   1213:     int exptype /* BLK_SNAP or BLK_NOCOPY */)
1.1       hannken  1214: {
                   1215:        int error;
                   1216:
                   1217:        if ((error = snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
                   1218:                return (error);
                   1219:        return (mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype));
                   1220: }
                   1221:
                   1222: /*
                   1223:  * Identify a set of blocks allocated in a snapshot inode.
                   1224:  */
                   1225: static int
1.18      thorpej  1226: snapacct_ufs2(struct vnode *vp, ufs2_daddr_t *oldblkp, ufs2_daddr_t *lastblkp,
                   1227:     struct fs *fs, ufs_lbn_t lblkno,
                   1228:     int expungetype /* BLK_SNAP or BLK_NOCOPY */)
1.1       hannken  1229: {
                   1230:        struct inode *ip = VTOI(vp);
                   1231:        ufs2_daddr_t blkno, *blkp;
                   1232:        ufs_lbn_t lbn;
                   1233:        struct buf *ibp;
                   1234:        int error, ns;
                   1235:
                   1236:        ns = UFS_FSNEEDSWAP(fs);
                   1237:
                   1238:        for ( ; oldblkp < lastblkp; oldblkp++) {
                   1239:                blkno = ufs_rw64(*oldblkp, ns);
                   1240:                if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
                   1241:                        continue;
                   1242:                lbn = fragstoblks(fs, blkno);
                   1243:                if (lbn < NDADDR) {
                   1244:                        blkp = &ip->i_ffs2_db[lbn];
                   1245:                        ip->i_flag |= IN_CHANGE | IN_UPDATE;
                   1246:                } else {
1.22      yamt     1247:                        error = ffs_balloc(vp, lblktosize(fs, (off_t)lbn),
1.1       hannken  1248:                            fs->fs_bsize, KERNCRED, B_METAONLY, &ibp);
                   1249:                        if (error)
                   1250:                                return (error);
                   1251:                        blkp = &((ufs2_daddr_t *)(ibp->b_data))
                   1252:                            [(lbn - NDADDR) % NINDIR(fs)];
                   1253:                }
                   1254:                /*
                   1255:                 * If we are expunging a snapshot vnode and we
                   1256:                 * find a block marked BLK_NOCOPY, then it is
                   1257:                 * one that has been allocated to this snapshot after
                   1258:                 * we took our current snapshot and can be ignored.
                   1259:                 */
                   1260:                blkno = ufs_rw64(*blkp, ns);
                   1261:                if (expungetype == BLK_SNAP && blkno == BLK_NOCOPY) {
                   1262:                        if (lbn >= NDADDR)
                   1263:                                brelse(ibp);
                   1264:                } else {
                   1265:                        if (blkno != 0)
                   1266:                                panic("snapacct_ufs2: bad block");
                   1267:                        *blkp = ufs_rw64(expungetype, ns);
                   1268:                        if (lbn >= NDADDR)
1.15      hannken  1269:                                bdwrite(ibp);
1.1       hannken  1270:                }
                   1271:        }
                   1272:        return (0);
                   1273: }
                   1274:
                   1275: /*
                   1276:  * Account for a set of blocks allocated in a snapshot inode.
                   1277:  */
                   1278: static int
1.18      thorpej  1279: mapacct_ufs2(struct vnode *vp, ufs2_daddr_t *oldblkp, ufs2_daddr_t *lastblkp,
                   1280:     struct fs *fs, ufs_lbn_t lblkno, int expungetype)
1.1       hannken  1281: {
                   1282:        ufs2_daddr_t blkno;
                   1283:        struct inode *ip;
                   1284:        ino_t inum;
                   1285:        int acctit, ns;
                   1286:
                   1287:        ns = UFS_FSNEEDSWAP(fs);
                   1288:        ip = VTOI(vp);
                   1289:        inum = ip->i_number;
                   1290:        if (lblkno == -1)
                   1291:                acctit = 0;
                   1292:        else
                   1293:                acctit = 1;
                   1294:        for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
                   1295:                blkno = ufs_rw64(*oldblkp, ns);
                   1296:                if (blkno == 0 || blkno == BLK_NOCOPY)
                   1297:                        continue;
                   1298:                if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
1.15      hannken  1299:                        *ip->i_snapblklist++ = lblkno;
1.1       hannken  1300:                if (blkno == BLK_SNAP)
                   1301:                        blkno = blkstofrags(fs, lblkno);
                   1302:                ffs_blkfree(fs, vp, blkno, fs->fs_bsize, inum);
                   1303:        }
                   1304:        return (0);
                   1305: }
1.10      hannken  1306: #endif /* defined(FFS_NO_SNAPSHOT) */
1.1       hannken  1307:
                   1308: /*
                   1309:  * Decrement extra reference on snapshot when last name is removed.
                   1310:  * It will not be freed until the last open reference goes away.
                   1311:  */
                   1312: void
1.18      thorpej  1313: ffs_snapgone(struct inode *ip)
1.1       hannken  1314: {
                   1315:        struct ufsmount *ump = VFSTOUFS(ip->i_devvp->v_specmountpoint);
                   1316:        struct inode *xp;
                   1317:        struct fs *fs;
                   1318:        int snaploc;
                   1319:
                   1320:        /*
                   1321:         * Find snapshot in incore list.
                   1322:         */
                   1323:        TAILQ_FOREACH(xp, &ump->um_snapshots, i_nextsnap)
                   1324:                if (xp == ip)
                   1325:                        break;
                   1326:        if (xp != NULL)
                   1327:                vrele(ITOV(ip));
                   1328: #ifdef DEBUG
                   1329:        else if (snapdebug)
1.19      christos 1330:                printf("ffs_snapgone: lost snapshot vnode %llu\n",
                   1331:                    (unsigned long long)ip->i_number);
1.1       hannken  1332: #endif
                   1333:        /*
                   1334:         * Delete snapshot inode from superblock. Keep list dense.
                   1335:         */
                   1336:        fs = ip->i_fs;
                   1337:        for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
                   1338:                if (fs->fs_snapinum[snaploc] == ip->i_number)
                   1339:                        break;
                   1340:        if (snaploc < FSMAXSNAP) {
                   1341:                for (snaploc++; snaploc < FSMAXSNAP; snaploc++) {
                   1342:                        if (fs->fs_snapinum[snaploc] == 0)
                   1343:                                break;
                   1344:                        fs->fs_snapinum[snaploc - 1] = fs->fs_snapinum[snaploc];
                   1345:                }
                   1346:                fs->fs_snapinum[snaploc - 1] = 0;
                   1347:        }
                   1348: }
                   1349:
                   1350: /*
                   1351:  * Prepare a snapshot file for being removed.
                   1352:  */
                   1353: void
1.18      thorpej  1354: ffs_snapremove(struct vnode *vp)
1.1       hannken  1355: {
1.15      hannken  1356:        struct inode *ip = VTOI(vp), *xp;
1.1       hannken  1357:        struct vnode *devvp = ip->i_devvp;
                   1358:        struct fs *fs = ip->i_fs;
                   1359:        struct ufsmount *ump = VFSTOUFS(devvp->v_specmountpoint);
                   1360:        struct lock *lkp;
                   1361:        struct buf *ibp;
1.15      hannken  1362:        ufs2_daddr_t numblks, blkno, dblk;
1.1       hannken  1363:        int error, ns, loc, last;
                   1364:
                   1365:        ns = UFS_FSNEEDSWAP(fs);
                   1366:        /*
                   1367:         * If active, delete from incore list (this snapshot may
                   1368:         * already have been in the process of being deleted, so
                   1369:         * would not have been active).
                   1370:         *
                   1371:         * Clear copy-on-write flag if last snapshot.
                   1372:         */
                   1373:        if (ip->i_nextsnap.tqe_prev != 0) {
                   1374:                VI_LOCK(devvp);
                   1375:                lockmgr(&vp->v_lock, LK_INTERLOCK | LK_EXCLUSIVE,
                   1376:                    VI_MTX(devvp));
                   1377:                VI_LOCK(devvp);
                   1378:                TAILQ_REMOVE(&ump->um_snapshots, ip, i_nextsnap);
                   1379:                ip->i_nextsnap.tqe_prev = 0;
                   1380:                lkp = vp->v_vnlock;
                   1381:                vp->v_vnlock = &vp->v_lock;
                   1382:                lockmgr(lkp, LK_RELEASE, NULL);
                   1383:                if (TAILQ_FIRST(&ump->um_snapshots) != 0) {
1.15      hannken  1384:                        /* Roll back the list of preallocated blocks. */
                   1385:                        xp = TAILQ_LAST(&ump->um_snapshots, inodelst);
                   1386:                        ump->um_snapblklist = xp->i_snapblklist;
1.1       hannken  1387:                        VI_UNLOCK(devvp);
                   1388:                } else {
                   1389:                        ump->um_snapblklist = 0;
                   1390:                        lockmgr(lkp, LK_DRAIN|LK_INTERLOCK, VI_MTX(devvp));
                   1391:                        lockmgr(lkp, LK_RELEASE, NULL);
                   1392:                        vn_cow_disestablish(devvp, ffs_copyonwrite, devvp);
                   1393:                        FREE(lkp, M_UFSMNT);
                   1394:                }
1.15      hannken  1395:                FREE(ip->i_snapblklist, M_UFSMNT);
                   1396:                ip->i_snapblklist = NULL;
1.1       hannken  1397:        }
                   1398:        /*
                   1399:         * Clear all BLK_NOCOPY fields. Pass any block claims to other
                   1400:         * snapshots that want them (see ffs_snapblkfree below).
                   1401:         */
                   1402:        for (blkno = 1; blkno < NDADDR; blkno++) {
                   1403:                dblk = db_get(ip, blkno);
                   1404:                if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
                   1405:                        db_assign(ip, blkno, 0);
                   1406:                else if ((dblk == blkstofrags(fs, blkno) &&
                   1407:                     ffs_snapblkfree(fs, ip->i_devvp, dblk, fs->fs_bsize,
                   1408:                     ip->i_number))) {
                   1409:                        DIP_ADD(ip, blocks, -btodb(fs->fs_bsize));
                   1410:                        db_assign(ip, blkno, 0);
                   1411:                }
                   1412:        }
                   1413:        numblks = howmany(ip->i_size, fs->fs_bsize);
                   1414:        for (blkno = NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
1.22      yamt     1415:                error = ffs_balloc(vp, lblktosize(fs, (off_t)blkno),
1.1       hannken  1416:                    fs->fs_bsize, KERNCRED, B_METAONLY, &ibp);
                   1417:                if (error)
                   1418:                        continue;
                   1419:                if (fs->fs_size - blkno > NINDIR(fs))
                   1420:                        last = NINDIR(fs);
                   1421:                else
                   1422:                        last = fs->fs_size - blkno;
                   1423:                for (loc = 0; loc < last; loc++) {
                   1424:                        dblk = idb_get(ip, ibp->b_data, loc);
                   1425:                        if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
                   1426:                                idb_assign(ip, ibp->b_data, loc, 0);
                   1427:                        else if (dblk == blkstofrags(fs, blkno) &&
                   1428:                            ffs_snapblkfree(fs, ip->i_devvp, dblk,
                   1429:                            fs->fs_bsize, ip->i_number)) {
                   1430:                                DIP_ADD(ip, blocks, -btodb(fs->fs_bsize));
                   1431:                                idb_assign(ip, ibp->b_data, loc, 0);
                   1432:                        }
                   1433:                }
1.15      hannken  1434:                bawrite(ibp);
1.1       hannken  1435:        }
                   1436:        /*
                   1437:         * Clear snapshot flag and drop reference.
                   1438:         */
                   1439:        ip->i_flags &= ~SF_SNAPSHOT;
                   1440:        DIP_ASSIGN(ip, flags, ip->i_flags);
                   1441:        ip->i_flag |= IN_CHANGE | IN_UPDATE;
                   1442: }
                   1443:
                   1444: /*
                   1445:  * Notification that a block is being freed. Return zero if the free
                   1446:  * should be allowed to proceed. Return non-zero if the snapshot file
                   1447:  * wants to claim the block. The block will be claimed if it is an
                   1448:  * uncopied part of one of the snapshots. It will be freed if it is
                   1449:  * either a BLK_NOCOPY or has already been copied in all of the snapshots.
                   1450:  * If a fragment is being freed, then all snapshots that care about
                   1451:  * it must make a copy since a snapshot file can only claim full sized
                   1452:  * blocks. Note that if more than one snapshot file maps the block,
                   1453:  * we can pick one at random to claim it. Since none of the snapshots
                   1454:  * can change, we are assurred that they will all see the same unmodified
                   1455:  * image. When deleting a snapshot file (see ffs_snapremove above), we
                   1456:  * must push any of these claimed blocks to one of the other snapshots
                   1457:  * that maps it. These claimed blocks are easily identified as they will
                   1458:  * have a block number equal to their logical block number within the
                   1459:  * snapshot. A copied block can never have this property because they
                   1460:  * must always have been allocated from a BLK_NOCOPY location.
                   1461:  */
                   1462: int
1.18      thorpej  1463: ffs_snapblkfree(struct fs *fs, struct vnode *devvp, ufs2_daddr_t bno,
                   1464:     long size, ino_t inum)
1.1       hannken  1465: {
                   1466:        struct ufsmount *ump = VFSTOUFS(devvp->v_specmountpoint);
                   1467:        struct buf *ibp;
                   1468:        struct inode *ip;
                   1469:        struct vnode *vp = NULL, *saved_vp = NULL;
                   1470:        caddr_t saved_data = NULL;
                   1471:        ufs_lbn_t lbn;
                   1472:        ufs2_daddr_t blkno;
1.4       hannken  1473:        int s, indiroff = 0, snapshot_locked = 0, error = 0, claimedblk = 0;
1.1       hannken  1474:
                   1475:        lbn = fragstoblks(fs, bno);
                   1476: retry:
                   1477:        VI_LOCK(devvp);
                   1478:        TAILQ_FOREACH(ip, &ump->um_snapshots, i_nextsnap) {
                   1479:                vp = ITOV(ip);
                   1480:                /*
                   1481:                 * Lookup block being written.
                   1482:                 */
                   1483:                if (lbn < NDADDR) {
                   1484:                        blkno = db_get(ip, lbn);
                   1485:                } else {
                   1486:                        if (snapshot_locked == 0 &&
                   1487:                            lockmgr(vp->v_vnlock,
                   1488:                              LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
                   1489:                              VI_MTX(devvp)) != 0)
                   1490:                                goto retry;
                   1491:                        snapshot_locked = 1;
1.4       hannken  1492:                        s = cow_enter();
1.22      yamt     1493:                        error = ffs_balloc(vp, lblktosize(fs, (off_t)lbn),
1.1       hannken  1494:                            fs->fs_bsize, KERNCRED, B_METAONLY, &ibp);
1.4       hannken  1495:                        cow_leave(s);
1.1       hannken  1496:                        if (error)
                   1497:                                break;
                   1498:                        indiroff = (lbn - NDADDR) % NINDIR(fs);
                   1499:                        blkno = idb_get(ip, ibp->b_data, indiroff);
                   1500:                }
                   1501:                /*
                   1502:                 * Check to see if block needs to be copied.
                   1503:                 */
                   1504:                if (blkno == 0) {
                   1505:                        /*
                   1506:                         * A block that we map is being freed. If it has not
                   1507:                         * been claimed yet, we will claim or copy it (below).
                   1508:                         */
                   1509:                        claimedblk = 1;
                   1510:                } else if (blkno == BLK_SNAP) {
                   1511:                        /*
                   1512:                         * No previous snapshot claimed the block,
                   1513:                         * so it will be freed and become a BLK_NOCOPY
                   1514:                         * (don't care) for us.
                   1515:                         */
                   1516:                        if (claimedblk)
                   1517:                                panic("snapblkfree: inconsistent block type");
                   1518:                        if (snapshot_locked == 0 &&
                   1519:                            lockmgr(vp->v_vnlock,
                   1520:                              LK_INTERLOCK | LK_EXCLUSIVE | LK_NOWAIT,
                   1521:                              VI_MTX(devvp)) != 0) {
                   1522:                                if (lbn >= NDADDR)
                   1523:                                        brelse(ibp);
                   1524:                                vn_lock(vp, LK_EXCLUSIVE | LK_SLEEPFAIL);
                   1525:                                goto retry;
                   1526:                        }
                   1527:                        snapshot_locked = 1;
                   1528:                        if (lbn < NDADDR) {
                   1529:                                db_assign(ip, lbn, BLK_NOCOPY);
                   1530:                                ip->i_flag |= IN_CHANGE | IN_UPDATE;
                   1531:                        } else {
                   1532:                                idb_assign(ip, ibp->b_data, indiroff,
                   1533:                                    BLK_NOCOPY);
                   1534:                                bwrite(ibp);
                   1535:                        }
                   1536:                        continue;
                   1537:                } else /* BLK_NOCOPY or default */ {
                   1538:                        /*
                   1539:                         * If the snapshot has already copied the block
                   1540:                         * (default), or does not care about the block,
                   1541:                         * it is not needed.
                   1542:                         */
                   1543:                        if (lbn >= NDADDR)
                   1544:                                brelse(ibp);
                   1545:                        continue;
                   1546:                }
                   1547:                /*
                   1548:                 * If this is a full size block, we will just grab it
                   1549:                 * and assign it to the snapshot inode. Otherwise we
                   1550:                 * will proceed to copy it. See explanation for this
                   1551:                 * routine as to why only a single snapshot needs to
                   1552:                 * claim this block.
                   1553:                 */
                   1554:                if (snapshot_locked == 0 &&
                   1555:                    lockmgr(vp->v_vnlock,
                   1556:                      LK_INTERLOCK | LK_EXCLUSIVE | LK_NOWAIT,
                   1557:                      VI_MTX(devvp)) != 0) {
                   1558:                        if (lbn >= NDADDR)
                   1559:                                brelse(ibp);
                   1560:                        vn_lock(vp, LK_EXCLUSIVE | LK_SLEEPFAIL);
                   1561:                        goto retry;
                   1562:                }
                   1563:                snapshot_locked = 1;
                   1564:                if (size == fs->fs_bsize) {
                   1565: #ifdef DEBUG
                   1566:                        if (snapdebug)
1.19      christos 1567:                                printf("%s %llu lbn %" PRId64
                   1568:                                    "from inum %llu\n",
                   1569:                                    "Grabonremove: snapino",
                   1570:                                    (unsigned long long)ip->i_number,
                   1571:                                    lbn, (unsigned long long)inum);
1.1       hannken  1572: #endif
                   1573:                        if (lbn < NDADDR) {
                   1574:                                db_assign(ip, lbn, bno);
                   1575:                        } else {
                   1576:                                idb_assign(ip, ibp->b_data, indiroff, bno);
                   1577:                                bwrite(ibp);
                   1578:                        }
                   1579:                        DIP_ADD(ip, blocks, btodb(size));
                   1580:                        ip->i_flag |= IN_CHANGE | IN_UPDATE;
                   1581:                        VOP_UNLOCK(vp, 0);
                   1582:                        return (1);
                   1583:                }
                   1584:                if (lbn >= NDADDR)
                   1585:                        brelse(ibp);
                   1586: #ifdef DEBUG
                   1587:                if (snapdebug)
1.19      christos 1588:                        printf("%s%llu lbn %" PRId64 " %s %llu size %ld\n",
                   1589:                            "Copyonremove: snapino ",
                   1590:                            (unsigned long long)ip->i_number,
                   1591:                            lbn, "for inum", (unsigned long long)inum, size);
1.1       hannken  1592: #endif
                   1593:                /*
                   1594:                 * If we have already read the old block contents, then
                   1595:                 * simply copy them to the new block. Note that we need
                   1596:                 * to synchronously write snapshots that have not been
                   1597:                 * unlinked, and hence will be visible after a crash,
                   1598:                 * to ensure their integrity.
                   1599:                 */
                   1600:                if (saved_data) {
                   1601:                        error = writevnblk(vp, saved_data, lbn);
                   1602:                        if (error)
                   1603:                                break;
                   1604:                        continue;
                   1605:                }
                   1606:                /*
                   1607:                 * Otherwise, read the old block contents into the buffer.
                   1608:                 */
                   1609:                saved_data = malloc(fs->fs_bsize, M_UFSMNT, M_WAITOK);
                   1610:                saved_vp = vp;
                   1611:                if ((error = readfsblk(vp, saved_data, lbn)) != 0) {
                   1612:                        free(saved_data, M_UFSMNT);
                   1613:                        saved_data = NULL;
                   1614:                        break;
                   1615:                }
                   1616:        }
                   1617:        /*
                   1618:         * Note that we need to synchronously write snapshots that
                   1619:         * have not been unlinked, and hence will be visible after
                   1620:         * a crash, to ensure their integrity.
                   1621:         */
                   1622:        if (saved_data) {
                   1623:                error = writevnblk(saved_vp, saved_data, lbn);
                   1624:                free(saved_data, M_UFSMNT);
                   1625:        }
                   1626:        /*
                   1627:         * If we have been unable to allocate a block in which to do
                   1628:         * the copy, then return non-zero so that the fragment will
                   1629:         * not be freed. Although space will be lost, the snapshot
                   1630:         * will stay consistent.
                   1631:         */
                   1632:        if (snapshot_locked)
                   1633:                VOP_UNLOCK(vp, 0);
                   1634:        else
                   1635:                VI_UNLOCK(devvp);
                   1636:        return (error);
                   1637: }
                   1638:
                   1639: /*
                   1640:  * Associate snapshot files when mounting.
                   1641:  */
                   1642: void
1.18      thorpej  1643: ffs_snapshot_mount(struct mount *mp)
1.1       hannken  1644: {
                   1645:        struct ufsmount *ump = VFSTOUFS(mp);
                   1646:        struct vnode *devvp = ump->um_devvp;
                   1647:        struct fs *fs = ump->um_fs;
                   1648:        struct proc *p = curproc;
                   1649:        struct vnode *vp;
                   1650:        struct inode *ip, *xp;
                   1651:        ufs2_daddr_t snaplistsize, *snapblklist;
1.15      hannken  1652:        int i, error, ns, snaploc, loc;
1.1       hannken  1653:
                   1654:        ns = UFS_FSNEEDSWAP(fs);
                   1655:        /*
1.22      yamt     1656:         * XXX The following needs to be set before ffs_truncate or
1.1       hannken  1657:         * VOP_READ can be called.
                   1658:         */
                   1659:        mp->mnt_stat.f_iosize = fs->fs_bsize;
                   1660:        /*
                   1661:         * Process each snapshot listed in the superblock.
                   1662:         */
                   1663:        vp = NULL;
                   1664:        for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++) {
                   1665:                if (fs->fs_snapinum[snaploc] == 0)
                   1666:                        break;
                   1667:                if ((error = VFS_VGET(mp, fs->fs_snapinum[snaploc],
                   1668:                    &vp)) != 0) {
                   1669:                        printf("ffs_snapshot_mount: vget failed %d\n", error);
                   1670:                        continue;
                   1671:                }
                   1672:                ip = VTOI(vp);
                   1673:                if ((ip->i_flags & SF_SNAPSHOT) == 0) {
                   1674:                        printf("ffs_snapshot_mount: non-snapshot inode %d\n",
                   1675:                            fs->fs_snapinum[snaploc]);
                   1676:                        vput(vp);
                   1677:                        vp = NULL;
                   1678:                        for (loc = snaploc + 1; loc < FSMAXSNAP; loc++) {
                   1679:                                if (fs->fs_snapinum[loc] == 0)
                   1680:                                        break;
                   1681:                                fs->fs_snapinum[loc - 1] = fs->fs_snapinum[loc];
                   1682:                        }
                   1683:                        fs->fs_snapinum[loc - 1] = 0;
                   1684:                        snaploc--;
                   1685:                        continue;
                   1686:                }
1.15      hannken  1687:
                   1688:                /*
                   1689:                 * Read the block hints list. Use an empty list on
                   1690:                 * read errors.
                   1691:                 */
                   1692:                error = vn_rdwr(UIO_READ, vp,
                   1693:                    (caddr_t)&snaplistsize, sizeof(snaplistsize),
                   1694:                    lblktosize(fs, howmany(fs->fs_size, fs->fs_frag)),
                   1695:                    UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT,
1.24.8.1  elad     1696:                    p->p_cred, NULL, NULL);
1.15      hannken  1697:                if (error) {
                   1698:                        printf("ffs_snapshot_mount: read_1 failed %d\n", error);
                   1699:                        snaplistsize = 1;
                   1700:                } else
                   1701:                        snaplistsize = ufs_rw64(snaplistsize, ns);
1.24.8.3! elad     1702:                snapblklist = malloc(
1.15      hannken  1703:                    snaplistsize * sizeof(ufs2_daddr_t), M_UFSMNT, M_WAITOK);
                   1704:                if (error)
                   1705:                        snapblklist[0] = 1;
                   1706:                else {
                   1707:                        error = vn_rdwr(UIO_READ, vp, (caddr_t)snapblklist,
                   1708:                            snaplistsize * sizeof(ufs2_daddr_t),
                   1709:                            lblktosize(fs, howmany(fs->fs_size, fs->fs_frag)),
                   1710:                            UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT,
1.24.8.1  elad     1711:                            p->p_cred, NULL, NULL);
1.15      hannken  1712:                        for (i = 0; i < snaplistsize; i++)
                   1713:                                snapblklist[i] = ufs_rw64(snapblklist[i], ns);
                   1714:                        if (error) {
                   1715:                                printf("ffs_snapshot_mount: read_2 failed %d\n",
                   1716:                                    error);
                   1717:                                snapblklist[0] = 1;
                   1718:                        }
                   1719:                }
                   1720:                ip->i_snapblklist = &snapblklist[0];
                   1721:
1.1       hannken  1722:                /*
                   1723:                 * If there already exist snapshots on this filesystem, grab a
                   1724:                 * reference to their shared lock. If this is the first snapshot
                   1725:                 * on this filesystem, we need to allocate a lock for the
                   1726:                 * snapshots to share. In either case, acquire the snapshot
                   1727:                 * lock and give up our original private lock.
                   1728:                 */
                   1729:                VI_LOCK(devvp);
                   1730:                if ((xp = TAILQ_FIRST(&ump->um_snapshots)) != NULL) {
                   1731:                        struct lock *lkp;
                   1732:
                   1733:                        lkp = ITOV(xp)->v_vnlock;
                   1734:                        VI_UNLOCK(devvp);
                   1735:                        VI_LOCK(vp);
                   1736:                        vp->v_vnlock = lkp;
                   1737:                } else {
                   1738:                        struct lock *lkp;
                   1739:
                   1740:                        VI_UNLOCK(devvp);
                   1741:                        MALLOC(lkp, struct lock *, sizeof(struct lock),
                   1742:                            M_UFSMNT, M_WAITOK);
                   1743:                        lockinit(lkp, PVFS, "snaplk", 0, LK_CANRECURSE);
                   1744:                        VI_LOCK(vp);
                   1745:                        vp->v_vnlock = lkp;
                   1746:                }
                   1747:                vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY);
                   1748:                transferlockers(&vp->v_lock, vp->v_vnlock);
                   1749:                lockmgr(&vp->v_lock, LK_RELEASE, NULL);
                   1750:                /*
                   1751:                 * Link it onto the active snapshot list.
                   1752:                 */
                   1753:                VI_LOCK(devvp);
                   1754:                if (ip->i_nextsnap.tqe_prev != 0)
1.19      christos 1755:                        panic("ffs_snapshot_mount: %llu already on list",
                   1756:                            (unsigned long long)ip->i_number);
1.1       hannken  1757:                else
                   1758:                        TAILQ_INSERT_TAIL(&ump->um_snapshots, ip, i_nextsnap);
                   1759:                vp->v_flag |= VSYSTEM;
                   1760:                VI_UNLOCK(devvp);
                   1761:                VOP_UNLOCK(vp, 0);
                   1762:        }
                   1763:        /*
                   1764:         * No usable snapshots found.
                   1765:         */
                   1766:        if (vp == NULL)
                   1767:                return;
                   1768:        /*
1.15      hannken  1769:         * Attach the block hints list. We always want to
1.1       hannken  1770:         * use the list from the newest snapshot.
1.15      hannken  1771:        */
                   1772:        xp = TAILQ_LAST(&ump->um_snapshots, inodelst);
1.1       hannken  1773:        VI_LOCK(devvp);
1.15      hannken  1774:        ump->um_snapblklist = xp->i_snapblklist;
1.1       hannken  1775:        VI_UNLOCK(devvp);
                   1776:        vn_cow_establish(devvp, ffs_copyonwrite, devvp);
                   1777: }
                   1778:
                   1779: /*
                   1780:  * Disassociate snapshot files when unmounting.
                   1781:  */
                   1782: void
1.18      thorpej  1783: ffs_snapshot_unmount(struct mount *mp)
1.1       hannken  1784: {
                   1785:        struct ufsmount *ump = VFSTOUFS(mp);
                   1786:        struct vnode *devvp = ump->um_devvp;
                   1787:        struct lock *lkp = NULL;
                   1788:        struct inode *xp;
                   1789:        struct vnode *vp;
                   1790:
                   1791:        VI_LOCK(devvp);
                   1792:        while ((xp = TAILQ_FIRST(&ump->um_snapshots)) != 0) {
                   1793:                vp = ITOV(xp);
                   1794:                lkp = vp->v_vnlock;
                   1795:                vp->v_vnlock = &vp->v_lock;
                   1796:                TAILQ_REMOVE(&ump->um_snapshots, xp, i_nextsnap);
                   1797:                xp->i_nextsnap.tqe_prev = 0;
1.15      hannken  1798:                if (xp->i_snapblklist == ump->um_snapblklist)
                   1799:                        ump->um_snapblklist = NULL;
                   1800:                VI_UNLOCK(devvp);
                   1801:                FREE(xp->i_snapblklist, M_UFSMNT);
                   1802:                if (xp->i_ffs_effnlink > 0)
1.1       hannken  1803:                        vrele(vp);
1.15      hannken  1804:                VI_LOCK(devvp);
1.1       hannken  1805:        }
                   1806:        VI_UNLOCK(devvp);
                   1807:        if (lkp != NULL) {
                   1808:                vn_cow_disestablish(devvp, ffs_copyonwrite, devvp);
                   1809:                FREE(lkp, M_UFSMNT);
                   1810:        }
                   1811: }
                   1812:
                   1813: /*
                   1814:  * Check for need to copy block that is about to be written,
                   1815:  * copying the block if necessary.
                   1816:  */
                   1817: static int
1.18      thorpej  1818: ffs_copyonwrite(void *v, struct buf *bp)
1.1       hannken  1819: {
                   1820:        struct buf *ibp;
                   1821:        struct fs *fs;
                   1822:        struct inode *ip;
                   1823:        struct vnode *devvp = v, *vp = 0, *saved_vp = NULL;
                   1824:        struct ufsmount *ump = VFSTOUFS(devvp->v_specmountpoint);
                   1825:        caddr_t saved_data = NULL;
                   1826:        ufs2_daddr_t lbn, blkno, *snapblklist;
1.4       hannken  1827:        int lower, upper, mid, s, ns, indiroff, snapshot_locked = 0, error = 0;
1.1       hannken  1828:
                   1829:        /*
                   1830:         * Check for valid snapshots.
                   1831:         */
                   1832:        VI_LOCK(devvp);
                   1833:        ip = TAILQ_FIRST(&ump->um_snapshots);
                   1834:        if (ip == NULL) {
                   1835:                VI_UNLOCK(devvp);
1.11      perry    1836:                return 0;
1.1       hannken  1837:        }
                   1838:        /*
                   1839:         * First check to see if it is in the preallocated list.
                   1840:         * By doing this check we avoid several potential deadlocks.
                   1841:         */
                   1842:        fs = ip->i_fs;
                   1843:        ns = UFS_FSNEEDSWAP(fs);
                   1844:        lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
                   1845:        snapblklist = ump->um_snapblklist;
1.15      hannken  1846:        upper = ump->um_snapblklist[0] - 1;
1.1       hannken  1847:        lower = 1;
                   1848:        while (lower <= upper) {
                   1849:                mid = (lower + upper) / 2;
1.15      hannken  1850:                if (snapblklist[mid] == lbn)
1.1       hannken  1851:                        break;
1.15      hannken  1852:                if (snapblklist[mid] < lbn)
1.1       hannken  1853:                        lower = mid + 1;
                   1854:                else
                   1855:                        upper = mid - 1;
                   1856:        }
                   1857:        if (lower <= upper) {
                   1858:                VI_UNLOCK(devvp);
                   1859:                return 0;
                   1860:        }
                   1861:        /*
                   1862:         * Not in the precomputed list, so check the snapshots.
                   1863:         */
                   1864: retry:
                   1865:        TAILQ_FOREACH(ip, &ump->um_snapshots, i_nextsnap) {
                   1866:                vp = ITOV(ip);
                   1867:                /*
                   1868:                 * We ensure that everything of our own that needs to be
                   1869:                 * copied will be done at the time that ffs_snapshot is
                   1870:                 * called. Thus we can skip the check here which can
1.22      yamt     1871:                 * deadlock in doing the lookup in ffs_balloc.
1.1       hannken  1872:                 */
                   1873:                if (bp->b_vp == vp)
                   1874:                        continue;
                   1875:                /*
                   1876:                 * Check to see if block needs to be copied. We do not have
                   1877:                 * to hold the snapshot lock while doing this lookup as it
                   1878:                 * will never require any additional allocations for the
                   1879:                 * snapshot inode.
                   1880:                 */
                   1881:                if (lbn < NDADDR) {
                   1882:                        blkno = db_get(ip, lbn);
                   1883:                } else {
                   1884:                        if (snapshot_locked == 0 &&
                   1885:                            lockmgr(vp->v_vnlock,
                   1886:                              LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
                   1887:                              VI_MTX(devvp)) != 0) {
                   1888:                                VI_LOCK(devvp);
                   1889:                                goto retry;
                   1890:                        }
                   1891:                        snapshot_locked = 1;
1.4       hannken  1892:                        s = cow_enter();
1.22      yamt     1893:                        error = ffs_balloc(vp, lblktosize(fs, (off_t)lbn),
1.1       hannken  1894:                           fs->fs_bsize, KERNCRED, B_METAONLY, &ibp);
1.4       hannken  1895:                        cow_leave(s);
1.1       hannken  1896:                        if (error)
                   1897:                                break;
                   1898:                        indiroff = (lbn - NDADDR) % NINDIR(fs);
                   1899:                        blkno = idb_get(ip, ibp->b_data, indiroff);
                   1900:                        brelse(ibp);
                   1901:                }
                   1902: #ifdef DIAGNOSTIC
                   1903:                if (blkno == BLK_SNAP && bp->b_lblkno >= 0)
                   1904:                        panic("ffs_copyonwrite: bad copy block");
                   1905: #endif
                   1906:                if (blkno != 0)
                   1907:                        continue;
1.4       hannken  1908: #ifdef DIAGNOSTIC
                   1909:                if (curlwp->l_flag & L_COWINPROGRESS)
                   1910:                        printf("ffs_copyonwrite: recursive call\n");
                   1911: #endif
1.1       hannken  1912:                /*
                   1913:                 * Allocate the block into which to do the copy. Since
                   1914:                 * multiple processes may all try to copy the same block,
                   1915:                 * we have to recheck our need to do a copy if we sleep
                   1916:                 * waiting for the lock.
                   1917:                 *
                   1918:                 * Because all snapshots on a filesystem share a single
                   1919:                 * lock, we ensure that we will never be in competition
                   1920:                 * with another process to allocate a block.
                   1921:                 */
                   1922:                if (snapshot_locked == 0 &&
                   1923:                    lockmgr(vp->v_vnlock,
                   1924:                      LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
                   1925:                      VI_MTX(devvp)) != 0) {
                   1926:                        VI_LOCK(devvp);
                   1927:                        goto retry;
                   1928:                }
                   1929:                snapshot_locked = 1;
                   1930: #ifdef DEBUG
                   1931:                if (snapdebug) {
1.19      christos 1932:                        printf("Copyonwrite: snapino %llu lbn %" PRId64 " for ",
                   1933:                            (unsigned long long)ip->i_number, lbn);
1.1       hannken  1934:                        if (bp->b_vp == devvp)
                   1935:                                printf("fs metadata");
                   1936:                        else
1.19      christos 1937:                                printf("inum %llu", (unsigned long long)
                   1938:                                    VTOI(bp->b_vp)->i_number);
1.4       hannken  1939:                        printf(" lblkno %" PRId64 "\n", bp->b_lblkno);
1.1       hannken  1940:                }
                   1941: #endif
                   1942:                /*
                   1943:                 * If we have already read the old block contents, then
                   1944:                 * simply copy them to the new block. Note that we need
                   1945:                 * to synchronously write snapshots that have not been
                   1946:                 * unlinked, and hence will be visible after a crash,
                   1947:                 * to ensure their integrity.
                   1948:                 */
                   1949:                if (saved_data) {
                   1950:                        error = writevnblk(vp, saved_data, lbn);
                   1951:                        if (error)
                   1952:                                break;
                   1953:                        continue;
                   1954:                }
                   1955:                /*
                   1956:                 * Otherwise, read the old block contents into the buffer.
                   1957:                 */
                   1958:                saved_data = malloc(fs->fs_bsize, M_UFSMNT, M_WAITOK);
                   1959:                saved_vp = vp;
                   1960:                if ((error = readfsblk(vp, saved_data, lbn)) != 0) {
                   1961:                        free(saved_data, M_UFSMNT);
                   1962:                        saved_data = NULL;
                   1963:                        break;
                   1964:                }
                   1965:        }
                   1966:        /*
                   1967:         * Note that we need to synchronously write snapshots that
                   1968:         * have not been unlinked, and hence will be visible after
                   1969:         * a crash, to ensure their integrity.
                   1970:         */
                   1971:        if (saved_data) {
                   1972:                error = writevnblk(saved_vp, saved_data, lbn);
                   1973:                free(saved_data, M_UFSMNT);
                   1974:        }
                   1975:        if (snapshot_locked)
                   1976:                VOP_UNLOCK(vp, 0);
                   1977:        else
                   1978:                VI_UNLOCK(devvp);
                   1979:        return error;
                   1980: }
                   1981:
                   1982: /*
                   1983:  * Read the specified block from disk. Vp is usually a snapshot vnode.
                   1984:  */
                   1985: static int
1.18      thorpej  1986: readfsblk(struct vnode *vp, caddr_t data, ufs2_daddr_t lbn)
1.1       hannken  1987: {
1.24      yamt     1988:        int error;
1.1       hannken  1989:        struct inode *ip = VTOI(vp);
                   1990:        struct fs *fs = ip->i_fs;
                   1991:        struct buf *nbp;
                   1992:
1.24      yamt     1993:        nbp = getiobuf();
1.1       hannken  1994:        nbp->b_flags = B_READ;
                   1995:        nbp->b_bcount = nbp->b_bufsize = fs->fs_bsize;
                   1996:        nbp->b_error = 0;
                   1997:        nbp->b_data = data;
                   1998:        nbp->b_blkno = nbp->b_rawblkno = fsbtodb(fs, blkstofrags(fs, lbn));
                   1999:        nbp->b_proc = NULL;
                   2000:        nbp->b_dev = ip->i_devvp->v_rdev;
                   2001:        nbp->b_vp = NULLVP;
                   2002:
                   2003:        DEV_STRATEGY(nbp);
                   2004:
                   2005:        error = biowait(nbp);
                   2006:
1.24      yamt     2007:        putiobuf(nbp);
1.1       hannken  2008:
                   2009:        return error;
                   2010: }
                   2011:
                   2012: /*
1.4       hannken  2013:  * Read the specified block. Bypass UBC to prevent deadlocks.
1.1       hannken  2014:  */
                   2015: static int
1.18      thorpej  2016: readvnblk(struct vnode *vp, caddr_t data, ufs2_daddr_t lbn)
1.1       hannken  2017: {
1.4       hannken  2018:        int error;
                   2019:        daddr_t bn;
                   2020:        off_t offset;
1.1       hannken  2021:        struct inode *ip = VTOI(vp);
                   2022:        struct fs *fs = ip->i_fs;
                   2023:
1.4       hannken  2024:        error = VOP_BMAP(vp, lbn, NULL, &bn, NULL);
                   2025:        if (error)
                   2026:                return error;
1.1       hannken  2027:
1.4       hannken  2028:        if (bn != (daddr_t)-1) {
                   2029:                offset = dbtob(bn);
                   2030:                simple_lock(&vp->v_interlock);
                   2031:                error = VOP_PUTPAGES(vp, trunc_page(offset),
                   2032:                    round_page(offset+fs->fs_bsize),
                   2033:                    PGO_CLEANIT|PGO_SYNCIO|PGO_FREE);
1.1       hannken  2034:                if (error)
1.4       hannken  2035:                        return error;
                   2036:
                   2037:                return readfsblk(vp, data, fragstoblks(fs, dbtofsb(fs, bn)));
1.1       hannken  2038:        }
                   2039:
1.4       hannken  2040:        bzero(data, fs->fs_bsize);
                   2041:
                   2042:        return 0;
1.1       hannken  2043: }
                   2044:
                   2045: /*
1.4       hannken  2046:  * Write the specified block. Bypass UBC to prevent deadlocks.
1.1       hannken  2047:  */
                   2048: static int
1.18      thorpej  2049: writevnblk(struct vnode *vp, caddr_t data, ufs2_daddr_t lbn)
1.1       hannken  2050: {
1.4       hannken  2051:        int s, error;
                   2052:        off_t offset;
                   2053:        struct buf *bp;
1.1       hannken  2054:        struct inode *ip = VTOI(vp);
                   2055:        struct fs *fs = ip->i_fs;
                   2056:
1.4       hannken  2057:        offset = lblktosize(fs, (off_t)lbn);
                   2058:        s = cow_enter();
                   2059:        simple_lock(&vp->v_interlock);
                   2060:        error = VOP_PUTPAGES(vp, trunc_page(offset),
                   2061:            round_page(offset+fs->fs_bsize), PGO_CLEANIT|PGO_SYNCIO|PGO_FREE);
                   2062:        if (error == 0)
1.22      yamt     2063:                error = ffs_balloc(vp, lblktosize(fs, (off_t)lbn),
1.4       hannken  2064:                    fs->fs_bsize, KERNCRED, B_SYNC, &bp);
                   2065:        cow_leave(s);
1.1       hannken  2066:        if (error)
                   2067:                return error;
                   2068:
1.4       hannken  2069:        bcopy(data, bp->b_data, fs->fs_bsize);
                   2070:        bp->b_flags |= B_NOCACHE;
                   2071:
                   2072:        return bwrite(bp);
                   2073: }
                   2074:
                   2075: /*
                   2076:  * Set/reset lwp's L_COWINPROGRESS flag.
                   2077:  * May be called recursive.
                   2078:  */
                   2079: static inline int
                   2080: cow_enter(void)
                   2081: {
                   2082:        struct lwp *l = curlwp;
                   2083:
                   2084:        if (l->l_flag & L_COWINPROGRESS) {
                   2085:                return 0;
                   2086:        } else {
                   2087:                l->l_flag |= L_COWINPROGRESS;
                   2088:                return L_COWINPROGRESS;
1.1       hannken  2089:        }
1.4       hannken  2090: }
                   2091:
                   2092: static inline void
                   2093: cow_leave(int flag)
                   2094: {
                   2095:        struct lwp *l = curlwp;
1.1       hannken  2096:
1.4       hannken  2097:        l->l_flag &= ~flag;
1.1       hannken  2098: }
                   2099:
                   2100: /*
                   2101:  * Get/Put direct block from inode or buffer containing disk addresses. Take
                   2102:  * care for fs type (UFS1/UFS2) and byte swapping. These functions should go
                   2103:  * into a global include.
                   2104:  */
                   2105: static inline ufs2_daddr_t
                   2106: db_get(struct inode *ip, int loc)
                   2107: {
                   2108:        if (ip->i_ump->um_fstype == UFS1)
1.2       hannken  2109:                return ufs_rw32(ip->i_ffs1_db[loc], UFS_IPNEEDSWAP(ip));
1.1       hannken  2110:        else
1.2       hannken  2111:                return ufs_rw64(ip->i_ffs2_db[loc], UFS_IPNEEDSWAP(ip));
1.1       hannken  2112: }
                   2113:
                   2114: static inline void
                   2115: db_assign(struct inode *ip, int loc, ufs2_daddr_t val)
                   2116: {
                   2117:        if (ip->i_ump->um_fstype == UFS1)
1.2       hannken  2118:                ip->i_ffs1_db[loc] = ufs_rw32(val, UFS_IPNEEDSWAP(ip));
1.1       hannken  2119:        else
1.2       hannken  2120:                ip->i_ffs2_db[loc] = ufs_rw64(val, UFS_IPNEEDSWAP(ip));
1.1       hannken  2121: }
                   2122:
                   2123: static inline ufs2_daddr_t
1.17      christos 2124: idb_get(struct inode *ip, caddr_t bf, int loc)
1.1       hannken  2125: {
                   2126:        if (ip->i_ump->um_fstype == UFS1)
1.17      christos 2127:                return ufs_rw32(((ufs1_daddr_t *)(bf))[loc],
1.2       hannken  2128:                    UFS_IPNEEDSWAP(ip));
1.1       hannken  2129:        else
1.17      christos 2130:                return ufs_rw64(((ufs2_daddr_t *)(bf))[loc],
1.2       hannken  2131:                    UFS_IPNEEDSWAP(ip));
1.1       hannken  2132: }
                   2133:
                   2134: static inline void
1.17      christos 2135: idb_assign(struct inode *ip, caddr_t bf, int loc, ufs2_daddr_t val)
1.1       hannken  2136: {
                   2137:        if (ip->i_ump->um_fstype == UFS1)
1.17      christos 2138:                ((ufs1_daddr_t *)(bf))[loc] =
1.2       hannken  2139:                    ufs_rw32(val, UFS_IPNEEDSWAP(ip));
1.1       hannken  2140:        else
1.17      christos 2141:                ((ufs2_daddr_t *)(bf))[loc] =
1.2       hannken  2142:                    ufs_rw64(val, UFS_IPNEEDSWAP(ip));
1.1       hannken  2143: }

CVSweb <webmaster@jp.NetBSD.org>