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