1 /* $NetBSD: ffs_snapshot.c,v 1.118 2011/10/07 09:35:06 hannken 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.118 2011/10/07 09:35:06 hannken 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>
81 kmutex_t si_lock
; /* Lock this snapinfo */
82 kmutex_t si_snaplock
; /* Snapshot vnode common lock */
83 lwp_t
*si_owner
; /* Sanplock owner */
84 TAILQ_HEAD(inodelst
, inode
) si_snapshots
; /* List of active snapshots */
85 daddr_t
*si_snapblklist
; /* Snapshot block hints list */
86 uint32_t si_gen
; /* Incremented on change */
89 #if !defined(FFS_NO_SNAPSHOT)
90 typedef int (*acctfunc_t
)
91 (struct vnode
*, void *, int, int, struct fs
*, daddr_t
, int);
93 static int snapshot_setup(struct mount
*, struct vnode
*);
94 static int snapshot_copyfs(struct mount
*, struct vnode
*, void **);
95 static int snapshot_expunge(struct mount
*, struct vnode
*,
96 struct fs
*, daddr_t
*, daddr_t
**);
97 static int snapshot_expunge_snap(struct mount
*, struct vnode
*,
98 struct fs
*, daddr_t
);
99 static int snapshot_writefs(struct mount
*, struct vnode
*, void *);
100 static int cgaccount(struct vnode
*, int, int *);
101 static int cgaccount1(int, struct vnode
*, void *, int);
102 static int expunge(struct vnode
*, struct inode
*, struct fs
*,
104 static int indiracct(struct vnode
*, struct vnode
*, int, daddr_t
,
105 daddr_t
, daddr_t
, daddr_t
, daddr_t
, struct fs
*, acctfunc_t
, int);
106 static int fullacct(struct vnode
*, void *, int, int, struct fs
*,
108 static int snapacct(struct vnode
*, void *, int, int, struct fs
*,
110 static int mapacct(struct vnode
*, void *, int, int, struct fs
*,
112 #endif /* !defined(FFS_NO_SNAPSHOT) */
114 static int ffs_copyonwrite(void *, struct buf
*, bool);
115 static int snapblkaddr(struct vnode
*, daddr_t
, daddr_t
*);
116 static int rwfsblk(struct vnode
*, int, void *, daddr_t
);
117 static int syncsnap(struct vnode
*);
118 static int wrsnapblk(struct vnode
*, void *, daddr_t
);
119 #if !defined(FFS_NO_SNAPSHOT)
120 static int blocks_in_journal(struct fs
*);
123 static inline bool is_active_snapshot(struct snap_info
*, struct inode
*);
124 static inline daddr_t
db_get(struct inode
*, int);
125 static inline void db_assign(struct inode
*, int, daddr_t
);
126 static inline daddr_t
ib_get(struct inode
*, int);
127 static inline void ib_assign(struct inode
*, int, daddr_t
);
128 static inline daddr_t
idb_get(struct inode
*, void *, int);
129 static inline void idb_assign(struct inode
*, void *, int, daddr_t
);
132 static int snapdebug
= 0;
136 ffs_snapshot_init(struct ufsmount
*ump
)
138 struct snap_info
*si
;
140 si
= ump
->um_snapinfo
= kmem_alloc(sizeof(*si
), KM_SLEEP
);
144 TAILQ_INIT(&si
->si_snapshots
);
145 mutex_init(&si
->si_lock
, MUTEX_DEFAULT
, IPL_NONE
);
146 mutex_init(&si
->si_snaplock
, MUTEX_DEFAULT
, IPL_NONE
);
149 si
->si_snapblklist
= NULL
;
155 ffs_snapshot_fini(struct ufsmount
*ump
)
157 struct snap_info
*si
;
159 si
= ump
->um_snapinfo
;
160 ump
->um_snapinfo
= NULL
;
162 KASSERT(TAILQ_EMPTY(&si
->si_snapshots
));
163 mutex_destroy(&si
->si_lock
);
164 mutex_destroy(&si
->si_snaplock
);
165 KASSERT(si
->si_snapblklist
== NULL
);
166 kmem_free(si
, sizeof(*si
));
170 * Create a snapshot file and initialize it for the filesystem.
171 * Vnode is locked on entry and return.
174 ffs_snapshot(struct mount
*mp
, struct vnode
*vp
, struct timespec
*ctime
)
176 #if defined(FFS_NO_SNAPSHOT)
179 #else /* defined(FFS_NO_SNAPSHOT) */
180 bool suspended
= false;
181 int error
, redo
= 0, snaploc
;
183 daddr_t
*snaplist
= NULL
, snaplistsize
= 0;
184 struct buf
*bp
, *nbp
;
185 struct fs
*copy_fs
= NULL
;
186 struct fs
*fs
= VFSTOUFS(mp
)->um_fs
;
187 struct inode
*ip
= VTOI(vp
);
188 struct lwp
*l
= curlwp
;
189 struct snap_info
*si
= VFSTOUFS(mp
)->um_snapinfo
;
191 struct timeval starttime
;
193 struct timeval endtime
;
195 struct vnode
*devvp
= ip
->i_devvp
;
198 * If the vnode already is a snapshot, return.
200 if ((VTOI(vp
)->i_flags
& SF_SNAPSHOT
)) {
201 if ((VTOI(vp
)->i_flags
& SF_SNAPINVAL
))
204 ctime
->tv_sec
= DIP(VTOI(vp
), mtime
);
205 ctime
->tv_nsec
= DIP(VTOI(vp
), mtimensec
);
210 * Check for free snapshot slot in the superblock.
212 for (snaploc
= 0; snaploc
< FSMAXSNAP
; snaploc
++)
213 if (fs
->fs_snapinum
[snaploc
] == 0)
215 if (snaploc
== FSMAXSNAP
)
218 * Prepare the vnode to become a snapshot.
220 error
= snapshot_setup(mp
, vp
);
225 * Copy all the cylinder group maps. Although the
226 * filesystem is still active, we hope that only a few
227 * cylinder groups will change between now and when we
228 * suspend operations. Thus, we will be able to quickly
229 * touch up the few cylinder groups that changed during
230 * the suspension period.
232 error
= cgaccount(vp
, 1, NULL
);
237 * snapshot is now valid
239 ip
->i_flags
&= ~SF_SNAPINVAL
;
240 DIP_ASSIGN(ip
, flags
, ip
->i_flags
);
241 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
244 * Ensure that the snapshot is completely on disk.
245 * Since we have marked it as a snapshot it is safe to
246 * unlock it as no process will be allowed to write to it.
248 error
= VOP_FSYNC(vp
, l
->l_cred
, FSYNC_WAIT
, 0, 0);
253 * All allocations are done, so we can now suspend the filesystem.
255 error
= vfs_suspend(vp
->v_mount
, 0);
256 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
260 getmicrotime(&starttime
);
262 * First, copy all the cylinder group maps that have changed.
264 error
= cgaccount(vp
, 2, &redo
);
268 * Create a copy of the superblock and its summary information.
270 error
= snapshot_copyfs(mp
, vp
, &sbbuf
);
271 copy_fs
= (struct fs
*)((char *)sbbuf
+ blkoff(fs
, fs
->fs_sblockloc
));
275 * Expunge unlinked files from our view.
277 error
= snapshot_expunge(mp
, vp
, copy_fs
, &snaplistsize
, &snaplist
);
281 * Record snapshot inode. Since this is the newest snapshot,
282 * it must be placed at the end of the list.
285 fs
->fs_snapinum
[snaploc
] = ip
->i_number
;
287 mutex_enter(&si
->si_lock
);
288 if (is_active_snapshot(si
, ip
))
289 panic("ffs_snapshot: %"PRIu64
" already on list", ip
->i_number
);
290 TAILQ_INSERT_TAIL(&si
->si_snapshots
, ip
, i_nextsnap
);
291 if (TAILQ_FIRST(&si
->si_snapshots
) == ip
) {
293 * If this is the first snapshot on this filesystem, put the
294 * preliminary list in place and establish the cow handler.
296 si
->si_snapblklist
= snaplist
;
297 fscow_establish(mp
, ffs_copyonwrite
, devvp
);
300 mutex_exit(&si
->si_lock
);
302 vp
->v_vflag
|= VV_SYSTEM
;
304 * Set the mtime to the time the snapshot has been taken.
306 TIMEVAL_TO_TIMESPEC(&starttime
, &ts
);
309 DIP_ASSIGN(ip
, mtime
, ts
.tv_sec
);
310 DIP_ASSIGN(ip
, mtimensec
, ts
.tv_nsec
);
311 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
313 * Copy allocation information from all snapshots and then
314 * expunge them from our view.
316 error
= snapshot_expunge_snap(mp
, vp
, copy_fs
, snaplistsize
);
320 * Write the superblock and its summary information to the snapshot.
322 error
= snapshot_writefs(mp
, vp
, sbbuf
);
326 * We're nearly done, ensure that the snapshot is completely on disk.
328 error
= VOP_FSYNC(vp
, l
->l_cred
, FSYNC_WAIT
, 0, 0);
332 * Invalidate and free all pages on the snapshot vnode.
333 * We will read and write through the buffercache.
335 mutex_enter(vp
->v_interlock
);
336 error
= VOP_PUTPAGES(vp
, 0, 0,
337 PGO_ALLPAGES
| PGO_CLEANIT
| PGO_SYNCIO
| PGO_FREE
);
341 * Invalidate short ( < fs_bsize ) buffers. We will always read
342 * full size buffers later.
344 mutex_enter(&bufcache_lock
);
345 KASSERT(LIST_FIRST(&vp
->v_dirtyblkhd
) == NULL
);
346 for (bp
= LIST_FIRST(&vp
->v_cleanblkhd
); bp
; bp
= nbp
) {
347 nbp
= LIST_NEXT(bp
, b_vnbufs
);
348 KASSERT((bp
->b_cflags
& BC_BUSY
) == 0);
349 if (bp
->b_bcount
< fs
->fs_bsize
) {
350 bp
->b_cflags
|= BC_BUSY
;
351 brelsel(bp
, BC_INVAL
| BC_VFLUSH
);
354 mutex_exit(&bufcache_lock
);
358 free(copy_fs
->fs_csp
, M_UFSMNT
);
359 free(sbbuf
, M_UFSMNT
);
361 if (fs
->fs_active
!= NULL
) {
362 free(fs
->fs_active
, M_DEVBUF
);
363 fs
->fs_active
= NULL
;
366 mutex_enter(&si
->si_lock
);
367 if (snaplist
!= NULL
) {
368 if (si
->si_snapblklist
== snaplist
)
369 si
->si_snapblklist
= NULL
;
370 free(snaplist
, M_UFSMNT
);
373 fs
->fs_snapinum
[snaploc
] = 0;
376 * As this is the newest list, it is the most inclusive, so
377 * should replace the previous list.
379 si
->si_snapblklist
= ip
->i_snapblklist
;
382 mutex_exit(&si
->si_lock
);
386 vfs_resume(vp
->v_mount
);
387 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
389 getmicrotime(&endtime
);
390 timersub(&endtime
, &starttime
, &endtime
);
391 printf("%s: suspended %lld.%03d sec, redo %d of %d\n",
392 mp
->mnt_stat
.f_mntonname
, (long long)endtime
.tv_sec
,
393 endtime
.tv_usec
/ 1000, redo
, fs
->fs_ncg
);
397 if (!UFS_WAPBL_BEGIN(mp
)) {
398 (void) ffs_truncate(vp
, (off_t
)0, 0, NOCRED
);
401 } else if (ip
->i_nlink
> 0)
407 * Prepare vnode to become a snapshot.
410 snapshot_setup(struct mount
*mp
, struct vnode
*vp
)
412 int error
, n
, len
, loc
, cg
;
413 daddr_t blkno
, numblks
;
414 struct buf
*ibp
, *nbp
;
415 struct fs
*fs
= VFSTOUFS(mp
)->um_fs
;
416 struct lwp
*l
= curlwp
;
417 const int wbreak
= blocks_in_journal(fs
)/8;
418 struct inode
*ip
= VTOI(vp
);
421 * Check mount, exclusive reference and owner.
423 if (vp
->v_mount
!= mp
)
425 if (vp
->v_usecount
!= 1 || vp
->v_writecount
!= 0)
427 if (kauth_authorize_generic(l
->l_cred
, KAUTH_GENERIC_ISSUSER
,
429 VTOI(vp
)->i_uid
!= kauth_cred_geteuid(l
->l_cred
))
432 if (vp
->v_size
!= 0) {
433 error
= ffs_truncate(vp
, 0, 0, NOCRED
);
438 /* Change inode to snapshot type file. */
439 error
= UFS_WAPBL_BEGIN(mp
);
442 #if defined(QUOTA) || defined(QUOTA2)
443 /* shapshot inodes are not accounted in quotas */
444 chkiq(ip
, -1, l
->l_cred
, 0);
446 ip
->i_flags
|= (SF_SNAPSHOT
| SF_SNAPINVAL
);
447 DIP_ASSIGN(ip
, flags
, ip
->i_flags
);
448 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
449 ffs_update(vp
, NULL
, NULL
, UPDATE_WAIT
);
452 KASSERT(ip
->i_flags
& SF_SNAPSHOT
);
454 * Write an empty list of preallocated blocks to the end of
455 * the snapshot to set size to at least that of the filesystem.
457 numblks
= howmany(fs
->fs_size
, fs
->fs_frag
);
459 blkno
= ufs_rw64(blkno
, UFS_FSNEEDSWAP(fs
));
460 error
= vn_rdwr(UIO_WRITE
, vp
,
461 (void *)&blkno
, sizeof(blkno
), lblktosize(fs
, (off_t
)numblks
),
462 UIO_SYSSPACE
, IO_NODELOCKED
|IO_UNIT
, l
->l_cred
, NULL
, NULL
);
466 * Preallocate critical data structures so that we can copy
467 * them in without further allocation after we suspend all
468 * operations on the filesystem. We would like to just release
469 * the allocated buffers without writing them since they will
470 * be filled in below once we are ready to go, but this upsets
471 * the soft update code, so we go ahead and write the new buffers.
473 * Allocate all indirect blocks and mark all of them as not
474 * needing to be copied.
476 error
= UFS_WAPBL_BEGIN(mp
);
479 for (blkno
= NDADDR
, n
= 0; blkno
< numblks
; blkno
+= NINDIR(fs
)) {
480 error
= ffs_balloc(vp
, lblktosize(fs
, (off_t
)blkno
),
481 fs
->fs_bsize
, l
->l_cred
, B_METAONLY
, &ibp
);
485 if (wbreak
> 0 && (++n
% wbreak
) == 0) {
487 error
= UFS_WAPBL_BEGIN(mp
);
493 * Allocate copies for the superblock and its summary information.
495 error
= ffs_balloc(vp
, fs
->fs_sblockloc
, fs
->fs_sbsize
, l
->l_cred
,
500 blkno
= fragstoblks(fs
, fs
->fs_csaddr
);
501 len
= howmany(fs
->fs_cssize
, fs
->fs_bsize
);
502 for (loc
= 0; loc
< len
; loc
++) {
503 error
= ffs_balloc(vp
, lblktosize(fs
, (off_t
)(blkno
+ loc
)),
504 fs
->fs_bsize
, l
->l_cred
, 0, &nbp
);
508 if (wbreak
> 0 && (++n
% wbreak
) == 0) {
510 error
= UFS_WAPBL_BEGIN(mp
);
516 * Allocate all cylinder group blocks.
518 for (cg
= 0; cg
< fs
->fs_ncg
; cg
++) {
519 error
= ffs_balloc(vp
, lfragtosize(fs
, cgtod(fs
, cg
)),
520 fs
->fs_bsize
, l
->l_cred
, 0, &nbp
);
524 if (wbreak
> 0 && (++n
% wbreak
) == 0) {
526 error
= UFS_WAPBL_BEGIN(mp
);
538 * Create a copy of the superblock and its summary information.
539 * It is up to the caller to free copyfs and copy_fs->fs_csp.
542 snapshot_copyfs(struct mount
*mp
, struct vnode
*vp
, void **sbbuf
)
544 int error
, i
, len
, loc
, size
;
548 struct fs
*copyfs
, *fs
= VFSTOUFS(mp
)->um_fs
;
549 struct lwp
*l
= curlwp
;
550 struct vnode
*devvp
= VTOI(vp
)->i_devvp
;
553 * Grab a copy of the superblock and its summary information.
554 * We delay writing it until the suspension is released below.
556 *sbbuf
= malloc(fs
->fs_bsize
, M_UFSMNT
, M_WAITOK
);
557 loc
= blkoff(fs
, fs
->fs_sblockloc
);
559 memset(*sbbuf
, 0, loc
);
560 copyfs
= (struct fs
*)((char *)(*sbbuf
) + loc
);
561 memcpy(copyfs
, fs
, fs
->fs_sbsize
);
562 size
= fs
->fs_bsize
< SBLOCKSIZE
? fs
->fs_bsize
: SBLOCKSIZE
;
563 if (fs
->fs_sbsize
< size
)
564 memset((char *)(*sbbuf
) + loc
+ fs
->fs_sbsize
, 0,
565 size
- fs
->fs_sbsize
);
566 size
= blkroundup(fs
, fs
->fs_cssize
);
567 if (fs
->fs_contigsumsize
> 0)
568 size
+= fs
->fs_ncg
* sizeof(int32_t);
569 space
= malloc(size
, M_UFSMNT
, M_WAITOK
);
570 copyfs
->fs_csp
= space
;
571 memcpy(copyfs
->fs_csp
, fs
->fs_csp
, fs
->fs_cssize
);
572 space
= (char *)space
+ fs
->fs_cssize
;
573 loc
= howmany(fs
->fs_cssize
, fs
->fs_fsize
);
574 i
= fs
->fs_frag
- loc
% fs
->fs_frag
;
575 len
= (i
== fs
->fs_frag
) ? 0 : i
* fs
->fs_fsize
;
577 if ((error
= bread(devvp
, fsbtodb(fs
, fs
->fs_csaddr
+ loc
),
578 len
, l
->l_cred
, 0, &bp
)) != 0) {
580 free(copyfs
->fs_csp
, M_UFSMNT
);
581 free(*sbbuf
, M_UFSMNT
);
585 memcpy(space
, bp
->b_data
, (u_int
)len
);
586 space
= (char *)space
+ len
;
587 brelse(bp
, BC_INVAL
| BC_NOCACHE
);
589 if (fs
->fs_contigsumsize
> 0) {
590 copyfs
->fs_maxcluster
= lp
= space
;
591 for (i
= 0; i
< fs
->fs_ncg
; i
++)
592 *lp
++ = fs
->fs_contigsumsize
;
595 copyfs
->fs_flags
&= ~FS_DOWAPBL
;
600 * We must check for active files that have been unlinked (e.g., with a zero
601 * link count). We have to expunge all trace of these files from the snapshot
602 * so that they are not reclaimed prematurely by fsck or unnecessarily dumped.
603 * Note that we skip unlinked snapshot files as they will be handled separately.
604 * Calculate the snapshot list size and create a preliminary list.
607 snapshot_expunge(struct mount
*mp
, struct vnode
*vp
, struct fs
*copy_fs
,
608 daddr_t
*snaplistsize
, daddr_t
**snaplist
)
610 int cg
, error
= 0, len
, loc
;
611 daddr_t blkno
, *blkp
;
612 struct fs
*fs
= VFSTOUFS(mp
)->um_fs
;
614 struct lwp
*l
= curlwp
;
616 struct vnode
*logvp
= NULL
, *mvp
= NULL
, *xvp
;
620 * Get the log inode if any.
622 if ((fs
->fs_flags
& FS_DOWAPBL
) &&
623 fs
->fs_journal_location
== UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM
) {
625 fs
->fs_journallocs
[UFS_WAPBL_INFS_INO
], &logvp
);
630 * Allocate a marker vnode.
634 * We also calculate the needed size for the snapshot list.
636 *snaplistsize
= fs
->fs_ncg
+ howmany(fs
->fs_cssize
, fs
->fs_bsize
) +
637 FSMAXSNAP
+ 1 /* superblock */ + 1 /* last block */ + 1 /* size */;
638 mutex_enter(&mntvnode_lock
);
640 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
641 * and vclean() can be called indirectly
643 for (xvp
= TAILQ_FIRST(&mp
->mnt_vnodelist
); xvp
; xvp
= vunmark(mvp
)) {
646 * Make sure this vnode wasn't reclaimed in getnewvnode().
647 * Start over if it has (it won't be on the list anymore).
649 if (xvp
->v_mount
!= mp
|| vismarker(xvp
))
651 mutex_enter(xvp
->v_interlock
);
652 if ((xvp
->v_iflag
& VI_XLOCK
) ||
653 xvp
->v_usecount
== 0 || xvp
->v_type
== VNON
||
655 (VTOI(xvp
)->i_flags
& SF_SNAPSHOT
)) {
656 mutex_exit(xvp
->v_interlock
);
659 mutex_exit(&mntvnode_lock
);
661 * XXXAD should increase vnode ref count to prevent it
662 * disappearing or being recycled.
664 mutex_exit(xvp
->v_interlock
);
667 vprint("ffs_snapshot: busy vnode", xvp
);
671 if (VOP_GETATTR(xvp
, &vat
, l
->l_cred
) == 0 &&
673 mutex_enter(&mntvnode_lock
);
676 if (ffs_checkfreefile(copy_fs
, vp
, xp
->i_number
)) {
677 mutex_enter(&mntvnode_lock
);
682 * If there is a fragment, clear it here.
685 loc
= howmany(xp
->i_size
, fs
->fs_bsize
) - 1;
687 len
= fragroundup(fs
, blkoff(fs
, xp
->i_size
));
688 if (len
> 0 && len
< fs
->fs_bsize
) {
689 error
= UFS_WAPBL_BEGIN(mp
);
694 ffs_blkfree_snap(copy_fs
, vp
, db_get(xp
, loc
),
696 blkno
= db_get(xp
, loc
);
697 db_assign(xp
, loc
, 0);
702 error
= expunge(vp
, xp
, copy_fs
, fullacct
, BLK_NOCOPY
);
704 db_assign(xp
, loc
, blkno
);
706 error
= UFS_WAPBL_BEGIN(mp
);
708 error
= ffs_freefile_snap(copy_fs
, vp
,
709 xp
->i_number
, xp
->i_mode
);
717 mutex_enter(&mntvnode_lock
);
719 mutex_exit(&mntvnode_lock
);
721 * Create a preliminary list of preallocated snapshot blocks.
723 *snaplist
= malloc(*snaplistsize
* sizeof(daddr_t
), M_UFSMNT
, M_WAITOK
);
724 blkp
= &(*snaplist
)[1];
725 *blkp
++ = lblkno(fs
, fs
->fs_sblockloc
);
726 blkno
= fragstoblks(fs
, fs
->fs_csaddr
);
727 for (cg
= 0; cg
< fs
->fs_ncg
; cg
++) {
728 if (fragstoblks(fs
, cgtod(fs
, cg
)) > blkno
)
730 *blkp
++ = fragstoblks(fs
, cgtod(fs
, cg
));
732 len
= howmany(fs
->fs_cssize
, fs
->fs_bsize
);
733 for (loc
= 0; loc
< len
; loc
++)
734 *blkp
++ = blkno
+ loc
;
735 for (; cg
< fs
->fs_ncg
; cg
++)
736 *blkp
++ = fragstoblks(fs
, cgtod(fs
, cg
));
737 (*snaplist
)[0] = blkp
- &(*snaplist
)[0];
744 if (error
&& *snaplist
!= NULL
) {
745 free(*snaplist
, M_UFSMNT
);
753 * Copy allocation information from all the snapshots in this snapshot and
754 * then expunge them from its view. Also, collect the list of allocated
755 * blocks in i_snapblklist.
758 snapshot_expunge_snap(struct mount
*mp
, struct vnode
*vp
,
759 struct fs
*copy_fs
, daddr_t snaplistsize
)
762 daddr_t numblks
, *snaplist
= NULL
;
763 struct fs
*fs
= VFSTOUFS(mp
)->um_fs
;
764 struct inode
*ip
= VTOI(vp
), *xp
;
765 struct lwp
*l
= curlwp
;
766 struct snap_info
*si
= VFSTOUFS(mp
)->um_snapinfo
;
768 TAILQ_FOREACH(xp
, &si
->si_snapshots
, i_nextsnap
) {
770 error
= expunge(vp
, xp
, fs
, snapacct
, BLK_SNAP
);
774 if (xp
->i_nlink
!= 0)
776 error
= UFS_WAPBL_BEGIN(mp
);
779 error
= ffs_freefile_snap(copy_fs
, vp
, xp
->i_number
, xp
->i_mode
);
787 * Allocate space for the full list of preallocated snapshot blocks.
789 snaplist
= malloc(snaplistsize
* sizeof(daddr_t
), M_UFSMNT
, M_WAITOK
);
790 ip
->i_snapblklist
= &snaplist
[1];
792 * Expunge the blocks used by the snapshots from the set of
793 * blocks marked as used in the snapshot bitmaps. Also, collect
794 * the list of allocated blocks in i_snapblklist.
796 error
= expunge(vp
, ip
, copy_fs
, mapacct
, BLK_SNAP
);
799 if (snaplistsize
< ip
->i_snapblklist
- snaplist
)
800 panic("ffs_snapshot: list too small");
801 snaplistsize
= ip
->i_snapblklist
- snaplist
;
802 snaplist
[0] = snaplistsize
;
803 ip
->i_snapblklist
= &snaplist
[0];
805 * Write out the list of allocated blocks to the end of the snapshot.
807 numblks
= howmany(fs
->fs_size
, fs
->fs_frag
);
808 for (i
= 0; i
< snaplistsize
; i
++)
809 snaplist
[i
] = ufs_rw64(snaplist
[i
], UFS_FSNEEDSWAP(fs
));
810 error
= vn_rdwr(UIO_WRITE
, vp
, (void *)snaplist
,
811 snaplistsize
* sizeof(daddr_t
), lblktosize(fs
, (off_t
)numblks
),
812 UIO_SYSSPACE
, IO_NODELOCKED
| IO_UNIT
, l
->l_cred
, NULL
, NULL
);
813 for (i
= 0; i
< snaplistsize
; i
++)
814 snaplist
[i
] = ufs_rw64(snaplist
[i
], UFS_FSNEEDSWAP(fs
));
816 if (error
&& snaplist
!= NULL
) {
817 free(snaplist
, M_UFSMNT
);
818 ip
->i_snapblklist
= NULL
;
824 * Write the superblock and its summary information to the snapshot.
825 * Make sure, the first NDADDR blocks get copied to the snapshot.
828 snapshot_writefs(struct mount
*mp
, struct vnode
*vp
, void *sbbuf
)
834 struct fs
*copyfs
, *fs
= VFSTOUFS(mp
)->um_fs
;
835 struct inode
*ip
= VTOI(vp
);
836 struct lwp
*l
= curlwp
;
838 copyfs
= (struct fs
*)((char *)sbbuf
+ blkoff(fs
, fs
->fs_sblockloc
));
841 * Write the superblock and its summary information
844 blkno
= fragstoblks(fs
, fs
->fs_csaddr
);
845 len
= howmany(fs
->fs_cssize
, fs
->fs_bsize
);
846 space
= copyfs
->fs_csp
;
848 if (UFS_FSNEEDSWAP(fs
)) {
849 ffs_sb_swap(copyfs
, copyfs
);
850 ffs_csum_swap(space
, space
, fs
->fs_cssize
);
853 error
= UFS_WAPBL_BEGIN(mp
);
856 for (loc
= 0; loc
< len
; loc
++) {
857 error
= bread(vp
, blkno
+ loc
, fs
->fs_bsize
, l
->l_cred
,
863 memcpy(bp
->b_data
, space
, fs
->fs_bsize
);
864 space
= (char *)space
+ fs
->fs_bsize
;
869 error
= bread(vp
, lblkno(fs
, fs
->fs_sblockloc
),
870 fs
->fs_bsize
, l
->l_cred
, B_MODIFY
, &bp
);
875 memcpy(bp
->b_data
, sbbuf
, fs
->fs_bsize
);
879 * Copy the first NDADDR blocks to the snapshot so ffs_copyonwrite()
880 * and ffs_snapblkfree() will always work on indirect blocks.
882 for (loc
= 0; loc
< NDADDR
; loc
++) {
883 if (db_get(ip
, loc
) != 0)
885 error
= ffs_balloc(vp
, lblktosize(fs
, (off_t
)loc
),
886 fs
->fs_bsize
, l
->l_cred
, 0, &bp
);
889 error
= rwfsblk(vp
, B_READ
, bp
->b_data
, loc
);
903 * Copy all cylinder group maps.
906 cgaccount(struct vnode
*vp
, int passno
, int *redo
)
910 struct fs
*fs
= VTOI(vp
)->i_fs
;
915 fs
->fs_active
= malloc(howmany(fs
->fs_ncg
, NBBY
),
916 M_DEVBUF
, M_WAITOK
| M_ZERO
);
917 for (cg
= 0; cg
< fs
->fs_ncg
; cg
++) {
918 if (passno
== 2 && ACTIVECG_ISSET(fs
, cg
))
923 error
= UFS_WAPBL_BEGIN(vp
->v_mount
);
926 error
= ffs_balloc(vp
, lfragtosize(fs
, cgtod(fs
, cg
)),
927 fs
->fs_bsize
, curlwp
->l_cred
, 0, &nbp
);
929 UFS_WAPBL_END(vp
->v_mount
);
932 error
= cgaccount1(cg
, vp
, nbp
->b_data
, passno
);
934 UFS_WAPBL_END(vp
->v_mount
);
942 * Copy a cylinder group map. All the unallocated blocks are marked
943 * BLK_NOCOPY so that the snapshot knows that it need not copy them
944 * if they are later written. If passno is one, then this is a first
945 * pass, so only setting needs to be done. If passno is 2, then this
946 * is a revision to a previous pass which must be undone as the
947 * replacement pass is done.
950 cgaccount1(int cg
, struct vnode
*vp
, void *data
, int passno
)
952 struct buf
*bp
, *ibp
;
956 struct lwp
*l
= curlwp
;
957 daddr_t base
, numblks
;
958 int error
, len
, loc
, ns
, indiroff
;
962 ns
= UFS_FSNEEDSWAP(fs
);
963 error
= bread(ip
->i_devvp
, fsbtodb(fs
, cgtod(fs
, cg
)),
964 (int)fs
->fs_cgsize
, l
->l_cred
, 0, &bp
);
969 cgp
= (struct cg
*)bp
->b_data
;
970 if (!cg_chkmagic(cgp
, ns
)) {
974 ACTIVECG_SET(fs
, cg
);
976 memcpy(data
, bp
->b_data
, fs
->fs_cgsize
);
978 if (fs
->fs_cgsize
< fs
->fs_bsize
)
979 memset((char *)data
+ fs
->fs_cgsize
, 0,
980 fs
->fs_bsize
- fs
->fs_cgsize
);
981 numblks
= howmany(fs
->fs_size
, fs
->fs_frag
);
982 len
= howmany(fs
->fs_fpg
, fs
->fs_frag
);
983 base
= cg
* fs
->fs_fpg
/ fs
->fs_frag
;
984 if (base
+ len
>= numblks
)
985 len
= numblks
- base
- 1;
988 for ( ; loc
< NDADDR
; loc
++) {
989 if (ffs_isblock(fs
, cg_blksfree(cgp
, ns
), loc
))
990 db_assign(ip
, loc
, BLK_NOCOPY
);
991 else if (db_get(ip
, loc
) == BLK_NOCOPY
) {
993 db_assign(ip
, loc
, 0);
994 else if (passno
== 1)
995 panic("ffs_snapshot: lost direct block");
999 if ((error
= ffs_balloc(vp
, lblktosize(fs
, (off_t
)(base
+ loc
)),
1000 fs
->fs_bsize
, l
->l_cred
, B_METAONLY
, &ibp
)) != 0)
1002 indiroff
= (base
+ loc
- NDADDR
) % NINDIR(fs
);
1003 for ( ; loc
< len
; loc
++, indiroff
++) {
1004 if (indiroff
>= NINDIR(fs
)) {
1006 if ((error
= ffs_balloc(vp
,
1007 lblktosize(fs
, (off_t
)(base
+ loc
)),
1008 fs
->fs_bsize
, l
->l_cred
, B_METAONLY
, &ibp
)) != 0)
1012 if (ffs_isblock(fs
, cg_blksfree(cgp
, ns
), loc
))
1013 idb_assign(ip
, ibp
->b_data
, indiroff
, BLK_NOCOPY
);
1014 else if (idb_get(ip
, ibp
->b_data
, indiroff
) == BLK_NOCOPY
) {
1016 idb_assign(ip
, ibp
->b_data
, indiroff
, 0);
1017 else if (passno
== 1)
1018 panic("ffs_snapshot: lost indirect block");
1026 * Before expunging a snapshot inode, note all the
1027 * blocks that it claims with BLK_SNAP so that fsck will
1028 * be able to account for those blocks properly and so
1029 * that this snapshot knows that it need not copy them
1030 * if the other snapshot holding them is freed.
1033 expunge(struct vnode
*snapvp
, struct inode
*cancelip
, struct fs
*fs
,
1034 acctfunc_t acctfunc
, int expungetype
)
1038 daddr_t len
, blkno
, numblks
, blksperindir
;
1039 struct ufs1_dinode
*dip1
;
1040 struct ufs2_dinode
*dip2
;
1041 struct lwp
*l
= curlwp
;
1046 ns
= UFS_FSNEEDSWAP(fs
);
1047 mp
= snapvp
->v_mount
;
1049 error
= UFS_WAPBL_BEGIN(mp
);
1053 * Prepare to expunge the inode. If its inode block has not
1054 * yet been copied, then allocate and fill the copy.
1056 lbn
= fragstoblks(fs
, ino_to_fsba(fs
, cancelip
->i_number
));
1057 error
= snapblkaddr(snapvp
, lbn
, &blkno
);
1061 error
= bread(snapvp
, lbn
, fs
->fs_bsize
, l
->l_cred
,
1064 error
= ffs_balloc(snapvp
, lblktosize(fs
, (off_t
)lbn
),
1065 fs
->fs_bsize
, l
->l_cred
, 0, &bp
);
1067 error
= rwfsblk(snapvp
, B_READ
, bp
->b_data
, lbn
);
1074 * Set a snapshot inode to be a zero length file, regular files
1075 * or unlinked snapshots to be completely unallocated.
1077 if (fs
->fs_magic
== FS_UFS1_MAGIC
) {
1078 dip1
= (struct ufs1_dinode
*)bp
->b_data
+
1079 ino_to_fsbo(fs
, cancelip
->i_number
);
1080 if (cancelip
->i_flags
& SF_SNAPSHOT
) {
1082 ufs_rw32(ufs_rw32(dip1
->di_flags
, ns
) |
1085 if (expungetype
== BLK_NOCOPY
|| cancelip
->i_nlink
== 0)
1088 dip1
->di_blocks
= 0;
1089 memset(&dip1
->di_db
[0], 0, (NDADDR
+ NIADDR
) * sizeof(int32_t));
1091 dip2
= (struct ufs2_dinode
*)bp
->b_data
+
1092 ino_to_fsbo(fs
, cancelip
->i_number
);
1093 if (cancelip
->i_flags
& SF_SNAPSHOT
) {
1095 ufs_rw32(ufs_rw32(dip2
->di_flags
, ns
) |
1098 if (expungetype
== BLK_NOCOPY
|| cancelip
->i_nlink
== 0)
1101 dip2
->di_blocks
= 0;
1102 memset(&dip2
->di_db
[0], 0, (NDADDR
+ NIADDR
) * sizeof(int64_t));
1107 * Now go through and expunge all the blocks in the file
1108 * using the function requested.
1110 numblks
= howmany(cancelip
->i_size
, fs
->fs_bsize
);
1111 if (fs
->fs_magic
== FS_UFS1_MAGIC
)
1112 bap
= &cancelip
->i_ffs1_db
[0];
1114 bap
= &cancelip
->i_ffs2_db
[0];
1115 error
= (*acctfunc
)(snapvp
, bap
, 0, NDADDR
, fs
, 0, expungetype
);
1118 if (fs
->fs_magic
== FS_UFS1_MAGIC
)
1119 bap
= &cancelip
->i_ffs1_ib
[0];
1121 bap
= &cancelip
->i_ffs2_ib
[0];
1122 error
= (*acctfunc
)(snapvp
, bap
, 0, NIADDR
, fs
, -1, expungetype
);
1127 len
= numblks
- NDADDR
;
1129 for (i
= 0; len
> 0 && i
< NIADDR
; i
++) {
1130 error
= indiracct(snapvp
, ITOV(cancelip
), i
,
1131 ib_get(cancelip
, i
), lbn
, rlbn
, len
,
1132 blksperindir
, fs
, acctfunc
, expungetype
);
1135 blksperindir
*= NINDIR(fs
);
1136 lbn
-= blksperindir
+ 1;
1137 len
-= blksperindir
;
1138 rlbn
+= blksperindir
;
1144 * Descend an indirect block chain for vnode cancelvp accounting for all
1145 * its indirect blocks in snapvp.
1148 indiracct(struct vnode
*snapvp
, struct vnode
*cancelvp
, int level
,
1149 daddr_t blkno
, daddr_t lbn
, daddr_t rlbn
, daddr_t remblks
,
1150 daddr_t blksperindir
, struct fs
*fs
, acctfunc_t acctfunc
, int expungetype
)
1153 daddr_t subblksperindir
;
1154 struct indir indirs
[NIADDR
+ 2];
1160 if (expungetype
== BLK_NOCOPY
)
1162 panic("indiracct: missing indir");
1164 if ((error
= ufs_getlbns(cancelvp
, rlbn
, indirs
, &num
)) != 0)
1166 if (lbn
!= indirs
[num
- 1 - level
].in_lbn
|| num
< 2)
1167 panic("indiracct: botched params");
1169 * We have to expand bread here since it will deadlock looking
1170 * up the block number for any blocks that are not in the cache.
1172 error
= ffs_getblk(cancelvp
, lbn
, fsbtodb(fs
, blkno
), fs
->fs_bsize
,
1176 if ((bp
->b_oflags
& (BO_DONE
| BO_DELWRI
)) == 0 && (error
=
1177 rwfsblk(bp
->b_vp
, B_READ
, bp
->b_data
, fragstoblks(fs
, blkno
)))) {
1182 * Account for the block pointers in this indirect block.
1184 last
= howmany(remblks
, blksperindir
);
1185 if (last
> NINDIR(fs
))
1187 bap
= malloc(fs
->fs_bsize
, M_DEVBUF
, M_WAITOK
| M_ZERO
);
1188 memcpy((void *)bap
, bp
->b_data
, fs
->fs_bsize
);
1190 error
= (*acctfunc
)(snapvp
, bap
, 0, last
,
1191 fs
, level
== 0 ? rlbn
: -1, expungetype
);
1192 if (error
|| level
== 0)
1195 * Account for the block pointers in each of the indirect blocks
1196 * in the levels below us.
1198 subblksperindir
= blksperindir
/ NINDIR(fs
);
1199 for (lbn
++, level
--, i
= 0; i
< last
; i
++) {
1200 error
= indiracct(snapvp
, cancelvp
, level
,
1201 idb_get(VTOI(snapvp
), bap
, i
), lbn
, rlbn
, remblks
,
1202 subblksperindir
, fs
, acctfunc
, expungetype
);
1205 rlbn
+= blksperindir
;
1206 lbn
-= blksperindir
;
1207 remblks
-= blksperindir
;
1210 free(bap
, M_DEVBUF
);
1215 * Do both snap accounting and map accounting.
1218 fullacct(struct vnode
*vp
, void *bap
, int oldblkp
, int lastblkp
,
1219 struct fs
*fs
, daddr_t lblkno
,
1220 int exptype
/* BLK_SNAP or BLK_NOCOPY */)
1224 if ((error
= snapacct(vp
, bap
, oldblkp
, lastblkp
, fs
, lblkno
, exptype
)))
1226 return (mapacct(vp
, bap
, oldblkp
, lastblkp
, fs
, lblkno
, exptype
));
1230 * Identify a set of blocks allocated in a snapshot inode.
1233 snapacct(struct vnode
*vp
, void *bap
, int oldblkp
, int lastblkp
,
1234 struct fs
*fs
, daddr_t lblkno
,
1235 int expungetype
/* BLK_SNAP or BLK_NOCOPY */)
1237 struct inode
*ip
= VTOI(vp
);
1238 struct lwp
*l
= curlwp
;
1239 struct mount
*mp
= vp
->v_mount
;
1244 const int wbreak
= blocks_in_journal(VFSTOUFS(mp
)->um_fs
)/8;
1246 error
= UFS_WAPBL_BEGIN(mp
);
1249 for ( n
= 0; oldblkp
< lastblkp
; oldblkp
++) {
1250 blkno
= idb_get(ip
, bap
, oldblkp
);
1251 if (blkno
== 0 || blkno
== BLK_NOCOPY
|| blkno
== BLK_SNAP
)
1253 lbn
= fragstoblks(fs
, blkno
);
1255 blkno
= db_get(ip
, lbn
);
1256 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
1258 error
= ffs_balloc(vp
, lblktosize(fs
, (off_t
)lbn
),
1259 fs
->fs_bsize
, l
->l_cred
, B_METAONLY
, &ibp
);
1262 blkno
= idb_get(ip
, ibp
->b_data
,
1263 (lbn
- NDADDR
) % NINDIR(fs
));
1266 * If we are expunging a snapshot vnode and we
1267 * find a block marked BLK_NOCOPY, then it is
1268 * one that has been allocated to this snapshot after
1269 * we took our current snapshot and can be ignored.
1271 if (expungetype
== BLK_SNAP
&& blkno
== BLK_NOCOPY
) {
1276 panic("snapacct: bad block");
1278 db_assign(ip
, lbn
, expungetype
);
1280 idb_assign(ip
, ibp
->b_data
,
1281 (lbn
- NDADDR
) % NINDIR(fs
), expungetype
);
1285 if (wbreak
> 0 && (++n
% wbreak
) == 0) {
1287 error
= UFS_WAPBL_BEGIN(mp
);
1297 * Account for a set of blocks allocated in a snapshot inode.
1300 mapacct(struct vnode
*vp
, void *bap
, int oldblkp
, int lastblkp
,
1301 struct fs
*fs
, daddr_t lblkno
, int expungetype
)
1305 struct mount
*mp
= vp
->v_mount
;
1307 int acctit
, error
, n
;
1308 const int wbreak
= blocks_in_journal(VFSTOUFS(mp
)->um_fs
)/8;
1310 error
= UFS_WAPBL_BEGIN(mp
);
1314 inum
= ip
->i_number
;
1319 for ( n
= 0; oldblkp
< lastblkp
; oldblkp
++, lblkno
++) {
1320 blkno
= idb_get(ip
, bap
, oldblkp
);
1321 if (blkno
== 0 || blkno
== BLK_NOCOPY
)
1323 if (acctit
&& expungetype
== BLK_SNAP
&& blkno
!= BLK_SNAP
)
1324 *ip
->i_snapblklist
++ = lblkno
;
1325 if (blkno
== BLK_SNAP
)
1326 blkno
= blkstofrags(fs
, lblkno
);
1327 ffs_blkfree_snap(fs
, vp
, blkno
, fs
->fs_bsize
, inum
);
1328 if (wbreak
> 0 && (++n
% wbreak
) == 0) {
1330 error
= UFS_WAPBL_BEGIN(mp
);
1340 * Number of blocks that fit into the journal or zero if not logging.
1343 blocks_in_journal(struct fs
*fs
)
1347 if ((fs
->fs_flags
& FS_DOWAPBL
) == 0)
1350 if (fs
->fs_journal_version
== UFS_WAPBL_VERSION
) {
1351 switch (fs
->fs_journal_location
) {
1352 case UFS_WAPBL_JOURNALLOC_END_PARTITION
:
1353 bpj
= (off_t
)fs
->fs_journallocs
[UFS_WAPBL_EPART_BLKSZ
]*
1354 fs
->fs_journallocs
[UFS_WAPBL_EPART_COUNT
];
1356 case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM
:
1357 bpj
= (off_t
)fs
->fs_journallocs
[UFS_WAPBL_INFS_BLKSZ
]*
1358 fs
->fs_journallocs
[UFS_WAPBL_INFS_COUNT
];
1362 bpj
/= fs
->fs_bsize
;
1363 return (bpj
> 0 ? bpj
: 1);
1365 #endif /* defined(FFS_NO_SNAPSHOT) */
1368 * Decrement extra reference on snapshot when last name is removed.
1369 * It will not be freed until the last open reference goes away.
1372 ffs_snapgone(struct inode
*ip
)
1374 struct mount
*mp
= ip
->i_devvp
->v_specmountpoint
;
1377 struct snap_info
*si
;
1380 si
= VFSTOUFS(mp
)->um_snapinfo
;
1383 * Find snapshot in incore list.
1385 mutex_enter(&si
->si_lock
);
1386 TAILQ_FOREACH(xp
, &si
->si_snapshots
, i_nextsnap
)
1389 mutex_exit(&si
->si_lock
);
1394 printf("ffs_snapgone: lost snapshot vnode %llu\n",
1395 (unsigned long long)ip
->i_number
);
1398 * Delete snapshot inode from superblock. Keep list dense.
1400 mutex_enter(&si
->si_lock
);
1402 for (snaploc
= 0; snaploc
< FSMAXSNAP
; snaploc
++)
1403 if (fs
->fs_snapinum
[snaploc
] == ip
->i_number
)
1405 if (snaploc
< FSMAXSNAP
) {
1406 for (snaploc
++; snaploc
< FSMAXSNAP
; snaploc
++) {
1407 if (fs
->fs_snapinum
[snaploc
] == 0)
1409 fs
->fs_snapinum
[snaploc
- 1] = fs
->fs_snapinum
[snaploc
];
1411 fs
->fs_snapinum
[snaploc
- 1] = 0;
1414 mutex_exit(&si
->si_lock
);
1418 * Prepare a snapshot file for being removed.
1421 ffs_snapremove(struct vnode
*vp
)
1423 struct inode
*ip
= VTOI(vp
), *xp
;
1424 struct vnode
*devvp
= ip
->i_devvp
;
1425 struct fs
*fs
= ip
->i_fs
;
1426 struct mount
*mp
= devvp
->v_specmountpoint
;
1428 struct snap_info
*si
;
1429 struct lwp
*l
= curlwp
;
1430 daddr_t numblks
, blkno
, dblk
;
1431 int error
, loc
, last
;
1433 si
= VFSTOUFS(mp
)->um_snapinfo
;
1435 * If active, delete from incore list (this snapshot may
1436 * already have been in the process of being deleted, so
1437 * would not have been active).
1439 * Clear copy-on-write flag if last snapshot.
1441 mutex_enter(&si
->si_snaplock
);
1442 mutex_enter(&si
->si_lock
);
1443 if (is_active_snapshot(si
, ip
)) {
1444 TAILQ_REMOVE(&si
->si_snapshots
, ip
, i_nextsnap
);
1445 if (TAILQ_FIRST(&si
->si_snapshots
) != 0) {
1446 /* Roll back the list of preallocated blocks. */
1447 xp
= TAILQ_LAST(&si
->si_snapshots
, inodelst
);
1448 si
->si_snapblklist
= xp
->i_snapblklist
;
1450 mutex_exit(&si
->si_lock
);
1451 mutex_exit(&si
->si_snaplock
);
1453 si
->si_snapblklist
= 0;
1455 mutex_exit(&si
->si_lock
);
1456 mutex_exit(&si
->si_snaplock
);
1457 fscow_disestablish(mp
, ffs_copyonwrite
, devvp
);
1459 if (ip
->i_snapblklist
!= NULL
) {
1460 free(ip
->i_snapblklist
, M_UFSMNT
);
1461 ip
->i_snapblklist
= NULL
;
1464 mutex_exit(&si
->si_lock
);
1465 mutex_exit(&si
->si_snaplock
);
1468 * Clear all BLK_NOCOPY fields. Pass any block claims to other
1469 * snapshots that want them (see ffs_snapblkfree below).
1471 for (blkno
= 1; blkno
< NDADDR
; blkno
++) {
1472 dblk
= db_get(ip
, blkno
);
1473 if (dblk
== BLK_NOCOPY
|| dblk
== BLK_SNAP
)
1474 db_assign(ip
, blkno
, 0);
1475 else if ((dblk
== blkstofrags(fs
, blkno
) &&
1476 ffs_snapblkfree(fs
, ip
->i_devvp
, dblk
, fs
->fs_bsize
,
1478 DIP_ADD(ip
, blocks
, -btodb(fs
->fs_bsize
));
1479 db_assign(ip
, blkno
, 0);
1482 numblks
= howmany(ip
->i_size
, fs
->fs_bsize
);
1483 for (blkno
= NDADDR
; blkno
< numblks
; blkno
+= NINDIR(fs
)) {
1484 error
= ffs_balloc(vp
, lblktosize(fs
, (off_t
)blkno
),
1485 fs
->fs_bsize
, l
->l_cred
, B_METAONLY
, &ibp
);
1488 if (fs
->fs_size
- blkno
> NINDIR(fs
))
1491 last
= fs
->fs_size
- blkno
;
1492 for (loc
= 0; loc
< last
; loc
++) {
1493 dblk
= idb_get(ip
, ibp
->b_data
, loc
);
1494 if (dblk
== BLK_NOCOPY
|| dblk
== BLK_SNAP
)
1495 idb_assign(ip
, ibp
->b_data
, loc
, 0);
1496 else if (dblk
== blkstofrags(fs
, blkno
) &&
1497 ffs_snapblkfree(fs
, ip
->i_devvp
, dblk
,
1498 fs
->fs_bsize
, ip
->i_number
)) {
1499 DIP_ADD(ip
, blocks
, -btodb(fs
->fs_bsize
));
1500 idb_assign(ip
, ibp
->b_data
, loc
, 0);
1505 error
= UFS_WAPBL_BEGIN(mp
);
1506 KASSERT(error
== 0);
1509 * Clear snapshot flag and drop reference.
1511 ip
->i_flags
&= ~(SF_SNAPSHOT
| SF_SNAPINVAL
);
1512 DIP_ASSIGN(ip
, flags
, ip
->i_flags
);
1513 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
1514 #if defined(QUOTA) || defined(QUOTA2)
1515 chkdq(ip
, DIP(ip
, blocks
), l
->l_cred
, FORCE
);
1516 chkiq(ip
, 1, l
->l_cred
, FORCE
);
1521 * Notification that a block is being freed. Return zero if the free
1522 * should be allowed to proceed. Return non-zero if the snapshot file
1523 * wants to claim the block. The block will be claimed if it is an
1524 * uncopied part of one of the snapshots. It will be freed if it is
1525 * either a BLK_NOCOPY or has already been copied in all of the snapshots.
1526 * If a fragment is being freed, then all snapshots that care about
1527 * it must make a copy since a snapshot file can only claim full sized
1528 * blocks. Note that if more than one snapshot file maps the block,
1529 * we can pick one at random to claim it. Since none of the snapshots
1530 * can change, we are assurred that they will all see the same unmodified
1531 * image. When deleting a snapshot file (see ffs_snapremove above), we
1532 * must push any of these claimed blocks to one of the other snapshots
1533 * that maps it. These claimed blocks are easily identified as they will
1534 * have a block number equal to their logical block number within the
1535 * snapshot. A copied block can never have this property because they
1536 * must always have been allocated from a BLK_NOCOPY location.
1539 ffs_snapblkfree(struct fs
*fs
, struct vnode
*devvp
, daddr_t bno
,
1540 long size
, ino_t inum
)
1542 struct mount
*mp
= devvp
->v_specmountpoint
;
1545 struct vnode
*vp
= NULL
;
1546 struct snap_info
*si
;
1547 void *saved_data
= NULL
;
1551 int indiroff
= 0, error
= 0, claimedblk
= 0;
1553 si
= VFSTOUFS(mp
)->um_snapinfo
;
1554 lbn
= fragstoblks(fs
, bno
);
1555 mutex_enter(&si
->si_snaplock
);
1556 mutex_enter(&si
->si_lock
);
1557 si
->si_owner
= curlwp
;
1561 TAILQ_FOREACH(ip
, &si
->si_snapshots
, i_nextsnap
) {
1564 * Lookup block being written.
1567 blkno
= db_get(ip
, lbn
);
1569 mutex_exit(&si
->si_lock
);
1570 error
= ffs_balloc(vp
, lblktosize(fs
, (off_t
)lbn
),
1571 fs
->fs_bsize
, FSCRED
, B_METAONLY
, &ibp
);
1573 mutex_enter(&si
->si_lock
);
1576 indiroff
= (lbn
- NDADDR
) % NINDIR(fs
);
1577 blkno
= idb_get(ip
, ibp
->b_data
, indiroff
);
1578 mutex_enter(&si
->si_lock
);
1579 if (gen
!= si
->si_gen
) {
1585 * Check to see if block needs to be copied.
1589 * A block that we map is being freed. If it has not
1590 * been claimed yet, we will claim or copy it (below).
1593 } else if (blkno
== BLK_SNAP
) {
1595 * No previous snapshot claimed the block,
1596 * so it will be freed and become a BLK_NOCOPY
1597 * (don't care) for us.
1600 panic("snapblkfree: inconsistent block type");
1602 db_assign(ip
, lbn
, BLK_NOCOPY
);
1603 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
1605 idb_assign(ip
, ibp
->b_data
, indiroff
,
1607 mutex_exit(&si
->si_lock
);
1608 if (ip
->i_nlink
> 0)
1612 mutex_enter(&si
->si_lock
);
1613 if (gen
!= si
->si_gen
)
1617 } else /* BLK_NOCOPY or default */ {
1619 * If the snapshot has already copied the block
1620 * (default), or does not care about the block,
1628 * If this is a full size block, we will just grab it
1629 * and assign it to the snapshot inode. Otherwise we
1630 * will proceed to copy it. See explanation for this
1631 * routine as to why only a single snapshot needs to
1634 if (size
== fs
->fs_bsize
) {
1637 printf("%s %llu lbn %" PRId64
1639 "Grabonremove: snapino",
1640 (unsigned long long)ip
->i_number
,
1641 lbn
, (unsigned long long)inum
);
1643 mutex_exit(&si
->si_lock
);
1645 db_assign(ip
, lbn
, bno
);
1647 idb_assign(ip
, ibp
->b_data
, indiroff
, bno
);
1648 if (ip
->i_nlink
> 0)
1653 DIP_ADD(ip
, blocks
, btodb(size
));
1654 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
1655 if (ip
->i_nlink
> 0 && mp
->mnt_wapbl
)
1656 error
= syncsnap(vp
);
1659 mutex_enter(&si
->si_lock
);
1660 si
->si_owner
= NULL
;
1661 mutex_exit(&si
->si_lock
);
1662 mutex_exit(&si
->si_snaplock
);
1663 return (error
== 0);
1669 printf("%s%llu lbn %" PRId64
" %s %llu size %ld\n",
1670 "Copyonremove: snapino ",
1671 (unsigned long long)ip
->i_number
,
1672 lbn
, "for inum", (unsigned long long)inum
, size
);
1675 * If we have already read the old block contents, then
1676 * simply copy them to the new block. Note that we need
1677 * to synchronously write snapshots that have not been
1678 * unlinked, and hence will be visible after a crash,
1679 * to ensure their integrity.
1681 mutex_exit(&si
->si_lock
);
1682 if (saved_data
== NULL
) {
1683 saved_data
= malloc(fs
->fs_bsize
, M_UFSMNT
, M_WAITOK
);
1684 error
= rwfsblk(vp
, B_READ
, saved_data
, lbn
);
1686 free(saved_data
, M_UFSMNT
);
1688 mutex_enter(&si
->si_lock
);
1692 error
= wrsnapblk(vp
, saved_data
, lbn
);
1693 if (error
== 0 && ip
->i_nlink
> 0 && mp
->mnt_wapbl
)
1694 error
= syncsnap(vp
);
1695 mutex_enter(&si
->si_lock
);
1698 if (gen
!= si
->si_gen
)
1701 si
->si_owner
= NULL
;
1702 mutex_exit(&si
->si_lock
);
1703 mutex_exit(&si
->si_snaplock
);
1705 free(saved_data
, M_UFSMNT
);
1707 * If we have been unable to allocate a block in which to do
1708 * the copy, then return non-zero so that the fragment will
1709 * not be freed. Although space will be lost, the snapshot
1710 * will stay consistent.
1716 * Associate snapshot files when mounting.
1719 ffs_snapshot_mount(struct mount
*mp
)
1721 struct vnode
*devvp
= VFSTOUFS(mp
)->um_devvp
;
1722 struct fs
*fs
= VFSTOUFS(mp
)->um_fs
;
1723 struct lwp
*l
= curlwp
;
1725 struct inode
*ip
, *xp
;
1726 struct snap_info
*si
;
1727 daddr_t snaplistsize
, *snapblklist
;
1728 int i
, error
, ns
, snaploc
, loc
;
1731 * No persistent snapshots on apple ufs file systems.
1733 if (UFS_MPISAPPLEUFS(VFSTOUFS(mp
)))
1736 si
= VFSTOUFS(mp
)->um_snapinfo
;
1737 ns
= UFS_FSNEEDSWAP(fs
);
1739 * XXX The following needs to be set before ffs_truncate or
1740 * VOP_READ can be called.
1742 mp
->mnt_stat
.f_iosize
= fs
->fs_bsize
;
1744 * Process each snapshot listed in the superblock.
1747 mutex_enter(&si
->si_lock
);
1748 for (snaploc
= 0; snaploc
< FSMAXSNAP
; snaploc
++) {
1749 if (fs
->fs_snapinum
[snaploc
] == 0)
1751 if ((error
= VFS_VGET(mp
, fs
->fs_snapinum
[snaploc
],
1753 printf("ffs_snapshot_mount: vget failed %d\n", error
);
1757 if ((ip
->i_flags
& (SF_SNAPSHOT
| SF_SNAPINVAL
)) !=
1759 printf("ffs_snapshot_mount: non-snapshot inode %d\n",
1760 fs
->fs_snapinum
[snaploc
]);
1763 for (loc
= snaploc
+ 1; loc
< FSMAXSNAP
; loc
++) {
1764 if (fs
->fs_snapinum
[loc
] == 0)
1766 fs
->fs_snapinum
[loc
- 1] = fs
->fs_snapinum
[loc
];
1768 fs
->fs_snapinum
[loc
- 1] = 0;
1774 * Read the block hints list. Use an empty list on
1777 error
= vn_rdwr(UIO_READ
, vp
,
1778 (void *)&snaplistsize
, sizeof(snaplistsize
),
1779 lblktosize(fs
, howmany(fs
->fs_size
, fs
->fs_frag
)),
1780 UIO_SYSSPACE
, IO_NODELOCKED
|IO_UNIT
|IO_ALTSEMANTICS
,
1781 l
->l_cred
, NULL
, NULL
);
1783 printf("ffs_snapshot_mount: read_1 failed %d\n", error
);
1786 snaplistsize
= ufs_rw64(snaplistsize
, ns
);
1787 snapblklist
= malloc(
1788 snaplistsize
* sizeof(daddr_t
), M_UFSMNT
, M_WAITOK
);
1792 error
= vn_rdwr(UIO_READ
, vp
, (void *)snapblklist
,
1793 snaplistsize
* sizeof(daddr_t
),
1794 lblktosize(fs
, howmany(fs
->fs_size
, fs
->fs_frag
)),
1795 UIO_SYSSPACE
, IO_NODELOCKED
|IO_UNIT
|IO_ALTSEMANTICS
,
1796 l
->l_cred
, NULL
, NULL
);
1797 for (i
= 0; i
< snaplistsize
; i
++)
1798 snapblklist
[i
] = ufs_rw64(snapblklist
[i
], ns
);
1800 printf("ffs_snapshot_mount: read_2 failed %d\n",
1805 ip
->i_snapblklist
= &snapblklist
[0];
1808 * Link it onto the active snapshot list.
1810 if (is_active_snapshot(si
, ip
))
1811 panic("ffs_snapshot_mount: %"PRIu64
" already on list",
1814 TAILQ_INSERT_TAIL(&si
->si_snapshots
, ip
, i_nextsnap
);
1815 vp
->v_vflag
|= VV_SYSTEM
;
1819 * No usable snapshots found.
1822 mutex_exit(&si
->si_lock
);
1826 * Attach the block hints list. We always want to
1827 * use the list from the newest snapshot.
1829 xp
= TAILQ_LAST(&si
->si_snapshots
, inodelst
);
1830 si
->si_snapblklist
= xp
->i_snapblklist
;
1831 fscow_establish(mp
, ffs_copyonwrite
, devvp
);
1833 mutex_exit(&si
->si_lock
);
1837 * Disassociate snapshot files when unmounting.
1840 ffs_snapshot_unmount(struct mount
*mp
)
1842 struct vnode
*devvp
= VFSTOUFS(mp
)->um_devvp
;
1844 struct vnode
*vp
= NULL
;
1845 struct snap_info
*si
;
1847 si
= VFSTOUFS(mp
)->um_snapinfo
;
1848 mutex_enter(&si
->si_lock
);
1849 while ((xp
= TAILQ_FIRST(&si
->si_snapshots
)) != 0) {
1851 TAILQ_REMOVE(&si
->si_snapshots
, xp
, i_nextsnap
);
1852 if (xp
->i_snapblklist
== si
->si_snapblklist
)
1853 si
->si_snapblklist
= NULL
;
1854 free(xp
->i_snapblklist
, M_UFSMNT
);
1855 if (xp
->i_nlink
> 0) {
1857 mutex_exit(&si
->si_lock
);
1859 mutex_enter(&si
->si_lock
);
1863 mutex_exit(&si
->si_lock
);
1865 fscow_disestablish(mp
, ffs_copyonwrite
, devvp
);
1869 * Check for need to copy block that is about to be written,
1870 * copying the block if necessary.
1873 ffs_copyonwrite(void *v
, struct buf
*bp
, bool data_valid
)
1877 struct vnode
*devvp
= v
, *vp
= NULL
;
1878 struct mount
*mp
= devvp
->v_specmountpoint
;
1879 struct snap_info
*si
;
1880 void *saved_data
= NULL
;
1881 daddr_t lbn
, blkno
, *snapblklist
;
1883 int lower
, upper
, mid
, snapshot_locked
= 0, error
= 0;
1886 * Check for valid snapshots.
1888 si
= VFSTOUFS(mp
)->um_snapinfo
;
1889 mutex_enter(&si
->si_lock
);
1890 ip
= TAILQ_FIRST(&si
->si_snapshots
);
1892 mutex_exit(&si
->si_lock
);
1896 * First check to see if it is after the file system,
1897 * in the journal or in the preallocated list.
1898 * By doing these checks we avoid several potential deadlocks.
1901 lbn
= fragstoblks(fs
, dbtofsb(fs
, bp
->b_blkno
));
1902 if (bp
->b_blkno
>= fsbtodb(fs
, fs
->fs_size
)) {
1903 mutex_exit(&si
->si_lock
);
1906 if ((fs
->fs_flags
& FS_DOWAPBL
) &&
1907 fs
->fs_journal_location
== UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM
) {
1908 off_t blk_off
, log_start
, log_end
;
1910 log_start
= (off_t
)fs
->fs_journallocs
[UFS_WAPBL_INFS_ADDR
] *
1911 fs
->fs_journallocs
[UFS_WAPBL_INFS_BLKSZ
];
1912 log_end
= log_start
+ fs
->fs_journallocs
[UFS_WAPBL_INFS_COUNT
] *
1913 fs
->fs_journallocs
[UFS_WAPBL_INFS_BLKSZ
];
1914 blk_off
= dbtob(bp
->b_blkno
);
1915 if (blk_off
>= log_start
&& blk_off
< log_end
) {
1916 mutex_exit(&si
->si_lock
);
1920 snapblklist
= si
->si_snapblklist
;
1921 upper
= (snapblklist
!= NULL
? snapblklist
[0] - 1 : 0);
1923 while (lower
<= upper
) {
1924 mid
= (lower
+ upper
) / 2;
1925 if (snapblklist
[mid
] == lbn
)
1927 if (snapblklist
[mid
] < lbn
)
1932 if (lower
<= upper
) {
1933 mutex_exit(&si
->si_lock
);
1937 * Not in the precomputed list, so check the snapshots.
1939 if (si
->si_owner
!= curlwp
) {
1940 if (!mutex_tryenter(&si
->si_snaplock
)) {
1941 mutex_exit(&si
->si_lock
);
1942 mutex_enter(&si
->si_snaplock
);
1943 mutex_enter(&si
->si_lock
);
1945 si
->si_owner
= curlwp
;
1946 snapshot_locked
= 1;
1948 if (data_valid
&& bp
->b_bcount
== fs
->fs_bsize
)
1949 saved_data
= bp
->b_data
;
1952 TAILQ_FOREACH(ip
, &si
->si_snapshots
, i_nextsnap
) {
1955 * We ensure that everything of our own that needs to be
1956 * copied will be done at the time that ffs_snapshot is
1957 * called. Thus we can skip the check here which can
1958 * deadlock in doing the lookup in ffs_balloc.
1963 * Check to see if block needs to be copied.
1966 blkno
= db_get(ip
, lbn
);
1968 mutex_exit(&si
->si_lock
);
1969 blkno
= 0; /* XXX: GCC */
1970 if ((error
= snapblkaddr(vp
, lbn
, &blkno
)) != 0) {
1971 mutex_enter(&si
->si_lock
);
1974 mutex_enter(&si
->si_lock
);
1975 if (gen
!= si
->si_gen
)
1979 if (blkno
== BLK_SNAP
&& bp
->b_lblkno
>= 0)
1980 panic("ffs_copyonwrite: bad copy block");
1985 if (curlwp
== uvm
.pagedaemon_lwp
) {
1989 /* Only one level of recursion allowed. */
1990 KASSERT(snapshot_locked
);
1992 * Allocate the block into which to do the copy. Since
1993 * multiple processes may all try to copy the same block,
1994 * we have to recheck our need to do a copy if we sleep
1995 * waiting for the lock.
1997 * Because all snapshots on a filesystem share a single
1998 * lock, we ensure that we will never be in competition
1999 * with another process to allocate a block.
2003 printf("Copyonwrite: snapino %llu lbn %" PRId64
" for ",
2004 (unsigned long long)ip
->i_number
, lbn
);
2005 if (bp
->b_vp
== devvp
)
2006 printf("fs metadata");
2008 printf("inum %llu", (unsigned long long)
2009 VTOI(bp
->b_vp
)->i_number
);
2010 printf(" lblkno %" PRId64
"\n", bp
->b_lblkno
);
2014 * If we have already read the old block contents, then
2015 * simply copy them to the new block. Note that we need
2016 * to synchronously write snapshots that have not been
2017 * unlinked, and hence will be visible after a crash,
2018 * to ensure their integrity.
2020 mutex_exit(&si
->si_lock
);
2021 if (saved_data
== NULL
) {
2022 saved_data
= malloc(fs
->fs_bsize
, M_UFSMNT
, M_WAITOK
);
2023 error
= rwfsblk(vp
, B_READ
, saved_data
, lbn
);
2025 free(saved_data
, M_UFSMNT
);
2027 mutex_enter(&si
->si_lock
);
2031 error
= wrsnapblk(vp
, saved_data
, lbn
);
2032 if (error
== 0 && ip
->i_nlink
> 0 && mp
->mnt_wapbl
)
2033 error
= syncsnap(vp
);
2034 mutex_enter(&si
->si_lock
);
2037 if (gen
!= si
->si_gen
)
2041 * Note that we need to synchronously write snapshots that
2042 * have not been unlinked, and hence will be visible after
2043 * a crash, to ensure their integrity.
2045 if (snapshot_locked
) {
2046 si
->si_owner
= NULL
;
2047 mutex_exit(&si
->si_lock
);
2048 mutex_exit(&si
->si_snaplock
);
2050 mutex_exit(&si
->si_lock
);
2051 if (saved_data
&& saved_data
!= bp
->b_data
)
2052 free(saved_data
, M_UFSMNT
);
2057 * Read from a snapshot.
2060 ffs_snapshot_read(struct vnode
*vp
, struct uio
*uio
, int ioflag
)
2062 struct inode
*ip
= VTOI(vp
);
2063 struct fs
*fs
= ip
->i_fs
;
2064 struct snap_info
*si
= VFSTOUFS(vp
->v_mount
)->um_snapinfo
;
2066 daddr_t lbn
, nextlbn
;
2067 off_t fsbytes
, bytesinfile
;
2068 long size
, xfersize
, blkoffset
;
2071 fstrans_start(vp
->v_mount
, FSTRANS_SHARED
);
2072 mutex_enter(&si
->si_snaplock
);
2074 if (ioflag
& IO_ALTSEMANTICS
)
2075 fsbytes
= ip
->i_size
;
2077 fsbytes
= lfragtosize(fs
, fs
->fs_size
);
2078 for (error
= 0, bp
= NULL
; uio
->uio_resid
> 0; bp
= NULL
) {
2079 bytesinfile
= fsbytes
- uio
->uio_offset
;
2080 if (bytesinfile
<= 0)
2082 lbn
= lblkno(fs
, uio
->uio_offset
);
2084 size
= fs
->fs_bsize
;
2085 blkoffset
= blkoff(fs
, uio
->uio_offset
);
2086 xfersize
= MIN(MIN(fs
->fs_bsize
- blkoffset
, uio
->uio_resid
),
2089 if (lblktosize(fs
, nextlbn
+ 1) >= fsbytes
) {
2090 if (lblktosize(fs
, lbn
) + size
> fsbytes
)
2091 size
= fragroundup(fs
,
2092 fsbytes
- lblktosize(fs
, lbn
));
2093 error
= bread(vp
, lbn
, size
, NOCRED
, 0, &bp
);
2095 int nextsize
= fs
->fs_bsize
;
2096 error
= breadn(vp
, lbn
,
2097 size
, &nextlbn
, &nextsize
, 1, NOCRED
, 0, &bp
);
2103 * We should only get non-zero b_resid when an I/O error
2104 * has occurred, which should cause us to break above.
2105 * However, if the short read did not cause an error,
2106 * then we want to ensure that we do not uiomove bad
2107 * or uninitialized data.
2109 size
-= bp
->b_resid
;
2110 if (size
< blkoffset
+ xfersize
) {
2111 xfersize
= size
- blkoffset
;
2115 error
= uiomove((char *)bp
->b_data
+ blkoffset
, xfersize
, uio
);
2123 mutex_exit(&si
->si_snaplock
);
2124 fstrans_done(vp
->v_mount
);
2129 * Lookup a snapshots data block address.
2130 * Simpler than UFS_BALLOC() as we know all metadata is already allocated
2131 * and safe even for the pagedaemon where we cannot bread().
2134 snapblkaddr(struct vnode
*vp
, daddr_t lbn
, daddr_t
*res
)
2136 struct indir indirs
[NIADDR
+ 2];
2137 struct inode
*ip
= VTOI(vp
);
2138 struct fs
*fs
= ip
->i_fs
;
2145 *res
= db_get(ip
, lbn
);
2148 if ((error
= ufs_getlbns(vp
, lbn
, indirs
, &num
)) != 0)
2150 if (curlwp
== uvm
.pagedaemon_lwp
) {
2151 mutex_enter(&bufcache_lock
);
2152 bp
= incore(vp
, indirs
[num
-1].in_lbn
);
2153 if (bp
&& (bp
->b_oflags
& (BO_DONE
| BO_DELWRI
))) {
2154 *res
= idb_get(ip
, bp
->b_data
, indirs
[num
-1].in_off
);
2158 mutex_exit(&bufcache_lock
);
2161 error
= bread(vp
, indirs
[num
-1].in_lbn
, fs
->fs_bsize
, NOCRED
, 0, &bp
);
2163 *res
= idb_get(ip
, bp
->b_data
, indirs
[num
-1].in_off
);
2170 * Read or write the specified block of the filesystem vp resides on
2171 * from or to the disk bypassing the buffer cache.
2174 rwfsblk(struct vnode
*vp
, int flags
, void *data
, daddr_t lbn
)
2177 struct inode
*ip
= VTOI(vp
);
2178 struct fs
*fs
= ip
->i_fs
;
2181 nbp
= getiobuf(NULL
, true);
2182 nbp
->b_flags
= flags
;
2183 nbp
->b_bcount
= nbp
->b_bufsize
= fs
->fs_bsize
;
2186 nbp
->b_blkno
= nbp
->b_rawblkno
= fsbtodb(fs
, blkstofrags(fs
, lbn
));
2188 nbp
->b_dev
= ip
->i_devvp
->v_rdev
;
2189 SET(nbp
->b_cflags
, BC_BUSY
); /* mark buffer busy */
2193 error
= biowait(nbp
);
2201 * Write all dirty buffers to disk and invalidate them.
2204 syncsnap(struct vnode
*vp
)
2208 struct fs
*fs
= VTOI(vp
)->i_fs
;
2210 mutex_enter(&bufcache_lock
);
2211 while ((bp
= LIST_FIRST(&vp
->v_dirtyblkhd
))) {
2212 error
= bbusy(bp
, false, 0, NULL
);
2213 if (error
== EPASSTHROUGH
)
2215 else if (error
!= 0) {
2216 mutex_exit(&bufcache_lock
);
2219 KASSERT(bp
->b_bcount
== fs
->fs_bsize
);
2220 mutex_exit(&bufcache_lock
);
2221 error
= rwfsblk(vp
, B_WRITE
, bp
->b_data
,
2222 fragstoblks(fs
, dbtofsb(fs
, bp
->b_blkno
)));
2223 brelse(bp
, BC_INVAL
| BC_VFLUSH
);
2226 mutex_enter(&bufcache_lock
);
2228 mutex_exit(&bufcache_lock
);
2234 * Write the specified block to a snapshot.
2237 wrsnapblk(struct vnode
*vp
, void *data
, daddr_t lbn
)
2239 struct inode
*ip
= VTOI(vp
);
2240 struct fs
*fs
= ip
->i_fs
;
2244 error
= ffs_balloc(vp
, lblktosize(fs
, (off_t
)lbn
), fs
->fs_bsize
,
2245 FSCRED
, (ip
->i_nlink
> 0 ? B_SYNC
: 0), &bp
);
2248 memcpy(bp
->b_data
, data
, fs
->fs_bsize
);
2249 if (ip
->i_nlink
> 0)
2258 * Check if this inode is present on the active snapshot list.
2259 * Must be called with snapinfo locked.
2262 is_active_snapshot(struct snap_info
*si
, struct inode
*ip
)
2266 KASSERT(mutex_owned(&si
->si_lock
));
2268 TAILQ_FOREACH(xp
, &si
->si_snapshots
, i_nextsnap
)
2275 * Get/Put direct block from inode or buffer containing disk addresses. Take
2276 * care for fs type (UFS1/UFS2) and byte swapping. These functions should go
2277 * into a global include.
2279 static inline daddr_t
2280 db_get(struct inode
*ip
, int loc
)
2282 if (ip
->i_ump
->um_fstype
== UFS1
)
2283 return ufs_rw32(ip
->i_ffs1_db
[loc
], UFS_IPNEEDSWAP(ip
));
2285 return ufs_rw64(ip
->i_ffs2_db
[loc
], UFS_IPNEEDSWAP(ip
));
2289 db_assign(struct inode
*ip
, int loc
, daddr_t val
)
2291 if (ip
->i_ump
->um_fstype
== UFS1
)
2292 ip
->i_ffs1_db
[loc
] = ufs_rw32(val
, UFS_IPNEEDSWAP(ip
));
2294 ip
->i_ffs2_db
[loc
] = ufs_rw64(val
, UFS_IPNEEDSWAP(ip
));
2297 static inline daddr_t
2298 ib_get(struct inode
*ip
, int loc
)
2300 if (ip
->i_ump
->um_fstype
== UFS1
)
2301 return ufs_rw32(ip
->i_ffs1_ib
[loc
], UFS_IPNEEDSWAP(ip
));
2303 return ufs_rw64(ip
->i_ffs2_ib
[loc
], UFS_IPNEEDSWAP(ip
));
2307 ib_assign(struct inode
*ip
, int loc
, daddr_t val
)
2309 if (ip
->i_ump
->um_fstype
== UFS1
)
2310 ip
->i_ffs1_ib
[loc
] = ufs_rw32(val
, UFS_IPNEEDSWAP(ip
));
2312 ip
->i_ffs2_ib
[loc
] = ufs_rw64(val
, UFS_IPNEEDSWAP(ip
));
2315 static inline daddr_t
2316 idb_get(struct inode
*ip
, void *bf
, int loc
)
2318 if (ip
->i_ump
->um_fstype
== UFS1
)
2319 return ufs_rw32(((int32_t *)(bf
))[loc
], UFS_IPNEEDSWAP(ip
));
2321 return ufs_rw64(((int64_t *)(bf
))[loc
], UFS_IPNEEDSWAP(ip
));
2325 idb_assign(struct inode
*ip
, void *bf
, int loc
, daddr_t val
)
2327 if (ip
->i_ump
->um_fstype
== UFS1
)
2328 ((int32_t *)(bf
))[loc
] = ufs_rw32(val
, UFS_IPNEEDSWAP(ip
));
2330 ((int64_t *)(bf
))[loc
] = ufs_rw64(val
, UFS_IPNEEDSWAP(ip
));