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