1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
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 [email protected]
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
38 #include <sys/cdefs.h>
39 #include "opt_quota.h"
40
41 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/systm.h>
44 #include <sys/conf.h>
45 #include <sys/gsb_crc32.h>
46 #include <sys/bio.h>
47 #include <sys/buf.h>
48 #include <sys/fcntl.h>
49 #include <sys/proc.h>
50 #include <sys/namei.h>
51 #include <sys/sched.h>
52 #include <sys/stat.h>
53 #include <sys/malloc.h>
54 #include <sys/mount.h>
55 #include <sys/resource.h>
56 #include <sys/resourcevar.h>
57 #include <sys/rwlock.h>
58 #include <sys/vnode.h>
59
60 #include <vm/vm.h>
61 #include <vm/vm_extern.h>
62
63 #include <geom/geom.h>
64 #include <geom/geom_vfs.h>
65
66 #include <ufs/ufs/extattr.h>
67 #include <ufs/ufs/quota.h>
68 #include <ufs/ufs/ufsmount.h>
69 #include <ufs/ufs/inode.h>
70 #include <ufs/ufs/ufs_extern.h>
71
72 #include <ufs/ffs/fs.h>
73 #include <ufs/ffs/ffs_extern.h>
74
75 #define KERNCRED thread0.td_ucred
76
77 #include "opt_ffs.h"
78
79 #ifdef NO_FFS_SNAPSHOT
80 int
ffs_snapshot(struct mount * mp,char * snapfile)81 ffs_snapshot(struct mount *mp, char *snapfile)
82 {
83 return (EINVAL);
84 }
85
86 int
ffs_snapblkfree(struct fs * fs,struct vnode * devvp,ufs2_daddr_t bno,long size,ino_t inum,__enum_uint8 (vtype)vtype,struct workhead * wkhd)87 ffs_snapblkfree(struct fs *fs,
88 struct vnode *devvp,
89 ufs2_daddr_t bno,
90 long size,
91 ino_t inum,
92 __enum_uint8(vtype) vtype,
93 struct workhead *wkhd)
94 {
95 return (EINVAL);
96 }
97
98 void
ffs_snapremove(struct vnode * vp)99 ffs_snapremove(struct vnode *vp)
100 {
101 }
102
103 void
ffs_snapshot_mount(struct mount * mp)104 ffs_snapshot_mount(struct mount *mp)
105 {
106 }
107
108 void
ffs_snapshot_unmount(struct mount * mp)109 ffs_snapshot_unmount(struct mount *mp)
110 {
111 }
112
113 void
ffs_snapgone(struct inode * ip)114 ffs_snapgone(struct inode *ip)
115 {
116 }
117
118 int
ffs_copyonwrite(struct vnode * devvp,struct buf * bp)119 ffs_copyonwrite(struct vnode *devvp, struct buf *bp)
120 {
121 return (EINVAL);
122 }
123
124 void
ffs_sync_snap(struct mount * mp,int waitfor)125 ffs_sync_snap(struct mount *mp, int waitfor)
126 {
127 }
128
129 #else
130 FEATURE(ffs_snapshot, "FFS snapshot support");
131
132 LIST_HEAD(, snapdata) snapfree;
133 static struct mtx snapfree_lock;
134 MTX_SYSINIT(ffs_snapfree, &snapfree_lock, "snapdata free list", MTX_DEF);
135
136 static int cgaccount(int, struct vnode *, struct buf *, int);
137 static int expunge_ufs1(struct vnode *, struct inode *, struct fs *,
138 int (*)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *, struct fs *,
139 ufs_lbn_t, int), int, int);
140 static int indiracct_ufs1(struct vnode *, struct vnode *, int,
141 ufs1_daddr_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, struct fs *,
142 int (*)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *, struct fs *,
143 ufs_lbn_t, int), int);
144 static int fullacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
145 struct fs *, ufs_lbn_t, int);
146 static int snapacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
147 struct fs *, ufs_lbn_t, int);
148 static int mapacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
149 struct fs *, ufs_lbn_t, int);
150 static int expunge_ufs2(struct vnode *, struct inode *, struct fs *,
151 int (*)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *, struct fs *,
152 ufs_lbn_t, int), int, int);
153 static int indiracct_ufs2(struct vnode *, struct vnode *, int,
154 ufs2_daddr_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, struct fs *,
155 int (*)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *, struct fs *,
156 ufs_lbn_t, int), int);
157 static int fullacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
158 struct fs *, ufs_lbn_t, int);
159 static int snapacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
160 struct fs *, ufs_lbn_t, int);
161 static int mapacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
162 struct fs *, ufs_lbn_t, int);
163 static int readblock(struct vnode *vp, struct buf *, ufs2_daddr_t);
164 static void try_free_snapdata(struct vnode *devvp);
165 static void revert_snaplock(struct vnode *, struct vnode *, struct snapdata *);
166 static struct snapdata *ffs_snapdata_acquire(struct vnode *devvp);
167 static int ffs_bp_snapblk(struct vnode *, struct buf *);
168
169 /*
170 * To ensure the consistency of snapshots across crashes, we must
171 * synchronously write out copied blocks before allowing the
172 * originals to be modified. Because of the rather severe speed
173 * penalty that this imposes, the code normally only ensures
174 * persistence for the filesystem metadata contained within a
175 * snapshot. Setting the following flag allows this crash
176 * persistence to be enabled for file contents.
177 */
178 int dopersistence = 0;
179
180 #ifdef DIAGNOSTIC
181 #include <sys/sysctl.h>
182 SYSCTL_INT(_debug, OID_AUTO, dopersistence, CTLFLAG_RW, &dopersistence, 0, "");
183 static int snapdebug = 0;
184 SYSCTL_INT(_debug, OID_AUTO, snapdebug, CTLFLAG_RW, &snapdebug, 0, "");
185 int collectsnapstats = 0;
186 SYSCTL_INT(_debug, OID_AUTO, collectsnapstats, CTLFLAG_RW, &collectsnapstats,
187 0, "");
188 #endif /* DIAGNOSTIC */
189
190 /*
191 * Create a snapshot file and initialize it for the filesystem.
192 */
193 int
ffs_snapshot(struct mount * mp,char * snapfile)194 ffs_snapshot(struct mount *mp, char *snapfile)
195 {
196 ufs2_daddr_t numblks, blkno, *blkp, *snapblklist;
197 int error, cg, snaploc;
198 int i, size, len, loc;
199 ufs2_daddr_t blockno;
200 uint64_t flag;
201 char saved_nice = 0;
202 #ifdef DIAGNOSTIC
203 long redo = 0;
204 #endif
205 long snaplistsize = 0;
206 int32_t *lp;
207 void *space;
208 struct fs *copy_fs = NULL, *fs, *bpfs;
209 struct thread *td = curthread;
210 struct inode *ip, *xp;
211 struct buf *bp, *nbp, *ibp;
212 struct nameidata nd;
213 struct mount *wrtmp;
214 struct vattr vat;
215 struct vnode *vp, *xvp, *mvp, *devvp;
216 struct uio auio;
217 struct iovec aiov;
218 struct snapdata *sn;
219 struct ufsmount *ump;
220 #ifdef DIAGNOSTIC
221 struct timespec starttime = {0, 0}, endtime;
222 #endif
223
224 ump = VFSTOUFS(mp);
225 fs = ump->um_fs;
226 sn = NULL;
227 /*
228 * At the moment, filesystems using gjournal cannot support
229 * taking snapshots.
230 */
231 if ((mp->mnt_flag & MNT_GJOURNAL) != 0) {
232 vfs_mount_error(mp, "%s: Snapshots are not yet supported when "
233 "using gjournal", fs->fs_fsmnt);
234 return (EOPNOTSUPP);
235 }
236 MNT_ILOCK(mp);
237 flag = mp->mnt_flag;
238 MNT_IUNLOCK(mp);
239 /*
240 * Need to serialize access to snapshot code per filesystem.
241 */
242 /*
243 * Assign a snapshot slot in the superblock.
244 */
245 UFS_LOCK(ump);
246 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
247 if (fs->fs_snapinum[snaploc] == 0)
248 break;
249 UFS_UNLOCK(ump);
250 if (snaploc == FSMAXSNAP)
251 return (ENOSPC);
252 /*
253 * Create the snapshot file.
254 */
255 restart:
256 NDINIT(&nd, CREATE, LOCKPARENT | LOCKLEAF | NOCACHE, UIO_SYSSPACE,
257 snapfile);
258 if ((error = namei(&nd)) != 0)
259 return (error);
260 if (nd.ni_vp != NULL) {
261 vput(nd.ni_vp);
262 error = EEXIST;
263 }
264 if (nd.ni_dvp->v_mount != mp)
265 error = EXDEV;
266 if (error) {
267 NDFREE_PNBUF(&nd);
268 if (nd.ni_dvp == nd.ni_vp)
269 vrele(nd.ni_dvp);
270 else
271 vput(nd.ni_dvp);
272 return (error);
273 }
274 VATTR_NULL(&vat);
275 vat.va_type = VREG;
276 vat.va_mode = S_IRUSR;
277 vat.va_vaflags |= VA_EXCLUSIVE;
278 if (VOP_GETWRITEMOUNT(nd.ni_dvp, &wrtmp))
279 wrtmp = NULL;
280 if (wrtmp != mp)
281 panic("ffs_snapshot: mount mismatch");
282 vfs_rel(wrtmp);
283 if (vn_start_write(NULL, &wrtmp, V_NOWAIT) != 0) {
284 NDFREE_PNBUF(&nd);
285 vput(nd.ni_dvp);
286 if ((error = vn_start_write(NULL, &wrtmp,
287 V_XSLEEP | PCATCH)) != 0)
288 return (error);
289 goto restart;
290 }
291 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vat);
292 if (error) {
293 VOP_VPUT_PAIR(nd.ni_dvp, NULL, true);
294 NDFREE_PNBUF(&nd);
295 vn_finished_write(wrtmp);
296 if (error == ERELOOKUP)
297 goto restart;
298 return (error);
299 }
300 vp = nd.ni_vp;
301 vref(nd.ni_dvp);
302 VOP_VPUT_PAIR(nd.ni_dvp, &vp, false);
303 if (VN_IS_DOOMED(vp)) {
304 error = EBADF;
305 goto out;
306 }
307 vnode_create_vobject(nd.ni_vp, fs->fs_size, td);
308 vp->v_vflag |= VV_SYSTEM;
309 ip = VTOI(vp);
310 devvp = ITODEVVP(ip);
311 /*
312 * Calculate the size of the filesystem then allocate the block
313 * immediately following the last block of the filesystem that
314 * will contain the snapshot list. This operation allows us to
315 * set the size of the snapshot.
316 */
317 numblks = howmany(fs->fs_size, fs->fs_frag);
318 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)numblks),
319 fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
320 if (error)
321 goto out;
322 bawrite(bp);
323 ip->i_size = lblktosize(fs, (off_t)(numblks + 1));
324 vnode_pager_setsize(vp, ip->i_size);
325 DIP_SET(ip, i_size, ip->i_size);
326 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
327 /*
328 * Preallocate critical data structures so that we can copy
329 * them in without further allocation after we suspend all
330 * operations on the filesystem. We would like to just release
331 * the allocated buffers without writing them since they will
332 * be filled in below once we are ready to go, but this upsets
333 * the soft update code, so we go ahead and write the new buffers.
334 *
335 * Allocate all indirect blocks and mark all of them as not
336 * needing to be copied.
337 */
338 for (blkno = UFS_NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
339 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)blkno),
340 fs->fs_bsize, td->td_ucred, BA_METAONLY, &ibp);
341 if (error)
342 goto out;
343 bawrite(ibp);
344 }
345 /*
346 * Allocate copies for the superblock and its summary information.
347 */
348 error = UFS_BALLOC(vp, fs->fs_sblockloc, fs->fs_sbsize, KERNCRED,
349 0, &nbp);
350 if (error)
351 goto out;
352 bawrite(nbp);
353 blkno = fragstoblks(fs, fs->fs_csaddr);
354 len = howmany(fs->fs_cssize, fs->fs_bsize);
355 for (loc = 0; loc < len; loc++) {
356 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(blkno + loc)),
357 fs->fs_bsize, KERNCRED, 0, &nbp);
358 if (error)
359 goto out;
360 bawrite(nbp);
361 }
362 /*
363 * Allocate all cylinder group blocks.
364 */
365 for (cg = 0; cg < fs->fs_ncg; cg++) {
366 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
367 fs->fs_bsize, KERNCRED, 0, &nbp);
368 if (error)
369 goto out;
370 bawrite(nbp);
371 if (cg % 10 == 0) {
372 error = ffs_syncvnode(vp, MNT_WAIT, 0);
373 /* vp possibly reclaimed if unlocked */
374 if (error != 0)
375 goto out;
376 }
377 }
378 /*
379 * Change inode to snapshot type file. Before setting its block
380 * pointers to BLK_SNAP and BLK_NOCOPY in cgaccount, we have to
381 * set its type to SF_SNAPSHOT so that VOP_REMOVE will know that
382 * they need to be rolled back before attempting deletion.
383 */
384 ip->i_flags |= SF_SNAPSHOT;
385 DIP_SET(ip, i_flags, ip->i_flags);
386 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
387 /*
388 * Copy all the cylinder group maps. Although the
389 * filesystem is still active, we hope that only a few
390 * cylinder groups will change between now and when we
391 * suspend operations. Thus, we will be able to quickly
392 * touch up the few cylinder groups that changed during
393 * the suspension period.
394 */
395 len = roundup2(howmany(fs->fs_ncg, NBBY), sizeof(uint64_t));
396 space = malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
397 UFS_LOCK(ump);
398 fs->fs_active = space;
399 UFS_UNLOCK(ump);
400 for (cg = 0; cg < fs->fs_ncg; cg++) {
401 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
402 fs->fs_bsize, KERNCRED, 0, &nbp);
403 if (error)
404 goto out;
405 error = cgaccount(cg, vp, nbp, 1);
406 bawrite(nbp);
407 if (cg % 10 == 0 && error == 0)
408 error = ffs_syncvnode(vp, MNT_WAIT, 0);
409 if (error)
410 goto out;
411 }
412 /*
413 * Ensure that the snapshot is completely on disk.
414 * Since we have marked it as a snapshot it is safe to
415 * unlock it as no process will be allowed to write to it.
416 */
417 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0)) != 0)
418 goto out;
419 VOP_UNLOCK(vp);
420 /*
421 * All allocations are done, so we can now snapshot the system.
422 *
423 * Recind nice scheduling while running with the filesystem suspended.
424 */
425 if (td->td_proc->p_nice > 0) {
426 struct proc *p;
427
428 p = td->td_proc;
429 PROC_LOCK(p);
430 saved_nice = p->p_nice;
431 sched_nice(p, 0);
432 PROC_UNLOCK(p);
433 }
434 /*
435 * Suspend operation on filesystem.
436 */
437 for (;;) {
438 vn_finished_write(wrtmp);
439 if ((error = vfs_write_suspend(vp->v_mount, 0)) != 0) {
440 vn_start_write(NULL, &wrtmp, V_WAIT);
441 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
442 goto out;
443 }
444 if (mp->mnt_kern_flag & MNTK_SUSPENDED)
445 break;
446 vn_start_write(NULL, &wrtmp, V_WAIT);
447 }
448 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
449 if (ip->i_effnlink == 0) {
450 error = ENOENT; /* Snapshot file unlinked */
451 goto resumefs;
452 }
453 #ifdef DIAGNOSTIC
454 if (collectsnapstats)
455 nanotime(&starttime);
456 #endif
457
458 /*
459 * First, copy all the cylinder group maps that have changed.
460 */
461 for (cg = 0; cg < fs->fs_ncg; cg++) {
462 if ((ACTIVECGNUM(fs, cg) & ACTIVECGOFF(cg)) != 0)
463 continue;
464 #ifdef DIAGNOSTIC
465 redo++;
466 #endif
467 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
468 fs->fs_bsize, KERNCRED, 0, &nbp);
469 if (error)
470 goto resumefs;
471 error = cgaccount(cg, vp, nbp, 2);
472 bawrite(nbp);
473 if (error)
474 goto resumefs;
475 }
476 /*
477 * Grab a copy of the superblock and its summary information.
478 * We delay writing it until the suspension is released below.
479 */
480 copy_fs = malloc((uint64_t)fs->fs_bsize, M_UFSMNT, M_WAITOK);
481 bcopy(fs, copy_fs, fs->fs_sbsize);
482 copy_fs->fs_si = malloc(sizeof(struct fs_summary_info), M_UFSMNT,
483 M_ZERO | M_WAITOK);
484 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
485 copy_fs->fs_clean = 1;
486 size = fs->fs_bsize < SBLOCKSIZE ? fs->fs_bsize : SBLOCKSIZE;
487 if (fs->fs_sbsize < size)
488 bzero(&((char *)copy_fs)[fs->fs_sbsize],
489 size - fs->fs_sbsize);
490 size = blkroundup(fs, fs->fs_cssize);
491 if (fs->fs_contigsumsize > 0)
492 size += fs->fs_ncg * sizeof(int32_t);
493 space = malloc((uint64_t)size, M_UFSMNT, M_WAITOK);
494 copy_fs->fs_csp = space;
495 bcopy(fs->fs_csp, copy_fs->fs_csp, fs->fs_cssize);
496 space = (char *)space + fs->fs_cssize;
497 loc = howmany(fs->fs_cssize, fs->fs_fsize);
498 i = fs->fs_frag - loc % fs->fs_frag;
499 len = (i == fs->fs_frag) ? 0 : i * fs->fs_fsize;
500 if (len > 0) {
501 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + loc),
502 len, KERNCRED, &bp)) != 0) {
503 brelse(bp);
504 goto resumefs;
505 }
506 bcopy(bp->b_data, space, (uint64_t)len);
507 space = (char *)space + len;
508 bp->b_flags |= B_INVAL | B_NOCACHE;
509 brelse(bp);
510 }
511 if (fs->fs_contigsumsize > 0) {
512 copy_fs->fs_maxcluster = lp = space;
513 for (i = 0; i < fs->fs_ncg; i++)
514 *lp++ = fs->fs_contigsumsize;
515 }
516 /*
517 * We must check for active files that have been unlinked
518 * (e.g., with a zero link count). We have to expunge all
519 * trace of these files from the snapshot so that they are
520 * not reclaimed prematurely by fsck or unnecessarily dumped.
521 * We turn off the MNTK_SUSPENDED flag to avoid a panic from
522 * spec_strategy about writing on a suspended filesystem.
523 * Note that we skip unlinked snapshot files as they will
524 * be handled separately below.
525 *
526 * We also calculate the size needed for the snapshot list.
527 * Initial number of entries is composed of:
528 * - one for each cylinder group map
529 * - one for each block used by superblock summary table
530 * - one for each snapshot inode block
531 * - one for the superblock
532 * - one for the snapshot list
533 * The direct block entries in the snapshot are always
534 * copied (see reason below). Note that the superblock and
535 * the first cylinder group will almost always be allocated
536 * in the direct blocks, but we add the slop for them in case
537 * they do not end up there. The snapshot list size may get
538 * expanded by one because of an update of an inode block for
539 * an unlinked but still open file when it is expunged.
540 *
541 * Because the direct block pointers are always copied, they
542 * are not added to the list. Instead ffs_copyonwrite()
543 * explicitly checks for them before checking the snapshot list.
544 */
545 snaplistsize = fs->fs_ncg + howmany(fs->fs_cssize, fs->fs_bsize) +
546 FSMAXSNAP + /* superblock */ 1 + /* snaplist */ 1;
547 MNT_ILOCK(mp);
548 mp->mnt_kern_flag &= ~MNTK_SUSPENDED;
549 MNT_IUNLOCK(mp);
550 loop:
551 MNT_VNODE_FOREACH_ALL(xvp, mp, mvp) {
552 if ((xvp->v_usecount == 0 &&
553 (xvp->v_iflag & (VI_OWEINACT | VI_DOINGINACT)) == 0) ||
554 xvp->v_type == VNON ||
555 IS_SNAPSHOT(VTOI(xvp))) {
556 VI_UNLOCK(xvp);
557 continue;
558 }
559 /*
560 * We can skip parent directory vnode because it must have
561 * this snapshot file in it.
562 */
563 if (xvp == nd.ni_dvp) {
564 VI_UNLOCK(xvp);
565 continue;
566 }
567 vholdl(xvp);
568 if (vn_lock(xvp, LK_EXCLUSIVE | LK_INTERLOCK) != 0) {
569 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
570 vdrop(xvp);
571 goto loop;
572 }
573 VI_LOCK(xvp);
574 if (xvp->v_usecount == 0 &&
575 (xvp->v_iflag & (VI_OWEINACT | VI_DOINGINACT)) == 0) {
576 VI_UNLOCK(xvp);
577 VOP_UNLOCK(xvp);
578 vdrop(xvp);
579 continue;
580 }
581 VI_UNLOCK(xvp);
582 #ifdef DIAGNOSTIC
583 if (snapdebug)
584 vn_printf(xvp, "ffs_snapshot: busy vnode ");
585 #endif
586 if (VOP_GETATTR(xvp, &vat, td->td_ucred) == 0 &&
587 vat.va_nlink > 0) {
588 VOP_UNLOCK(xvp);
589 vdrop(xvp);
590 continue;
591 }
592 xp = VTOI(xvp);
593 if (ffs_checkfreefile(copy_fs, vp, xp->i_number)) {
594 VOP_UNLOCK(xvp);
595 vdrop(xvp);
596 continue;
597 }
598 /*
599 * If there is a fragment, clear it here.
600 */
601 blkno = 0;
602 loc = howmany(xp->i_size, fs->fs_bsize) - 1;
603 if (loc < UFS_NDADDR) {
604 len = fragroundup(fs, blkoff(fs, xp->i_size));
605 if (len != 0 && len < fs->fs_bsize) {
606 ffs_blkfree(ump, copy_fs, vp,
607 DIP(xp, i_db[loc]), len, xp->i_number,
608 xvp->v_type, NULL, SINGLETON_KEY);
609 blkno = DIP(xp, i_db[loc]);
610 DIP_SET(xp, i_db[loc], 0);
611 }
612 }
613 snaplistsize += 1;
614 if (I_IS_UFS1(xp))
615 error = expunge_ufs1(vp, xp, copy_fs, fullacct_ufs1,
616 BLK_NOCOPY, 1);
617 else
618 error = expunge_ufs2(vp, xp, copy_fs, fullacct_ufs2,
619 BLK_NOCOPY, 1);
620 if (blkno)
621 DIP_SET(xp, i_db[loc], blkno);
622 if (!error)
623 error = ffs_freefile(ump, copy_fs, vp, xp->i_number,
624 xp->i_mode, NULL);
625 VOP_UNLOCK(xvp);
626 vdrop(xvp);
627 if (error) {
628 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
629 goto resumefs;
630 }
631 }
632 /*
633 * Erase the journal file from the snapshot.
634 */
635 if (fs->fs_flags & FS_SUJ) {
636 error = softdep_journal_lookup(mp, &xvp);
637 if (error)
638 goto resumefs;
639 xp = VTOI(xvp);
640 if (I_IS_UFS1(xp))
641 error = expunge_ufs1(vp, xp, copy_fs, fullacct_ufs1,
642 BLK_NOCOPY, 0);
643 else
644 error = expunge_ufs2(vp, xp, copy_fs, fullacct_ufs2,
645 BLK_NOCOPY, 0);
646 vput(xvp);
647 }
648 /*
649 * Preallocate all the direct blocks in the snapshot inode so
650 * that we never have to write the inode itself to commit an
651 * update to the contents of the snapshot. Note that once
652 * created, the size of the snapshot will never change, so
653 * there will never be a need to write the inode except to
654 * update the non-integrity-critical time fields and
655 * allocated-block count.
656 */
657 for (blockno = 0; blockno < UFS_NDADDR; blockno++) {
658 if (DIP(ip, i_db[blockno]) != 0)
659 continue;
660 error = UFS_BALLOC(vp, lblktosize(fs, blockno),
661 fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
662 if (error)
663 goto resumefs;
664 error = readblock(vp, bp, blockno);
665 bawrite(bp);
666 if (error != 0)
667 goto resumefs;
668 }
669 /*
670 * Acquire a lock on the snapdata structure, creating it if necessary.
671 */
672 sn = ffs_snapdata_acquire(devvp);
673 /*
674 * Change vnode to use shared snapshot lock instead of the original
675 * private lock.
676 */
677 vp->v_vnlock = &sn->sn_lock;
678 lockmgr(&vp->v_lock, LK_RELEASE, NULL);
679 xp = TAILQ_FIRST(&sn->sn_head);
680 /*
681 * If this is the first snapshot on this filesystem, then we need
682 * to allocate the space for the list of preallocated snapshot blocks.
683 * This list will be refined below, but this preliminary one will
684 * keep us out of deadlock until the full one is ready.
685 */
686 if (xp == NULL) {
687 snapblklist = malloc(snaplistsize * sizeof(daddr_t),
688 M_UFSMNT, M_WAITOK);
689 blkp = &snapblklist[1];
690 *blkp++ = lblkno(fs, fs->fs_sblockloc);
691 blkno = fragstoblks(fs, fs->fs_csaddr);
692 for (cg = 0; cg < fs->fs_ncg; cg++) {
693 if (fragstoblks(fs, cgtod(fs, cg)) > blkno)
694 break;
695 *blkp++ = fragstoblks(fs, cgtod(fs, cg));
696 }
697 len = howmany(fs->fs_cssize, fs->fs_bsize);
698 for (loc = 0; loc < len; loc++)
699 *blkp++ = blkno + loc;
700 for (; cg < fs->fs_ncg; cg++)
701 *blkp++ = fragstoblks(fs, cgtod(fs, cg));
702 snapblklist[0] = blkp - snapblklist;
703 VI_LOCK(devvp);
704 if (sn->sn_blklist != NULL)
705 panic("ffs_snapshot: non-empty list");
706 sn->sn_blklist = snapblklist;
707 sn->sn_listsize = blkp - snapblklist;
708 VI_UNLOCK(devvp);
709 }
710 /*
711 * Record snapshot inode. Since this is the newest snapshot,
712 * it must be placed at the end of the list.
713 */
714 VI_LOCK(devvp);
715 fs->fs_snapinum[snaploc] = ip->i_number;
716 if (ip->i_nextsnap.tqe_prev != 0)
717 panic("ffs_snapshot: %ju already on list",
718 (uintmax_t)ip->i_number);
719 TAILQ_INSERT_TAIL(&sn->sn_head, ip, i_nextsnap);
720 devvp->v_vflag |= VV_COPYONWRITE;
721 VI_UNLOCK(devvp);
722 resumefs:
723 ASSERT_VOP_LOCKED(vp, "ffs_snapshot vp");
724 if (error != 0 && copy_fs != NULL) {
725 free(copy_fs->fs_csp, M_UFSMNT);
726 free(copy_fs->fs_si, M_UFSMNT);
727 free(copy_fs, M_UFSMNT);
728 copy_fs = NULL;
729 }
730 KASSERT(error != 0 || (sn != NULL && copy_fs != NULL),
731 ("missing snapshot setup parameters"));
732 /*
733 * Resume operation on filesystem.
734 */
735 vfs_write_resume(vp->v_mount, VR_START_WRITE | VR_NO_SUSPCLR);
736 #ifdef DIAGNOSTIC
737 if (collectsnapstats && starttime.tv_sec > 0) {
738 nanotime(&endtime);
739 timespecsub(&endtime, &starttime, &endtime);
740 printf("%s: suspended %ld.%03ld sec, redo %ld of %d\n",
741 vp->v_mount->mnt_stat.f_mntonname, (long)endtime.tv_sec,
742 endtime.tv_nsec / 1000000, redo, fs->fs_ncg);
743 }
744 #endif
745 if (copy_fs == NULL)
746 goto out;
747 /*
748 * Copy allocation information from all the snapshots in
749 * this snapshot and then expunge them from its view.
750 */
751 TAILQ_FOREACH(xp, &sn->sn_head, i_nextsnap) {
752 if (xp == ip)
753 break;
754 if (I_IS_UFS1(xp))
755 error = expunge_ufs1(vp, xp, fs, snapacct_ufs1,
756 BLK_SNAP, 0);
757 else
758 error = expunge_ufs2(vp, xp, fs, snapacct_ufs2,
759 BLK_SNAP, 0);
760 if (error == 0 && xp->i_effnlink == 0) {
761 error = ffs_freefile(ump,
762 copy_fs,
763 vp,
764 xp->i_number,
765 xp->i_mode, NULL);
766 }
767 if (error) {
768 fs->fs_snapinum[snaploc] = 0;
769 goto done;
770 }
771 }
772 /*
773 * Allocate space for the full list of preallocated snapshot blocks.
774 */
775 snapblklist = malloc(snaplistsize * sizeof(daddr_t),
776 M_UFSMNT, M_WAITOK);
777 ip->i_snapblklist = &snapblklist[1];
778 /*
779 * Expunge the blocks used by the snapshots from the set of
780 * blocks marked as used in the snapshot bitmaps. Also, collect
781 * the list of allocated blocks in i_snapblklist.
782 */
783 if (I_IS_UFS1(ip))
784 error = expunge_ufs1(vp, ip, copy_fs, mapacct_ufs1,
785 BLK_SNAP, 0);
786 else
787 error = expunge_ufs2(vp, ip, copy_fs, mapacct_ufs2,
788 BLK_SNAP, 0);
789 if (error) {
790 fs->fs_snapinum[snaploc] = 0;
791 free(snapblklist, M_UFSMNT);
792 goto done;
793 }
794 if (snaplistsize < ip->i_snapblklist - snapblklist)
795 panic("ffs_snapshot: list too small");
796 snaplistsize = ip->i_snapblklist - snapblklist;
797 snapblklist[0] = snaplistsize;
798 ip->i_snapblklist = 0;
799 /*
800 * Write out the list of allocated blocks to the end of the snapshot.
801 */
802 auio.uio_iov = &aiov;
803 auio.uio_iovcnt = 1;
804 aiov.iov_base = (void *)snapblklist;
805 aiov.iov_len = snaplistsize * sizeof(daddr_t);
806 auio.uio_resid = aiov.iov_len;
807 auio.uio_offset = lblktosize(fs, (off_t)numblks);
808 auio.uio_segflg = UIO_SYSSPACE;
809 auio.uio_rw = UIO_WRITE;
810 auio.uio_td = td;
811 if ((error = VOP_WRITE(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
812 fs->fs_snapinum[snaploc] = 0;
813 free(snapblklist, M_UFSMNT);
814 goto done;
815 }
816 /*
817 * Write the superblock and its summary information
818 * to the snapshot.
819 */
820 blkno = fragstoblks(fs, fs->fs_csaddr);
821 len = howmany(fs->fs_cssize, fs->fs_bsize);
822 space = copy_fs->fs_csp;
823 for (loc = 0; loc < len; loc++) {
824 error = bread(vp, blkno + loc, fs->fs_bsize, KERNCRED, &nbp);
825 if (error) {
826 fs->fs_snapinum[snaploc] = 0;
827 free(snapblklist, M_UFSMNT);
828 goto done;
829 }
830 bcopy(space, nbp->b_data, fs->fs_bsize);
831 space = (char *)space + fs->fs_bsize;
832 bawrite(nbp);
833 }
834 error = bread(vp, lblkno(fs, fs->fs_sblockloc), fs->fs_bsize,
835 KERNCRED, &nbp);
836 if (error) {
837 brelse(nbp);
838 } else {
839 loc = blkoff(fs, fs->fs_sblockloc);
840 copy_fs->fs_fmod = 0;
841 bpfs = (struct fs *)&nbp->b_data[loc];
842 bcopy((caddr_t)copy_fs, (caddr_t)bpfs, (uint64_t)fs->fs_sbsize);
843 ffs_oldfscompat_write(bpfs, ump);
844 bpfs->fs_ckhash = ffs_calc_sbhash(bpfs);
845 bawrite(nbp);
846 }
847 /*
848 * As this is the newest list, it is the most inclusive, so
849 * should replace the previous list.
850 */
851 VI_LOCK(devvp);
852 space = sn->sn_blklist;
853 sn->sn_blklist = snapblklist;
854 sn->sn_listsize = snaplistsize;
855 VI_UNLOCK(devvp);
856 if (space != NULL)
857 free(space, M_UFSMNT);
858 done:
859 free(copy_fs->fs_csp, M_UFSMNT);
860 free(copy_fs->fs_si, M_UFSMNT);
861 free(copy_fs, M_UFSMNT);
862 copy_fs = NULL;
863 out:
864 if (saved_nice > 0) {
865 struct proc *p;
866
867 p = td->td_proc;
868 PROC_LOCK(p);
869 sched_nice(td->td_proc, saved_nice);
870 PROC_UNLOCK(td->td_proc);
871 }
872 UFS_LOCK(ump);
873 if (fs->fs_active != 0) {
874 free(fs->fs_active, M_DEVBUF);
875 fs->fs_active = 0;
876 }
877 UFS_UNLOCK(ump);
878 MNT_ILOCK(mp);
879 mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | (flag & ~MNT_QUOTA);
880 MNT_IUNLOCK(mp);
881 NDFREE_PNBUF(&nd);
882 vrele(nd.ni_dvp);
883 if (error == 0) {
884 (void) ffs_syncvnode(vp, MNT_WAIT, 0);
885 VOP_UNLOCK(vp);
886 } else if (VN_IS_DOOMED(vp)) {
887 vput(vp);
888 } else {
889 int rmerr;
890
891 /* Remove snapshot as its creation has failed. */
892 vput(vp);
893 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_SYSSPACE,
894 snapfile);
895 if ((rmerr = namei(&nd)) != 0 ||
896 (rmerr = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd)) != 0)
897 printf("Delete of %s failed with error %d\n",
898 nd.ni_dirp, rmerr);
899 NDFREE_PNBUF(&nd);
900 if (nd.ni_dvp != NULL)
901 vput(nd.ni_dvp);
902 if (nd.ni_vp != NULL)
903 vput(nd.ni_vp);
904 }
905 vn_finished_write(wrtmp);
906 process_deferred_inactive(mp);
907 return (error);
908 }
909
910 /*
911 * Copy a cylinder group map. All the unallocated blocks are marked
912 * BLK_NOCOPY so that the snapshot knows that it need not copy them
913 * if they are later written. If passno is one, then this is a first
914 * pass, so only setting needs to be done. If passno is 2, then this
915 * is a revision to a previous pass which must be undone as the
916 * replacement pass is done.
917 */
918 static int
cgaccount(int cg,struct vnode * vp,struct buf * nbp,int passno)919 cgaccount(int cg,
920 struct vnode *vp,
921 struct buf *nbp,
922 int passno)
923 {
924 struct buf *bp, *ibp;
925 struct inode *ip;
926 struct cg *cgp;
927 struct fs *fs;
928 ufs2_daddr_t base, numblks;
929 int error, len, loc, indiroff;
930
931 ip = VTOI(vp);
932 fs = ITOFS(ip);
933 if ((error = ffs_getcg(fs, ITODEVVP(ip), cg, 0, &bp, &cgp)) != 0)
934 return (error);
935 UFS_LOCK(ITOUMP(ip));
936 ACTIVESET(fs, cg);
937 /*
938 * Recomputation of summary information might not have been performed
939 * at mount time. Sync up summary information for current cylinder
940 * group while data is in memory to ensure that result of background
941 * fsck is slightly more consistent.
942 */
943 fs->fs_cs(fs, cg) = cgp->cg_cs;
944 UFS_UNLOCK(ITOUMP(ip));
945 bcopy(bp->b_data, nbp->b_data, fs->fs_cgsize);
946 if (fs->fs_cgsize < fs->fs_bsize)
947 bzero(&nbp->b_data[fs->fs_cgsize],
948 fs->fs_bsize - fs->fs_cgsize);
949 cgp = (struct cg *)nbp->b_data;
950 bqrelse(bp);
951 if (passno == 2)
952 nbp->b_flags |= B_VALIDSUSPWRT;
953 numblks = howmany(fs->fs_size, fs->fs_frag);
954 len = howmany(fs->fs_fpg, fs->fs_frag);
955 base = cgbase(fs, cg) / fs->fs_frag;
956 if (base + len >= numblks)
957 len = numblks - base - 1;
958 loc = 0;
959 if (base < UFS_NDADDR) {
960 for ( ; loc < UFS_NDADDR; loc++) {
961 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
962 DIP_SET(ip, i_db[loc], BLK_NOCOPY);
963 else if (passno == 2 && DIP(ip, i_db[loc])== BLK_NOCOPY)
964 DIP_SET(ip, i_db[loc], 0);
965 else if (passno == 1 && DIP(ip, i_db[loc])== BLK_NOCOPY)
966 panic("ffs_snapshot: lost direct block");
967 }
968 }
969 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(base + loc)),
970 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
971 if (error) {
972 goto out;
973 }
974 indiroff = (base + loc - UFS_NDADDR) % NINDIR(fs);
975 for ( ; loc < len; loc++, indiroff++) {
976 if (indiroff >= NINDIR(fs)) {
977 if (passno == 2)
978 ibp->b_flags |= B_VALIDSUSPWRT;
979 bawrite(ibp);
980 error = UFS_BALLOC(vp,
981 lblktosize(fs, (off_t)(base + loc)),
982 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
983 if (error) {
984 goto out;
985 }
986 indiroff = 0;
987 }
988 if (I_IS_UFS1(ip)) {
989 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
990 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] =
991 BLK_NOCOPY;
992 else if (passno == 2 && ((ufs1_daddr_t *)(ibp->b_data))
993 [indiroff] == BLK_NOCOPY)
994 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] = 0;
995 else if (passno == 1 && ((ufs1_daddr_t *)(ibp->b_data))
996 [indiroff] == BLK_NOCOPY)
997 panic("ffs_snapshot: lost indirect block");
998 continue;
999 }
1000 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
1001 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = BLK_NOCOPY;
1002 else if (passno == 2 &&
1003 ((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY)
1004 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = 0;
1005 else if (passno == 1 &&
1006 ((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY)
1007 panic("ffs_snapshot: lost indirect block");
1008 }
1009 if (passno == 2)
1010 ibp->b_flags |= B_VALIDSUSPWRT;
1011 bdwrite(ibp);
1012 out:
1013 /*
1014 * We have to calculate the crc32c here rather than just setting the
1015 * BX_CYLGRP b_xflags because the allocation of the block for the
1016 * the cylinder group map will always be a full size block (fs_bsize)
1017 * even though the cylinder group may be smaller (fs_cgsize). The
1018 * crc32c must be computed only over fs_cgsize whereas the BX_CYLGRP
1019 * flag causes it to be computed over the size of the buffer.
1020 */
1021 if ((fs->fs_metackhash & CK_CYLGRP) != 0) {
1022 ((struct cg *)nbp->b_data)->cg_ckhash = 0;
1023 ((struct cg *)nbp->b_data)->cg_ckhash =
1024 calculate_crc32c(~0L, nbp->b_data, fs->fs_cgsize);
1025 }
1026 return (error);
1027 }
1028
1029 /*
1030 * Before expunging a snapshot inode, note all the
1031 * blocks that it claims with BLK_SNAP so that fsck will
1032 * be able to account for those blocks properly and so
1033 * that this snapshot knows that it need not copy them
1034 * if the other snapshot holding them is freed. This code
1035 * is reproduced once each for UFS1 and UFS2.
1036 */
1037 static int
expunge_ufs1(struct vnode * snapvp,struct inode * cancelip,struct fs * fs,int (* acctfunc)(struct vnode *,ufs1_daddr_t *,ufs1_daddr_t *,struct fs *,ufs_lbn_t,int),int expungetype,int clearmode)1038 expunge_ufs1(struct vnode *snapvp,
1039 struct inode *cancelip,
1040 struct fs *fs,
1041 int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
1042 struct fs *, ufs_lbn_t, int),
1043 int expungetype,
1044 int clearmode)
1045 {
1046 int i, error, indiroff;
1047 ufs_lbn_t lbn, rlbn;
1048 ufs2_daddr_t len, blkno, numblks, blksperindir;
1049 struct ufs1_dinode *dip;
1050 struct thread *td = curthread;
1051 struct buf *bp;
1052
1053 /*
1054 * Prepare to expunge the inode. If its inode block has not
1055 * yet been copied, then allocate and fill the copy.
1056 */
1057 lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
1058 blkno = 0;
1059 if (lbn < UFS_NDADDR) {
1060 blkno = VTOI(snapvp)->i_din1->di_db[lbn];
1061 } else {
1062 if (DOINGSOFTDEP(snapvp))
1063 softdep_prealloc(snapvp, MNT_WAIT);
1064 td->td_pflags |= TDP_COWINPROGRESS;
1065 error = ffs_balloc_ufs1(snapvp, lblktosize(fs, (off_t)lbn),
1066 fs->fs_bsize, KERNCRED, BA_METAONLY, &bp);
1067 td->td_pflags &= ~TDP_COWINPROGRESS;
1068 if (error)
1069 return (error);
1070 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs);
1071 blkno = ((ufs1_daddr_t *)(bp->b_data))[indiroff];
1072 bqrelse(bp);
1073 }
1074 if (blkno != 0) {
1075 if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp)))
1076 return (error);
1077 } else {
1078 error = ffs_balloc_ufs1(snapvp, lblktosize(fs, (off_t)lbn),
1079 fs->fs_bsize, KERNCRED, 0, &bp);
1080 if (error)
1081 return (error);
1082 if ((error = readblock(snapvp, bp, lbn)) != 0)
1083 return (error);
1084 }
1085 /*
1086 * Set a snapshot inode to be a zero length file, regular files
1087 * or unlinked snapshots to be completely unallocated.
1088 */
1089 dip = (struct ufs1_dinode *)bp->b_data +
1090 ino_to_fsbo(fs, cancelip->i_number);
1091 if (clearmode || cancelip->i_effnlink == 0)
1092 dip->di_mode = 0;
1093 dip->di_size = 0;
1094 dip->di_blocks = 0;
1095 dip->di_flags &= ~SF_SNAPSHOT;
1096 bzero(dip->di_db, UFS_NDADDR * sizeof(ufs1_daddr_t));
1097 bzero(dip->di_ib, UFS_NIADDR * sizeof(ufs1_daddr_t));
1098 bdwrite(bp);
1099 /*
1100 * Now go through and expunge all the blocks in the file
1101 * using the function requested.
1102 */
1103 numblks = howmany(cancelip->i_size, fs->fs_bsize);
1104 if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_db[0],
1105 &cancelip->i_din1->di_db[UFS_NDADDR], fs, 0, expungetype)))
1106 return (error);
1107 if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_ib[0],
1108 &cancelip->i_din1->di_ib[UFS_NIADDR], fs, -1, expungetype)))
1109 return (error);
1110 blksperindir = 1;
1111 lbn = -UFS_NDADDR;
1112 len = numblks - UFS_NDADDR;
1113 rlbn = UFS_NDADDR;
1114 for (i = 0; len > 0 && i < UFS_NIADDR; i++) {
1115 error = indiracct_ufs1(snapvp, ITOV(cancelip), i,
1116 cancelip->i_din1->di_ib[i], lbn, rlbn, len,
1117 blksperindir, fs, acctfunc, expungetype);
1118 if (error)
1119 return (error);
1120 blksperindir *= NINDIR(fs);
1121 lbn -= blksperindir + 1;
1122 len -= blksperindir;
1123 rlbn += blksperindir;
1124 }
1125 return (0);
1126 }
1127
1128 /*
1129 * Descend an indirect block chain for vnode cancelvp accounting for all
1130 * its indirect blocks in snapvp.
1131 */
1132 static int
indiracct_ufs1(struct vnode * snapvp,struct vnode * cancelvp,int level,ufs1_daddr_t blkno,ufs_lbn_t lbn,ufs_lbn_t rlbn,ufs_lbn_t remblks,ufs_lbn_t blksperindir,struct fs * fs,int (* acctfunc)(struct vnode *,ufs1_daddr_t *,ufs1_daddr_t *,struct fs *,ufs_lbn_t,int),int expungetype)1133 indiracct_ufs1(struct vnode *snapvp,
1134 struct vnode *cancelvp,
1135 int level,
1136 ufs1_daddr_t blkno,
1137 ufs_lbn_t lbn,
1138 ufs_lbn_t rlbn,
1139 ufs_lbn_t remblks,
1140 ufs_lbn_t blksperindir,
1141 struct fs *fs,
1142 int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
1143 struct fs *, ufs_lbn_t, int),
1144 int expungetype)
1145 {
1146 int error, num, i;
1147 ufs_lbn_t subblksperindir;
1148 struct indir indirs[UFS_NIADDR + 2];
1149 ufs1_daddr_t last, *bap;
1150 struct buf *bp;
1151
1152 if (blkno == 0) {
1153 if (expungetype == BLK_NOCOPY)
1154 return (0);
1155 panic("indiracct_ufs1: missing indir");
1156 }
1157 if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
1158 return (error);
1159 if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
1160 panic("indiracct_ufs1: botched params");
1161 /*
1162 * We have to expand bread here since it will deadlock looking
1163 * up the block number for any blocks that are not in the cache.
1164 */
1165 bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0, 0);
1166 bp->b_blkno = fsbtodb(fs, blkno);
1167 if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0 &&
1168 (error = readblock(cancelvp, bp, fragstoblks(fs, blkno)))) {
1169 brelse(bp);
1170 return (error);
1171 }
1172 /*
1173 * Account for the block pointers in this indirect block.
1174 */
1175 last = howmany(remblks, blksperindir);
1176 if (last > NINDIR(fs))
1177 last = NINDIR(fs);
1178 bap = malloc(fs->fs_bsize, M_DEVBUF, M_WAITOK);
1179 bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
1180 bqrelse(bp);
1181 error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
1182 level == 0 ? rlbn : -1, expungetype);
1183 if (error || level == 0)
1184 goto out;
1185 /*
1186 * Account for the block pointers in each of the indirect blocks
1187 * in the levels below us.
1188 */
1189 subblksperindir = blksperindir / NINDIR(fs);
1190 for (lbn++, level--, i = 0; i < last; i++) {
1191 error = indiracct_ufs1(snapvp, cancelvp, level, bap[i], lbn,
1192 rlbn, remblks, subblksperindir, fs, acctfunc, expungetype);
1193 if (error)
1194 goto out;
1195 rlbn += blksperindir;
1196 lbn -= blksperindir;
1197 remblks -= blksperindir;
1198 }
1199 out:
1200 free(bap, M_DEVBUF);
1201 return (error);
1202 }
1203
1204 /*
1205 * Do both snap accounting and map accounting.
1206 */
1207 static int
fullacct_ufs1(struct vnode * vp,ufs1_daddr_t * oldblkp,ufs1_daddr_t * lastblkp,struct fs * fs,ufs_lbn_t lblkno,int exptype)1208 fullacct_ufs1(struct vnode *vp,
1209 ufs1_daddr_t *oldblkp,
1210 ufs1_daddr_t *lastblkp,
1211 struct fs *fs,
1212 ufs_lbn_t lblkno,
1213 int exptype) /* BLK_SNAP or BLK_NOCOPY */
1214 {
1215 int error;
1216
1217 if ((error = snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
1218 return (error);
1219 return (mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype));
1220 }
1221
1222 /*
1223 * Identify a set of blocks allocated in a snapshot inode.
1224 */
1225 static int
snapacct_ufs1(struct vnode * vp,ufs1_daddr_t * oldblkp,ufs1_daddr_t * lastblkp,struct fs * fs,ufs_lbn_t lblkno,int expungetype)1226 snapacct_ufs1(struct vnode *vp,
1227 ufs1_daddr_t *oldblkp,
1228 ufs1_daddr_t *lastblkp,
1229 struct fs *fs,
1230 ufs_lbn_t lblkno,
1231 int expungetype) /* BLK_SNAP or BLK_NOCOPY */
1232 {
1233 struct inode *ip = VTOI(vp);
1234 ufs1_daddr_t blkno, *blkp;
1235 ufs_lbn_t lbn;
1236 struct buf *ibp;
1237 int error;
1238
1239 for ( ; oldblkp < lastblkp; oldblkp++) {
1240 blkno = *oldblkp;
1241 if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
1242 continue;
1243 lbn = fragstoblks(fs, blkno);
1244 if (lbn < UFS_NDADDR) {
1245 blkp = &ip->i_din1->di_db[lbn];
1246 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
1247 } else {
1248 error = ffs_balloc_ufs1(vp, lblktosize(fs, (off_t)lbn),
1249 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1250 if (error)
1251 return (error);
1252 blkp = &((ufs1_daddr_t *)(ibp->b_data))
1253 [(lbn - UFS_NDADDR) % NINDIR(fs)];
1254 }
1255 /*
1256 * If we are expunging a snapshot vnode and we
1257 * find a block marked BLK_NOCOPY, then it is
1258 * one that has been allocated to this snapshot after
1259 * we took our current snapshot and can be ignored.
1260 */
1261 if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) {
1262 if (lbn >= UFS_NDADDR)
1263 brelse(ibp);
1264 } else {
1265 if (*blkp != 0)
1266 panic("snapacct_ufs1: bad block");
1267 *blkp = expungetype;
1268 if (lbn >= UFS_NDADDR)
1269 bdwrite(ibp);
1270 }
1271 }
1272 return (0);
1273 }
1274
1275 /*
1276 * Account for a set of blocks allocated in a snapshot inode.
1277 */
1278 static int
mapacct_ufs1(struct vnode * vp,ufs1_daddr_t * oldblkp,ufs1_daddr_t * lastblkp,struct fs * fs,ufs_lbn_t lblkno,int expungetype)1279 mapacct_ufs1(struct vnode *vp,
1280 ufs1_daddr_t *oldblkp,
1281 ufs1_daddr_t *lastblkp,
1282 struct fs *fs,
1283 ufs_lbn_t lblkno,
1284 int expungetype)
1285 {
1286 ufs1_daddr_t blkno;
1287 struct inode *ip;
1288 ino_t inum;
1289 int acctit;
1290
1291 ip = VTOI(vp);
1292 inum = ip->i_number;
1293 if (lblkno == -1)
1294 acctit = 0;
1295 else
1296 acctit = 1;
1297 for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
1298 blkno = *oldblkp;
1299 if (blkno == 0 || blkno == BLK_NOCOPY)
1300 continue;
1301 if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
1302 *ip->i_snapblklist++ = lblkno;
1303 if (blkno == BLK_SNAP)
1304 blkno = blkstofrags(fs, lblkno);
1305 ffs_blkfree(ITOUMP(ip), fs, vp, blkno, fs->fs_bsize, inum,
1306 vp->v_type, NULL, SINGLETON_KEY);
1307 }
1308 return (0);
1309 }
1310
1311 /*
1312 * Before expunging a snapshot inode, note all the
1313 * blocks that it claims with BLK_SNAP so that fsck will
1314 * be able to account for those blocks properly and so
1315 * that this snapshot knows that it need not copy them
1316 * if the other snapshot holding them is freed. This code
1317 * is reproduced once each for UFS1 and UFS2.
1318 */
1319 static int
expunge_ufs2(struct vnode * snapvp,struct inode * cancelip,struct fs * fs,int (* acctfunc)(struct vnode *,ufs2_daddr_t *,ufs2_daddr_t *,struct fs *,ufs_lbn_t,int),int expungetype,int clearmode)1320 expunge_ufs2(struct vnode *snapvp,
1321 struct inode *cancelip,
1322 struct fs *fs,
1323 int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
1324 struct fs *, ufs_lbn_t, int),
1325 int expungetype,
1326 int clearmode)
1327 {
1328 int i, error, indiroff;
1329 ufs_lbn_t lbn, rlbn;
1330 ufs2_daddr_t len, blkno, numblks, blksperindir;
1331 struct ufs2_dinode *dip;
1332 struct thread *td = curthread;
1333 struct buf *bp;
1334
1335 /*
1336 * Prepare to expunge the inode. If its inode block has not
1337 * yet been copied, then allocate and fill the copy.
1338 */
1339 lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
1340 blkno = 0;
1341 if (lbn < UFS_NDADDR) {
1342 blkno = VTOI(snapvp)->i_din2->di_db[lbn];
1343 } else {
1344 if (DOINGSOFTDEP(snapvp))
1345 softdep_prealloc(snapvp, MNT_WAIT);
1346 td->td_pflags |= TDP_COWINPROGRESS;
1347 error = ffs_balloc_ufs2(snapvp, lblktosize(fs, (off_t)lbn),
1348 fs->fs_bsize, KERNCRED, BA_METAONLY, &bp);
1349 td->td_pflags &= ~TDP_COWINPROGRESS;
1350 if (error)
1351 return (error);
1352 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs);
1353 blkno = ((ufs2_daddr_t *)(bp->b_data))[indiroff];
1354 bqrelse(bp);
1355 }
1356 if (blkno != 0) {
1357 if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp)))
1358 return (error);
1359 } else {
1360 error = ffs_balloc_ufs2(snapvp, lblktosize(fs, (off_t)lbn),
1361 fs->fs_bsize, KERNCRED, 0, &bp);
1362 if (error)
1363 return (error);
1364 if ((error = readblock(snapvp, bp, lbn)) != 0)
1365 return (error);
1366 }
1367 /*
1368 * Set a snapshot inode to be a zero length file, regular files
1369 * to be completely unallocated.
1370 */
1371 dip = (struct ufs2_dinode *)bp->b_data +
1372 ino_to_fsbo(fs, cancelip->i_number);
1373 dip->di_size = 0;
1374 dip->di_blocks = 0;
1375 dip->di_flags &= ~SF_SNAPSHOT;
1376 bzero(dip->di_db, UFS_NDADDR * sizeof(ufs2_daddr_t));
1377 bzero(dip->di_ib, UFS_NIADDR * sizeof(ufs2_daddr_t));
1378 if (clearmode || cancelip->i_effnlink == 0)
1379 dip->di_mode = 0;
1380 else
1381 ffs_update_dinode_ckhash(fs, dip);
1382 bdwrite(bp);
1383 /*
1384 * Now go through and expunge all the blocks in the file
1385 * using the function requested.
1386 */
1387 numblks = howmany(cancelip->i_size, fs->fs_bsize);
1388 if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_db[0],
1389 &cancelip->i_din2->di_db[UFS_NDADDR], fs, 0, expungetype)))
1390 return (error);
1391 if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_ib[0],
1392 &cancelip->i_din2->di_ib[UFS_NIADDR], fs, -1, expungetype)))
1393 return (error);
1394 blksperindir = 1;
1395 lbn = -UFS_NDADDR;
1396 len = numblks - UFS_NDADDR;
1397 rlbn = UFS_NDADDR;
1398 for (i = 0; len > 0 && i < UFS_NIADDR; i++) {
1399 error = indiracct_ufs2(snapvp, ITOV(cancelip), i,
1400 cancelip->i_din2->di_ib[i], lbn, rlbn, len,
1401 blksperindir, fs, acctfunc, expungetype);
1402 if (error)
1403 return (error);
1404 blksperindir *= NINDIR(fs);
1405 lbn -= blksperindir + 1;
1406 len -= blksperindir;
1407 rlbn += blksperindir;
1408 }
1409 return (0);
1410 }
1411
1412 /*
1413 * Descend an indirect block chain for vnode cancelvp accounting for all
1414 * its indirect blocks in snapvp.
1415 */
1416 static int
indiracct_ufs2(struct vnode * snapvp,struct vnode * cancelvp,int level,ufs2_daddr_t blkno,ufs_lbn_t lbn,ufs_lbn_t rlbn,ufs_lbn_t remblks,ufs_lbn_t blksperindir,struct fs * fs,int (* acctfunc)(struct vnode *,ufs2_daddr_t *,ufs2_daddr_t *,struct fs *,ufs_lbn_t,int),int expungetype)1417 indiracct_ufs2(struct vnode *snapvp,
1418 struct vnode *cancelvp,
1419 int level,
1420 ufs2_daddr_t blkno,
1421 ufs_lbn_t lbn,
1422 ufs_lbn_t rlbn,
1423 ufs_lbn_t remblks,
1424 ufs_lbn_t blksperindir,
1425 struct fs *fs,
1426 int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
1427 struct fs *, ufs_lbn_t, int),
1428 int expungetype)
1429 {
1430 int error, num, i;
1431 ufs_lbn_t subblksperindir;
1432 struct indir indirs[UFS_NIADDR + 2];
1433 ufs2_daddr_t last, *bap;
1434 struct buf *bp;
1435
1436 if (blkno == 0) {
1437 if (expungetype == BLK_NOCOPY)
1438 return (0);
1439 panic("indiracct_ufs2: missing indir");
1440 }
1441 if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
1442 return (error);
1443 if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
1444 panic("indiracct_ufs2: botched params");
1445 /*
1446 * We have to expand bread here since it will deadlock looking
1447 * up the block number for any blocks that are not in the cache.
1448 */
1449 bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0, 0);
1450 bp->b_blkno = fsbtodb(fs, blkno);
1451 if ((bp->b_flags & B_CACHE) == 0 &&
1452 (error = readblock(cancelvp, bp, fragstoblks(fs, blkno)))) {
1453 brelse(bp);
1454 return (error);
1455 }
1456 /*
1457 * Account for the block pointers in this indirect block.
1458 */
1459 last = howmany(remblks, blksperindir);
1460 if (last > NINDIR(fs))
1461 last = NINDIR(fs);
1462 bap = malloc(fs->fs_bsize, M_DEVBUF, M_WAITOK);
1463 bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
1464 bqrelse(bp);
1465 error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
1466 level == 0 ? rlbn : -1, expungetype);
1467 if (error || level == 0)
1468 goto out;
1469 /*
1470 * Account for the block pointers in each of the indirect blocks
1471 * in the levels below us.
1472 */
1473 subblksperindir = blksperindir / NINDIR(fs);
1474 for (lbn++, level--, i = 0; i < last; i++) {
1475 error = indiracct_ufs2(snapvp, cancelvp, level, bap[i], lbn,
1476 rlbn, remblks, subblksperindir, fs, acctfunc, expungetype);
1477 if (error)
1478 goto out;
1479 rlbn += blksperindir;
1480 lbn -= blksperindir;
1481 remblks -= blksperindir;
1482 }
1483 out:
1484 free(bap, M_DEVBUF);
1485 return (error);
1486 }
1487
1488 /*
1489 * Do both snap accounting and map accounting.
1490 */
1491 static int
fullacct_ufs2(struct vnode * vp,ufs2_daddr_t * oldblkp,ufs2_daddr_t * lastblkp,struct fs * fs,ufs_lbn_t lblkno,int exptype)1492 fullacct_ufs2(struct vnode *vp,
1493 ufs2_daddr_t *oldblkp,
1494 ufs2_daddr_t *lastblkp,
1495 struct fs *fs,
1496 ufs_lbn_t lblkno,
1497 int exptype) /* BLK_SNAP or BLK_NOCOPY */
1498 {
1499 int error;
1500
1501 if ((error = snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
1502 return (error);
1503 return (mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype));
1504 }
1505
1506 /*
1507 * Identify a set of blocks allocated in a snapshot inode.
1508 */
1509 static int
snapacct_ufs2(struct vnode * vp,ufs2_daddr_t * oldblkp,ufs2_daddr_t * lastblkp,struct fs * fs,ufs_lbn_t lblkno,int expungetype)1510 snapacct_ufs2(struct vnode *vp,
1511 ufs2_daddr_t *oldblkp,
1512 ufs2_daddr_t *lastblkp,
1513 struct fs *fs,
1514 ufs_lbn_t lblkno,
1515 int expungetype) /* BLK_SNAP or BLK_NOCOPY */
1516 {
1517 struct inode *ip = VTOI(vp);
1518 ufs2_daddr_t blkno, *blkp;
1519 ufs_lbn_t lbn;
1520 struct buf *ibp;
1521 int error;
1522
1523 for ( ; oldblkp < lastblkp; oldblkp++) {
1524 blkno = *oldblkp;
1525 if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
1526 continue;
1527 lbn = fragstoblks(fs, blkno);
1528 if (lbn < UFS_NDADDR) {
1529 blkp = &ip->i_din2->di_db[lbn];
1530 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
1531 } else {
1532 error = ffs_balloc_ufs2(vp, lblktosize(fs, (off_t)lbn),
1533 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1534 if (error)
1535 return (error);
1536 blkp = &((ufs2_daddr_t *)(ibp->b_data))
1537 [(lbn - UFS_NDADDR) % NINDIR(fs)];
1538 }
1539 /*
1540 * If we are expunging a snapshot vnode and we
1541 * find a block marked BLK_NOCOPY, then it is
1542 * one that has been allocated to this snapshot after
1543 * we took our current snapshot and can be ignored.
1544 */
1545 if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) {
1546 if (lbn >= UFS_NDADDR)
1547 brelse(ibp);
1548 } else {
1549 if (*blkp != 0)
1550 panic("snapacct_ufs2: bad block");
1551 *blkp = expungetype;
1552 if (lbn >= UFS_NDADDR)
1553 bdwrite(ibp);
1554 }
1555 }
1556 return (0);
1557 }
1558
1559 /*
1560 * Account for a set of blocks allocated in a snapshot inode.
1561 */
1562 static int
mapacct_ufs2(struct vnode * vp,ufs2_daddr_t * oldblkp,ufs2_daddr_t * lastblkp,struct fs * fs,ufs_lbn_t lblkno,int expungetype)1563 mapacct_ufs2(struct vnode *vp,
1564 ufs2_daddr_t *oldblkp,
1565 ufs2_daddr_t *lastblkp,
1566 struct fs *fs,
1567 ufs_lbn_t lblkno,
1568 int expungetype)
1569 {
1570 ufs2_daddr_t blkno;
1571 struct inode *ip;
1572 ino_t inum;
1573 int acctit;
1574
1575 ip = VTOI(vp);
1576 inum = ip->i_number;
1577 if (lblkno == -1)
1578 acctit = 0;
1579 else
1580 acctit = 1;
1581 for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
1582 blkno = *oldblkp;
1583 if (blkno == 0 || blkno == BLK_NOCOPY)
1584 continue;
1585 if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP &&
1586 lblkno >= UFS_NDADDR)
1587 *ip->i_snapblklist++ = lblkno;
1588 if (blkno == BLK_SNAP)
1589 blkno = blkstofrags(fs, lblkno);
1590 ffs_blkfree(ITOUMP(ip), fs, vp, blkno, fs->fs_bsize, inum,
1591 vp->v_type, NULL, SINGLETON_KEY);
1592 }
1593 return (0);
1594 }
1595
1596 /*
1597 * Decrement extra reference on snapshot when last name is removed.
1598 * It will not be freed until the last open reference goes away.
1599 */
1600 void
ffs_snapgone(struct inode * ip)1601 ffs_snapgone(struct inode *ip)
1602 {
1603 struct inode *xp;
1604 struct fs *fs;
1605 int snaploc;
1606 struct snapdata *sn;
1607 struct ufsmount *ump;
1608
1609 /*
1610 * Find snapshot in incore list.
1611 */
1612 xp = NULL;
1613 sn = ITODEVVP(ip)->v_rdev->si_snapdata;
1614 if (sn != NULL)
1615 TAILQ_FOREACH(xp, &sn->sn_head, i_nextsnap)
1616 if (xp == ip)
1617 break;
1618 if (xp != NULL)
1619 vrele(ITOV(ip));
1620 #ifdef DIAGNOSTIC
1621 else if (snapdebug)
1622 printf("ffs_snapgone: lost snapshot vnode %ju\n",
1623 (uintmax_t)ip->i_number);
1624 #endif
1625 /*
1626 * Delete snapshot inode from superblock. Keep list dense.
1627 */
1628 ump = ITOUMP(ip);
1629 fs = ump->um_fs;
1630 UFS_LOCK(ump);
1631 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
1632 if (fs->fs_snapinum[snaploc] == ip->i_number)
1633 break;
1634 if (snaploc < FSMAXSNAP) {
1635 for (snaploc++; snaploc < FSMAXSNAP; snaploc++) {
1636 if (fs->fs_snapinum[snaploc] == 0)
1637 break;
1638 fs->fs_snapinum[snaploc - 1] = fs->fs_snapinum[snaploc];
1639 }
1640 fs->fs_snapinum[snaploc - 1] = 0;
1641 }
1642 UFS_UNLOCK(ump);
1643 }
1644
1645 /*
1646 * Prepare a snapshot file for being removed.
1647 */
1648 void
ffs_snapremove(struct vnode * vp)1649 ffs_snapremove(struct vnode *vp)
1650 {
1651 struct inode *ip;
1652 struct vnode *devvp;
1653 struct buf *ibp;
1654 struct fs *fs;
1655 ufs2_daddr_t numblks, blkno, dblk;
1656 int error, last, loc;
1657 struct snapdata *sn;
1658
1659 ip = VTOI(vp);
1660 fs = ITOFS(ip);
1661 devvp = ITODEVVP(ip);
1662 /*
1663 * If active, delete from incore list (this snapshot may
1664 * already have been in the process of being deleted, so
1665 * would not have been active).
1666 *
1667 * Clear copy-on-write flag if last snapshot.
1668 */
1669 VI_LOCK(devvp);
1670 if (ip->i_nextsnap.tqe_prev != 0) {
1671 sn = devvp->v_rdev->si_snapdata;
1672 TAILQ_REMOVE(&sn->sn_head, ip, i_nextsnap);
1673 ip->i_nextsnap.tqe_prev = 0;
1674 revert_snaplock(vp, devvp, sn);
1675 try_free_snapdata(devvp);
1676 }
1677 VI_UNLOCK(devvp);
1678 /*
1679 * Clear all BLK_NOCOPY fields. Pass any block claims to other
1680 * snapshots that want them (see ffs_snapblkfree below).
1681 */
1682 for (blkno = 1; blkno < UFS_NDADDR; blkno++) {
1683 dblk = DIP(ip, i_db[blkno]);
1684 if (dblk == 0)
1685 continue;
1686 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1687 DIP_SET(ip, i_db[blkno], 0);
1688 else if ((dblk == blkstofrags(fs, blkno) &&
1689 ffs_snapblkfree(fs, ITODEVVP(ip), dblk, fs->fs_bsize,
1690 ip->i_number, vp->v_type, NULL))) {
1691 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) -
1692 btodb(fs->fs_bsize));
1693 DIP_SET(ip, i_db[blkno], 0);
1694 }
1695 }
1696 numblks = howmany(ip->i_size, fs->fs_bsize);
1697 for (blkno = UFS_NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
1698 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)blkno),
1699 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1700 if (error)
1701 continue;
1702 if (fs->fs_size - blkno > NINDIR(fs))
1703 last = NINDIR(fs);
1704 else
1705 last = fs->fs_size - blkno;
1706 for (loc = 0; loc < last; loc++) {
1707 if (I_IS_UFS1(ip)) {
1708 dblk = ((ufs1_daddr_t *)(ibp->b_data))[loc];
1709 if (dblk == 0)
1710 continue;
1711 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1712 ((ufs1_daddr_t *)(ibp->b_data))[loc]= 0;
1713 else if ((dblk == blkstofrags(fs, blkno) &&
1714 ffs_snapblkfree(fs, ITODEVVP(ip), dblk,
1715 fs->fs_bsize, ip->i_number, vp->v_type,
1716 NULL))) {
1717 ip->i_din1->di_blocks -=
1718 btodb(fs->fs_bsize);
1719 ((ufs1_daddr_t *)(ibp->b_data))[loc]= 0;
1720 }
1721 continue;
1722 }
1723 dblk = ((ufs2_daddr_t *)(ibp->b_data))[loc];
1724 if (dblk == 0)
1725 continue;
1726 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1727 ((ufs2_daddr_t *)(ibp->b_data))[loc] = 0;
1728 else if ((dblk == blkstofrags(fs, blkno) &&
1729 ffs_snapblkfree(fs, ITODEVVP(ip), dblk,
1730 fs->fs_bsize, ip->i_number, vp->v_type, NULL))) {
1731 ip->i_din2->di_blocks -= btodb(fs->fs_bsize);
1732 ((ufs2_daddr_t *)(ibp->b_data))[loc] = 0;
1733 }
1734 }
1735 bawrite(ibp);
1736 }
1737 /*
1738 * Clear snapshot flag and drop reference.
1739 */
1740 ip->i_flags &= ~SF_SNAPSHOT;
1741 DIP_SET(ip, i_flags, ip->i_flags);
1742 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
1743 /*
1744 * The dirtied indirects must be written out before
1745 * softdep_setup_freeblocks() is called. Otherwise indir_trunc()
1746 * may find indirect pointers using the magic BLK_* values.
1747 */
1748 if (DOINGSOFTDEP(vp))
1749 ffs_syncvnode(vp, MNT_WAIT, 0);
1750 #ifdef QUOTA
1751 /*
1752 * Reenable disk quotas for ex-snapshot file.
1753 */
1754 if (!getinoquota(ip))
1755 (void) chkdq(ip, DIP(ip, i_blocks), KERNCRED, FORCE);
1756 #endif
1757 }
1758
1759 /*
1760 * Notification that a block is being freed. Return zero if the free
1761 * should be allowed to proceed. Return non-zero if the snapshot file
1762 * wants to claim the block. The block will be claimed if it is an
1763 * uncopied part of one of the snapshots. It will be freed if it is
1764 * either a BLK_NOCOPY or has already been copied in all of the snapshots.
1765 * If a fragment is being freed, then all snapshots that care about
1766 * it must make a copy since a snapshot file can only claim full sized
1767 * blocks. Note that if more than one snapshot file maps the block,
1768 * we can pick one at random to claim it. Since none of the snapshots
1769 * can change, we are assurred that they will all see the same unmodified
1770 * image. When deleting a snapshot file (see ffs_snapremove above), we
1771 * must push any of these claimed blocks to one of the other snapshots
1772 * that maps it. These claimed blocks are easily identified as they will
1773 * have a block number equal to their logical block number within the
1774 * snapshot. A copied block can never have this property because they
1775 * must always have been allocated from a BLK_NOCOPY location.
1776 */
1777 int
ffs_snapblkfree(struct fs * fs,struct vnode * devvp,ufs2_daddr_t bno,long size,ino_t inum,__enum_uint8 (vtype)vtype,struct workhead * wkhd)1778 ffs_snapblkfree(struct fs *fs,
1779 struct vnode *devvp,
1780 ufs2_daddr_t bno,
1781 long size,
1782 ino_t inum,
1783 __enum_uint8(vtype) vtype,
1784 struct workhead *wkhd)
1785 {
1786 struct buf *ibp, *cbp, *savedcbp = NULL;
1787 struct thread *td = curthread;
1788 struct inode *ip;
1789 struct vnode *vp = NULL;
1790 ufs_lbn_t lbn;
1791 ufs2_daddr_t blkno;
1792 int indiroff = 0, error = 0, claimedblk = 0;
1793 struct snapdata *sn;
1794
1795 lbn = fragstoblks(fs, bno);
1796 retry:
1797 VI_LOCK(devvp);
1798 sn = devvp->v_rdev->si_snapdata;
1799 if (sn == NULL) {
1800 VI_UNLOCK(devvp);
1801 return (0);
1802 }
1803
1804 /*
1805 * Use LK_SLEEPFAIL because sn might be freed under us while
1806 * both devvp interlock and snaplk are not owned.
1807 */
1808 if (lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
1809 VI_MTX(devvp)) != 0)
1810 goto retry;
1811
1812 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
1813 vp = ITOV(ip);
1814 if (DOINGSOFTDEP(vp))
1815 softdep_prealloc(vp, MNT_WAIT);
1816 /*
1817 * Lookup block being written.
1818 */
1819 if (lbn < UFS_NDADDR) {
1820 blkno = DIP(ip, i_db[lbn]);
1821 } else {
1822 td->td_pflags |= TDP_COWINPROGRESS;
1823 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
1824 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1825 td->td_pflags &= ~TDP_COWINPROGRESS;
1826 if (error)
1827 break;
1828 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs);
1829 if (I_IS_UFS1(ip))
1830 blkno=((ufs1_daddr_t *)(ibp->b_data))[indiroff];
1831 else
1832 blkno=((ufs2_daddr_t *)(ibp->b_data))[indiroff];
1833 }
1834 /*
1835 * Check to see if block needs to be copied.
1836 */
1837 if (blkno == 0) {
1838 /*
1839 * A block that we map is being freed. If it has not
1840 * been claimed yet, we will claim or copy it (below).
1841 */
1842 claimedblk = 1;
1843 } else if (blkno == BLK_SNAP) {
1844 /*
1845 * No previous snapshot claimed the block,
1846 * so it will be freed and become a BLK_NOCOPY
1847 * (don't care) for us.
1848 */
1849 if (claimedblk)
1850 panic("snapblkfree: inconsistent block type");
1851 if (lbn < UFS_NDADDR) {
1852 DIP_SET(ip, i_db[lbn], BLK_NOCOPY);
1853 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
1854 } else if (I_IS_UFS1(ip)) {
1855 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] =
1856 BLK_NOCOPY;
1857 bdwrite(ibp);
1858 } else {
1859 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] =
1860 BLK_NOCOPY;
1861 bdwrite(ibp);
1862 }
1863 continue;
1864 } else /* BLK_NOCOPY or default */ {
1865 /*
1866 * If the snapshot has already copied the block
1867 * (default), or does not care about the block,
1868 * it is not needed.
1869 */
1870 if (lbn >= UFS_NDADDR)
1871 bqrelse(ibp);
1872 continue;
1873 }
1874 /*
1875 * If this is a full size block, we will just grab it
1876 * and assign it to the snapshot inode. Otherwise we
1877 * will proceed to copy it. See explanation for this
1878 * routine as to why only a single snapshot needs to
1879 * claim this block.
1880 */
1881 if (size == fs->fs_bsize) {
1882 #ifdef DIAGNOSTIC
1883 if (snapdebug)
1884 printf("%s %ju lbn %jd from inum %ju\n",
1885 "Grabonremove: snapino",
1886 (uintmax_t)ip->i_number,
1887 (intmax_t)lbn, (uintmax_t)inum);
1888 #endif
1889 /*
1890 * If journaling is tracking this write we must add
1891 * the work to the inode or indirect being written.
1892 */
1893 if (wkhd != NULL) {
1894 if (lbn < UFS_NDADDR)
1895 softdep_inode_append(ip,
1896 curthread->td_ucred, wkhd);
1897 else
1898 softdep_buf_append(ibp, wkhd);
1899 }
1900 if (lbn < UFS_NDADDR) {
1901 DIP_SET(ip, i_db[lbn], bno);
1902 } else if (I_IS_UFS1(ip)) {
1903 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] = bno;
1904 bdwrite(ibp);
1905 } else {
1906 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = bno;
1907 bdwrite(ibp);
1908 }
1909 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + btodb(size));
1910 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
1911 lockmgr(vp->v_vnlock, LK_RELEASE, NULL);
1912 return (1);
1913 }
1914 if (lbn >= UFS_NDADDR)
1915 bqrelse(ibp);
1916 /*
1917 * Allocate the block into which to do the copy. Note that this
1918 * allocation will never require any additional allocations for
1919 * the snapshot inode.
1920 */
1921 td->td_pflags |= TDP_COWINPROGRESS;
1922 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
1923 fs->fs_bsize, KERNCRED, 0, &cbp);
1924 td->td_pflags &= ~TDP_COWINPROGRESS;
1925 if (error)
1926 break;
1927 #ifdef DIAGNOSTIC
1928 if (snapdebug)
1929 printf("%s%ju lbn %jd %s %ju size %ld to blkno %jd\n",
1930 "Copyonremove: snapino ", (uintmax_t)ip->i_number,
1931 (intmax_t)lbn, "for inum", (uintmax_t)inum, size,
1932 (intmax_t)cbp->b_blkno);
1933 #endif
1934 /*
1935 * If we have already read the old block contents, then
1936 * simply copy them to the new block. Note that we need
1937 * to synchronously write snapshots that have not been
1938 * unlinked, and hence will be visible after a crash,
1939 * to ensure their integrity. At a minimum we ensure the
1940 * integrity of the filesystem metadata, but use the
1941 * dopersistence sysctl-setable flag to decide on the
1942 * persistence needed for file content data.
1943 */
1944 if (savedcbp != NULL) {
1945 bcopy(savedcbp->b_data, cbp->b_data, fs->fs_bsize);
1946 bawrite(cbp);
1947 if ((vtype == VDIR || dopersistence) &&
1948 ip->i_effnlink > 0)
1949 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
1950 continue;
1951 }
1952 /*
1953 * Otherwise, read the old block contents into the buffer.
1954 */
1955 if ((error = readblock(vp, cbp, lbn)) != 0) {
1956 bzero(cbp->b_data, fs->fs_bsize);
1957 bawrite(cbp);
1958 if ((vtype == VDIR || dopersistence) &&
1959 ip->i_effnlink > 0)
1960 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
1961 break;
1962 }
1963 savedcbp = cbp;
1964 }
1965 /*
1966 * Note that we need to synchronously write snapshots that
1967 * have not been unlinked, and hence will be visible after
1968 * a crash, to ensure their integrity. At a minimum we
1969 * ensure the integrity of the filesystem metadata, but
1970 * use the dopersistence sysctl-setable flag to decide on
1971 * the persistence needed for file content data.
1972 */
1973 if (savedcbp) {
1974 vp = savedcbp->b_vp;
1975 bawrite(savedcbp);
1976 if ((vtype == VDIR || dopersistence) &&
1977 VTOI(vp)->i_effnlink > 0)
1978 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
1979 }
1980 /*
1981 * If we have been unable to allocate a block in which to do
1982 * the copy, then return non-zero so that the fragment will
1983 * not be freed. Although space will be lost, the snapshot
1984 * will stay consistent.
1985 */
1986 if (error != 0 && wkhd != NULL)
1987 softdep_freework(wkhd);
1988 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
1989 return (error);
1990 }
1991
1992 /*
1993 * Associate snapshot files when mounting.
1994 */
1995 void
ffs_snapshot_mount(struct mount * mp)1996 ffs_snapshot_mount(struct mount *mp)
1997 {
1998 struct ufsmount *ump = VFSTOUFS(mp);
1999 struct vnode *devvp = ump->um_devvp;
2000 struct fs *fs = ump->um_fs;
2001 struct thread *td = curthread;
2002 struct snapdata *sn;
2003 struct vnode *vp;
2004 struct vnode *lastvp;
2005 struct inode *ip;
2006 struct uio auio;
2007 struct iovec aiov;
2008 void *snapblklist;
2009 char *reason;
2010 daddr_t snaplistsize;
2011 int error, snaploc, loc;
2012
2013 /*
2014 * XXX The following needs to be set before ffs_truncate or
2015 * VOP_READ can be called.
2016 */
2017 mp->mnt_stat.f_iosize = fs->fs_bsize;
2018 /*
2019 * Process each snapshot listed in the superblock.
2020 */
2021 vp = NULL;
2022 lastvp = NULL;
2023 sn = NULL;
2024 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++) {
2025 if (fs->fs_snapinum[snaploc] == 0)
2026 break;
2027 if ((error = ffs_vget(mp, fs->fs_snapinum[snaploc],
2028 LK_EXCLUSIVE, &vp)) != 0){
2029 printf("ffs_snapshot_mount: vget failed %d\n", error);
2030 continue;
2031 }
2032 ip = VTOI(vp);
2033 if (vp->v_type != VREG) {
2034 reason = "non-file snapshot";
2035 } else if (!IS_SNAPSHOT(ip)) {
2036 reason = "non-snapshot";
2037 } else if (ip->i_size ==
2038 lblktosize(fs, howmany(fs->fs_size, fs->fs_frag))) {
2039 reason = "old format snapshot";
2040 (void)ffs_truncate(vp, (off_t)0, 0, NOCRED);
2041 (void)ffs_syncvnode(vp, MNT_WAIT, 0);
2042 } else {
2043 reason = NULL;
2044 }
2045 if (reason != NULL) {
2046 printf("ffs_snapshot_mount: %s inode %d\n",
2047 reason, fs->fs_snapinum[snaploc]);
2048 vput(vp);
2049 vp = NULL;
2050 for (loc = snaploc + 1; loc < FSMAXSNAP; loc++) {
2051 if (fs->fs_snapinum[loc] == 0)
2052 break;
2053 fs->fs_snapinum[loc - 1] = fs->fs_snapinum[loc];
2054 }
2055 fs->fs_snapinum[loc - 1] = 0;
2056 snaploc--;
2057 continue;
2058 }
2059 /*
2060 * Acquire a lock on the snapdata structure, creating it if
2061 * necessary.
2062 */
2063 sn = ffs_snapdata_acquire(devvp);
2064 /*
2065 * Change vnode to use shared snapshot lock instead of the
2066 * original private lock.
2067 */
2068 vp->v_vnlock = &sn->sn_lock;
2069 lockmgr(&vp->v_lock, LK_RELEASE, NULL);
2070 /*
2071 * Link it onto the active snapshot list.
2072 */
2073 VI_LOCK(devvp);
2074 if (ip->i_nextsnap.tqe_prev != 0)
2075 panic("ffs_snapshot_mount: %ju already on list",
2076 (uintmax_t)ip->i_number);
2077 else
2078 TAILQ_INSERT_TAIL(&sn->sn_head, ip, i_nextsnap);
2079 vp->v_vflag |= VV_SYSTEM;
2080 VI_UNLOCK(devvp);
2081 VOP_UNLOCK(vp);
2082 lastvp = vp;
2083 }
2084 vp = lastvp;
2085 /*
2086 * No usable snapshots found.
2087 */
2088 if (sn == NULL || vp == NULL)
2089 return;
2090 /*
2091 * Allocate the space for the block hints list. We always want to
2092 * use the list from the newest snapshot.
2093 */
2094 auio.uio_iov = &aiov;
2095 auio.uio_iovcnt = 1;
2096 aiov.iov_base = (void *)&snaplistsize;
2097 aiov.iov_len = sizeof(snaplistsize);
2098 auio.uio_resid = aiov.iov_len;
2099 auio.uio_offset =
2100 lblktosize(fs, howmany(fs->fs_size, fs->fs_frag));
2101 auio.uio_segflg = UIO_SYSSPACE;
2102 auio.uio_rw = UIO_READ;
2103 auio.uio_td = td;
2104 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2105 if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
2106 printf("ffs_snapshot_mount: read_1 failed %d\n", error);
2107 VOP_UNLOCK(vp);
2108 return;
2109 }
2110 snapblklist = malloc(snaplistsize * sizeof(daddr_t),
2111 M_UFSMNT, M_WAITOK);
2112 auio.uio_iovcnt = 1;
2113 aiov.iov_base = snapblklist;
2114 aiov.iov_len = snaplistsize * sizeof (daddr_t);
2115 auio.uio_resid = aiov.iov_len;
2116 auio.uio_offset -= sizeof(snaplistsize);
2117 if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
2118 printf("ffs_snapshot_mount: read_2 failed %d\n", error);
2119 VOP_UNLOCK(vp);
2120 free(snapblklist, M_UFSMNT);
2121 return;
2122 }
2123 VOP_UNLOCK(vp);
2124 VI_LOCK(devvp);
2125 sn->sn_listsize = snaplistsize;
2126 sn->sn_blklist = (daddr_t *)snapblklist;
2127 devvp->v_vflag |= VV_COPYONWRITE;
2128 VI_UNLOCK(devvp);
2129 }
2130
2131 /*
2132 * Disassociate snapshot files when unmounting.
2133 */
2134 void
ffs_snapshot_unmount(struct mount * mp)2135 ffs_snapshot_unmount(struct mount *mp)
2136 {
2137 struct vnode *devvp = VFSTOUFS(mp)->um_devvp;
2138 struct snapdata *sn;
2139 struct inode *xp;
2140 struct vnode *vp;
2141
2142 VI_LOCK(devvp);
2143 sn = devvp->v_rdev->si_snapdata;
2144 while (sn != NULL && (xp = TAILQ_FIRST(&sn->sn_head)) != NULL) {
2145 vp = ITOV(xp);
2146 TAILQ_REMOVE(&sn->sn_head, xp, i_nextsnap);
2147 xp->i_nextsnap.tqe_prev = 0;
2148 lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE,
2149 VI_MTX(devvp));
2150 VI_LOCK(devvp);
2151 revert_snaplock(vp, devvp, sn);
2152 lockmgr(&vp->v_lock, LK_RELEASE, NULL);
2153 if (xp->i_effnlink > 0) {
2154 VI_UNLOCK(devvp);
2155 vrele(vp);
2156 VI_LOCK(devvp);
2157 }
2158 sn = devvp->v_rdev->si_snapdata;
2159 }
2160 try_free_snapdata(devvp);
2161 VI_UNLOCK(devvp);
2162 }
2163
2164 /*
2165 * Check the buffer block to be belong to device buffer that shall be
2166 * locked after snaplk. devvp shall be locked on entry, and will be
2167 * leaved locked upon exit.
2168 */
2169 static int
ffs_bp_snapblk(struct vnode * devvp,struct buf * bp)2170 ffs_bp_snapblk(struct vnode *devvp, struct buf *bp)
2171 {
2172 struct snapdata *sn;
2173 struct fs *fs;
2174 ufs2_daddr_t lbn, *snapblklist;
2175 int lower, upper, mid;
2176
2177 ASSERT_VI_LOCKED(devvp, "ffs_bp_snapblk");
2178 KASSERT(devvp->v_type == VCHR, ("Not a device %p", devvp));
2179 sn = devvp->v_rdev->si_snapdata;
2180 if (sn == NULL || TAILQ_FIRST(&sn->sn_head) == NULL)
2181 return (0);
2182 fs = ITOFS(TAILQ_FIRST(&sn->sn_head));
2183 lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
2184 snapblklist = sn->sn_blklist;
2185 upper = sn->sn_listsize - 1;
2186 lower = 1;
2187 while (lower <= upper) {
2188 mid = (lower + upper) / 2;
2189 if (snapblklist[mid] == lbn)
2190 break;
2191 if (snapblklist[mid] < lbn)
2192 lower = mid + 1;
2193 else
2194 upper = mid - 1;
2195 }
2196 if (lower <= upper)
2197 return (1);
2198 return (0);
2199 }
2200
2201 void
ffs_bdflush(struct bufobj * bo,struct buf * bp)2202 ffs_bdflush(struct bufobj *bo, struct buf *bp)
2203 {
2204 struct thread *td;
2205 struct vnode *vp, *devvp;
2206 struct buf *nbp;
2207 int bp_bdskip;
2208
2209 if (bo->bo_dirty.bv_cnt <= dirtybufthresh)
2210 return;
2211
2212 td = curthread;
2213 vp = bp->b_vp;
2214 devvp = bo2vnode(bo);
2215 KASSERT(vp == devvp, ("devvp != vp %p %p", bo, bp));
2216
2217 VI_LOCK(devvp);
2218 bp_bdskip = ffs_bp_snapblk(devvp, bp);
2219 if (bp_bdskip)
2220 bdwriteskip++;
2221 VI_UNLOCK(devvp);
2222 if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10 && !bp_bdskip) {
2223 (void) VOP_FSYNC(vp, MNT_NOWAIT, td);
2224 altbufferflushes++;
2225 } else {
2226 BO_LOCK(bo);
2227 /*
2228 * Try to find a buffer to flush.
2229 */
2230 TAILQ_FOREACH(nbp, &bo->bo_dirty.bv_hd, b_bobufs) {
2231 if ((nbp->b_vflags & BV_BKGRDINPROG) ||
2232 BUF_LOCK(nbp,
2233 LK_EXCLUSIVE | LK_NOWAIT, NULL))
2234 continue;
2235 if (bp == nbp)
2236 panic("bdwrite: found ourselves");
2237 BO_UNLOCK(bo);
2238 /*
2239 * Don't countdeps with the bo lock
2240 * held.
2241 */
2242 if (buf_countdeps(nbp, 0)) {
2243 BO_LOCK(bo);
2244 BUF_UNLOCK(nbp);
2245 continue;
2246 }
2247 if (bp_bdskip) {
2248 VI_LOCK(devvp);
2249 if (!ffs_bp_snapblk(vp, nbp)) {
2250 VI_UNLOCK(devvp);
2251 BO_LOCK(bo);
2252 BUF_UNLOCK(nbp);
2253 continue;
2254 }
2255 VI_UNLOCK(devvp);
2256 }
2257 if (nbp->b_flags & B_CLUSTEROK) {
2258 vfs_bio_awrite(nbp);
2259 } else {
2260 bremfree(nbp);
2261 bawrite(nbp);
2262 }
2263 dirtybufferflushes++;
2264 break;
2265 }
2266 if (nbp == NULL)
2267 BO_UNLOCK(bo);
2268 }
2269 }
2270
2271 /*
2272 * Check for need to copy block that is about to be written,
2273 * copying the block if necessary.
2274 */
2275 int
ffs_copyonwrite(struct vnode * devvp,struct buf * bp)2276 ffs_copyonwrite(struct vnode *devvp, struct buf *bp)
2277 {
2278 struct snapdata *sn;
2279 struct buf *ibp, *cbp, *savedcbp = NULL;
2280 struct thread *td = curthread;
2281 struct fs *fs;
2282 struct inode *ip;
2283 struct vnode *vp = NULL;
2284 ufs2_daddr_t lbn, blkno, *snapblklist;
2285 int lower, upper, mid, indiroff, error = 0;
2286 int launched_async_io, prev_norunningbuf;
2287 long saved_runningbufspace;
2288
2289 if (devvp != bp->b_vp && IS_SNAPSHOT(VTOI(bp->b_vp)))
2290 return (0); /* Update on a snapshot file */
2291 if (td->td_pflags & TDP_COWINPROGRESS)
2292 panic("ffs_copyonwrite: recursive call");
2293 /*
2294 * First check to see if it is in the preallocated list.
2295 * By doing this check we avoid several potential deadlocks.
2296 */
2297 VI_LOCK(devvp);
2298 sn = devvp->v_rdev->si_snapdata;
2299 if (sn == NULL ||
2300 TAILQ_EMPTY(&sn->sn_head)) {
2301 VI_UNLOCK(devvp);
2302 return (0); /* No snapshot */
2303 }
2304 ip = TAILQ_FIRST(&sn->sn_head);
2305 fs = ITOFS(ip);
2306 lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
2307 if (lbn < UFS_NDADDR) {
2308 VI_UNLOCK(devvp);
2309 return (0); /* Direct blocks are always copied */
2310 }
2311 snapblklist = sn->sn_blklist;
2312 upper = sn->sn_listsize - 1;
2313 lower = 1;
2314 while (lower <= upper) {
2315 mid = (lower + upper) / 2;
2316 if (snapblklist[mid] == lbn)
2317 break;
2318 if (snapblklist[mid] < lbn)
2319 lower = mid + 1;
2320 else
2321 upper = mid - 1;
2322 }
2323 if (lower <= upper) {
2324 VI_UNLOCK(devvp);
2325 return (0);
2326 }
2327 launched_async_io = 0;
2328 prev_norunningbuf = td->td_pflags & TDP_NORUNNINGBUF;
2329 /*
2330 * Since I/O on bp isn't yet in progress and it may be blocked
2331 * for a long time waiting on snaplk, back it out of
2332 * runningbufspace, possibly waking other threads waiting for space.
2333 */
2334 saved_runningbufspace = bp->b_runningbufspace;
2335 if (saved_runningbufspace != 0)
2336 runningbufwakeup(bp);
2337 /*
2338 * Not in the precomputed list, so check the snapshots.
2339 */
2340 while (lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
2341 VI_MTX(devvp)) != 0) {
2342 VI_LOCK(devvp);
2343 sn = devvp->v_rdev->si_snapdata;
2344 if (sn == NULL ||
2345 TAILQ_EMPTY(&sn->sn_head)) {
2346 VI_UNLOCK(devvp);
2347 if (saved_runningbufspace != 0) {
2348 bp->b_runningbufspace = saved_runningbufspace;
2349 atomic_add_long(&runningbufspace,
2350 bp->b_runningbufspace);
2351 }
2352 return (0); /* Snapshot gone */
2353 }
2354 }
2355 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
2356 vp = ITOV(ip);
2357 if (DOINGSOFTDEP(vp))
2358 softdep_prealloc(vp, MNT_WAIT);
2359 /*
2360 * We ensure that everything of our own that needs to be
2361 * copied will be done at the time that ffs_snapshot is
2362 * called. Thus we can skip the check here which can
2363 * deadlock in doing the lookup in UFS_BALLOC.
2364 */
2365 if (bp->b_vp == vp)
2366 continue;
2367 /*
2368 * Check to see if block needs to be copied. We do not have
2369 * to hold the snapshot lock while doing this lookup as it
2370 * will never require any additional allocations for the
2371 * snapshot inode.
2372 */
2373 if (lbn < UFS_NDADDR) {
2374 blkno = DIP(ip, i_db[lbn]);
2375 } else {
2376 td->td_pflags |= TDP_COWINPROGRESS | TDP_NORUNNINGBUF;
2377 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
2378 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
2379 td->td_pflags &= ~TDP_COWINPROGRESS;
2380 if (error)
2381 break;
2382 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs);
2383 if (I_IS_UFS1(ip))
2384 blkno=((ufs1_daddr_t *)(ibp->b_data))[indiroff];
2385 else
2386 blkno=((ufs2_daddr_t *)(ibp->b_data))[indiroff];
2387 bqrelse(ibp);
2388 }
2389 #ifdef INVARIANTS
2390 if (blkno == BLK_SNAP && bp->b_lblkno >= 0)
2391 panic("ffs_copyonwrite: bad copy block");
2392 #endif
2393 if (blkno != 0)
2394 continue;
2395 /*
2396 * Allocate the block into which to do the copy. Since
2397 * multiple processes may all try to copy the same block,
2398 * we have to recheck our need to do a copy if we sleep
2399 * waiting for the lock.
2400 *
2401 * Because all snapshots on a filesystem share a single
2402 * lock, we ensure that we will never be in competition
2403 * with another process to allocate a block.
2404 */
2405 td->td_pflags |= TDP_COWINPROGRESS | TDP_NORUNNINGBUF;
2406 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
2407 fs->fs_bsize, KERNCRED, 0, &cbp);
2408 td->td_pflags &= ~TDP_COWINPROGRESS;
2409 if (error)
2410 break;
2411 #ifdef DIAGNOSTIC
2412 if (snapdebug) {
2413 printf("Copyonwrite: snapino %ju lbn %jd for ",
2414 (uintmax_t)ip->i_number, (intmax_t)lbn);
2415 if (bp->b_vp == devvp)
2416 printf("fs metadata");
2417 else
2418 printf("inum %ju",
2419 (uintmax_t)VTOI(bp->b_vp)->i_number);
2420 printf(" lblkno %jd to blkno %jd\n",
2421 (intmax_t)bp->b_lblkno, (intmax_t)cbp->b_blkno);
2422 }
2423 #endif
2424 /*
2425 * If we have already read the old block contents, then
2426 * simply copy them to the new block. Note that we need
2427 * to synchronously write snapshots that have not been
2428 * unlinked, and hence will be visible after a crash,
2429 * to ensure their integrity. At a minimum we ensure the
2430 * integrity of the filesystem metadata, but use the
2431 * dopersistence sysctl-setable flag to decide on the
2432 * persistence needed for file content data.
2433 */
2434 if (savedcbp != NULL) {
2435 bcopy(savedcbp->b_data, cbp->b_data, fs->fs_bsize);
2436 bawrite(cbp);
2437 if ((devvp == bp->b_vp || bp->b_vp->v_type == VDIR ||
2438 dopersistence) && ip->i_effnlink > 0)
2439 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
2440 else
2441 launched_async_io = 1;
2442 continue;
2443 }
2444 /*
2445 * Otherwise, read the old block contents into the buffer.
2446 */
2447 if ((error = readblock(vp, cbp, lbn)) != 0) {
2448 bzero(cbp->b_data, fs->fs_bsize);
2449 bawrite(cbp);
2450 if ((devvp == bp->b_vp || bp->b_vp->v_type == VDIR ||
2451 dopersistence) && ip->i_effnlink > 0)
2452 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
2453 else
2454 launched_async_io = 1;
2455 break;
2456 }
2457 savedcbp = cbp;
2458 }
2459 /*
2460 * Note that we need to synchronously write snapshots that
2461 * have not been unlinked, and hence will be visible after
2462 * a crash, to ensure their integrity. At a minimum we
2463 * ensure the integrity of the filesystem metadata, but
2464 * use the dopersistence sysctl-setable flag to decide on
2465 * the persistence needed for file content data.
2466 */
2467 if (savedcbp) {
2468 vp = savedcbp->b_vp;
2469 bawrite(savedcbp);
2470 if ((devvp == bp->b_vp || bp->b_vp->v_type == VDIR ||
2471 dopersistence) && VTOI(vp)->i_effnlink > 0)
2472 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
2473 else
2474 launched_async_io = 1;
2475 }
2476 lockmgr(vp->v_vnlock, LK_RELEASE, NULL);
2477 td->td_pflags = (td->td_pflags & ~TDP_NORUNNINGBUF) |
2478 prev_norunningbuf;
2479 if (launched_async_io && (td->td_pflags & TDP_NORUNNINGBUF) == 0)
2480 waitrunningbufspace();
2481 /*
2482 * I/O on bp will now be started, so count it in runningbufspace.
2483 */
2484 if (saved_runningbufspace != 0) {
2485 bp->b_runningbufspace = saved_runningbufspace;
2486 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
2487 }
2488 return (error);
2489 }
2490
2491 /*
2492 * sync snapshots to force freework records waiting on snapshots to claim
2493 * blocks to free.
2494 */
2495 void
ffs_sync_snap(struct mount * mp,int waitfor)2496 ffs_sync_snap(struct mount *mp, int waitfor)
2497 {
2498 struct snapdata *sn;
2499 struct vnode *devvp;
2500 struct vnode *vp;
2501 struct inode *ip;
2502
2503 devvp = VFSTOUFS(mp)->um_devvp;
2504 if ((devvp->v_vflag & VV_COPYONWRITE) == 0)
2505 return;
2506 for (;;) {
2507 VI_LOCK(devvp);
2508 sn = devvp->v_rdev->si_snapdata;
2509 if (sn == NULL) {
2510 VI_UNLOCK(devvp);
2511 return;
2512 }
2513 if (lockmgr(&sn->sn_lock,
2514 LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
2515 VI_MTX(devvp)) == 0)
2516 break;
2517 }
2518 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
2519 vp = ITOV(ip);
2520 ffs_syncvnode(vp, waitfor, NO_INO_UPDT);
2521 }
2522 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2523 }
2524
2525 /*
2526 * Read the specified block into the given buffer.
2527 * Much of this boiler-plate comes from bwrite().
2528 */
2529 static int
readblock(struct vnode * vp,struct buf * bp,ufs2_daddr_t lbn)2530 readblock(struct vnode *vp,
2531 struct buf *bp,
2532 ufs2_daddr_t lbn)
2533 {
2534 struct inode *ip;
2535 struct fs *fs;
2536
2537 ip = VTOI(vp);
2538 fs = ITOFS(ip);
2539
2540 bp->b_iocmd = BIO_READ;
2541 bp->b_iooffset = dbtob(fsbtodb(fs, blkstofrags(fs, lbn)));
2542 bp->b_iodone = bdone;
2543 g_vfs_strategy(&ITODEVVP(ip)->v_bufobj, bp);
2544 bufwait(bp);
2545 return (bp->b_error);
2546 }
2547
2548 #endif
2549
2550 /*
2551 * Process file deletes that were deferred by ufs_inactive() due to
2552 * the file system being suspended. Transfer IN_LAZYACCESS into
2553 * IN_MODIFIED for vnodes that were accessed during suspension.
2554 */
2555 void
process_deferred_inactive(struct mount * mp)2556 process_deferred_inactive(struct mount *mp)
2557 {
2558 struct vnode *vp, *mvp;
2559 struct inode *ip;
2560 int error;
2561
2562 (void) vn_start_secondary_write(NULL, &mp, V_WAIT);
2563 loop:
2564 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
2565 /*
2566 * IN_LAZYACCESS is checked here without holding any
2567 * vnode lock, but this flag is set only while holding
2568 * vnode interlock.
2569 */
2570 if (vp->v_type == VNON ||
2571 ((VTOI(vp)->i_flag & IN_LAZYACCESS) == 0 &&
2572 ((vp->v_iflag & VI_OWEINACT) == 0 || vp->v_usecount > 0))) {
2573 VI_UNLOCK(vp);
2574 continue;
2575 }
2576 vholdl(vp);
2577 retry_vnode:
2578 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2579 if (error != 0) {
2580 vdrop(vp);
2581 if (error == ENOENT)
2582 continue; /* vnode recycled */
2583 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
2584 goto loop;
2585 }
2586 ip = VTOI(vp);
2587 if ((ip->i_flag & IN_LAZYACCESS) != 0) {
2588 ip->i_flag &= ~IN_LAZYACCESS;
2589 UFS_INODE_SET_FLAG(ip, IN_MODIFIED);
2590 }
2591 VI_LOCK(vp);
2592 error = vinactive(vp);
2593 if (error == ERELOOKUP && vp->v_usecount == 0) {
2594 VI_UNLOCK(vp);
2595 VOP_UNLOCK(vp);
2596 goto retry_vnode;
2597 }
2598 VI_UNLOCK(vp);
2599 VOP_UNLOCK(vp);
2600 vdrop(vp);
2601 }
2602 vn_finished_secondary_write(mp);
2603 }
2604
2605 #ifndef NO_FFS_SNAPSHOT
2606
2607 static struct snapdata *
ffs_snapdata_alloc(void)2608 ffs_snapdata_alloc(void)
2609 {
2610 struct snapdata *sn;
2611
2612 /*
2613 * Fetch a snapdata from the free list if there is one available.
2614 */
2615 mtx_lock(&snapfree_lock);
2616 sn = LIST_FIRST(&snapfree);
2617 if (sn != NULL)
2618 LIST_REMOVE(sn, sn_link);
2619 mtx_unlock(&snapfree_lock);
2620 if (sn != NULL)
2621 return (sn);
2622 /*
2623 * If there were no free snapdatas allocate one.
2624 */
2625 sn = malloc(sizeof *sn, M_UFSMNT, M_WAITOK | M_ZERO);
2626 TAILQ_INIT(&sn->sn_head);
2627 lockinit(&sn->sn_lock, PVFS, "snaplk", VLKTIMEOUT,
2628 LK_CANRECURSE | LK_NOSHARE);
2629 return (sn);
2630 }
2631
2632 /*
2633 * The snapdata is never freed because we can not be certain that
2634 * there are no threads sleeping on the snap lock. Persisting
2635 * them permanently avoids costly synchronization in ffs_lock().
2636 */
2637 static void
ffs_snapdata_free(struct snapdata * sn)2638 ffs_snapdata_free(struct snapdata *sn)
2639 {
2640 mtx_lock(&snapfree_lock);
2641 LIST_INSERT_HEAD(&snapfree, sn, sn_link);
2642 mtx_unlock(&snapfree_lock);
2643 }
2644
2645 /* Try to free snapdata associated with devvp */
2646 static void
try_free_snapdata(struct vnode * devvp)2647 try_free_snapdata(struct vnode *devvp)
2648 {
2649 struct snapdata *sn;
2650 ufs2_daddr_t *snapblklist;
2651
2652 ASSERT_VI_LOCKED(devvp, "try_free_snapdata");
2653 sn = devvp->v_rdev->si_snapdata;
2654
2655 if (sn == NULL || TAILQ_FIRST(&sn->sn_head) != NULL ||
2656 (devvp->v_vflag & VV_COPYONWRITE) == 0)
2657 return;
2658
2659 devvp->v_rdev->si_snapdata = NULL;
2660 devvp->v_vflag &= ~VV_COPYONWRITE;
2661 lockmgr(&sn->sn_lock, LK_DRAIN|LK_INTERLOCK, VI_MTX(devvp));
2662 snapblklist = sn->sn_blklist;
2663 sn->sn_blklist = NULL;
2664 sn->sn_listsize = 0;
2665 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2666 if (snapblklist != NULL)
2667 free(snapblklist, M_UFSMNT);
2668 ffs_snapdata_free(sn);
2669 VI_LOCK(devvp);
2670 }
2671
2672 /*
2673 * Revert a vnode lock from using the snapshot lock back to its own lock.
2674 *
2675 * Aquire a lock on the vnode's own lock and release the lock on the
2676 * snapshot lock. If there are any recursions on the snapshot lock
2677 * get the same number of recursions on the vnode's own lock.
2678 */
2679 static void
revert_snaplock(struct vnode * vp,struct vnode * devvp,struct snapdata * sn)2680 revert_snaplock(struct vnode *vp,
2681 struct vnode *devvp,
2682 struct snapdata *sn)
2683 {
2684 int i;
2685
2686 ASSERT_VI_LOCKED(devvp, "revert_snaplock");
2687 /*
2688 * Avoid LOR with snapshot lock. The LK_NOWAIT should
2689 * never fail as the lock is currently unused. Rather than
2690 * panic, we recover by doing the blocking lock.
2691 */
2692 for (i = 0; i <= sn->sn_lock.lk_recurse; i++) {
2693 if (lockmgr(&vp->v_lock, LK_EXCLUSIVE | LK_NOWAIT |
2694 LK_INTERLOCK, VI_MTX(devvp)) != 0) {
2695 printf("revert_snaplock: Unexpected LK_NOWAIT "
2696 "failure\n");
2697 lockmgr(&vp->v_lock, LK_EXCLUSIVE | LK_INTERLOCK,
2698 VI_MTX(devvp));
2699 }
2700 VI_LOCK(devvp);
2701 }
2702 KASSERT(vp->v_vnlock == &sn->sn_lock,
2703 ("revert_snaplock: lost lock mutation"));
2704 vp->v_vnlock = &vp->v_lock;
2705 while (sn->sn_lock.lk_recurse > 0)
2706 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2707 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2708 }
2709
2710 static struct snapdata *
ffs_snapdata_acquire(struct vnode * devvp)2711 ffs_snapdata_acquire(struct vnode *devvp)
2712 {
2713 struct snapdata *nsn, *sn;
2714 int error;
2715
2716 /*
2717 * Allocate a free snapdata. This is done before acquiring the
2718 * devvp lock to avoid allocation while the devvp interlock is
2719 * held.
2720 */
2721 nsn = ffs_snapdata_alloc();
2722
2723 for (;;) {
2724 VI_LOCK(devvp);
2725 sn = devvp->v_rdev->si_snapdata;
2726 if (sn == NULL) {
2727 /*
2728 * This is the first snapshot on this
2729 * filesystem and we use our pre-allocated
2730 * snapdata. Publish sn with the sn_lock
2731 * owned by us, to avoid the race.
2732 */
2733 error = lockmgr(&nsn->sn_lock, LK_EXCLUSIVE |
2734 LK_NOWAIT, NULL);
2735 if (error != 0)
2736 panic("leaked sn, lockmgr error %d", error);
2737 sn = devvp->v_rdev->si_snapdata = nsn;
2738 VI_UNLOCK(devvp);
2739 nsn = NULL;
2740 break;
2741 }
2742
2743 /*
2744 * There is a snapshots which already exists on this
2745 * filesystem, grab a reference to the common lock.
2746 */
2747 error = lockmgr(&sn->sn_lock, LK_INTERLOCK |
2748 LK_EXCLUSIVE | LK_SLEEPFAIL, VI_MTX(devvp));
2749 if (error == 0)
2750 break;
2751 }
2752
2753 /*
2754 * Free any unused snapdata.
2755 */
2756 if (nsn != NULL)
2757 ffs_snapdata_free(nsn);
2758
2759 return (sn);
2760 }
2761
2762 #endif
2763