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