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