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