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