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