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