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