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