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