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