ffs_snapshot.c revision 1.70 1 /* $NetBSD: ffs_snapshot.c,v 1.70 2008/06/17 14:53:11 reinoud 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.70 2008/06/17 14:53:11 reinoud 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 error = ffs_getblk(cancelvp, lbn, fsbtodb(fs, blkno), fs->fs_bsize,
930 false, &bp);
931 if (error)
932 return error;
933 if ((bp->b_oflags & (BO_DONE | BO_DELWRI)) == 0 &&
934 (error = readfsblk(bp->b_vp, bp->b_data, fragstoblks(fs, blkno)))) {
935 brelse(bp, 0);
936 return (error);
937 }
938 /*
939 * Account for the block pointers in this indirect block.
940 */
941 last = howmany(remblks, blksperindir);
942 if (last > NINDIR(fs))
943 last = NINDIR(fs);
944 bap = malloc(fs->fs_bsize, M_DEVBUF, M_WAITOK);
945 bcopy(bp->b_data, (void *)bap, fs->fs_bsize);
946 brelse(bp, 0);
947 error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
948 level == 0 ? rlbn : -1, expungetype);
949 if (error || level == 0)
950 goto out;
951 /*
952 * Account for the block pointers in each of the indirect blocks
953 * in the levels below us.
954 */
955 subblksperindir = blksperindir / NINDIR(fs);
956 for (lbn++, level--, i = 0; i < last; i++) {
957 error = indiracct_ufs1(snapvp, cancelvp, level,
958 ufs_rw32(bap[i], ns), lbn, rlbn, remblks, subblksperindir,
959 fs, acctfunc, expungetype);
960 if (error)
961 goto out;
962 rlbn += blksperindir;
963 lbn -= blksperindir;
964 remblks -= blksperindir;
965 }
966 out:
967 FREE(bap, M_DEVBUF);
968 return (error);
969 }
970
971 /*
972 * Do both snap accounting and map accounting.
973 */
974 static int
975 fullacct_ufs1(struct vnode *vp, ufs1_daddr_t *oldblkp, ufs1_daddr_t *lastblkp,
976 struct fs *fs, ufs_lbn_t lblkno,
977 int exptype /* BLK_SNAP or BLK_NOCOPY */)
978 {
979 int error;
980
981 if ((error = snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
982 return (error);
983 return (mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype));
984 }
985
986 /*
987 * Identify a set of blocks allocated in a snapshot inode.
988 */
989 static int
990 snapacct_ufs1(struct vnode *vp, ufs1_daddr_t *oldblkp, ufs1_daddr_t *lastblkp,
991 struct fs *fs, ufs_lbn_t lblkno,
992 int expungetype /* BLK_SNAP or BLK_NOCOPY */)
993 {
994 struct inode *ip = VTOI(vp);
995 ufs1_daddr_t blkno, *blkp;
996 ufs_lbn_t lbn;
997 struct buf *ibp;
998 int error, ns;
999
1000 ns = UFS_FSNEEDSWAP(fs);
1001
1002 for ( ; oldblkp < lastblkp; oldblkp++) {
1003 blkno = ufs_rw32(*oldblkp, ns);
1004 if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
1005 continue;
1006 lbn = fragstoblks(fs, blkno);
1007 if (lbn < NDADDR) {
1008 blkp = &ip->i_ffs1_db[lbn];
1009 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1010 } else {
1011 error = ffs_balloc(vp, lblktosize(fs, (off_t)lbn),
1012 fs->fs_bsize, KERNCRED, B_METAONLY, &ibp);
1013 if (error)
1014 return (error);
1015 blkp = &((ufs1_daddr_t *)(ibp->b_data))
1016 [(lbn - NDADDR) % NINDIR(fs)];
1017 }
1018 /*
1019 * If we are expunging a snapshot vnode and we
1020 * find a block marked BLK_NOCOPY, then it is
1021 * one that has been allocated to this snapshot after
1022 * we took our current snapshot and can be ignored.
1023 */
1024 blkno = ufs_rw32(*blkp, ns);
1025 if (expungetype == BLK_SNAP && blkno == BLK_NOCOPY) {
1026 if (lbn >= NDADDR)
1027 brelse(ibp, 0);
1028 } else {
1029 if (blkno != 0)
1030 panic("snapacct_ufs1: bad block");
1031 *blkp = ufs_rw32(expungetype, ns);
1032 if (lbn >= NDADDR)
1033 bdwrite(ibp);
1034 }
1035 }
1036 return (0);
1037 }
1038
1039 /*
1040 * Account for a set of blocks allocated in a snapshot inode.
1041 */
1042 static int
1043 mapacct_ufs1(struct vnode *vp, ufs1_daddr_t *oldblkp, ufs1_daddr_t *lastblkp,
1044 struct fs *fs, ufs_lbn_t lblkno, int expungetype)
1045 {
1046 ufs1_daddr_t blkno;
1047 struct inode *ip;
1048 ino_t inum;
1049 int acctit, ns;
1050
1051 ns = UFS_FSNEEDSWAP(fs);
1052 ip = VTOI(vp);
1053 inum = ip->i_number;
1054 if (lblkno == -1)
1055 acctit = 0;
1056 else
1057 acctit = 1;
1058 for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
1059 blkno = ufs_rw32(*oldblkp, ns);
1060 if (blkno == 0 || blkno == BLK_NOCOPY)
1061 continue;
1062 if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
1063 *ip->i_snapblklist++ = lblkno;
1064 if (blkno == BLK_SNAP)
1065 blkno = blkstofrags(fs, lblkno);
1066 ffs_blkfree(fs, vp, blkno, fs->fs_bsize, inum);
1067 }
1068 return (0);
1069 }
1070
1071 /*
1072 * Before expunging a snapshot inode, note all the
1073 * blocks that it claims with BLK_SNAP so that fsck will
1074 * be able to account for those blocks properly and so
1075 * that this snapshot knows that it need not copy them
1076 * if the other snapshot holding them is freed. This code
1077 * is reproduced once each for UFS1 and UFS2.
1078 */
1079 static int
1080 expunge_ufs2(struct vnode *snapvp, struct inode *cancelip, struct fs *fs,
1081 int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
1082 struct fs *, ufs_lbn_t, int),
1083 int expungetype)
1084 {
1085 int i, error, ns, indiroff;
1086 ufs_lbn_t lbn, rlbn;
1087 ufs2_daddr_t len, blkno, numblks, blksperindir;
1088 struct ufs2_dinode *dip;
1089 struct buf *bp;
1090 void *bf;
1091
1092 ns = UFS_FSNEEDSWAP(fs);
1093 /*
1094 * Prepare to expunge the inode. If its inode block has not
1095 * yet been copied, then allocate and fill the copy.
1096 */
1097 lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
1098 blkno = 0;
1099 if (lbn < NDADDR) {
1100 blkno = db_get(VTOI(snapvp), lbn);
1101 } else {
1102 error = ffs_balloc(snapvp, lblktosize(fs, (off_t)lbn),
1103 fs->fs_bsize, KERNCRED, B_METAONLY, &bp);
1104 if (error)
1105 return (error);
1106 indiroff = (lbn - NDADDR) % NINDIR(fs);
1107 blkno = idb_get(VTOI(snapvp), bp->b_data, indiroff);
1108 brelse(bp, 0);
1109 }
1110 bf = malloc(fs->fs_bsize, M_UFSMNT, M_WAITOK);
1111 if (blkno != 0)
1112 error = readvnblk(snapvp, bf, lbn);
1113 else
1114 error = readfsblk(snapvp, bf, lbn);
1115 if (error) {
1116 free(bf, M_UFSMNT);
1117 return error;
1118 }
1119 /*
1120 * Set a snapshot inode to be a zero length file, regular files
1121 * or unlinked snapshots to be completely unallocated.
1122 */
1123 dip = (struct ufs2_dinode *)bf + ino_to_fsbo(fs, cancelip->i_number);
1124 if (expungetype == BLK_NOCOPY || cancelip->i_ffs_effnlink == 0)
1125 dip->di_mode = 0;
1126 dip->di_size = 0;
1127 dip->di_blocks = 0;
1128 dip->di_flags =
1129 ufs_rw32(ufs_rw32(dip->di_flags, ns) & ~SF_SNAPSHOT, ns);
1130 bzero(&dip->di_db[0], (NDADDR + NIADDR) * sizeof(ufs2_daddr_t));
1131 error = writevnblk(snapvp, bf, lbn);
1132 free(bf, M_UFSMNT);
1133 if (error)
1134 return error;
1135 /*
1136 * Now go through and expunge all the blocks in the file
1137 * using the function requested.
1138 */
1139 numblks = howmany(cancelip->i_size, fs->fs_bsize);
1140 if ((error = (*acctfunc)(snapvp, &cancelip->i_ffs2_db[0],
1141 &cancelip->i_ffs2_db[NDADDR], fs, 0, expungetype)))
1142 return (error);
1143 if ((error = (*acctfunc)(snapvp, &cancelip->i_ffs2_ib[0],
1144 &cancelip->i_ffs2_ib[NIADDR], fs, -1, expungetype)))
1145 return (error);
1146 blksperindir = 1;
1147 lbn = -NDADDR;
1148 len = numblks - NDADDR;
1149 rlbn = NDADDR;
1150 for (i = 0; len > 0 && i < NIADDR; i++) {
1151 error = indiracct_ufs2(snapvp, ITOV(cancelip), i,
1152 ufs_rw64(cancelip->i_ffs2_ib[i], ns), lbn, rlbn, len,
1153 blksperindir, fs, acctfunc, expungetype);
1154 if (error)
1155 return (error);
1156 blksperindir *= NINDIR(fs);
1157 lbn -= blksperindir + 1;
1158 len -= blksperindir;
1159 rlbn += blksperindir;
1160 }
1161 return (0);
1162 }
1163
1164 /*
1165 * Descend an indirect block chain for vnode cancelvp accounting for all
1166 * its indirect blocks in snapvp.
1167 */
1168 static int
1169 indiracct_ufs2(struct vnode *snapvp, struct vnode *cancelvp, int level,
1170 ufs2_daddr_t blkno, ufs_lbn_t lbn, ufs_lbn_t rlbn, ufs_lbn_t remblks,
1171 ufs_lbn_t blksperindir, struct fs *fs,
1172 int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
1173 struct fs *, ufs_lbn_t, int),
1174 int expungetype)
1175 {
1176 int error, ns, num, i;
1177 ufs_lbn_t subblksperindir;
1178 struct indir indirs[NIADDR + 2];
1179 ufs2_daddr_t last, *bap;
1180 struct buf *bp;
1181
1182 ns = UFS_FSNEEDSWAP(fs);
1183
1184 if (blkno == 0) {
1185 if (expungetype == BLK_NOCOPY)
1186 return (0);
1187 panic("indiracct_ufs2: missing indir");
1188 }
1189 if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
1190 return (error);
1191 if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
1192 panic("indiracct_ufs2: botched params");
1193 /*
1194 * We have to expand bread here since it will deadlock looking
1195 * up the block number for any blocks that are not in the cache.
1196 */
1197 error = ffs_getblk(cancelvp, lbn, fsbtodb(fs, blkno), fs->fs_bsize,
1198 false, &bp);
1199 if (error)
1200 return error;
1201 if ((bp->b_oflags & (BO_DONE | BO_DELWRI)) == 0 &&
1202 (error = readfsblk(bp->b_vp, bp->b_data, fragstoblks(fs, blkno)))) {
1203 brelse(bp, 0);
1204 return (error);
1205 }
1206 /*
1207 * Account for the block pointers in this indirect block.
1208 */
1209 last = howmany(remblks, blksperindir);
1210 if (last > NINDIR(fs))
1211 last = NINDIR(fs);
1212 bap = malloc(fs->fs_bsize, M_DEVBUF, M_WAITOK);
1213 bcopy(bp->b_data, (void *)bap, fs->fs_bsize);
1214 brelse(bp, 0);
1215 error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
1216 level == 0 ? rlbn : -1, expungetype);
1217 if (error || level == 0)
1218 goto out;
1219 /*
1220 * Account for the block pointers in each of the indirect blocks
1221 * in the levels below us.
1222 */
1223 subblksperindir = blksperindir / NINDIR(fs);
1224 for (lbn++, level--, i = 0; i < last; i++) {
1225 error = indiracct_ufs2(snapvp, cancelvp, level,
1226 ufs_rw64(bap[i], ns), lbn, rlbn, remblks, subblksperindir,
1227 fs, acctfunc, expungetype);
1228 if (error)
1229 goto out;
1230 rlbn += blksperindir;
1231 lbn -= blksperindir;
1232 remblks -= blksperindir;
1233 }
1234 out:
1235 FREE(bap, M_DEVBUF);
1236 return (error);
1237 }
1238
1239 /*
1240 * Do both snap accounting and map accounting.
1241 */
1242 static int
1243 fullacct_ufs2(struct vnode *vp, ufs2_daddr_t *oldblkp, ufs2_daddr_t *lastblkp,
1244 struct fs *fs, ufs_lbn_t lblkno,
1245 int exptype /* BLK_SNAP or BLK_NOCOPY */)
1246 {
1247 int error;
1248
1249 if ((error = snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
1250 return (error);
1251 return (mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype));
1252 }
1253
1254 /*
1255 * Identify a set of blocks allocated in a snapshot inode.
1256 */
1257 static int
1258 snapacct_ufs2(struct vnode *vp, ufs2_daddr_t *oldblkp, ufs2_daddr_t *lastblkp,
1259 struct fs *fs, ufs_lbn_t lblkno,
1260 int expungetype /* BLK_SNAP or BLK_NOCOPY */)
1261 {
1262 struct inode *ip = VTOI(vp);
1263 ufs2_daddr_t blkno, *blkp;
1264 ufs_lbn_t lbn;
1265 struct buf *ibp;
1266 int error, ns;
1267
1268 ns = UFS_FSNEEDSWAP(fs);
1269
1270 for ( ; oldblkp < lastblkp; oldblkp++) {
1271 blkno = ufs_rw64(*oldblkp, ns);
1272 if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
1273 continue;
1274 lbn = fragstoblks(fs, blkno);
1275 if (lbn < NDADDR) {
1276 blkp = &ip->i_ffs2_db[lbn];
1277 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1278 } else {
1279 error = ffs_balloc(vp, lblktosize(fs, (off_t)lbn),
1280 fs->fs_bsize, KERNCRED, B_METAONLY, &ibp);
1281 if (error)
1282 return (error);
1283 blkp = &((ufs2_daddr_t *)(ibp->b_data))
1284 [(lbn - NDADDR) % NINDIR(fs)];
1285 }
1286 /*
1287 * If we are expunging a snapshot vnode and we
1288 * find a block marked BLK_NOCOPY, then it is
1289 * one that has been allocated to this snapshot after
1290 * we took our current snapshot and can be ignored.
1291 */
1292 blkno = ufs_rw64(*blkp, ns);
1293 if (expungetype == BLK_SNAP && blkno == BLK_NOCOPY) {
1294 if (lbn >= NDADDR)
1295 brelse(ibp, 0);
1296 } else {
1297 if (blkno != 0)
1298 panic("snapacct_ufs2: bad block");
1299 *blkp = ufs_rw64(expungetype, ns);
1300 if (lbn >= NDADDR)
1301 bdwrite(ibp);
1302 }
1303 }
1304 return (0);
1305 }
1306
1307 /*
1308 * Account for a set of blocks allocated in a snapshot inode.
1309 */
1310 static int
1311 mapacct_ufs2(struct vnode *vp, ufs2_daddr_t *oldblkp, ufs2_daddr_t *lastblkp,
1312 struct fs *fs, ufs_lbn_t lblkno, int expungetype)
1313 {
1314 ufs2_daddr_t blkno;
1315 struct inode *ip;
1316 ino_t inum;
1317 int acctit, ns;
1318
1319 ns = UFS_FSNEEDSWAP(fs);
1320 ip = VTOI(vp);
1321 inum = ip->i_number;
1322 if (lblkno == -1)
1323 acctit = 0;
1324 else
1325 acctit = 1;
1326 for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
1327 blkno = ufs_rw64(*oldblkp, ns);
1328 if (blkno == 0 || blkno == BLK_NOCOPY)
1329 continue;
1330 if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
1331 *ip->i_snapblklist++ = lblkno;
1332 if (blkno == BLK_SNAP)
1333 blkno = blkstofrags(fs, lblkno);
1334 ffs_blkfree(fs, vp, blkno, fs->fs_bsize, inum);
1335 }
1336 return (0);
1337 }
1338 #endif /* defined(FFS_NO_SNAPSHOT) */
1339
1340 /*
1341 * Decrement extra reference on snapshot when last name is removed.
1342 * It will not be freed until the last open reference goes away.
1343 */
1344 void
1345 ffs_snapgone(struct inode *ip)
1346 {
1347 struct mount *mp = ip->i_devvp->v_specmountpoint;
1348 struct inode *xp;
1349 struct fs *fs;
1350 struct snap_info *si;
1351 int snaploc;
1352
1353 si = VFSTOUFS(mp)->um_snapinfo;
1354
1355 /*
1356 * Find snapshot in incore list.
1357 */
1358 mutex_enter(&si->si_lock);
1359 TAILQ_FOREACH(xp, &si->si_snapshots, i_nextsnap)
1360 if (xp == ip)
1361 break;
1362 mutex_exit(&si->si_lock);
1363 if (xp != NULL)
1364 vrele(ITOV(ip));
1365 #ifdef DEBUG
1366 else if (snapdebug)
1367 printf("ffs_snapgone: lost snapshot vnode %llu\n",
1368 (unsigned long long)ip->i_number);
1369 #endif
1370 /*
1371 * Delete snapshot inode from superblock. Keep list dense.
1372 */
1373 mutex_enter(&si->si_lock);
1374 fs = ip->i_fs;
1375 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
1376 if (fs->fs_snapinum[snaploc] == ip->i_number)
1377 break;
1378 if (snaploc < FSMAXSNAP) {
1379 for (snaploc++; snaploc < FSMAXSNAP; snaploc++) {
1380 if (fs->fs_snapinum[snaploc] == 0)
1381 break;
1382 fs->fs_snapinum[snaploc - 1] = fs->fs_snapinum[snaploc];
1383 }
1384 fs->fs_snapinum[snaploc - 1] = 0;
1385 }
1386 si->si_gen++;
1387 mutex_exit(&si->si_lock);
1388 }
1389
1390 /*
1391 * Prepare a snapshot file for being removed.
1392 */
1393 void
1394 ffs_snapremove(struct vnode *vp)
1395 {
1396 struct inode *ip = VTOI(vp), *xp;
1397 struct vnode *devvp = ip->i_devvp;
1398 struct fs *fs = ip->i_fs;
1399 struct mount *mp = devvp->v_specmountpoint;
1400 struct vnlock *lkp;
1401 struct buf *ibp;
1402 struct snap_info *si;
1403 ufs2_daddr_t numblks, blkno, dblk;
1404 int error, ns, loc, last;
1405
1406 si = VFSTOUFS(mp)->um_snapinfo;
1407 ns = UFS_FSNEEDSWAP(fs);
1408 /*
1409 * If active, delete from incore list (this snapshot may
1410 * already have been in the process of being deleted, so
1411 * would not have been active).
1412 *
1413 * Clear copy-on-write flag if last snapshot.
1414 */
1415 if (ip->i_nextsnap.tqe_prev != 0) {
1416 mutex_enter(&si->si_lock);
1417 vlockmgr(&vp->v_lock, LK_EXCLUSIVE);
1418 TAILQ_REMOVE(&si->si_snapshots, ip, i_nextsnap);
1419 ip->i_nextsnap.tqe_prev = 0;
1420 lkp = vp->v_vnlock;
1421 KASSERT(lkp == &si->si_vnlock);
1422 vp->v_vnlock = &vp->v_lock;
1423 vlockmgr(lkp, LK_RELEASE);
1424 if (TAILQ_FIRST(&si->si_snapshots) != 0) {
1425 /* Roll back the list of preallocated blocks. */
1426 xp = TAILQ_LAST(&si->si_snapshots, inodelst);
1427 si->si_snapblklist = xp->i_snapblklist;
1428 } else {
1429 si->si_snapblklist = 0;
1430 si->si_gen++;
1431 mutex_exit(&si->si_lock);
1432 fscow_disestablish(mp, ffs_copyonwrite, devvp);
1433 mutex_enter(&si->si_lock);
1434 }
1435 si->si_gen++;
1436 mutex_exit(&si->si_lock);
1437 FREE(ip->i_snapblklist, M_UFSMNT);
1438 ip->i_snapblklist = NULL;
1439 }
1440 /*
1441 * Clear all BLK_NOCOPY fields. Pass any block claims to other
1442 * snapshots that want them (see ffs_snapblkfree below).
1443 */
1444 for (blkno = 1; blkno < NDADDR; blkno++) {
1445 dblk = db_get(ip, blkno);
1446 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1447 db_assign(ip, blkno, 0);
1448 else if ((dblk == blkstofrags(fs, blkno) &&
1449 ffs_snapblkfree(fs, ip->i_devvp, dblk, fs->fs_bsize,
1450 ip->i_number))) {
1451 DIP_ADD(ip, blocks, -btodb(fs->fs_bsize));
1452 db_assign(ip, blkno, 0);
1453 }
1454 }
1455 numblks = howmany(ip->i_size, fs->fs_bsize);
1456 for (blkno = NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
1457 error = ffs_balloc(vp, lblktosize(fs, (off_t)blkno),
1458 fs->fs_bsize, KERNCRED, B_METAONLY, &ibp);
1459 if (error)
1460 continue;
1461 if (fs->fs_size - blkno > NINDIR(fs))
1462 last = NINDIR(fs);
1463 else
1464 last = fs->fs_size - blkno;
1465 for (loc = 0; loc < last; loc++) {
1466 dblk = idb_get(ip, ibp->b_data, loc);
1467 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1468 idb_assign(ip, ibp->b_data, loc, 0);
1469 else if (dblk == blkstofrags(fs, blkno) &&
1470 ffs_snapblkfree(fs, ip->i_devvp, dblk,
1471 fs->fs_bsize, ip->i_number)) {
1472 DIP_ADD(ip, blocks, -btodb(fs->fs_bsize));
1473 idb_assign(ip, ibp->b_data, loc, 0);
1474 }
1475 }
1476 bawrite(ibp);
1477 }
1478 /*
1479 * Clear snapshot flag and drop reference.
1480 */
1481 ip->i_flags &= ~SF_SNAPSHOT;
1482 DIP_ASSIGN(ip, flags, ip->i_flags);
1483 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1484 }
1485
1486 /*
1487 * Notification that a block is being freed. Return zero if the free
1488 * should be allowed to proceed. Return non-zero if the snapshot file
1489 * wants to claim the block. The block will be claimed if it is an
1490 * uncopied part of one of the snapshots. It will be freed if it is
1491 * either a BLK_NOCOPY or has already been copied in all of the snapshots.
1492 * If a fragment is being freed, then all snapshots that care about
1493 * it must make a copy since a snapshot file can only claim full sized
1494 * blocks. Note that if more than one snapshot file maps the block,
1495 * we can pick one at random to claim it. Since none of the snapshots
1496 * can change, we are assurred that they will all see the same unmodified
1497 * image. When deleting a snapshot file (see ffs_snapremove above), we
1498 * must push any of these claimed blocks to one of the other snapshots
1499 * that maps it. These claimed blocks are easily identified as they will
1500 * have a block number equal to their logical block number within the
1501 * snapshot. A copied block can never have this property because they
1502 * must always have been allocated from a BLK_NOCOPY location.
1503 */
1504 int
1505 ffs_snapblkfree(struct fs *fs, struct vnode *devvp, ufs2_daddr_t bno,
1506 long size, ino_t inum)
1507 {
1508 struct mount *mp = devvp->v_specmountpoint;
1509 struct buf *ibp;
1510 struct inode *ip;
1511 struct vnode *vp = NULL;
1512 struct snap_info *si;
1513 void *saved_data = NULL;
1514 ufs_lbn_t lbn;
1515 ufs2_daddr_t blkno;
1516 uint32_t gen;
1517 int indiroff = 0, snapshot_locked = 0, error = 0, claimedblk = 0;
1518
1519 si = VFSTOUFS(mp)->um_snapinfo;
1520 lbn = fragstoblks(fs, bno);
1521 mutex_enter(&si->si_lock);
1522 retry:
1523 gen = si->si_gen;
1524 TAILQ_FOREACH(ip, &si->si_snapshots, i_nextsnap) {
1525 vp = ITOV(ip);
1526 if (snapshot_locked == 0) {
1527 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1528 mutex_exit(&si->si_lock);
1529 kpause("snaplock", false, 1, NULL);
1530 mutex_enter(&si->si_lock);
1531 goto retry;
1532 }
1533 snapshot_locked = 1;
1534 if (gen != si->si_gen)
1535 goto retry;
1536 }
1537 /*
1538 * Lookup block being written.
1539 */
1540 if (lbn < NDADDR) {
1541 blkno = db_get(ip, lbn);
1542 } else {
1543 mutex_exit(&si->si_lock);
1544 error = ffs_balloc(vp, lblktosize(fs, (off_t)lbn),
1545 fs->fs_bsize, KERNCRED, B_METAONLY, &ibp);
1546 if (error) {
1547 mutex_enter(&si->si_lock);
1548 break;
1549 }
1550 indiroff = (lbn - NDADDR) % NINDIR(fs);
1551 blkno = idb_get(ip, ibp->b_data, indiroff);
1552 mutex_enter(&si->si_lock);
1553 if (gen != si->si_gen) {
1554 brelse(ibp, 0);
1555 goto retry;
1556 }
1557 }
1558 /*
1559 * Check to see if block needs to be copied.
1560 */
1561 if (blkno == 0) {
1562 /*
1563 * A block that we map is being freed. If it has not
1564 * been claimed yet, we will claim or copy it (below).
1565 */
1566 claimedblk = 1;
1567 } else if (blkno == BLK_SNAP) {
1568 /*
1569 * No previous snapshot claimed the block,
1570 * so it will be freed and become a BLK_NOCOPY
1571 * (don't care) for us.
1572 */
1573 if (claimedblk)
1574 panic("snapblkfree: inconsistent block type");
1575 if (lbn < NDADDR) {
1576 db_assign(ip, lbn, BLK_NOCOPY);
1577 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1578 } else {
1579 idb_assign(ip, ibp->b_data, indiroff,
1580 BLK_NOCOPY);
1581 mutex_exit(&si->si_lock);
1582 bwrite(ibp);
1583 mutex_enter(&si->si_lock);
1584 if (gen != si->si_gen)
1585 goto retry;
1586 }
1587 continue;
1588 } else /* BLK_NOCOPY or default */ {
1589 /*
1590 * If the snapshot has already copied the block
1591 * (default), or does not care about the block,
1592 * it is not needed.
1593 */
1594 if (lbn >= NDADDR)
1595 brelse(ibp, 0);
1596 continue;
1597 }
1598 /*
1599 * If this is a full size block, we will just grab it
1600 * and assign it to the snapshot inode. Otherwise we
1601 * will proceed to copy it. See explanation for this
1602 * routine as to why only a single snapshot needs to
1603 * claim this block.
1604 */
1605 if (size == fs->fs_bsize) {
1606 #ifdef DEBUG
1607 if (snapdebug)
1608 printf("%s %llu lbn %" PRId64
1609 "from inum %llu\n",
1610 "Grabonremove: snapino",
1611 (unsigned long long)ip->i_number,
1612 lbn, (unsigned long long)inum);
1613 #endif
1614 mutex_exit(&si->si_lock);
1615 if (lbn < NDADDR) {
1616 db_assign(ip, lbn, bno);
1617 } else {
1618 idb_assign(ip, ibp->b_data, indiroff, bno);
1619 bwrite(ibp);
1620 }
1621 DIP_ADD(ip, blocks, btodb(size));
1622 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1623 VOP_UNLOCK(vp, 0);
1624 return (1);
1625 }
1626 if (lbn >= NDADDR)
1627 brelse(ibp, 0);
1628 #ifdef DEBUG
1629 if (snapdebug)
1630 printf("%s%llu lbn %" PRId64 " %s %llu size %ld\n",
1631 "Copyonremove: snapino ",
1632 (unsigned long long)ip->i_number,
1633 lbn, "for inum", (unsigned long long)inum, size);
1634 #endif
1635 /*
1636 * If we have already read the old block contents, then
1637 * simply copy them to the new block. Note that we need
1638 * to synchronously write snapshots that have not been
1639 * unlinked, and hence will be visible after a crash,
1640 * to ensure their integrity.
1641 */
1642 mutex_exit(&si->si_lock);
1643 if (saved_data == NULL) {
1644 saved_data = malloc(fs->fs_bsize, M_UFSMNT, M_WAITOK);
1645 if ((error = readfsblk(vp, saved_data, lbn)) != 0) {
1646 free(saved_data, M_UFSMNT);
1647 saved_data = NULL;
1648 mutex_enter(&si->si_lock);
1649 break;
1650 }
1651 }
1652 error = writevnblk(vp, saved_data, lbn);
1653 mutex_enter(&si->si_lock);
1654 if (error)
1655 break;
1656 if (gen != si->si_gen)
1657 goto retry;
1658 }
1659 mutex_exit(&si->si_lock);
1660 if (saved_data)
1661 free(saved_data, M_UFSMNT);
1662 /*
1663 * If we have been unable to allocate a block in which to do
1664 * the copy, then return non-zero so that the fragment will
1665 * not be freed. Although space will be lost, the snapshot
1666 * will stay consistent.
1667 */
1668 if (snapshot_locked)
1669 VOP_UNLOCK(vp, 0);
1670 return (error);
1671 }
1672
1673 /*
1674 * Associate snapshot files when mounting.
1675 */
1676 void
1677 ffs_snapshot_mount(struct mount *mp)
1678 {
1679 struct vnode *devvp = VFSTOUFS(mp)->um_devvp;
1680 struct fs *fs = VFSTOUFS(mp)->um_fs;
1681 struct lwp *l = curlwp;
1682 struct vnode *vp;
1683 struct inode *ip, *xp;
1684 struct snap_info *si;
1685 ufs2_daddr_t snaplistsize, *snapblklist;
1686 int i, error, ns, snaploc, loc;
1687
1688 /*
1689 * No persistent snapshots on apple ufs file systems.
1690 */
1691 if (UFS_MPISAPPLEUFS(VFSTOUFS(mp)))
1692 return;
1693
1694 si = VFSTOUFS(mp)->um_snapinfo;
1695 ns = UFS_FSNEEDSWAP(fs);
1696 /*
1697 * XXX The following needs to be set before ffs_truncate or
1698 * VOP_READ can be called.
1699 */
1700 mp->mnt_stat.f_iosize = fs->fs_bsize;
1701 /*
1702 * Process each snapshot listed in the superblock.
1703 */
1704 vp = NULL;
1705 mutex_enter(&si->si_lock);
1706 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++) {
1707 if (fs->fs_snapinum[snaploc] == 0)
1708 break;
1709 if ((error = VFS_VGET(mp, fs->fs_snapinum[snaploc],
1710 &vp)) != 0) {
1711 printf("ffs_snapshot_mount: vget failed %d\n", error);
1712 continue;
1713 }
1714 ip = VTOI(vp);
1715 if ((ip->i_flags & SF_SNAPSHOT) == 0) {
1716 printf("ffs_snapshot_mount: non-snapshot inode %d\n",
1717 fs->fs_snapinum[snaploc]);
1718 vput(vp);
1719 vp = NULL;
1720 for (loc = snaploc + 1; loc < FSMAXSNAP; loc++) {
1721 if (fs->fs_snapinum[loc] == 0)
1722 break;
1723 fs->fs_snapinum[loc - 1] = fs->fs_snapinum[loc];
1724 }
1725 fs->fs_snapinum[loc - 1] = 0;
1726 snaploc--;
1727 continue;
1728 }
1729
1730 /*
1731 * Read the block hints list. Use an empty list on
1732 * read errors.
1733 */
1734 error = vn_rdwr(UIO_READ, vp,
1735 (void *)&snaplistsize, sizeof(snaplistsize),
1736 lblktosize(fs, howmany(fs->fs_size, fs->fs_frag)),
1737 UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT,
1738 l->l_cred, NULL, NULL);
1739 if (error) {
1740 printf("ffs_snapshot_mount: read_1 failed %d\n", error);
1741 snaplistsize = 1;
1742 } else
1743 snaplistsize = ufs_rw64(snaplistsize, ns);
1744 snapblklist = malloc(
1745 snaplistsize * sizeof(ufs2_daddr_t), M_UFSMNT, M_WAITOK);
1746 if (error)
1747 snapblklist[0] = 1;
1748 else {
1749 error = vn_rdwr(UIO_READ, vp, (void *)snapblklist,
1750 snaplistsize * sizeof(ufs2_daddr_t),
1751 lblktosize(fs, howmany(fs->fs_size, fs->fs_frag)),
1752 UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT,
1753 l->l_cred, NULL, NULL);
1754 for (i = 0; i < snaplistsize; i++)
1755 snapblklist[i] = ufs_rw64(snapblklist[i], ns);
1756 if (error) {
1757 printf("ffs_snapshot_mount: read_2 failed %d\n",
1758 error);
1759 snapblklist[0] = 1;
1760 }
1761 }
1762 ip->i_snapblklist = &snapblklist[0];
1763
1764 /*
1765 * Acquire the snapshot lock and give up our original
1766 * private lock.
1767 */
1768 VI_LOCK(vp);
1769 vp->v_vnlock = &si->si_vnlock;
1770 vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY);
1771 vlockmgr(&vp->v_lock, LK_RELEASE);
1772 /*
1773 * Link it onto the active snapshot list.
1774 */
1775 if (ip->i_nextsnap.tqe_prev != 0)
1776 panic("ffs_snapshot_mount: %llu already on list",
1777 (unsigned long long)ip->i_number);
1778 else
1779 TAILQ_INSERT_TAIL(&si->si_snapshots, ip, i_nextsnap);
1780 vp->v_vflag |= VV_SYSTEM;
1781 VOP_UNLOCK(vp, 0);
1782 }
1783 /*
1784 * No usable snapshots found.
1785 */
1786 if (vp == NULL) {
1787 mutex_exit(&si->si_lock);
1788 return;
1789 }
1790 /*
1791 * Attach the block hints list. We always want to
1792 * use the list from the newest snapshot.
1793 */
1794 xp = TAILQ_LAST(&si->si_snapshots, inodelst);
1795 si->si_snapblklist = xp->i_snapblklist;
1796 fscow_establish(mp, ffs_copyonwrite, devvp);
1797 si->si_gen++;
1798 mutex_exit(&si->si_lock);
1799 }
1800
1801 /*
1802 * Disassociate snapshot files when unmounting.
1803 */
1804 void
1805 ffs_snapshot_unmount(struct mount *mp)
1806 {
1807 struct vnode *devvp = VFSTOUFS(mp)->um_devvp;
1808 struct inode *xp;
1809 struct vnode *vp = NULL;
1810 struct snap_info *si;
1811
1812 si = VFSTOUFS(mp)->um_snapinfo;
1813 mutex_enter(&si->si_lock);
1814 while ((xp = TAILQ_FIRST(&si->si_snapshots)) != 0) {
1815 vp = ITOV(xp);
1816 vp->v_vnlock = &vp->v_lock;
1817 TAILQ_REMOVE(&si->si_snapshots, xp, i_nextsnap);
1818 xp->i_nextsnap.tqe_prev = 0;
1819 if (xp->i_snapblklist == si->si_snapblklist)
1820 si->si_snapblklist = NULL;
1821 FREE(xp->i_snapblklist, M_UFSMNT);
1822 if (xp->i_ffs_effnlink > 0) {
1823 si->si_gen++;
1824 mutex_exit(&si->si_lock);
1825 vrele(vp);
1826 mutex_enter(&si->si_lock);
1827 }
1828 }
1829 if (vp)
1830 fscow_disestablish(mp, ffs_copyonwrite, devvp);
1831 si->si_gen++;
1832 mutex_exit(&si->si_lock);
1833 }
1834
1835 /*
1836 * Lookup a snapshots data block address.
1837 * Simpler than UFS_BALLOC() as we know all metadata is already allocated.
1838 */
1839 static int
1840 ffs_blkaddr(struct vnode *vp, daddr_t lbn, daddr_t *res)
1841 {
1842 struct indir indirs[NIADDR + 2];
1843 struct inode *ip = VTOI(vp);
1844 struct fs *fs = ip->i_fs;
1845 struct buf *bp;
1846 int error, num;
1847
1848 KASSERT(lbn >= 0);
1849
1850 if (lbn < NDADDR) {
1851 *res = db_get(ip, lbn);
1852 return 0;
1853 }
1854 if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
1855 return error;
1856 error = bread(vp, indirs[num-1].in_lbn, fs->fs_bsize, NOCRED, 0, &bp);
1857 if (error == 0)
1858 *res = idb_get(ip, bp->b_data, indirs[num-1].in_off);
1859 brelse(bp, 0);
1860
1861 return error;
1862 }
1863
1864 /*
1865 * Check for need to copy block that is about to be written,
1866 * copying the block if necessary.
1867 */
1868 static int
1869 ffs_copyonwrite(void *v, struct buf *bp, bool data_valid)
1870 {
1871 struct fs *fs;
1872 struct inode *ip;
1873 struct vnode *devvp = v, *vp = NULL;
1874 struct mount *mp = devvp->v_specmountpoint;
1875 struct snap_info *si;
1876 void *saved_data = NULL;
1877 ufs2_daddr_t lbn, blkno, *snapblklist;
1878 uint32_t gen;
1879 int lower, upper, mid, ns, snapshot_locked = 0, error = 0;
1880
1881 /*
1882 * Check for valid snapshots.
1883 */
1884 si = VFSTOUFS(mp)->um_snapinfo;
1885 mutex_enter(&si->si_lock);
1886 ip = TAILQ_FIRST(&si->si_snapshots);
1887 if (ip == NULL) {
1888 mutex_exit(&si->si_lock);
1889 return 0;
1890 }
1891 /*
1892 * First check to see if it is in the preallocated list.
1893 * By doing this check we avoid several potential deadlocks.
1894 */
1895 fs = ip->i_fs;
1896 ns = UFS_FSNEEDSWAP(fs);
1897 lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
1898 snapblklist = si->si_snapblklist;
1899 upper = si->si_snapblklist[0] - 1;
1900 lower = 1;
1901 while (lower <= upper) {
1902 mid = (lower + upper) / 2;
1903 if (snapblklist[mid] == lbn)
1904 break;
1905 if (snapblklist[mid] < lbn)
1906 lower = mid + 1;
1907 else
1908 upper = mid - 1;
1909 }
1910 if (lower <= upper) {
1911 mutex_exit(&si->si_lock);
1912 return 0;
1913 }
1914 /*
1915 * Not in the precomputed list, so check the snapshots.
1916 */
1917 if (data_valid && bp->b_bcount == fs->fs_bsize)
1918 saved_data = bp->b_data;
1919 retry:
1920 gen = si->si_gen;
1921 TAILQ_FOREACH(ip, &si->si_snapshots, i_nextsnap) {
1922 vp = ITOV(ip);
1923 /*
1924 * We ensure that everything of our own that needs to be
1925 * copied will be done at the time that ffs_snapshot is
1926 * called. Thus we can skip the check here which can
1927 * deadlock in doing the lookup in ffs_balloc.
1928 */
1929 if (bp->b_vp == vp)
1930 continue;
1931 if (snapshot_locked == 0) {
1932 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1933 mutex_exit(&si->si_lock);
1934 kpause("snaplock", false, 1, NULL);
1935 mutex_enter(&si->si_lock);
1936 goto retry;
1937 }
1938 snapshot_locked = 1;
1939 if (gen != si->si_gen)
1940 goto retry;
1941 }
1942 /*
1943 * Check to see if block needs to be copied.
1944 */
1945 if (lbn < NDADDR) {
1946 blkno = db_get(ip, lbn);
1947 } else {
1948 mutex_exit(&si->si_lock);
1949 if ((error = ffs_blkaddr(vp, lbn, &blkno)) != 0) {
1950 mutex_enter(&si->si_lock);
1951 break;
1952 }
1953 mutex_enter(&si->si_lock);
1954 if (gen != si->si_gen)
1955 goto retry;
1956 }
1957 #ifdef DIAGNOSTIC
1958 if (blkno == BLK_SNAP && bp->b_lblkno >= 0)
1959 panic("ffs_copyonwrite: bad copy block");
1960 #endif
1961 if (blkno != 0)
1962 continue;
1963 /*
1964 * Allocate the block into which to do the copy. Since
1965 * multiple processes may all try to copy the same block,
1966 * we have to recheck our need to do a copy if we sleep
1967 * waiting for the lock.
1968 *
1969 * Because all snapshots on a filesystem share a single
1970 * lock, we ensure that we will never be in competition
1971 * with another process to allocate a block.
1972 */
1973 #ifdef DEBUG
1974 if (snapdebug) {
1975 printf("Copyonwrite: snapino %llu lbn %" PRId64 " for ",
1976 (unsigned long long)ip->i_number, lbn);
1977 if (bp->b_vp == devvp)
1978 printf("fs metadata");
1979 else
1980 printf("inum %llu", (unsigned long long)
1981 VTOI(bp->b_vp)->i_number);
1982 printf(" lblkno %" PRId64 "\n", bp->b_lblkno);
1983 }
1984 #endif
1985 /*
1986 * If we have already read the old block contents, then
1987 * simply copy them to the new block. Note that we need
1988 * to synchronously write snapshots that have not been
1989 * unlinked, and hence will be visible after a crash,
1990 * to ensure their integrity.
1991 */
1992 mutex_exit(&si->si_lock);
1993 if (saved_data == NULL) {
1994 saved_data = malloc(fs->fs_bsize, M_UFSMNT, M_WAITOK);
1995 if ((error = readfsblk(vp, saved_data, lbn)) != 0) {
1996 free(saved_data, M_UFSMNT);
1997 saved_data = NULL;
1998 mutex_enter(&si->si_lock);
1999 break;
2000 }
2001 }
2002 error = writevnblk(vp, saved_data, lbn);
2003 mutex_enter(&si->si_lock);
2004 if (error)
2005 break;
2006 if (gen != si->si_gen)
2007 goto retry;
2008 }
2009 /*
2010 * Note that we need to synchronously write snapshots that
2011 * have not been unlinked, and hence will be visible after
2012 * a crash, to ensure their integrity.
2013 */
2014 mutex_exit(&si->si_lock);
2015 if (saved_data && saved_data != bp->b_data)
2016 free(saved_data, M_UFSMNT);
2017 if (snapshot_locked)
2018 VOP_UNLOCK(vp, 0);
2019 return error;
2020 }
2021
2022 /*
2023 * Read the specified block from disk. Vp is usually a snapshot vnode.
2024 */
2025 static int
2026 readfsblk(struct vnode *vp, void *data, ufs2_daddr_t lbn)
2027 {
2028 int error;
2029 struct inode *ip = VTOI(vp);
2030 struct fs *fs = ip->i_fs;
2031 struct buf *nbp;
2032
2033 nbp = getiobuf(NULL, true);
2034 nbp->b_flags = B_READ;
2035 nbp->b_bcount = nbp->b_bufsize = fs->fs_bsize;
2036 nbp->b_error = 0;
2037 nbp->b_data = data;
2038 nbp->b_blkno = nbp->b_rawblkno = fsbtodb(fs, blkstofrags(fs, lbn));
2039 nbp->b_proc = NULL;
2040 nbp->b_dev = ip->i_devvp->v_rdev;
2041 SET(nbp->b_cflags, BC_BUSY); /* mark buffer busy */
2042
2043 bdev_strategy(nbp);
2044
2045 error = biowait(nbp);
2046
2047 putiobuf(nbp);
2048
2049 return error;
2050 }
2051
2052 #if !defined(FFS_NO_SNAPSHOT)
2053 /*
2054 * Read the specified block. Bypass UBC to prevent deadlocks.
2055 */
2056 static int
2057 readvnblk(struct vnode *vp, void *data, ufs2_daddr_t lbn)
2058 {
2059 int error;
2060 daddr_t bn;
2061 off_t offset;
2062 struct inode *ip = VTOI(vp);
2063 struct fs *fs = ip->i_fs;
2064
2065 error = VOP_BMAP(vp, lbn, NULL, &bn, NULL);
2066 if (error)
2067 return error;
2068
2069 if (bn != (daddr_t)-1) {
2070 offset = dbtob(bn);
2071 mutex_enter(&vp->v_interlock);
2072 error = VOP_PUTPAGES(vp, trunc_page(offset),
2073 round_page(offset+fs->fs_bsize),
2074 PGO_CLEANIT|PGO_SYNCIO|PGO_FREE);
2075 if (error)
2076 return error;
2077
2078 return readfsblk(vp, data, fragstoblks(fs, dbtofsb(fs, bn)));
2079 }
2080
2081 bzero(data, fs->fs_bsize);
2082
2083 return 0;
2084 }
2085 #endif /* !defined(FFS_NO_SNAPSHOT) */
2086
2087 /*
2088 * Write the specified block. Bypass UBC to prevent deadlocks.
2089 */
2090 static int
2091 writevnblk(struct vnode *vp, void *data, ufs2_daddr_t lbn)
2092 {
2093 int error;
2094 off_t offset;
2095 struct buf *bp;
2096 struct inode *ip = VTOI(vp);
2097 struct fs *fs = ip->i_fs;
2098
2099 offset = lblktosize(fs, (off_t)lbn);
2100 mutex_enter(&vp->v_interlock);
2101 error = VOP_PUTPAGES(vp, trunc_page(offset),
2102 round_page(offset+fs->fs_bsize), PGO_CLEANIT|PGO_SYNCIO|PGO_FREE);
2103 if (error == 0)
2104 error = ffs_balloc(vp, lblktosize(fs, (off_t)lbn),
2105 fs->fs_bsize, KERNCRED, B_SYNC, &bp);
2106 if (error)
2107 return error;
2108
2109 bcopy(data, bp->b_data, fs->fs_bsize);
2110 mutex_enter(&bufcache_lock);
2111 /* XXX Shouldn't need to lock for this, NOCACHE is only read later. */
2112 bp->b_cflags |= BC_NOCACHE;
2113 mutex_exit(&bufcache_lock);
2114
2115 return bwrite(bp);
2116 }
2117
2118 /*
2119 * Get/Put direct block from inode or buffer containing disk addresses. Take
2120 * care for fs type (UFS1/UFS2) and byte swapping. These functions should go
2121 * into a global include.
2122 */
2123 static inline ufs2_daddr_t
2124 db_get(struct inode *ip, int loc)
2125 {
2126 if (ip->i_ump->um_fstype == UFS1)
2127 return ufs_rw32(ip->i_ffs1_db[loc], UFS_IPNEEDSWAP(ip));
2128 else
2129 return ufs_rw64(ip->i_ffs2_db[loc], UFS_IPNEEDSWAP(ip));
2130 }
2131
2132 static inline void
2133 db_assign(struct inode *ip, int loc, ufs2_daddr_t val)
2134 {
2135 if (ip->i_ump->um_fstype == UFS1)
2136 ip->i_ffs1_db[loc] = ufs_rw32(val, UFS_IPNEEDSWAP(ip));
2137 else
2138 ip->i_ffs2_db[loc] = ufs_rw64(val, UFS_IPNEEDSWAP(ip));
2139 }
2140
2141 static inline ufs2_daddr_t
2142 idb_get(struct inode *ip, void *bf, int loc)
2143 {
2144 if (ip->i_ump->um_fstype == UFS1)
2145 return ufs_rw32(((ufs1_daddr_t *)(bf))[loc],
2146 UFS_IPNEEDSWAP(ip));
2147 else
2148 return ufs_rw64(((ufs2_daddr_t *)(bf))[loc],
2149 UFS_IPNEEDSWAP(ip));
2150 }
2151
2152 static inline void
2153 idb_assign(struct inode *ip, void *bf, int loc, ufs2_daddr_t val)
2154 {
2155 if (ip->i_ump->um_fstype == UFS1)
2156 ((ufs1_daddr_t *)(bf))[loc] =
2157 ufs_rw32(val, UFS_IPNEEDSWAP(ip));
2158 else
2159 ((ufs2_daddr_t *)(bf))[loc] =
2160 ufs_rw64(val, UFS_IPNEEDSWAP(ip));
2161 }
2162