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