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