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