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