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