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