1 /* $NetBSD: ffs_snapshot.c,v 1.140 2015/06/28 10:04:32 maxv Exp $ */
4 * Copyright 2000 Marshall Kirk McKusick. All Rights Reserved.
6 * Further information about snapshots can be obtained from:
8 * Marshall Kirk McKusick http://www.mckusick.com/softdep/
9 * 1614 Oxford Street mckusick@mckusick.com
10 * Berkeley, CA 94709-1608 +1-510-843-9542
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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
35 * @(#)ffs_snapshot.c 8.11 (McKusick) 7/23/00
37 * from FreeBSD: ffs_snapshot.c,v 1.79 2004/02/13 02:02:06 kuriyama Exp
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: ffs_snapshot.c,v 1.140 2015/06/28 10:04:32 maxv Exp $");
43 #if defined(_KERNEL_OPT)
45 #include "opt_quota.h"
48 #include <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/systm.h>
54 #include <sys/namei.h>
55 #include <sys/sched.h>
57 #include <sys/malloc.h>
58 #include <sys/mount.h>
59 #include <sys/resource.h>
60 #include <sys/resourcevar.h>
61 #include <sys/vnode.h>
62 #include <sys/kauth.h>
63 #include <sys/fstrans.h>
64 #include <sys/wapbl.h>
66 #include <miscfs/specfs/specdev.h>
68 #include <ufs/ufs/quota.h>
69 #include <ufs/ufs/ufsmount.h>
70 #include <ufs/ufs/inode.h>
71 #include <ufs/ufs/ufs_extern.h>
72 #include <ufs/ufs/ufs_bswap.h>
73 #include <ufs/ufs/ufs_wapbl.h>
75 #include <ufs/ffs/fs.h>
76 #include <ufs/ffs/ffs_extern.h>
80 TAILQ_HEAD(inodelst
, inode
); /* List of active snapshots */
83 kmutex_t si_lock
; /* Lock this snapinfo */
84 kmutex_t si_snaplock
; /* Snapshot vnode common lock */
85 lwp_t
*si_owner
; /* Snaplock owner */
86 struct inodelst si_snapshots
; /* List of active snapshots */
87 daddr_t
*si_snapblklist
; /* Snapshot block hints list */
88 uint32_t si_gen
; /* Incremented on change */
91 #if !defined(FFS_NO_SNAPSHOT)
92 typedef int (*acctfunc_t
)
93 (struct vnode
*, void *, int, int, struct fs
*, daddr_t
, int);
95 static int snapshot_setup(struct mount
*, struct vnode
*);
96 static int snapshot_copyfs(struct mount
*, struct vnode
*, void **);
97 static int snapshot_expunge(struct mount
*, struct vnode
*,
98 struct fs
*, daddr_t
*, daddr_t
**);
99 static int snapshot_expunge_snap(struct mount
*, struct vnode
*,
100 struct fs
*, daddr_t
);
101 static int snapshot_writefs(struct mount
*, struct vnode
*, void *);
102 static int cgaccount(struct vnode
*, int, int *);
103 static int cgaccount1(int, struct vnode
*, void *, int);
104 static int expunge(struct vnode
*, struct inode
*, struct fs
*,
106 static int indiracct(struct vnode
*, struct vnode
*, int, daddr_t
,
107 daddr_t
, daddr_t
, daddr_t
, daddr_t
, struct fs
*, acctfunc_t
, int);
108 static int fullacct(struct vnode
*, void *, int, int, struct fs
*,
110 static int snapacct(struct vnode
*, void *, int, int, struct fs
*,
112 static int mapacct(struct vnode
*, void *, int, int, struct fs
*,
114 #endif /* !defined(FFS_NO_SNAPSHOT) */
116 static int ffs_copyonwrite(void *, struct buf
*, bool);
117 static int snapblkaddr(struct vnode
*, daddr_t
, daddr_t
*);
118 static int rwfsblk(struct vnode
*, int, void *, daddr_t
);
119 static int syncsnap(struct vnode
*);
120 static int wrsnapblk(struct vnode
*, void *, daddr_t
);
121 #if !defined(FFS_NO_SNAPSHOT)
122 static int blocks_in_journal(struct fs
*);
125 static inline bool is_active_snapshot(struct snap_info
*, struct inode
*);
126 static inline daddr_t
db_get(struct inode
*, int);
127 static inline void db_assign(struct inode
*, int, daddr_t
);
128 static inline daddr_t
ib_get(struct inode
*, int);
129 static inline daddr_t
idb_get(struct inode
*, void *, int);
130 static inline void idb_assign(struct inode
*, void *, int, daddr_t
);
133 static int snapdebug
= 0;
137 ffs_snapshot_init(struct ufsmount
*ump
)
139 struct snap_info
*si
;
141 si
= ump
->um_snapinfo
= kmem_alloc(sizeof(*si
), KM_SLEEP
);
145 TAILQ_INIT(&si
->si_snapshots
);
146 mutex_init(&si
->si_lock
, MUTEX_DEFAULT
, IPL_NONE
);
147 mutex_init(&si
->si_snaplock
, MUTEX_DEFAULT
, IPL_NONE
);
150 si
->si_snapblklist
= NULL
;
156 ffs_snapshot_fini(struct ufsmount
*ump
)
158 struct snap_info
*si
;
160 si
= ump
->um_snapinfo
;
161 ump
->um_snapinfo
= NULL
;
163 KASSERT(TAILQ_EMPTY(&si
->si_snapshots
));
164 mutex_destroy(&si
->si_lock
);
165 mutex_destroy(&si
->si_snaplock
);
166 KASSERT(si
->si_snapblklist
== NULL
);
167 kmem_free(si
, sizeof(*si
));
171 * Create a snapshot file and initialize it for the filesystem.
172 * Vnode is locked on entry and return.
175 ffs_snapshot(struct mount
*mp
, struct vnode
*vp
, struct timespec
*ctime
)
177 #if defined(FFS_NO_SNAPSHOT)
180 #else /* defined(FFS_NO_SNAPSHOT) */
181 bool suspended
= false;
182 int error
, redo
= 0, snaploc
;
184 daddr_t
*snaplist
= NULL
, snaplistsize
= 0;
185 struct buf
*bp
, *nbp
;
186 struct fs
*copy_fs
= NULL
;
187 struct fs
*fs
= VFSTOUFS(mp
)->um_fs
;
188 struct inode
*ip
= VTOI(vp
);
189 struct lwp
*l
= curlwp
;
190 struct snap_info
*si
= VFSTOUFS(mp
)->um_snapinfo
;
192 struct timeval starttime
;
194 struct timeval endtime
;
196 struct vnode
*devvp
= ip
->i_devvp
;
199 * If the vnode already is a snapshot, return.
201 if ((ip
->i_flags
& SF_SNAPSHOT
)) {
202 if ((ip
->i_flags
& SF_SNAPINVAL
))
205 ctime
->tv_sec
= DIP(ip
, mtime
);
206 ctime
->tv_nsec
= DIP(ip
, mtimensec
);
211 * Check for free snapshot slot in the superblock.
213 for (snaploc
= 0; snaploc
< FSMAXSNAP
; snaploc
++)
214 if (fs
->fs_snapinum
[snaploc
] == 0)
216 if (snaploc
== FSMAXSNAP
)
219 * Prepare the vnode to become a snapshot.
221 error
= snapshot_setup(mp
, vp
);
226 * Copy all the cylinder group maps. Although the
227 * filesystem is still active, we hope that only a few
228 * cylinder groups will change between now and when we
229 * suspend operations. Thus, we will be able to quickly
230 * touch up the few cylinder groups that changed during
231 * the suspension period.
233 error
= cgaccount(vp
, 1, NULL
);
238 * snapshot is now valid
240 ip
->i_flags
&= ~SF_SNAPINVAL
;
241 DIP_ASSIGN(ip
, flags
, ip
->i_flags
);
242 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
245 * Ensure that the snapshot is completely on disk.
246 * Since we have marked it as a snapshot it is safe to
247 * unlock it as no process will be allowed to write to it.
249 error
= VOP_FSYNC(vp
, l
->l_cred
, FSYNC_WAIT
, 0, 0);
254 * All allocations are done, so we can now suspend the filesystem.
256 error
= vfs_suspend(vp
->v_mount
, 0);
257 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
261 getmicrotime(&starttime
);
263 * First, copy all the cylinder group maps that have changed.
265 error
= cgaccount(vp
, 2, &redo
);
269 * Create a copy of the superblock and its summary information.
271 error
= snapshot_copyfs(mp
, vp
, &sbbuf
);
274 copy_fs
= (struct fs
*)((char *)sbbuf
+ ffs_blkoff(fs
, fs
->fs_sblockloc
));
276 * Expunge unlinked files from our view.
278 error
= snapshot_expunge(mp
, vp
, copy_fs
, &snaplistsize
, &snaplist
);
282 * Record snapshot inode. Since this is the newest snapshot,
283 * it must be placed at the end of the list.
286 fs
->fs_snapinum
[snaploc
] = ip
->i_number
;
288 mutex_enter(&si
->si_lock
);
289 if (is_active_snapshot(si
, ip
))
290 panic("ffs_snapshot: %"PRIu64
" already on list", ip
->i_number
);
291 TAILQ_INSERT_TAIL(&si
->si_snapshots
, ip
, i_nextsnap
);
292 if (TAILQ_FIRST(&si
->si_snapshots
) == ip
) {
294 * If this is the first snapshot on this filesystem, put the
295 * preliminary list in place and establish the cow handler.
297 si
->si_snapblklist
= snaplist
;
298 fscow_establish(mp
, ffs_copyonwrite
, devvp
);
301 mutex_exit(&si
->si_lock
);
303 vp
->v_vflag
|= VV_SYSTEM
;
305 * Set the mtime to the time the snapshot has been taken.
307 TIMEVAL_TO_TIMESPEC(&starttime
, &ts
);
310 DIP_ASSIGN(ip
, mtime
, ts
.tv_sec
);
311 DIP_ASSIGN(ip
, mtimensec
, ts
.tv_nsec
);
312 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
314 * Copy allocation information from all snapshots and then
315 * expunge them from our view.
317 error
= snapshot_expunge_snap(mp
, vp
, copy_fs
, snaplistsize
);
321 * Write the superblock and its summary information to the snapshot.
323 error
= snapshot_writefs(mp
, vp
, sbbuf
);
327 * We're nearly done, ensure that the snapshot is completely on disk.
329 error
= VOP_FSYNC(vp
, l
->l_cred
, FSYNC_WAIT
, 0, 0);
333 * Invalidate and free all pages on the snapshot vnode.
334 * We will read and write through the buffercache.
336 mutex_enter(vp
->v_interlock
);
337 error
= VOP_PUTPAGES(vp
, 0, 0,
338 PGO_ALLPAGES
| PGO_CLEANIT
| PGO_SYNCIO
| PGO_FREE
);
342 * Invalidate short ( < fs_bsize ) buffers. We will always read
343 * full size buffers later.
345 mutex_enter(&bufcache_lock
);
346 KASSERT(LIST_FIRST(&vp
->v_dirtyblkhd
) == NULL
);
347 for (bp
= LIST_FIRST(&vp
->v_cleanblkhd
); bp
; bp
= nbp
) {
348 nbp
= LIST_NEXT(bp
, b_vnbufs
);
349 if (bp
->b_bcount
== fs
->fs_bsize
)
351 error
= bbusy(bp
, false, 0, NULL
);
353 if (error
== EPASSTHROUGH
) {
354 nbp
= LIST_FIRST(&vp
->v_cleanblkhd
);
359 brelsel(bp
, BC_INVAL
| BC_VFLUSH
);
361 mutex_exit(&bufcache_lock
);
365 free(copy_fs
->fs_csp
, M_UFSMNT
);
366 free(sbbuf
, M_UFSMNT
);
368 if (fs
->fs_active
!= NULL
) {
369 free(fs
->fs_active
, M_DEVBUF
);
370 fs
->fs_active
= NULL
;
373 mutex_enter(&si
->si_lock
);
374 if (snaplist
!= NULL
) {
375 if (si
->si_snapblklist
== snaplist
)
376 si
->si_snapblklist
= NULL
;
377 free(snaplist
, M_UFSMNT
);
380 fs
->fs_snapinum
[snaploc
] = 0;
383 * As this is the newest list, it is the most inclusive, so
384 * should replace the previous list.
386 si
->si_snapblklist
= ip
->i_snapblklist
;
389 mutex_exit(&si
->si_lock
);
393 vfs_resume(vp
->v_mount
);
394 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
396 getmicrotime(&endtime
);
397 timersub(&endtime
, &starttime
, &endtime
);
398 printf("%s: suspended %lld.%03d sec, redo %d of %d\n",
399 mp
->mnt_stat
.f_mntonname
, (long long)endtime
.tv_sec
,
400 endtime
.tv_usec
/ 1000, redo
, fs
->fs_ncg
);
404 if (UFS_WAPBL_BEGIN(mp
) == 0) {
405 (void) ffs_truncate(vp
, (off_t
)0, 0, NOCRED
);
408 } else if (ip
->i_nlink
> 0)
414 * Prepare vnode to become a snapshot.
417 snapshot_setup(struct mount
*mp
, struct vnode
*vp
)
419 int error
, n
, len
, loc
, cg
;
420 daddr_t blkno
, numblks
;
421 struct buf
*ibp
, *nbp
;
422 struct fs
*fs
= VFSTOUFS(mp
)->um_fs
;
423 struct lwp
*l
= curlwp
;
424 const int wbreak
= blocks_in_journal(fs
)/8;
425 struct inode
*ip
= VTOI(vp
);
428 * Check mount, readonly reference and owner.
430 if (vp
->v_mount
!= mp
)
432 if (vp
->v_writecount
!= 0)
434 error
= kauth_authorize_system(l
->l_cred
, KAUTH_SYSTEM_FS_SNAPSHOT
,
439 if (vp
->v_size
!= 0) {
440 error
= ffs_truncate(vp
, 0, 0, NOCRED
);
445 /* Change inode to snapshot type file. */
446 error
= UFS_WAPBL_BEGIN(mp
);
449 #if defined(QUOTA) || defined(QUOTA2)
450 /* shapshot inodes are not accounted in quotas */
451 chkiq(ip
, -1, l
->l_cred
, 0);
453 ip
->i_flags
|= (SF_SNAPSHOT
| SF_SNAPINVAL
);
454 DIP_ASSIGN(ip
, flags
, ip
->i_flags
);
455 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
456 ffs_update(vp
, NULL
, NULL
, UPDATE_WAIT
);
459 KASSERT(ip
->i_flags
& SF_SNAPSHOT
);
461 * Write an empty list of preallocated blocks to the end of
462 * the snapshot to set size to at least that of the filesystem.
464 numblks
= howmany(fs
->fs_size
, fs
->fs_frag
);
466 blkno
= ufs_rw64(blkno
, UFS_FSNEEDSWAP(fs
));
467 error
= vn_rdwr(UIO_WRITE
, vp
,
468 (void *)&blkno
, sizeof(blkno
), ffs_lblktosize(fs
, (off_t
)numblks
),
469 UIO_SYSSPACE
, IO_NODELOCKED
|IO_UNIT
, l
->l_cred
, NULL
, NULL
);
473 * Preallocate critical data structures so that we can copy
474 * them in without further allocation after we suspend all
475 * operations on the filesystem. We would like to just release
476 * the allocated buffers without writing them since they will
477 * be filled in below once we are ready to go, but this upsets
478 * the soft update code, so we go ahead and write the new buffers.
480 * Allocate all indirect blocks and mark all of them as not
481 * needing to be copied.
483 error
= UFS_WAPBL_BEGIN(mp
);
486 for (blkno
= UFS_NDADDR
, n
= 0; blkno
< numblks
; blkno
+= FFS_NINDIR(fs
)) {
487 error
= ffs_balloc(vp
, ffs_lblktosize(fs
, (off_t
)blkno
),
488 fs
->fs_bsize
, l
->l_cred
, B_METAONLY
, &ibp
);
492 if (wbreak
> 0 && (++n
% wbreak
) == 0) {
494 error
= UFS_WAPBL_BEGIN(mp
);
500 * Allocate copies for the superblock and its summary information.
502 error
= ffs_balloc(vp
, fs
->fs_sblockloc
, fs
->fs_sbsize
, l
->l_cred
,
507 blkno
= ffs_fragstoblks(fs
, fs
->fs_csaddr
);
508 len
= howmany(fs
->fs_cssize
, fs
->fs_bsize
);
509 for (loc
= 0; loc
< len
; loc
++) {
510 error
= ffs_balloc(vp
, ffs_lblktosize(fs
, (off_t
)(blkno
+ loc
)),
511 fs
->fs_bsize
, l
->l_cred
, 0, &nbp
);
515 if (wbreak
> 0 && (++n
% wbreak
) == 0) {
517 error
= UFS_WAPBL_BEGIN(mp
);
523 * Allocate all cylinder group blocks.
525 for (cg
= 0; cg
< fs
->fs_ncg
; cg
++) {
526 error
= ffs_balloc(vp
, ffs_lfragtosize(fs
, cgtod(fs
, cg
)),
527 fs
->fs_bsize
, l
->l_cred
, 0, &nbp
);
531 if (wbreak
> 0 && (++n
% wbreak
) == 0) {
533 error
= UFS_WAPBL_BEGIN(mp
);
545 * Create a copy of the superblock and its summary information.
546 * It is up to the caller to free copyfs and copy_fs->fs_csp.
549 snapshot_copyfs(struct mount
*mp
, struct vnode
*vp
, void **sbbuf
)
551 int error
, i
, len
, loc
, size
;
555 struct fs
*copyfs
, *fs
= VFSTOUFS(mp
)->um_fs
;
556 struct vnode
*devvp
= VTOI(vp
)->i_devvp
;
559 * Grab a copy of the superblock and its summary information.
560 * We delay writing it until the suspension is released below.
562 *sbbuf
= malloc(fs
->fs_bsize
, M_UFSMNT
, M_WAITOK
);
563 loc
= ffs_blkoff(fs
, fs
->fs_sblockloc
);
565 memset(*sbbuf
, 0, loc
);
566 copyfs
= (struct fs
*)((char *)(*sbbuf
) + loc
);
567 memcpy(copyfs
, fs
, fs
->fs_sbsize
);
568 size
= fs
->fs_bsize
< SBLOCKSIZE
? fs
->fs_bsize
: SBLOCKSIZE
;
569 if (fs
->fs_sbsize
< size
)
570 memset((char *)(*sbbuf
) + loc
+ fs
->fs_sbsize
, 0,
571 size
- fs
->fs_sbsize
);
572 size
= ffs_blkroundup(fs
, fs
->fs_cssize
);
573 if (fs
->fs_contigsumsize
> 0)
574 size
+= fs
->fs_ncg
* sizeof(int32_t);
575 space
= malloc(size
, M_UFSMNT
, M_WAITOK
);
576 copyfs
->fs_csp
= space
;
577 memcpy(copyfs
->fs_csp
, fs
->fs_csp
, fs
->fs_cssize
);
578 space
= (char *)space
+ fs
->fs_cssize
;
579 loc
= howmany(fs
->fs_cssize
, fs
->fs_fsize
);
580 i
= fs
->fs_frag
- loc
% fs
->fs_frag
;
581 len
= (i
== fs
->fs_frag
) ? 0 : i
* fs
->fs_fsize
;
583 if ((error
= bread(devvp
, FFS_FSBTODB(fs
, fs
->fs_csaddr
+ loc
),
584 len
, 0, &bp
)) != 0) {
585 free(copyfs
->fs_csp
, M_UFSMNT
);
586 free(*sbbuf
, M_UFSMNT
);
590 memcpy(space
, bp
->b_data
, (u_int
)len
);
591 space
= (char *)space
+ len
;
592 brelse(bp
, BC_INVAL
| BC_NOCACHE
);
594 if (fs
->fs_contigsumsize
> 0) {
595 copyfs
->fs_maxcluster
= lp
= space
;
596 for (i
= 0; i
< fs
->fs_ncg
; i
++)
597 *lp
++ = fs
->fs_contigsumsize
;
600 copyfs
->fs_flags
&= ~FS_DOWAPBL
;
604 struct snapshot_expunge_ctx
{
612 snapshot_expunge_selector(void *cl
, struct vnode
*xvp
)
615 struct snapshot_expunge_ctx
*c
= cl
;
619 if (xvp
->v_type
== VNON
|| VTOI(xvp
) == NULL
||
620 (xp
->i_flags
& SF_SNAPSHOT
))
624 vprint("ffs_snapshot: busy vnode", xvp
);
630 if (VOP_GETATTR(xvp
, &vat
, c
->l
->l_cred
) == 0 &&
634 if (ffs_checkfreefile(c
->copy_fs
, c
->vp
, xp
->i_number
))
641 * We must check for active files that have been unlinked (e.g., with a zero
642 * link count). We have to expunge all trace of these files from the snapshot
643 * so that they are not reclaimed prematurely by fsck or unnecessarily dumped.
644 * Note that we skip unlinked snapshot files as they will be handled separately.
645 * Calculate the snapshot list size and create a preliminary list.
648 snapshot_expunge(struct mount
*mp
, struct vnode
*vp
, struct fs
*copy_fs
,
649 daddr_t
*snaplistsize
, daddr_t
**snaplist
)
651 int cg
, error
= 0, len
, loc
;
652 daddr_t blkno
, *blkp
;
653 struct fs
*fs
= VFSTOUFS(mp
)->um_fs
;
655 struct lwp
*l
= curlwp
;
656 struct vnode
*logvp
= NULL
, *xvp
;
657 struct vnode_iterator
*marker
;
658 struct snapshot_expunge_ctx ctx
;
662 * Get the log inode if any.
664 if ((fs
->fs_flags
& FS_DOWAPBL
) &&
665 fs
->fs_journal_location
== UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM
) {
667 fs
->fs_journallocs
[UFS_WAPBL_INFS_INO
], &logvp
);
672 * We also calculate the needed size for the snapshot list.
674 *snaplistsize
= fs
->fs_ncg
+ howmany(fs
->fs_cssize
, fs
->fs_bsize
) +
675 FSMAXSNAP
+ 1 /* superblock */ + 1 /* last block */ + 1 /* size */;
677 vfs_vnode_iterator_init(mp
, &marker
);
681 ctx
.copy_fs
= copy_fs
;
682 while ((xvp
= vfs_vnode_iterator_next(marker
, snapshot_expunge_selector
,
686 * If there is a fragment, clear it here.
690 loc
= howmany(xp
->i_size
, fs
->fs_bsize
) - 1;
691 if (loc
< UFS_NDADDR
) {
692 len
= ffs_fragroundup(fs
, ffs_blkoff(fs
, xp
->i_size
));
693 if (len
> 0 && len
< fs
->fs_bsize
) {
694 error
= UFS_WAPBL_BEGIN(mp
);
697 vfs_vnode_iterator_destroy(marker
);
700 ffs_blkfree_snap(copy_fs
, vp
, db_get(xp
, loc
),
702 blkno
= db_get(xp
, loc
);
703 db_assign(xp
, loc
, 0);
708 error
= expunge(vp
, xp
, copy_fs
, fullacct
, BLK_NOCOPY
);
710 db_assign(xp
, loc
, blkno
);
712 error
= UFS_WAPBL_BEGIN(mp
);
714 error
= ffs_freefile_snap(copy_fs
, vp
,
715 xp
->i_number
, xp
->i_mode
);
721 vfs_vnode_iterator_destroy(marker
);
725 vfs_vnode_iterator_destroy(marker
);
728 * Create a preliminary list of preallocated snapshot blocks.
730 *snaplist
= malloc(*snaplistsize
* sizeof(daddr_t
), M_UFSMNT
, M_WAITOK
);
731 blkp
= &(*snaplist
)[1];
732 *blkp
++ = ffs_lblkno(fs
, fs
->fs_sblockloc
);
733 blkno
= ffs_fragstoblks(fs
, fs
->fs_csaddr
);
734 for (cg
= 0; cg
< fs
->fs_ncg
; cg
++) {
735 if (ffs_fragstoblks(fs
, cgtod(fs
, cg
)) > blkno
)
737 *blkp
++ = ffs_fragstoblks(fs
, cgtod(fs
, cg
));
739 len
= howmany(fs
->fs_cssize
, fs
->fs_bsize
);
740 for (loc
= 0; loc
< len
; loc
++)
741 *blkp
++ = blkno
+ loc
;
742 for (; cg
< fs
->fs_ncg
; cg
++)
743 *blkp
++ = ffs_fragstoblks(fs
, cgtod(fs
, cg
));
744 (*snaplist
)[0] = blkp
- &(*snaplist
)[0];
749 if (error
&& *snaplist
!= NULL
) {
750 free(*snaplist
, M_UFSMNT
);
758 * Copy allocation information from all the snapshots in this snapshot and
759 * then expunge them from its view. Also, collect the list of allocated
760 * blocks in i_snapblklist.
763 snapshot_expunge_snap(struct mount
*mp
, struct vnode
*vp
,
764 struct fs
*copy_fs
, daddr_t snaplistsize
)
767 daddr_t numblks
, *snaplist
= NULL
;
768 struct fs
*fs
= VFSTOUFS(mp
)->um_fs
;
769 struct inode
*ip
= VTOI(vp
), *xp
;
770 struct lwp
*l
= curlwp
;
771 struct snap_info
*si
= VFSTOUFS(mp
)->um_snapinfo
;
773 TAILQ_FOREACH(xp
, &si
->si_snapshots
, i_nextsnap
) {
775 error
= expunge(vp
, xp
, fs
, snapacct
, BLK_SNAP
);
779 if (xp
->i_nlink
!= 0)
781 error
= UFS_WAPBL_BEGIN(mp
);
784 error
= ffs_freefile_snap(copy_fs
, vp
, xp
->i_number
, xp
->i_mode
);
792 * Allocate space for the full list of preallocated snapshot blocks.
794 snaplist
= malloc(snaplistsize
* sizeof(daddr_t
), M_UFSMNT
, M_WAITOK
);
795 ip
->i_snapblklist
= &snaplist
[1];
797 * Expunge the blocks used by the snapshots from the set of
798 * blocks marked as used in the snapshot bitmaps. Also, collect
799 * the list of allocated blocks in i_snapblklist.
801 error
= expunge(vp
, ip
, copy_fs
, mapacct
, BLK_SNAP
);
804 if (snaplistsize
< ip
->i_snapblklist
- snaplist
)
805 panic("ffs_snapshot: list too small");
806 snaplistsize
= ip
->i_snapblklist
- snaplist
;
807 snaplist
[0] = snaplistsize
;
808 ip
->i_snapblklist
= &snaplist
[0];
810 * Write out the list of allocated blocks to the end of the snapshot.
812 numblks
= howmany(fs
->fs_size
, fs
->fs_frag
);
813 for (i
= 0; i
< snaplistsize
; i
++)
814 snaplist
[i
] = ufs_rw64(snaplist
[i
], UFS_FSNEEDSWAP(fs
));
815 error
= vn_rdwr(UIO_WRITE
, vp
, (void *)snaplist
,
816 snaplistsize
* sizeof(daddr_t
), ffs_lblktosize(fs
, (off_t
)numblks
),
817 UIO_SYSSPACE
, IO_NODELOCKED
| IO_UNIT
, l
->l_cred
, NULL
, NULL
);
818 for (i
= 0; i
< snaplistsize
; i
++)
819 snaplist
[i
] = ufs_rw64(snaplist
[i
], UFS_FSNEEDSWAP(fs
));
821 if (error
&& snaplist
!= NULL
) {
822 free(snaplist
, M_UFSMNT
);
823 ip
->i_snapblklist
= NULL
;
829 * Write the superblock and its summary information to the snapshot.
830 * Make sure, the first UFS_NDADDR blocks get copied to the snapshot.
833 snapshot_writefs(struct mount
*mp
, struct vnode
*vp
, void *sbbuf
)
839 struct fs
*copyfs
, *fs
= VFSTOUFS(mp
)->um_fs
;
840 struct inode
*ip
= VTOI(vp
);
841 struct lwp
*l
= curlwp
;
843 copyfs
= (struct fs
*)((char *)sbbuf
+ ffs_blkoff(fs
, fs
->fs_sblockloc
));
846 * Write the superblock and its summary information
849 blkno
= ffs_fragstoblks(fs
, fs
->fs_csaddr
);
850 len
= howmany(fs
->fs_cssize
, fs
->fs_bsize
);
851 space
= copyfs
->fs_csp
;
853 if (UFS_FSNEEDSWAP(fs
)) {
854 ffs_sb_swap(copyfs
, copyfs
);
855 ffs_csum_swap(space
, space
, fs
->fs_cssize
);
858 error
= UFS_WAPBL_BEGIN(mp
);
861 for (loc
= 0; loc
< len
; loc
++) {
862 error
= bread(vp
, blkno
+ loc
, fs
->fs_bsize
,
867 memcpy(bp
->b_data
, space
, fs
->fs_bsize
);
868 space
= (char *)space
+ fs
->fs_bsize
;
873 error
= bread(vp
, ffs_lblkno(fs
, fs
->fs_sblockloc
),
874 fs
->fs_bsize
, B_MODIFY
, &bp
);
878 memcpy(bp
->b_data
, sbbuf
, fs
->fs_bsize
);
882 * Copy the first UFS_NDADDR blocks to the snapshot so
883 * ffs_copyonwrite() and ffs_snapblkfree() will always work on
886 for (loc
= 0; loc
< UFS_NDADDR
; loc
++) {
887 if (db_get(ip
, loc
) != 0)
889 error
= ffs_balloc(vp
, ffs_lblktosize(fs
, (off_t
)loc
),
890 fs
->fs_bsize
, l
->l_cred
, 0, &bp
);
893 error
= rwfsblk(vp
, B_READ
, bp
->b_data
, loc
);
907 * Copy all cylinder group maps.
910 cgaccount(struct vnode
*vp
, int passno
, int *redo
)
914 struct fs
*fs
= VTOI(vp
)->i_fs
;
919 fs
->fs_active
= malloc(howmany(fs
->fs_ncg
, NBBY
),
920 M_DEVBUF
, M_WAITOK
| M_ZERO
);
921 for (cg
= 0; cg
< fs
->fs_ncg
; cg
++) {
922 if (passno
== 2 && ACTIVECG_ISSET(fs
, cg
))
927 error
= UFS_WAPBL_BEGIN(vp
->v_mount
);
930 error
= ffs_balloc(vp
, ffs_lfragtosize(fs
, cgtod(fs
, cg
)),
931 fs
->fs_bsize
, curlwp
->l_cred
, 0, &nbp
);
933 UFS_WAPBL_END(vp
->v_mount
);
936 error
= cgaccount1(cg
, vp
, nbp
->b_data
, passno
);
938 UFS_WAPBL_END(vp
->v_mount
);
946 * Copy a cylinder group map. All the unallocated blocks are marked
947 * BLK_NOCOPY so that the snapshot knows that it need not copy them
948 * if they are later written. If passno is one, then this is a first
949 * pass, so only setting needs to be done. If passno is 2, then this
950 * is a revision to a previous pass which must be undone as the
951 * replacement pass is done.
954 cgaccount1(int cg
, struct vnode
*vp
, void *data
, int passno
)
956 struct buf
*bp
, *ibp
;
960 struct lwp
*l
= curlwp
;
961 daddr_t base
, numblks
;
962 int error
, len
, loc
, ns __unused
, indiroff
;
966 ns
= UFS_FSNEEDSWAP(fs
);
967 error
= bread(ip
->i_devvp
, FFS_FSBTODB(fs
, cgtod(fs
, cg
)),
968 (int)fs
->fs_cgsize
, 0, &bp
);
972 cgp
= (struct cg
*)bp
->b_data
;
973 if (!cg_chkmagic(cgp
, ns
)) {
977 ACTIVECG_SET(fs
, cg
);
979 memcpy(data
, bp
->b_data
, fs
->fs_cgsize
);
981 if (fs
->fs_cgsize
< fs
->fs_bsize
)
982 memset((char *)data
+ fs
->fs_cgsize
, 0,
983 fs
->fs_bsize
- fs
->fs_cgsize
);
984 numblks
= howmany(fs
->fs_size
, fs
->fs_frag
);
985 len
= howmany(fs
->fs_fpg
, fs
->fs_frag
);
986 base
= cg
* fs
->fs_fpg
/ fs
->fs_frag
;
987 if (base
+ len
>= numblks
)
988 len
= numblks
- base
- 1;
990 if (base
< UFS_NDADDR
) {
991 for ( ; loc
< UFS_NDADDR
; loc
++) {
992 if (ffs_isblock(fs
, cg_blksfree(cgp
, ns
), loc
))
993 db_assign(ip
, loc
, BLK_NOCOPY
);
994 else if (db_get(ip
, loc
) == BLK_NOCOPY
) {
996 db_assign(ip
, loc
, 0);
997 else if (passno
== 1)
998 panic("ffs_snapshot: lost direct block");
1002 if ((error
= ffs_balloc(vp
, ffs_lblktosize(fs
, (off_t
)(base
+ loc
)),
1003 fs
->fs_bsize
, l
->l_cred
, B_METAONLY
, &ibp
)) != 0)
1005 indiroff
= (base
+ loc
- UFS_NDADDR
) % FFS_NINDIR(fs
);
1006 for ( ; loc
< len
; loc
++, indiroff
++) {
1007 if (indiroff
>= FFS_NINDIR(fs
)) {
1009 if ((error
= ffs_balloc(vp
,
1010 ffs_lblktosize(fs
, (off_t
)(base
+ loc
)),
1011 fs
->fs_bsize
, l
->l_cred
, B_METAONLY
, &ibp
)) != 0)
1015 if (ffs_isblock(fs
, cg_blksfree(cgp
, ns
), loc
))
1016 idb_assign(ip
, ibp
->b_data
, indiroff
, BLK_NOCOPY
);
1017 else if (idb_get(ip
, ibp
->b_data
, indiroff
) == BLK_NOCOPY
) {
1019 idb_assign(ip
, ibp
->b_data
, indiroff
, 0);
1020 else if (passno
== 1)
1021 panic("ffs_snapshot: lost indirect block");
1029 * Before expunging a snapshot inode, note all the
1030 * blocks that it claims with BLK_SNAP so that fsck will
1031 * be able to account for those blocks properly and so
1032 * that this snapshot knows that it need not copy them
1033 * if the other snapshot holding them is freed.
1036 expunge(struct vnode
*snapvp
, struct inode
*cancelip
, struct fs
*fs
,
1037 acctfunc_t acctfunc
, int expungetype
)
1039 int i
, error
, ns __unused
;
1041 daddr_t len
, blkno
, numblks
, blksperindir
;
1042 struct ufs1_dinode
*dip1
;
1043 struct ufs2_dinode
*dip2
;
1044 struct lwp
*l
= curlwp
;
1049 ns
= UFS_FSNEEDSWAP(fs
);
1050 mp
= snapvp
->v_mount
;
1052 error
= UFS_WAPBL_BEGIN(mp
);
1056 * Prepare to expunge the inode. If its inode block has not
1057 * yet been copied, then allocate and fill the copy.
1059 lbn
= ffs_fragstoblks(fs
, ino_to_fsba(fs
, cancelip
->i_number
));
1060 error
= snapblkaddr(snapvp
, lbn
, &blkno
);
1064 error
= bread(snapvp
, lbn
, fs
->fs_bsize
,
1067 error
= ffs_balloc(snapvp
, ffs_lblktosize(fs
, (off_t
)lbn
),
1068 fs
->fs_bsize
, l
->l_cred
, 0, &bp
);
1070 error
= rwfsblk(snapvp
, B_READ
, bp
->b_data
, lbn
);
1077 * Set a snapshot inode to be a zero length file, regular files
1078 * or unlinked snapshots to be completely unallocated.
1080 if (fs
->fs_magic
== FS_UFS1_MAGIC
) {
1081 dip1
= (struct ufs1_dinode
*)bp
->b_data
+
1082 ino_to_fsbo(fs
, cancelip
->i_number
);
1083 if (cancelip
->i_flags
& SF_SNAPSHOT
) {
1085 ufs_rw32(ufs_rw32(dip1
->di_flags
, ns
) |
1088 if (expungetype
== BLK_NOCOPY
|| cancelip
->i_nlink
== 0)
1091 dip1
->di_blocks
= 0;
1092 memset(&dip1
->di_db
[0], 0, (UFS_NDADDR
+ UFS_NIADDR
) * sizeof(int32_t));
1094 dip2
= (struct ufs2_dinode
*)bp
->b_data
+
1095 ino_to_fsbo(fs
, cancelip
->i_number
);
1096 if (cancelip
->i_flags
& SF_SNAPSHOT
) {
1098 ufs_rw32(ufs_rw32(dip2
->di_flags
, ns
) |
1101 if (expungetype
== BLK_NOCOPY
|| cancelip
->i_nlink
== 0)
1104 dip2
->di_blocks
= 0;
1105 memset(&dip2
->di_db
[0], 0, (UFS_NDADDR
+ UFS_NIADDR
) * sizeof(int64_t));
1110 * Now go through and expunge all the blocks in the file
1111 * using the function requested.
1113 numblks
= howmany(cancelip
->i_size
, fs
->fs_bsize
);
1114 if (fs
->fs_magic
== FS_UFS1_MAGIC
)
1115 bap
= &cancelip
->i_ffs1_db
[0];
1117 bap
= &cancelip
->i_ffs2_db
[0];
1118 error
= (*acctfunc
)(snapvp
, bap
, 0, UFS_NDADDR
, fs
, 0, expungetype
);
1121 if (fs
->fs_magic
== FS_UFS1_MAGIC
)
1122 bap
= &cancelip
->i_ffs1_ib
[0];
1124 bap
= &cancelip
->i_ffs2_ib
[0];
1125 error
= (*acctfunc
)(snapvp
, bap
, 0, UFS_NIADDR
, fs
, -1, expungetype
);
1130 len
= numblks
- UFS_NDADDR
;
1132 for (i
= 0; len
> 0 && i
< UFS_NIADDR
; i
++) {
1133 error
= indiracct(snapvp
, ITOV(cancelip
), i
,
1134 ib_get(cancelip
, i
), lbn
, rlbn
, len
,
1135 blksperindir
, fs
, acctfunc
, expungetype
);
1138 blksperindir
*= FFS_NINDIR(fs
);
1139 lbn
-= blksperindir
+ 1;
1140 len
-= blksperindir
;
1141 rlbn
+= blksperindir
;
1147 * Descend an indirect block chain for vnode cancelvp accounting for all
1148 * its indirect blocks in snapvp.
1151 indiracct(struct vnode
*snapvp
, struct vnode
*cancelvp
, int level
,
1152 daddr_t blkno
, daddr_t lbn
, daddr_t rlbn
, daddr_t remblks
,
1153 daddr_t blksperindir
, struct fs
*fs
, acctfunc_t acctfunc
, int expungetype
)
1156 daddr_t subblksperindir
;
1157 struct indir indirs
[UFS_NIADDR
+ 2];
1163 if (expungetype
== BLK_NOCOPY
)
1165 panic("indiracct: missing indir");
1167 if ((error
= ufs_getlbns(cancelvp
, rlbn
, indirs
, &num
)) != 0)
1169 if (lbn
!= indirs
[num
- 1 - level
].in_lbn
|| num
< 2)
1170 panic("indiracct: botched params");
1172 * We have to expand bread here since it will deadlock looking
1173 * up the block number for any blocks that are not in the cache.
1175 error
= ffs_getblk(cancelvp
, lbn
, FFS_FSBTODB(fs
, blkno
), fs
->fs_bsize
,
1179 if ((bp
->b_oflags
& (BO_DONE
| BO_DELWRI
)) == 0 && (error
=
1180 rwfsblk(bp
->b_vp
, B_READ
, bp
->b_data
, ffs_fragstoblks(fs
, blkno
)))) {
1185 * Account for the block pointers in this indirect block.
1187 last
= howmany(remblks
, blksperindir
);
1188 if (last
> FFS_NINDIR(fs
))
1189 last
= FFS_NINDIR(fs
);
1190 bap
= malloc(fs
->fs_bsize
, M_DEVBUF
, M_WAITOK
| M_ZERO
);
1191 memcpy((void *)bap
, bp
->b_data
, fs
->fs_bsize
);
1193 error
= (*acctfunc
)(snapvp
, bap
, 0, last
,
1194 fs
, level
== 0 ? rlbn
: -1, expungetype
);
1195 if (error
|| level
== 0)
1198 * Account for the block pointers in each of the indirect blocks
1199 * in the levels below us.
1201 subblksperindir
= blksperindir
/ FFS_NINDIR(fs
);
1202 for (lbn
++, level
--, i
= 0; i
< last
; i
++) {
1203 error
= indiracct(snapvp
, cancelvp
, level
,
1204 idb_get(VTOI(snapvp
), bap
, i
), lbn
, rlbn
, remblks
,
1205 subblksperindir
, fs
, acctfunc
, expungetype
);
1208 rlbn
+= blksperindir
;
1209 lbn
-= blksperindir
;
1210 remblks
-= blksperindir
;
1213 free(bap
, M_DEVBUF
);
1218 * Do both snap accounting and map accounting.
1221 fullacct(struct vnode
*vp
, void *bap
, int oldblkp
, int lastblkp
,
1222 struct fs
*fs
, daddr_t lblkno
,
1223 int exptype
/* BLK_SNAP or BLK_NOCOPY */)
1227 if ((error
= snapacct(vp
, bap
, oldblkp
, lastblkp
, fs
, lblkno
, exptype
)))
1229 return (mapacct(vp
, bap
, oldblkp
, lastblkp
, fs
, lblkno
, exptype
));
1233 * Identify a set of blocks allocated in a snapshot inode.
1236 snapacct(struct vnode
*vp
, void *bap
, int oldblkp
, int lastblkp
,
1237 struct fs
*fs
, daddr_t lblkno
,
1238 int expungetype
/* BLK_SNAP or BLK_NOCOPY */)
1240 struct inode
*ip
= VTOI(vp
);
1241 struct lwp
*l
= curlwp
;
1242 struct mount
*mp
= vp
->v_mount
;
1247 const int wbreak
= blocks_in_journal(VFSTOUFS(mp
)->um_fs
)/8;
1249 error
= UFS_WAPBL_BEGIN(mp
);
1252 for ( n
= 0; oldblkp
< lastblkp
; oldblkp
++) {
1253 blkno
= idb_get(ip
, bap
, oldblkp
);
1254 if (blkno
== 0 || blkno
== BLK_NOCOPY
|| blkno
== BLK_SNAP
)
1256 lbn
= ffs_fragstoblks(fs
, blkno
);
1257 if (lbn
< UFS_NDADDR
) {
1258 blkno
= db_get(ip
, lbn
);
1259 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
1261 error
= ffs_balloc(vp
, ffs_lblktosize(fs
, (off_t
)lbn
),
1262 fs
->fs_bsize
, l
->l_cred
, B_METAONLY
, &ibp
);
1265 blkno
= idb_get(ip
, ibp
->b_data
,
1266 (lbn
- UFS_NDADDR
) % FFS_NINDIR(fs
));
1269 * If we are expunging a snapshot vnode and we
1270 * find a block marked BLK_NOCOPY, then it is
1271 * one that has been allocated to this snapshot after
1272 * we took our current snapshot and can be ignored.
1274 if (expungetype
== BLK_SNAP
&& blkno
== BLK_NOCOPY
) {
1275 if (lbn
>= UFS_NDADDR
)
1279 panic("snapacct: bad block");
1280 if (lbn
< UFS_NDADDR
)
1281 db_assign(ip
, lbn
, expungetype
);
1283 idb_assign(ip
, ibp
->b_data
,
1284 (lbn
- UFS_NDADDR
) % FFS_NINDIR(fs
), expungetype
);
1288 if (wbreak
> 0 && (++n
% wbreak
) == 0) {
1290 error
= UFS_WAPBL_BEGIN(mp
);
1300 * Account for a set of blocks allocated in a snapshot inode.
1303 mapacct(struct vnode
*vp
, void *bap
, int oldblkp
, int lastblkp
,
1304 struct fs
*fs
, daddr_t lblkno
, int expungetype
)
1308 struct mount
*mp
= vp
->v_mount
;
1310 int acctit
, error
, n
;
1311 const int wbreak
= blocks_in_journal(VFSTOUFS(mp
)->um_fs
)/8;
1313 error
= UFS_WAPBL_BEGIN(mp
);
1317 inum
= ip
->i_number
;
1322 for ( n
= 0; oldblkp
< lastblkp
; oldblkp
++, lblkno
++) {
1323 blkno
= idb_get(ip
, bap
, oldblkp
);
1324 if (blkno
== 0 || blkno
== BLK_NOCOPY
)
1326 if (acctit
&& expungetype
== BLK_SNAP
&& blkno
!= BLK_SNAP
)
1327 *ip
->i_snapblklist
++ = lblkno
;
1328 if (blkno
== BLK_SNAP
)
1329 blkno
= ffs_blkstofrags(fs
, lblkno
);
1330 ffs_blkfree_snap(fs
, vp
, blkno
, fs
->fs_bsize
, inum
);
1331 if (wbreak
> 0 && (++n
% wbreak
) == 0) {
1333 error
= UFS_WAPBL_BEGIN(mp
);
1343 * Number of blocks that fit into the journal or zero if not logging.
1346 blocks_in_journal(struct fs
*fs
)
1350 if ((fs
->fs_flags
& FS_DOWAPBL
) == 0)
1353 if (fs
->fs_journal_version
== UFS_WAPBL_VERSION
) {
1354 switch (fs
->fs_journal_location
) {
1355 case UFS_WAPBL_JOURNALLOC_END_PARTITION
:
1356 bpj
= (off_t
)fs
->fs_journallocs
[UFS_WAPBL_EPART_BLKSZ
]*
1357 fs
->fs_journallocs
[UFS_WAPBL_EPART_COUNT
];
1359 case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM
:
1360 bpj
= (off_t
)fs
->fs_journallocs
[UFS_WAPBL_INFS_BLKSZ
]*
1361 fs
->fs_journallocs
[UFS_WAPBL_INFS_COUNT
];
1365 bpj
/= fs
->fs_bsize
;
1366 return (bpj
> 0 ? bpj
: 1);
1368 #endif /* defined(FFS_NO_SNAPSHOT) */
1371 * Decrement extra reference on snapshot when last name is removed.
1372 * It will not be freed until the last open reference goes away.
1375 ffs_snapgone(struct vnode
*vp
)
1377 struct inode
*xp
, *ip
= VTOI(vp
);
1378 struct mount
*mp
= spec_node_getmountedfs(ip
->i_devvp
);
1380 struct snap_info
*si
;
1383 si
= VFSTOUFS(mp
)->um_snapinfo
;
1386 * Find snapshot in incore list.
1388 mutex_enter(&si
->si_lock
);
1389 TAILQ_FOREACH(xp
, &si
->si_snapshots
, i_nextsnap
)
1392 mutex_exit(&si
->si_lock
);
1397 printf("ffs_snapgone: lost snapshot vnode %llu\n",
1398 (unsigned long long)ip
->i_number
);
1401 * Delete snapshot inode from superblock. Keep list dense.
1403 mutex_enter(&si
->si_lock
);
1405 for (snaploc
= 0; snaploc
< FSMAXSNAP
; snaploc
++)
1406 if (fs
->fs_snapinum
[snaploc
] == ip
->i_number
)
1408 if (snaploc
< FSMAXSNAP
) {
1409 for (snaploc
++; snaploc
< FSMAXSNAP
; snaploc
++) {
1410 if (fs
->fs_snapinum
[snaploc
] == 0)
1412 fs
->fs_snapinum
[snaploc
- 1] = fs
->fs_snapinum
[snaploc
];
1414 fs
->fs_snapinum
[snaploc
- 1] = 0;
1417 mutex_exit(&si
->si_lock
);
1421 * Prepare a snapshot file for being removed.
1424 ffs_snapremove(struct vnode
*vp
)
1426 struct inode
*ip
= VTOI(vp
), *xp
;
1427 struct vnode
*devvp
= ip
->i_devvp
;
1428 struct fs
*fs
= ip
->i_fs
;
1429 struct mount
*mp
= spec_node_getmountedfs(devvp
);
1431 struct snap_info
*si
;
1432 struct lwp
*l
= curlwp
;
1433 daddr_t numblks
, blkno
, dblk
;
1434 int error
, loc
, last
;
1436 si
= VFSTOUFS(mp
)->um_snapinfo
;
1438 * If active, delete from incore list (this snapshot may
1439 * already have been in the process of being deleted, so
1440 * would not have been active).
1442 * Clear copy-on-write flag if last snapshot.
1444 mutex_enter(&si
->si_snaplock
);
1445 mutex_enter(&si
->si_lock
);
1446 if (is_active_snapshot(si
, ip
)) {
1447 TAILQ_REMOVE(&si
->si_snapshots
, ip
, i_nextsnap
);
1448 if (TAILQ_FIRST(&si
->si_snapshots
) != 0) {
1449 /* Roll back the list of preallocated blocks. */
1450 xp
= TAILQ_LAST(&si
->si_snapshots
, inodelst
);
1451 si
->si_snapblklist
= xp
->i_snapblklist
;
1453 mutex_exit(&si
->si_lock
);
1454 mutex_exit(&si
->si_snaplock
);
1456 si
->si_snapblklist
= 0;
1458 mutex_exit(&si
->si_lock
);
1459 mutex_exit(&si
->si_snaplock
);
1460 fscow_disestablish(mp
, ffs_copyonwrite
, devvp
);
1462 if (ip
->i_snapblklist
!= NULL
) {
1463 free(ip
->i_snapblklist
, M_UFSMNT
);
1464 ip
->i_snapblklist
= NULL
;
1467 mutex_exit(&si
->si_lock
);
1468 mutex_exit(&si
->si_snaplock
);
1471 * Clear all BLK_NOCOPY fields. Pass any block claims to other
1472 * snapshots that want them (see ffs_snapblkfree below).
1474 for (blkno
= 1; blkno
< UFS_NDADDR
; blkno
++) {
1475 dblk
= db_get(ip
, blkno
);
1476 if (dblk
== BLK_NOCOPY
|| dblk
== BLK_SNAP
)
1477 db_assign(ip
, blkno
, 0);
1478 else if ((dblk
== ffs_blkstofrags(fs
, blkno
) &&
1479 ffs_snapblkfree(fs
, ip
->i_devvp
, dblk
, fs
->fs_bsize
,
1481 DIP_ADD(ip
, blocks
, -btodb(fs
->fs_bsize
));
1482 db_assign(ip
, blkno
, 0);
1485 numblks
= howmany(ip
->i_size
, fs
->fs_bsize
);
1486 for (blkno
= UFS_NDADDR
; blkno
< numblks
; blkno
+= FFS_NINDIR(fs
)) {
1487 error
= ffs_balloc(vp
, ffs_lblktosize(fs
, (off_t
)blkno
),
1488 fs
->fs_bsize
, l
->l_cred
, B_METAONLY
, &ibp
);
1491 if (fs
->fs_size
- blkno
> FFS_NINDIR(fs
))
1492 last
= FFS_NINDIR(fs
);
1494 last
= fs
->fs_size
- blkno
;
1495 for (loc
= 0; loc
< last
; loc
++) {
1496 dblk
= idb_get(ip
, ibp
->b_data
, loc
);
1497 if (dblk
== BLK_NOCOPY
|| dblk
== BLK_SNAP
)
1498 idb_assign(ip
, ibp
->b_data
, loc
, 0);
1499 else if (dblk
== ffs_blkstofrags(fs
, blkno
) &&
1500 ffs_snapblkfree(fs
, ip
->i_devvp
, dblk
,
1501 fs
->fs_bsize
, ip
->i_number
)) {
1502 DIP_ADD(ip
, blocks
, -btodb(fs
->fs_bsize
));
1503 idb_assign(ip
, ibp
->b_data
, loc
, 0);
1508 error
= UFS_WAPBL_BEGIN(mp
);
1509 KASSERT(error
== 0);
1512 * Clear snapshot flag and drop reference.
1514 ip
->i_flags
&= ~(SF_SNAPSHOT
| SF_SNAPINVAL
);
1515 DIP_ASSIGN(ip
, flags
, ip
->i_flags
);
1516 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
1517 #if defined(QUOTA) || defined(QUOTA2)
1518 chkdq(ip
, DIP(ip
, blocks
), l
->l_cred
, FORCE
);
1519 chkiq(ip
, 1, l
->l_cred
, FORCE
);
1524 * Notification that a block is being freed. Return zero if the free
1525 * should be allowed to proceed. Return non-zero if the snapshot file
1526 * wants to claim the block. The block will be claimed if it is an
1527 * uncopied part of one of the snapshots. It will be freed if it is
1528 * either a BLK_NOCOPY or has already been copied in all of the snapshots.
1529 * If a fragment is being freed, then all snapshots that care about
1530 * it must make a copy since a snapshot file can only claim full sized
1531 * blocks. Note that if more than one snapshot file maps the block,
1532 * we can pick one at random to claim it. Since none of the snapshots
1533 * can change, we are assurred that they will all see the same unmodified
1534 * image. When deleting a snapshot file (see ffs_snapremove above), we
1535 * must push any of these claimed blocks to one of the other snapshots
1536 * that maps it. These claimed blocks are easily identified as they will
1537 * have a block number equal to their logical block number within the
1538 * snapshot. A copied block can never have this property because they
1539 * must always have been allocated from a BLK_NOCOPY location.
1542 ffs_snapblkfree(struct fs
*fs
, struct vnode
*devvp
, daddr_t bno
,
1543 long size
, ino_t inum
)
1545 struct mount
*mp
= spec_node_getmountedfs(devvp
);
1548 struct vnode
*vp
= NULL
;
1549 struct snap_info
*si
;
1550 void *saved_data
= NULL
;
1554 int indiroff
= 0, error
= 0, claimedblk
= 0;
1556 si
= VFSTOUFS(mp
)->um_snapinfo
;
1557 lbn
= ffs_fragstoblks(fs
, bno
);
1558 mutex_enter(&si
->si_snaplock
);
1559 mutex_enter(&si
->si_lock
);
1560 si
->si_owner
= curlwp
;
1564 TAILQ_FOREACH(ip
, &si
->si_snapshots
, i_nextsnap
) {
1567 * Lookup block being written.
1569 if (lbn
< UFS_NDADDR
) {
1570 blkno
= db_get(ip
, lbn
);
1572 mutex_exit(&si
->si_lock
);
1573 error
= ffs_balloc(vp
, ffs_lblktosize(fs
, (off_t
)lbn
),
1574 fs
->fs_bsize
, FSCRED
, B_METAONLY
, &ibp
);
1576 mutex_enter(&si
->si_lock
);
1579 indiroff
= (lbn
- UFS_NDADDR
) % FFS_NINDIR(fs
);
1580 blkno
= idb_get(ip
, ibp
->b_data
, indiroff
);
1581 mutex_enter(&si
->si_lock
);
1582 if (gen
!= si
->si_gen
) {
1588 * Check to see if block needs to be copied.
1592 * A block that we map is being freed. If it has not
1593 * been claimed yet, we will claim or copy it (below).
1596 } else if (blkno
== BLK_SNAP
) {
1598 * No previous snapshot claimed the block,
1599 * so it will be freed and become a BLK_NOCOPY
1600 * (don't care) for us.
1603 panic("snapblkfree: inconsistent block type");
1604 if (lbn
< UFS_NDADDR
) {
1605 db_assign(ip
, lbn
, BLK_NOCOPY
);
1606 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
1608 idb_assign(ip
, ibp
->b_data
, indiroff
,
1610 mutex_exit(&si
->si_lock
);
1611 if (ip
->i_nlink
> 0)
1615 mutex_enter(&si
->si_lock
);
1616 if (gen
!= si
->si_gen
)
1620 } else /* BLK_NOCOPY or default */ {
1622 * If the snapshot has already copied the block
1623 * (default), or does not care about the block,
1626 if (lbn
>= UFS_NDADDR
)
1631 * If this is a full size block, we will just grab it
1632 * and assign it to the snapshot inode. Otherwise we
1633 * will proceed to copy it. See explanation for this
1634 * routine as to why only a single snapshot needs to
1637 if (size
== fs
->fs_bsize
) {
1640 printf("%s %llu lbn %" PRId64
1642 "Grabonremove: snapino",
1643 (unsigned long long)ip
->i_number
,
1644 lbn
, (unsigned long long)inum
);
1646 mutex_exit(&si
->si_lock
);
1647 if (lbn
< UFS_NDADDR
) {
1648 db_assign(ip
, lbn
, bno
);
1650 idb_assign(ip
, ibp
->b_data
, indiroff
, bno
);
1651 if (ip
->i_nlink
> 0)
1656 DIP_ADD(ip
, blocks
, btodb(size
));
1657 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
1658 if (ip
->i_nlink
> 0 && mp
->mnt_wapbl
)
1659 error
= syncsnap(vp
);
1662 mutex_enter(&si
->si_lock
);
1663 si
->si_owner
= NULL
;
1664 mutex_exit(&si
->si_lock
);
1665 mutex_exit(&si
->si_snaplock
);
1666 return (error
== 0);
1668 if (lbn
>= UFS_NDADDR
)
1672 printf("%s%llu lbn %" PRId64
" %s %llu size %ld\n",
1673 "Copyonremove: snapino ",
1674 (unsigned long long)ip
->i_number
,
1675 lbn
, "for inum", (unsigned long long)inum
, size
);
1678 * If we have already read the old block contents, then
1679 * simply copy them to the new block. Note that we need
1680 * to synchronously write snapshots that have not been
1681 * unlinked, and hence will be visible after a crash,
1682 * to ensure their integrity.
1684 mutex_exit(&si
->si_lock
);
1685 if (saved_data
== NULL
) {
1686 saved_data
= malloc(fs
->fs_bsize
, M_UFSMNT
, M_WAITOK
);
1687 error
= rwfsblk(vp
, B_READ
, saved_data
, lbn
);
1689 free(saved_data
, M_UFSMNT
);
1691 mutex_enter(&si
->si_lock
);
1695 error
= wrsnapblk(vp
, saved_data
, lbn
);
1696 if (error
== 0 && ip
->i_nlink
> 0 && mp
->mnt_wapbl
)
1697 error
= syncsnap(vp
);
1698 mutex_enter(&si
->si_lock
);
1701 if (gen
!= si
->si_gen
)
1704 si
->si_owner
= NULL
;
1705 mutex_exit(&si
->si_lock
);
1706 mutex_exit(&si
->si_snaplock
);
1708 free(saved_data
, M_UFSMNT
);
1710 * If we have been unable to allocate a block in which to do
1711 * the copy, then return non-zero so that the fragment will
1712 * not be freed. Although space will be lost, the snapshot
1713 * will stay consistent.
1719 * Associate snapshot files when mounting.
1722 ffs_snapshot_mount(struct mount
*mp
)
1724 struct vnode
*devvp
= VFSTOUFS(mp
)->um_devvp
;
1725 struct fs
*fs
= VFSTOUFS(mp
)->um_fs
;
1726 struct lwp
*l
= curlwp
;
1728 struct inode
*ip
, *xp
;
1729 struct snap_info
*si
;
1730 daddr_t snaplistsize
, *snapblklist
;
1731 int i
, error
, ns __unused
, snaploc
, loc
;
1734 * No persistent snapshots on apple ufs file systems.
1736 if (UFS_MPISAPPLEUFS(VFSTOUFS(mp
)))
1739 si
= VFSTOUFS(mp
)->um_snapinfo
;
1740 ns
= UFS_FSNEEDSWAP(fs
);
1742 * XXX The following needs to be set before ffs_truncate or
1743 * VOP_READ can be called.
1745 mp
->mnt_stat
.f_iosize
= fs
->fs_bsize
;
1747 * Process each snapshot listed in the superblock.
1750 mutex_enter(&si
->si_lock
);
1751 for (snaploc
= 0; snaploc
< FSMAXSNAP
; snaploc
++) {
1752 if (fs
->fs_snapinum
[snaploc
] == 0)
1754 if ((error
= VFS_VGET(mp
, fs
->fs_snapinum
[snaploc
],
1756 printf("ffs_snapshot_mount: vget failed %d\n", error
);
1760 if ((ip
->i_flags
& (SF_SNAPSHOT
| SF_SNAPINVAL
)) !=
1762 printf("ffs_snapshot_mount: non-snapshot inode %d\n",
1763 fs
->fs_snapinum
[snaploc
]);
1766 for (loc
= snaploc
+ 1; loc
< FSMAXSNAP
; loc
++) {
1767 if (fs
->fs_snapinum
[loc
] == 0)
1769 fs
->fs_snapinum
[loc
- 1] = fs
->fs_snapinum
[loc
];
1771 fs
->fs_snapinum
[loc
- 1] = 0;
1777 * Read the block hints list. Use an empty list on
1780 error
= vn_rdwr(UIO_READ
, vp
,
1781 (void *)&snaplistsize
, sizeof(snaplistsize
),
1782 ffs_lblktosize(fs
, howmany(fs
->fs_size
, fs
->fs_frag
)),
1783 UIO_SYSSPACE
, IO_NODELOCKED
|IO_UNIT
|IO_ALTSEMANTICS
,
1784 l
->l_cred
, NULL
, NULL
);
1786 printf("ffs_snapshot_mount: read_1 failed %d\n", error
);
1789 snaplistsize
= ufs_rw64(snaplistsize
, ns
);
1790 snapblklist
= malloc(
1791 snaplistsize
* sizeof(daddr_t
), M_UFSMNT
, M_WAITOK
);
1795 error
= vn_rdwr(UIO_READ
, vp
, (void *)snapblklist
,
1796 snaplistsize
* sizeof(daddr_t
),
1797 ffs_lblktosize(fs
, howmany(fs
->fs_size
, fs
->fs_frag
)),
1798 UIO_SYSSPACE
, IO_NODELOCKED
|IO_UNIT
|IO_ALTSEMANTICS
,
1799 l
->l_cred
, NULL
, NULL
);
1800 for (i
= 0; i
< snaplistsize
; i
++)
1801 snapblklist
[i
] = ufs_rw64(snapblklist
[i
], ns
);
1803 printf("ffs_snapshot_mount: read_2 failed %d\n",
1808 ip
->i_snapblklist
= &snapblklist
[0];
1811 * Link it onto the active snapshot list.
1813 if (is_active_snapshot(si
, ip
))
1814 panic("ffs_snapshot_mount: %"PRIu64
" already on list",
1817 TAILQ_INSERT_TAIL(&si
->si_snapshots
, ip
, i_nextsnap
);
1818 vp
->v_vflag
|= VV_SYSTEM
;
1822 * No usable snapshots found.
1825 mutex_exit(&si
->si_lock
);
1829 * Attach the block hints list. We always want to
1830 * use the list from the newest snapshot.
1832 xp
= TAILQ_LAST(&si
->si_snapshots
, inodelst
);
1833 si
->si_snapblklist
= xp
->i_snapblklist
;
1834 fscow_establish(mp
, ffs_copyonwrite
, devvp
);
1836 mutex_exit(&si
->si_lock
);
1840 * Disassociate snapshot files when unmounting.
1843 ffs_snapshot_unmount(struct mount
*mp
)
1845 struct vnode
*devvp
= VFSTOUFS(mp
)->um_devvp
;
1847 struct vnode
*vp
= NULL
;
1848 struct snap_info
*si
;
1850 si
= VFSTOUFS(mp
)->um_snapinfo
;
1851 mutex_enter(&si
->si_lock
);
1852 while ((xp
= TAILQ_FIRST(&si
->si_snapshots
)) != 0) {
1854 TAILQ_REMOVE(&si
->si_snapshots
, xp
, i_nextsnap
);
1855 if (xp
->i_snapblklist
== si
->si_snapblklist
)
1856 si
->si_snapblklist
= NULL
;
1857 free(xp
->i_snapblklist
, M_UFSMNT
);
1858 if (xp
->i_nlink
> 0) {
1860 mutex_exit(&si
->si_lock
);
1862 mutex_enter(&si
->si_lock
);
1866 mutex_exit(&si
->si_lock
);
1868 fscow_disestablish(mp
, ffs_copyonwrite
, devvp
);
1872 * Check for need to copy block that is about to be written,
1873 * copying the block if necessary.
1876 ffs_copyonwrite(void *v
, struct buf
*bp
, bool data_valid
)
1880 struct vnode
*devvp
= v
, *vp
= NULL
;
1881 struct mount
*mp
= spec_node_getmountedfs(devvp
);
1882 struct snap_info
*si
;
1883 void *saved_data
= NULL
;
1884 daddr_t lbn
, blkno
, *snapblklist
;
1886 int lower
, upper
, mid
, snapshot_locked
= 0, error
= 0;
1889 * Check for valid snapshots.
1891 si
= VFSTOUFS(mp
)->um_snapinfo
;
1892 mutex_enter(&si
->si_lock
);
1893 ip
= TAILQ_FIRST(&si
->si_snapshots
);
1895 mutex_exit(&si
->si_lock
);
1899 * First check to see if it is after the file system,
1900 * in the journal or in the preallocated list.
1901 * By doing these checks we avoid several potential deadlocks.
1904 lbn
= ffs_fragstoblks(fs
, FFS_DBTOFSB(fs
, bp
->b_blkno
));
1905 if (bp
->b_blkno
>= FFS_FSBTODB(fs
, fs
->fs_size
)) {
1906 mutex_exit(&si
->si_lock
);
1909 if ((fs
->fs_flags
& FS_DOWAPBL
) &&
1910 fs
->fs_journal_location
== UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM
) {
1911 off_t blk_off
, log_start
, log_end
;
1913 log_start
= (off_t
)fs
->fs_journallocs
[UFS_WAPBL_INFS_ADDR
] *
1914 fs
->fs_journallocs
[UFS_WAPBL_INFS_BLKSZ
];
1915 log_end
= log_start
+ fs
->fs_journallocs
[UFS_WAPBL_INFS_COUNT
] *
1916 fs
->fs_journallocs
[UFS_WAPBL_INFS_BLKSZ
];
1917 blk_off
= dbtob(bp
->b_blkno
);
1918 if (blk_off
>= log_start
&& blk_off
< log_end
) {
1919 mutex_exit(&si
->si_lock
);
1923 snapblklist
= si
->si_snapblklist
;
1924 upper
= (snapblklist
!= NULL
? snapblklist
[0] - 1 : 0);
1926 while (lower
<= upper
) {
1927 mid
= (lower
+ upper
) / 2;
1928 if (snapblklist
[mid
] == lbn
)
1930 if (snapblklist
[mid
] < lbn
)
1935 if (lower
<= upper
) {
1936 mutex_exit(&si
->si_lock
);
1940 * Not in the precomputed list, so check the snapshots.
1942 if (si
->si_owner
!= curlwp
) {
1943 if (!mutex_tryenter(&si
->si_snaplock
)) {
1944 mutex_exit(&si
->si_lock
);
1945 mutex_enter(&si
->si_snaplock
);
1946 mutex_enter(&si
->si_lock
);
1948 si
->si_owner
= curlwp
;
1949 snapshot_locked
= 1;
1951 if (data_valid
&& bp
->b_bcount
== fs
->fs_bsize
)
1952 saved_data
= bp
->b_data
;
1955 TAILQ_FOREACH(ip
, &si
->si_snapshots
, i_nextsnap
) {
1958 * We ensure that everything of our own that needs to be
1959 * copied will be done at the time that ffs_snapshot is
1960 * called. Thus we can skip the check here which can
1961 * deadlock in doing the lookup in ffs_balloc.
1966 * Check to see if block needs to be copied.
1968 if (lbn
< UFS_NDADDR
) {
1969 blkno
= db_get(ip
, lbn
);
1971 mutex_exit(&si
->si_lock
);
1972 blkno
= 0; /* XXX: GCC */
1973 if ((error
= snapblkaddr(vp
, lbn
, &blkno
)) != 0) {
1974 mutex_enter(&si
->si_lock
);
1977 mutex_enter(&si
->si_lock
);
1978 if (gen
!= si
->si_gen
)
1982 if (blkno
== BLK_SNAP
&& bp
->b_lblkno
>= 0)
1983 panic("ffs_copyonwrite: bad copy block");
1988 if (curlwp
== uvm
.pagedaemon_lwp
) {
1992 /* Only one level of recursion allowed. */
1993 KASSERT(snapshot_locked
);
1995 * Allocate the block into which to do the copy. Since
1996 * multiple processes may all try to copy the same block,
1997 * we have to recheck our need to do a copy if we sleep
1998 * waiting for the lock.
2000 * Because all snapshots on a filesystem share a single
2001 * lock, we ensure that we will never be in competition
2002 * with another process to allocate a block.
2006 printf("Copyonwrite: snapino %llu lbn %" PRId64
" for ",
2007 (unsigned long long)ip
->i_number
, lbn
);
2008 if (bp
->b_vp
== devvp
)
2009 printf("fs metadata");
2011 printf("inum %llu", (unsigned long long)
2012 VTOI(bp
->b_vp
)->i_number
);
2013 printf(" lblkno %" PRId64
"\n", bp
->b_lblkno
);
2017 * If we have already read the old block contents, then
2018 * simply copy them to the new block. Note that we need
2019 * to synchronously write snapshots that have not been
2020 * unlinked, and hence will be visible after a crash,
2021 * to ensure their integrity.
2023 mutex_exit(&si
->si_lock
);
2024 if (saved_data
== NULL
) {
2025 saved_data
= malloc(fs
->fs_bsize
, M_UFSMNT
, M_WAITOK
);
2026 error
= rwfsblk(vp
, B_READ
, saved_data
, lbn
);
2028 free(saved_data
, M_UFSMNT
);
2030 mutex_enter(&si
->si_lock
);
2034 error
= wrsnapblk(vp
, saved_data
, lbn
);
2035 if (error
== 0 && ip
->i_nlink
> 0 && mp
->mnt_wapbl
)
2036 error
= syncsnap(vp
);
2037 mutex_enter(&si
->si_lock
);
2040 if (gen
!= si
->si_gen
)
2044 * Note that we need to synchronously write snapshots that
2045 * have not been unlinked, and hence will be visible after
2046 * a crash, to ensure their integrity.
2048 if (snapshot_locked
) {
2049 si
->si_owner
= NULL
;
2050 mutex_exit(&si
->si_lock
);
2051 mutex_exit(&si
->si_snaplock
);
2053 mutex_exit(&si
->si_lock
);
2054 if (saved_data
&& saved_data
!= bp
->b_data
)
2055 free(saved_data
, M_UFSMNT
);
2060 * Read from a snapshot.
2063 ffs_snapshot_read(struct vnode
*vp
, struct uio
*uio
, int ioflag
)
2065 struct inode
*ip
= VTOI(vp
);
2066 struct fs
*fs
= ip
->i_fs
;
2067 struct snap_info
*si
= VFSTOUFS(vp
->v_mount
)->um_snapinfo
;
2069 daddr_t lbn
, nextlbn
;
2070 off_t fsbytes
, bytesinfile
;
2071 long size
, xfersize
, blkoffset
;
2074 fstrans_start(vp
->v_mount
, FSTRANS_SHARED
);
2075 mutex_enter(&si
->si_snaplock
);
2077 if (ioflag
& IO_ALTSEMANTICS
)
2078 fsbytes
= ip
->i_size
;
2080 fsbytes
= ffs_lfragtosize(fs
, fs
->fs_size
);
2081 for (error
= 0, bp
= NULL
; uio
->uio_resid
> 0; bp
= NULL
) {
2082 bytesinfile
= fsbytes
- uio
->uio_offset
;
2083 if (bytesinfile
<= 0)
2085 lbn
= ffs_lblkno(fs
, uio
->uio_offset
);
2087 size
= fs
->fs_bsize
;
2088 blkoffset
= ffs_blkoff(fs
, uio
->uio_offset
);
2089 xfersize
= MIN(MIN(fs
->fs_bsize
- blkoffset
, uio
->uio_resid
),
2092 if (ffs_lblktosize(fs
, nextlbn
+ 1) >= fsbytes
) {
2093 if (ffs_lblktosize(fs
, lbn
) + size
> fsbytes
)
2094 size
= ffs_fragroundup(fs
,
2095 fsbytes
- ffs_lblktosize(fs
, lbn
));
2096 error
= bread(vp
, lbn
, size
, 0, &bp
);
2098 int nextsize
= fs
->fs_bsize
;
2099 error
= breadn(vp
, lbn
,
2100 size
, &nextlbn
, &nextsize
, 1, 0, &bp
);
2106 * We should only get non-zero b_resid when an I/O error
2107 * has occurred, which should cause us to break above.
2108 * However, if the short read did not cause an error,
2109 * then we want to ensure that we do not uiomove bad
2110 * or uninitialized data.
2112 size
-= bp
->b_resid
;
2113 if (size
< blkoffset
+ xfersize
) {
2114 xfersize
= size
- blkoffset
;
2118 error
= uiomove((char *)bp
->b_data
+ blkoffset
, xfersize
, uio
);
2126 mutex_exit(&si
->si_snaplock
);
2127 fstrans_done(vp
->v_mount
);
2132 * Lookup a snapshots data block address.
2133 * Simpler than UFS_BALLOC() as we know all metadata is already allocated
2134 * and safe even for the pagedaemon where we cannot bread().
2137 snapblkaddr(struct vnode
*vp
, daddr_t lbn
, daddr_t
*res
)
2139 struct indir indirs
[UFS_NIADDR
+ 2];
2140 struct inode
*ip
= VTOI(vp
);
2141 struct fs
*fs
= ip
->i_fs
;
2147 if (lbn
< UFS_NDADDR
) {
2148 *res
= db_get(ip
, lbn
);
2151 if ((error
= ufs_getlbns(vp
, lbn
, indirs
, &num
)) != 0)
2153 if (curlwp
== uvm
.pagedaemon_lwp
) {
2154 mutex_enter(&bufcache_lock
);
2155 bp
= incore(vp
, indirs
[num
-1].in_lbn
);
2156 if (bp
&& (bp
->b_oflags
& (BO_DONE
| BO_DELWRI
))) {
2157 *res
= idb_get(ip
, bp
->b_data
, indirs
[num
-1].in_off
);
2161 mutex_exit(&bufcache_lock
);
2164 error
= bread(vp
, indirs
[num
-1].in_lbn
, fs
->fs_bsize
, 0, &bp
);
2166 *res
= idb_get(ip
, bp
->b_data
, indirs
[num
-1].in_off
);
2174 * Read or write the specified block of the filesystem vp resides on
2175 * from or to the disk bypassing the buffer cache.
2178 rwfsblk(struct vnode
*vp
, int flags
, void *data
, daddr_t lbn
)
2181 struct inode
*ip
= VTOI(vp
);
2182 struct fs
*fs
= ip
->i_fs
;
2185 nbp
= getiobuf(NULL
, true);
2186 nbp
->b_flags
= flags
;
2187 nbp
->b_bcount
= nbp
->b_bufsize
= fs
->fs_bsize
;
2190 nbp
->b_blkno
= nbp
->b_rawblkno
= FFS_FSBTODB(fs
, ffs_blkstofrags(fs
, lbn
));
2192 nbp
->b_dev
= ip
->i_devvp
->v_rdev
;
2193 SET(nbp
->b_cflags
, BC_BUSY
); /* mark buffer busy */
2197 error
= biowait(nbp
);
2205 * Write all dirty buffers to disk and invalidate them.
2208 syncsnap(struct vnode
*vp
)
2212 struct fs
*fs
= VTOI(vp
)->i_fs
;
2214 mutex_enter(&bufcache_lock
);
2215 while ((bp
= LIST_FIRST(&vp
->v_dirtyblkhd
))) {
2216 error
= bbusy(bp
, false, 0, NULL
);
2217 if (error
== EPASSTHROUGH
)
2219 else if (error
!= 0) {
2220 mutex_exit(&bufcache_lock
);
2223 KASSERT(bp
->b_bcount
== fs
->fs_bsize
);
2224 mutex_exit(&bufcache_lock
);
2225 error
= rwfsblk(vp
, B_WRITE
, bp
->b_data
,
2226 ffs_fragstoblks(fs
, FFS_DBTOFSB(fs
, bp
->b_blkno
)));
2227 brelse(bp
, BC_INVAL
| BC_VFLUSH
);
2230 mutex_enter(&bufcache_lock
);
2232 mutex_exit(&bufcache_lock
);
2238 * Write the specified block to a snapshot.
2241 wrsnapblk(struct vnode
*vp
, void *data
, daddr_t lbn
)
2243 struct inode
*ip
= VTOI(vp
);
2244 struct fs
*fs
= ip
->i_fs
;
2248 error
= ffs_balloc(vp
, ffs_lblktosize(fs
, (off_t
)lbn
), fs
->fs_bsize
,
2249 FSCRED
, (ip
->i_nlink
> 0 ? B_SYNC
: 0), &bp
);
2252 memcpy(bp
->b_data
, data
, fs
->fs_bsize
);
2253 if (ip
->i_nlink
> 0)
2262 * Check if this inode is present on the active snapshot list.
2263 * Must be called with snapinfo locked.
2266 is_active_snapshot(struct snap_info
*si
, struct inode
*ip
)
2270 KASSERT(mutex_owned(&si
->si_lock
));
2272 TAILQ_FOREACH(xp
, &si
->si_snapshots
, i_nextsnap
)
2279 * Get/Put direct block from inode or buffer containing disk addresses. Take
2280 * care for fs type (UFS1/UFS2) and byte swapping. These functions should go
2281 * into a global include.
2283 static inline daddr_t
2284 db_get(struct inode
*ip
, int loc
)
2286 if (ip
->i_ump
->um_fstype
== UFS1
)
2287 return ufs_rw32(ip
->i_ffs1_db
[loc
], UFS_IPNEEDSWAP(ip
));
2289 return ufs_rw64(ip
->i_ffs2_db
[loc
], UFS_IPNEEDSWAP(ip
));
2293 db_assign(struct inode
*ip
, int loc
, daddr_t val
)
2295 if (ip
->i_ump
->um_fstype
== UFS1
)
2296 ip
->i_ffs1_db
[loc
] = ufs_rw32(val
, UFS_IPNEEDSWAP(ip
));
2298 ip
->i_ffs2_db
[loc
] = ufs_rw64(val
, UFS_IPNEEDSWAP(ip
));
2301 __unused
static inline daddr_t
2302 ib_get(struct inode
*ip
, int loc
)
2304 if (ip
->i_ump
->um_fstype
== UFS1
)
2305 return ufs_rw32(ip
->i_ffs1_ib
[loc
], UFS_IPNEEDSWAP(ip
));
2307 return ufs_rw64(ip
->i_ffs2_ib
[loc
], UFS_IPNEEDSWAP(ip
));
2310 static inline daddr_t
2311 idb_get(struct inode
*ip
, void *bf
, int loc
)
2313 if (ip
->i_ump
->um_fstype
== UFS1
)
2314 return ufs_rw32(((int32_t *)(bf
))[loc
], UFS_IPNEEDSWAP(ip
));
2316 return ufs_rw64(((int64_t *)(bf
))[loc
], UFS_IPNEEDSWAP(ip
));
2320 idb_assign(struct inode
*ip
, void *bf
, int loc
, daddr_t val
)
2322 if (ip
->i_ump
->um_fstype
== UFS1
)
2323 ((int32_t *)(bf
))[loc
] = ufs_rw32(val
, UFS_IPNEEDSWAP(ip
));
2325 ((int64_t *)(bf
))[loc
] = ufs_rw64(val
, UFS_IPNEEDSWAP(ip
));