1 /* $NetBSD: ffs_snapshot.c,v 1.96 2009/10/13 12:38:14 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.96 2009/10/13 12:38:14 hannken Exp $");
43 #if defined(_KERNEL_OPT)
47 #include <sys/param.h>
48 #include <sys/kernel.h>
49 #include <sys/systm.h>
53 #include <sys/namei.h>
54 #include <sys/sched.h>
56 #include <sys/malloc.h>
57 #include <sys/mount.h>
58 #include <sys/resource.h>
59 #include <sys/resourcevar.h>
60 #include <sys/vnode.h>
61 #include <sys/kauth.h>
62 #include <sys/fstrans.h>
63 #include <sys/wapbl.h>
65 #include <miscfs/specfs/specdev.h>
67 #include <ufs/ufs/quota.h>
68 #include <ufs/ufs/ufsmount.h>
69 #include <ufs/ufs/inode.h>
70 #include <ufs/ufs/ufs_extern.h>
71 #include <ufs/ufs/ufs_bswap.h>
72 #include <ufs/ufs/ufs_wapbl.h>
74 #include <ufs/ffs/fs.h>
75 #include <ufs/ffs/ffs_extern.h>
80 kmutex_t si_lock
; /* Lock this snapinfo */
81 kmutex_t si_snaplock
; /* Snapshot vnode common lock */
82 TAILQ_HEAD(inodelst
, inode
) si_snapshots
; /* List of active snapshots */
83 daddr_t
*si_snapblklist
; /* Snapshot block hints list */
84 uint32_t si_gen
; /* Incremented on change */
87 #if !defined(FFS_NO_SNAPSHOT)
88 typedef int (*acctfunc_t
)
89 (struct vnode
*, void *, int, int, struct fs
*, daddr_t
, int);
91 static int snapshot_setup(struct mount
*, struct vnode
*);
92 static int snapshot_copyfs(struct mount
*, struct vnode
*, void **);
93 static int snapshot_expunge(struct mount
*, struct vnode
*,
94 struct fs
*, daddr_t
*, daddr_t
**);
95 static int snapshot_expunge_snap(struct mount
*, struct vnode
*,
96 struct fs
*, daddr_t
);
97 static int snapshot_writefs(struct mount
*, struct vnode
*, void *);
98 static int cgaccount(struct vnode
*, int, int *);
99 static int cgaccount1(int, struct vnode
*, void *, int);
100 static int expunge(struct vnode
*, struct inode
*, struct fs
*,
102 static int indiracct(struct vnode
*, struct vnode
*, int, daddr_t
,
103 daddr_t
, daddr_t
, daddr_t
, daddr_t
, struct fs
*, acctfunc_t
, int);
104 static int fullacct(struct vnode
*, void *, int, int, struct fs
*,
106 static int snapacct(struct vnode
*, void *, int, int, struct fs
*,
108 static int mapacct(struct vnode
*, void *, int, int, struct fs
*,
110 #endif /* !defined(FFS_NO_SNAPSHOT) */
112 static int ffs_copyonwrite(void *, struct buf
*, bool);
113 static int snapblkaddr(struct vnode
*, daddr_t
, daddr_t
*);
114 static int rwfsblk(struct vnode
*, int, void *, daddr_t
);
115 static int syncsnap(struct vnode
*);
116 static int wrsnapblk(struct vnode
*, void *, daddr_t
);
118 static inline bool is_active_snapshot(struct snap_info
*, struct inode
*);
119 static inline daddr_t
db_get(struct inode
*, int);
120 static inline void db_assign(struct inode
*, int, daddr_t
);
121 static inline daddr_t
ib_get(struct inode
*, int);
122 static inline void ib_assign(struct inode
*, int, daddr_t
);
123 static inline daddr_t
idb_get(struct inode
*, void *, int);
124 static inline void idb_assign(struct inode
*, void *, int, daddr_t
);
127 static int snapdebug
= 0;
131 ffs_snapshot_init(struct ufsmount
*ump
)
133 struct snap_info
*si
;
135 si
= ump
->um_snapinfo
= kmem_alloc(sizeof(*si
), KM_SLEEP
);
139 TAILQ_INIT(&si
->si_snapshots
);
140 mutex_init(&si
->si_lock
, MUTEX_DEFAULT
, IPL_NONE
);
141 mutex_init(&si
->si_snaplock
, MUTEX_DEFAULT
, IPL_NONE
);
143 si
->si_snapblklist
= NULL
;
149 ffs_snapshot_fini(struct ufsmount
*ump
)
151 struct snap_info
*si
;
153 si
= ump
->um_snapinfo
;
154 ump
->um_snapinfo
= NULL
;
156 KASSERT(TAILQ_EMPTY(&si
->si_snapshots
));
157 mutex_destroy(&si
->si_lock
);
158 mutex_destroy(&si
->si_snaplock
);
159 KASSERT(si
->si_snapblklist
== NULL
);
160 kmem_free(si
, sizeof(*si
));
164 * Create a snapshot file and initialize it for the filesystem.
165 * Vnode is locked on entry and return.
168 ffs_snapshot(struct mount
*mp
, struct vnode
*vp
, struct timespec
*ctime
)
170 #if defined(FFS_NO_SNAPSHOT)
173 #else /* defined(FFS_NO_SNAPSHOT) */
174 bool suspended
= false;
175 bool snapshot_locked
= false;
176 int error
, redo
= 0, snaploc
;
178 daddr_t
*snaplist
= NULL
, snaplistsize
= 0;
179 struct buf
*bp
, *nbp
;
180 struct fs
*copy_fs
, *fs
= VFSTOUFS(mp
)->um_fs
;
181 struct inode
*ip
= VTOI(vp
);
182 struct lwp
*l
= curlwp
;
183 struct snap_info
*si
= VFSTOUFS(mp
)->um_snapinfo
;
185 struct timeval starttime
;
187 struct timeval endtime
;
189 struct vnode
*devvp
= ip
->i_devvp
;
192 * If the vnode already is a snapshot, return.
194 if (VTOI(vp
)->i_flags
& SF_SNAPSHOT
) {
196 ctime
->tv_sec
= DIP(VTOI(vp
), mtime
);
197 ctime
->tv_nsec
= DIP(VTOI(vp
), mtimensec
);
202 * Check for free snapshot slot in the superblock.
204 for (snaploc
= 0; snaploc
< FSMAXSNAP
; snaploc
++)
205 if (fs
->fs_snapinum
[snaploc
] == 0)
207 if (snaploc
== FSMAXSNAP
)
210 * Prepare the vnode to become a snapshot.
212 error
= snapshot_setup(mp
, vp
);
216 * Change inode to snapshot type file.
218 ip
->i_flags
|= SF_SNAPSHOT
;
219 DIP_ASSIGN(ip
, flags
, ip
->i_flags
);
220 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
222 * Copy all the cylinder group maps. Although the
223 * filesystem is still active, we hope that only a few
224 * cylinder groups will change between now and when we
225 * suspend operations. Thus, we will be able to quickly
226 * touch up the few cylinder groups that changed during
227 * the suspension period.
229 error
= cgaccount(vp
, 1, NULL
);
233 * Ensure that the snapshot is completely on disk.
234 * Since we have marked it as a snapshot it is safe to
235 * unlock it as no process will be allowed to write to it.
237 error
= VOP_FSYNC(vp
, l
->l_cred
, FSYNC_WAIT
, 0, 0);
242 * All allocations are done, so we can now suspend the filesystem.
244 error
= vfs_suspend(vp
->v_mount
, 0);
245 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
249 getmicrotime(&starttime
);
251 * First, copy all the cylinder group maps that have changed.
253 error
= cgaccount(vp
, 2, &redo
);
257 * Create a copy of the superblock and its summary information.
259 error
= snapshot_copyfs(mp
, vp
, &sbbuf
);
260 copy_fs
= (struct fs
*)((char *)sbbuf
+ blkoff(fs
, fs
->fs_sblockloc
));
264 * Expunge unlinked files from our view.
266 error
= snapshot_expunge(mp
, vp
, copy_fs
, &snaplistsize
, &snaplist
);
270 * Acquire the snapshot lock.
272 mutex_enter(&si
->si_snaplock
);
273 snapshot_locked
= true;
275 * Record snapshot inode. Since this is the newest snapshot,
276 * it must be placed at the end of the list.
278 fs
->fs_snapinum
[snaploc
] = ip
->i_number
;
280 mutex_enter(&si
->si_lock
);
281 if (is_active_snapshot(si
, ip
))
282 panic("ffs_snapshot: %"PRIu64
" already on list", ip
->i_number
);
283 TAILQ_INSERT_TAIL(&si
->si_snapshots
, ip
, i_nextsnap
);
284 if (TAILQ_FIRST(&si
->si_snapshots
) == ip
) {
286 * If this is the first snapshot on this filesystem, put the
287 * preliminary list in place and establish the cow handler.
289 si
->si_snapblklist
= snaplist
;
290 fscow_establish(mp
, ffs_copyonwrite
, devvp
);
293 mutex_exit(&si
->si_lock
);
295 vp
->v_vflag
|= VV_SYSTEM
;
297 * Set the mtime to the time the snapshot has been taken.
299 TIMEVAL_TO_TIMESPEC(&starttime
, &ts
);
302 DIP_ASSIGN(ip
, mtime
, ts
.tv_sec
);
303 DIP_ASSIGN(ip
, mtimensec
, ts
.tv_nsec
);
304 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
306 * Copy allocation information from all snapshots and then
307 * expunge them from our view.
309 error
= snapshot_expunge_snap(mp
, vp
, copy_fs
, snaplistsize
);
313 * Write the superblock and its summary information to the snapshot.
315 error
= snapshot_writefs(mp
, vp
, sbbuf
);
319 * We're nearly done, ensure that the snapshot is completely on disk.
321 error
= VOP_FSYNC(vp
, l
->l_cred
, FSYNC_WAIT
, 0, 0);
325 * Invalidate and free all pages on the snapshot vnode.
326 * We will read and write through the buffercache.
328 mutex_enter(&vp
->v_interlock
);
329 error
= VOP_PUTPAGES(vp
, 0, 0,
330 PGO_ALLPAGES
| PGO_CLEANIT
| PGO_SYNCIO
| PGO_FREE
);
334 * Invalidate short ( < fs_bsize ) buffers. We will always read
335 * full size buffers later.
337 mutex_enter(&bufcache_lock
);
338 KASSERT(LIST_FIRST(&vp
->v_dirtyblkhd
) == NULL
);
339 for (bp
= LIST_FIRST(&vp
->v_cleanblkhd
); bp
; bp
= nbp
) {
340 nbp
= LIST_NEXT(bp
, b_vnbufs
);
341 KASSERT((bp
->b_cflags
& BC_BUSY
) == 0);
342 if (bp
->b_bcount
< fs
->fs_bsize
) {
343 bp
->b_cflags
|= BC_BUSY
;
344 brelsel(bp
, BC_INVAL
| BC_VFLUSH
);
347 mutex_exit(&bufcache_lock
);
351 free(copy_fs
->fs_csp
, M_UFSMNT
);
352 free(sbbuf
, M_UFSMNT
);
354 if (fs
->fs_active
!= NULL
) {
355 free(fs
->fs_active
, M_DEVBUF
);
356 fs
->fs_active
= NULL
;
359 mutex_enter(&si
->si_lock
);
360 if (snaplist
!= NULL
) {
361 if (si
->si_snapblklist
== snaplist
)
362 si
->si_snapblklist
= NULL
;
363 free(snaplist
, M_UFSMNT
);
366 fs
->fs_snapinum
[snaploc
] = 0;
369 * As this is the newest list, it is the most inclusive, so
370 * should replace the previous list.
372 si
->si_snapblklist
= ip
->i_snapblklist
;
375 mutex_exit(&si
->si_lock
);
378 mutex_exit(&si
->si_snaplock
);
380 vfs_resume(vp
->v_mount
);
382 getmicrotime(&endtime
);
383 timersub(&endtime
, &starttime
, &endtime
);
384 printf("%s: suspended %lld.%03d sec, redo %d of %d\n",
385 mp
->mnt_stat
.f_mntonname
, (long long)endtime
.tv_sec
,
386 endtime
.tv_usec
/ 1000, redo
, fs
->fs_ncg
);
390 if (!UFS_WAPBL_BEGIN(mp
)) {
391 (void) ffs_truncate(vp
, (off_t
)0, 0, NOCRED
);
400 * Prepare vnode to become a snapshot.
403 snapshot_setup(struct mount
*mp
, struct vnode
*vp
)
405 int error
, i
, len
, loc
;
406 daddr_t blkno
, numblks
;
407 struct buf
*ibp
, *nbp
;
408 struct fs
*fs
= VFSTOUFS(mp
)->um_fs
;
409 struct lwp
*l
= curlwp
;
412 * Check mount, exclusive reference and owner.
414 if (vp
->v_mount
!= mp
)
416 if (vp
->v_usecount
!= 1 || vp
->v_writecount
!= 0)
418 if (kauth_authorize_generic(l
->l_cred
, KAUTH_GENERIC_ISSUSER
,
420 VTOI(vp
)->i_uid
!= kauth_cred_geteuid(l
->l_cred
))
423 if (vp
->v_size
!= 0) {
424 error
= ffs_truncate(vp
, 0, 0, NOCRED
);
429 * Write an empty list of preallocated blocks to the end of
430 * the snapshot to set size to at least that of the filesystem.
432 numblks
= howmany(fs
->fs_size
, fs
->fs_frag
);
434 blkno
= ufs_rw64(blkno
, UFS_FSNEEDSWAP(fs
));
435 error
= vn_rdwr(UIO_WRITE
, vp
,
436 (void *)&blkno
, sizeof(blkno
), lblktosize(fs
, (off_t
)numblks
),
437 UIO_SYSSPACE
, IO_NODELOCKED
|IO_UNIT
, l
->l_cred
, NULL
, NULL
);
441 * Preallocate critical data structures so that we can copy
442 * them in without further allocation after we suspend all
443 * operations on the filesystem. We would like to just release
444 * the allocated buffers without writing them since they will
445 * be filled in below once we are ready to go, but this upsets
446 * the soft update code, so we go ahead and write the new buffers.
448 * Allocate all indirect blocks and mark all of them as not
449 * needing to be copied.
451 error
= UFS_WAPBL_BEGIN(mp
);
454 for (blkno
= NDADDR
, i
= 0; blkno
< numblks
; blkno
+= NINDIR(fs
)) {
455 error
= ffs_balloc(vp
, lblktosize(fs
, (off_t
)blkno
),
456 fs
->fs_bsize
, l
->l_cred
, B_METAONLY
, &ibp
);
460 if ((++i
% 16) == 0) {
462 error
= UFS_WAPBL_BEGIN(mp
);
468 * Allocate copies for the superblock and its summary information.
470 error
= ffs_balloc(vp
, fs
->fs_sblockloc
, fs
->fs_sbsize
, l
->l_cred
,
475 blkno
= fragstoblks(fs
, fs
->fs_csaddr
);
476 len
= howmany(fs
->fs_cssize
, fs
->fs_bsize
);
477 for (loc
= 0; loc
< len
; loc
++) {
478 error
= ffs_balloc(vp
, lblktosize(fs
, (off_t
)(blkno
+ loc
)),
479 fs
->fs_bsize
, l
->l_cred
, 0, &nbp
);
491 * Create a copy of the superblock and its summary information.
492 * It is up to the caller to free copyfs and copy_fs->fs_csp.
495 snapshot_copyfs(struct mount
*mp
, struct vnode
*vp
, void **sbbuf
)
497 int error
, i
, len
, loc
, size
;
501 struct fs
*copyfs
, *fs
= VFSTOUFS(mp
)->um_fs
;
502 struct lwp
*l
= curlwp
;
503 struct vnode
*devvp
= VTOI(vp
)->i_devvp
;
506 * Grab a copy of the superblock and its summary information.
507 * We delay writing it until the suspension is released below.
509 *sbbuf
= malloc(fs
->fs_bsize
, M_UFSMNT
, M_WAITOK
);
510 loc
= blkoff(fs
, fs
->fs_sblockloc
);
512 memset(*sbbuf
, 0, loc
);
513 copyfs
= (struct fs
*)((char *)(*sbbuf
) + loc
);
514 memcpy(copyfs
, fs
, fs
->fs_sbsize
);
515 size
= fs
->fs_bsize
< SBLOCKSIZE
? fs
->fs_bsize
: SBLOCKSIZE
;
516 if (fs
->fs_sbsize
< size
)
517 memset((char *)(*sbbuf
) + loc
+ fs
->fs_sbsize
, 0,
518 size
- fs
->fs_sbsize
);
519 size
= blkroundup(fs
, fs
->fs_cssize
);
520 if (fs
->fs_contigsumsize
> 0)
521 size
+= fs
->fs_ncg
* sizeof(int32_t);
522 space
= malloc(size
, M_UFSMNT
, M_WAITOK
);
523 copyfs
->fs_csp
= space
;
524 memcpy(copyfs
->fs_csp
, fs
->fs_csp
, fs
->fs_cssize
);
525 space
= (char *)space
+ fs
->fs_cssize
;
526 loc
= howmany(fs
->fs_cssize
, fs
->fs_fsize
);
527 i
= fs
->fs_frag
- loc
% fs
->fs_frag
;
528 len
= (i
== fs
->fs_frag
) ? 0 : i
* fs
->fs_fsize
;
530 if ((error
= bread(devvp
, fsbtodb(fs
, fs
->fs_csaddr
+ loc
),
531 len
, l
->l_cred
, 0, &bp
)) != 0) {
533 free(copyfs
->fs_csp
, M_UFSMNT
);
534 free(*sbbuf
, M_UFSMNT
);
538 memcpy(space
, bp
->b_data
, (u_int
)len
);
539 space
= (char *)space
+ len
;
540 brelse(bp
, BC_INVAL
| BC_NOCACHE
);
542 if (fs
->fs_contigsumsize
> 0) {
543 copyfs
->fs_maxcluster
= lp
= space
;
544 for (i
= 0; i
< fs
->fs_ncg
; i
++)
545 *lp
++ = fs
->fs_contigsumsize
;
548 copyfs
->fs_flags
&= ~FS_DOWAPBL
;
553 * We must check for active files that have been unlinked (e.g., with a zero
554 * link count). We have to expunge all trace of these files from the snapshot
555 * so that they are not reclaimed prematurely by fsck or unnecessarily dumped.
556 * Note that we skip unlinked snapshot files as they will be handled separately.
557 * Calculate the snapshot list size and create a preliminary list.
560 snapshot_expunge(struct mount
*mp
, struct vnode
*vp
, struct fs
*copy_fs
,
561 daddr_t
*snaplistsize
, daddr_t
**snaplist
)
563 bool has_wapbl
= false;
564 int cg
, error
, len
, loc
;
565 daddr_t blkno
, *blkp
;
566 struct fs
*fs
= VFSTOUFS(mp
)->um_fs
;
568 struct lwp
*l
= curlwp
;
570 struct vnode
*logvp
= NULL
, *mvp
= NULL
, *xvp
;
574 * Get the log inode if any.
576 if ((fs
->fs_flags
& FS_DOWAPBL
) &&
577 fs
->fs_journal_location
== UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM
) {
579 fs
->fs_journallocs
[UFS_WAPBL_INFS_INO
], &logvp
);
584 * Allocate a marker vnode.
586 if ((mvp
= vnalloc(mp
)) == NULL
) {
591 * We also calculate the needed size for the snapshot list.
593 *snaplistsize
= fs
->fs_ncg
+ howmany(fs
->fs_cssize
, fs
->fs_bsize
) +
594 FSMAXSNAP
+ 1 /* superblock */ + 1 /* last block */ + 1 /* size */;
595 error
= UFS_WAPBL_BEGIN(mp
);
599 mutex_enter(&mntvnode_lock
);
601 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
602 * and vclean() can be called indirectly
604 for (xvp
= TAILQ_FIRST(&mp
->mnt_vnodelist
); xvp
; xvp
= vunmark(mvp
)) {
607 * Make sure this vnode wasn't reclaimed in getnewvnode().
608 * Start over if it has (it won't be on the list anymore).
610 if (xvp
->v_mount
!= mp
|| vismarker(xvp
))
612 mutex_enter(&xvp
->v_interlock
);
613 if ((xvp
->v_iflag
& VI_XLOCK
) ||
614 xvp
->v_usecount
== 0 || xvp
->v_type
== VNON
||
616 (VTOI(xvp
)->i_flags
& SF_SNAPSHOT
)) {
617 mutex_exit(&xvp
->v_interlock
);
620 mutex_exit(&mntvnode_lock
);
622 * XXXAD should increase vnode ref count to prevent it
623 * disappearing or being recycled.
625 mutex_exit(&xvp
->v_interlock
);
628 vprint("ffs_snapshot: busy vnode", xvp
);
632 if (VOP_GETATTR(xvp
, &vat
, l
->l_cred
) == 0 &&
634 mutex_enter(&mntvnode_lock
);
637 if (ffs_checkfreefile(copy_fs
, vp
, xp
->i_number
)) {
638 mutex_enter(&mntvnode_lock
);
643 * If there is a fragment, clear it here.
646 loc
= howmany(xp
->i_size
, fs
->fs_bsize
) - 1;
648 len
= fragroundup(fs
, blkoff(fs
, xp
->i_size
));
649 if (len
> 0 && len
< fs
->fs_bsize
) {
650 ffs_blkfree_snap(copy_fs
, vp
, db_get(xp
, loc
),
652 blkno
= db_get(xp
, loc
);
653 db_assign(xp
, loc
, 0);
657 error
= expunge(vp
, xp
, copy_fs
, fullacct
, BLK_NOCOPY
);
659 db_assign(xp
, loc
, blkno
);
661 error
= ffs_freefile_snap(copy_fs
, vp
, xp
->i_number
,
667 mutex_enter(&mntvnode_lock
);
669 mutex_exit(&mntvnode_lock
);
671 * Create a preliminary list of preallocated snapshot blocks.
673 *snaplist
= malloc(*snaplistsize
* sizeof(daddr_t
), M_UFSMNT
, M_WAITOK
);
674 blkp
= &(*snaplist
)[1];
675 *blkp
++ = lblkno(fs
, fs
->fs_sblockloc
);
676 blkno
= fragstoblks(fs
, fs
->fs_csaddr
);
677 for (cg
= 0; cg
< fs
->fs_ncg
; cg
++) {
678 if (fragstoblks(fs
, cgtod(fs
, cg
)) > blkno
)
680 *blkp
++ = fragstoblks(fs
, cgtod(fs
, cg
));
682 len
= howmany(fs
->fs_cssize
, fs
->fs_bsize
);
683 for (loc
= 0; loc
< len
; loc
++)
684 *blkp
++ = blkno
+ loc
;
685 for (; cg
< fs
->fs_ncg
; cg
++)
686 *blkp
++ = fragstoblks(fs
, cgtod(fs
, cg
));
695 if (error
&& *snaplist
!= NULL
) {
696 free(*snaplist
, M_UFSMNT
);
704 * Copy allocation information from all the snapshots in this snapshot and
705 * then expunge them from its view. Also, collect the list of allocated
706 * blocks in i_snapblklist.
709 snapshot_expunge_snap(struct mount
*mp
, struct vnode
*vp
,
710 struct fs
*copy_fs
, daddr_t snaplistsize
)
713 daddr_t numblks
, *snaplist
= NULL
;
714 struct fs
*fs
= VFSTOUFS(mp
)->um_fs
;
715 struct inode
*ip
= VTOI(vp
), *xp
;
716 struct lwp
*l
= curlwp
;
717 struct snap_info
*si
= VFSTOUFS(mp
)->um_snapinfo
;
719 error
= UFS_WAPBL_BEGIN(mp
);
722 TAILQ_FOREACH(xp
, &si
->si_snapshots
, i_nextsnap
) {
725 error
= expunge(vp
, xp
, fs
, snapacct
, BLK_SNAP
);
728 if (xp
->i_nlink
!= 0)
730 error
= ffs_freefile_snap(copy_fs
, vp
, xp
->i_number
, xp
->i_mode
);
737 * Allocate space for the full list of preallocated snapshot blocks.
739 snaplist
= malloc(snaplistsize
* sizeof(daddr_t
), M_UFSMNT
, M_WAITOK
);
740 ip
->i_snapblklist
= &snaplist
[1];
742 * Expunge the blocks used by the snapshots from the set of
743 * blocks marked as used in the snapshot bitmaps. Also, collect
744 * the list of allocated blocks in i_snapblklist.
746 error
= expunge(vp
, ip
, copy_fs
, mapacct
, BLK_SNAP
);
749 if (snaplistsize
< ip
->i_snapblklist
- snaplist
)
750 panic("ffs_snapshot: list too small");
751 snaplistsize
= ip
->i_snapblklist
- snaplist
;
752 snaplist
[0] = snaplistsize
;
753 ip
->i_snapblklist
= &snaplist
[0];
755 * Write out the list of allocated blocks to the end of the snapshot.
757 numblks
= howmany(fs
->fs_size
, fs
->fs_frag
);
758 for (i
= 0; i
< snaplistsize
; i
++)
759 snaplist
[i
] = ufs_rw64(snaplist
[i
], UFS_FSNEEDSWAP(fs
));
760 error
= vn_rdwr(UIO_WRITE
, vp
, (void *)snaplist
,
761 snaplistsize
* sizeof(daddr_t
), lblktosize(fs
, (off_t
)numblks
),
762 UIO_SYSSPACE
, IO_NODELOCKED
| IO_JOURNALLOCKED
| IO_UNIT
,
763 l
->l_cred
, NULL
, NULL
);
764 for (i
= 0; i
< snaplistsize
; i
++)
765 snaplist
[i
] = ufs_rw64(snaplist
[i
], UFS_FSNEEDSWAP(fs
));
768 if (error
&& snaplist
!= NULL
) {
769 free(snaplist
, M_UFSMNT
);
770 ip
->i_snapblklist
= NULL
;
776 * Write the superblock and its summary information to the snapshot.
777 * Make sure, the first NDADDR blocks get copied to the snapshot.
780 snapshot_writefs(struct mount
*mp
, struct vnode
*vp
, void *sbbuf
)
786 struct fs
*copyfs
, *fs
= VFSTOUFS(mp
)->um_fs
;
787 struct inode
*ip
= VTOI(vp
);
788 struct lwp
*l
= curlwp
;
790 copyfs
= (struct fs
*)((char *)sbbuf
+ blkoff(fs
, fs
->fs_sblockloc
));
793 * Write the superblock and its summary information
796 blkno
= fragstoblks(fs
, fs
->fs_csaddr
);
797 len
= howmany(fs
->fs_cssize
, fs
->fs_bsize
);
798 space
= copyfs
->fs_csp
;
800 if (UFS_FSNEEDSWAP(fs
)) {
801 ffs_sb_swap(copyfs
, copyfs
);
802 ffs_csum_swap(space
, space
, fs
->fs_cssize
);
805 error
= UFS_WAPBL_BEGIN(mp
);
808 for (loc
= 0; loc
< len
; loc
++) {
809 error
= bread(vp
, blkno
+ loc
, fs
->fs_bsize
, l
->l_cred
,
815 memcpy(bp
->b_data
, space
, fs
->fs_bsize
);
816 space
= (char *)space
+ fs
->fs_bsize
;
821 error
= bread(vp
, lblkno(fs
, fs
->fs_sblockloc
),
822 fs
->fs_bsize
, l
->l_cred
, B_MODIFY
, &bp
);
827 memcpy(bp
->b_data
, sbbuf
, fs
->fs_bsize
);
831 * Copy the first NDADDR blocks to the snapshot so ffs_copyonwrite()
832 * and ffs_snapblkfree() will always work on indirect blocks.
834 for (loc
= 0; loc
< NDADDR
; loc
++) {
835 if (db_get(ip
, loc
) != 0)
837 error
= ffs_balloc(vp
, lblktosize(fs
, (off_t
)loc
),
838 fs
->fs_bsize
, l
->l_cred
, 0, &bp
);
841 error
= rwfsblk(vp
, B_READ
, bp
->b_data
, loc
);
855 * Copy all cylinder group maps.
858 cgaccount(struct vnode
*vp
, int passno
, int *redo
)
862 struct fs
*fs
= VTOI(vp
)->i_fs
;
864 error
= UFS_WAPBL_BEGIN(vp
->v_mount
);
870 fs
->fs_active
= malloc(howmany(fs
->fs_ncg
, NBBY
),
871 M_DEVBUF
, M_WAITOK
| M_ZERO
);
872 for (cg
= 0; cg
< fs
->fs_ncg
; cg
++) {
873 if (passno
== 2 && ACTIVECG_ISSET(fs
, cg
))
877 error
= ffs_balloc(vp
, lfragtosize(fs
, cgtod(fs
, cg
)),
878 fs
->fs_bsize
, curlwp
->l_cred
, 0, &nbp
);
881 error
= cgaccount1(cg
, vp
, nbp
->b_data
, passno
);
886 UFS_WAPBL_END(vp
->v_mount
);
891 * Copy a cylinder group map. All the unallocated blocks are marked
892 * BLK_NOCOPY so that the snapshot knows that it need not copy them
893 * if they are later written. If passno is one, then this is a first
894 * pass, so only setting needs to be done. If passno is 2, then this
895 * is a revision to a previous pass which must be undone as the
896 * replacement pass is done.
899 cgaccount1(int cg
, struct vnode
*vp
, void *data
, int passno
)
901 struct buf
*bp
, *ibp
;
905 struct lwp
*l
= curlwp
;
906 daddr_t base
, numblks
;
907 int error
, len
, loc
, ns
, indiroff
;
911 ns
= UFS_FSNEEDSWAP(fs
);
912 error
= bread(ip
->i_devvp
, fsbtodb(fs
, cgtod(fs
, cg
)),
913 (int)fs
->fs_cgsize
, l
->l_cred
, 0, &bp
);
918 cgp
= (struct cg
*)bp
->b_data
;
919 if (!cg_chkmagic(cgp
, ns
)) {
923 ACTIVECG_SET(fs
, cg
);
925 memcpy(data
, bp
->b_data
, fs
->fs_cgsize
);
927 if (fs
->fs_cgsize
< fs
->fs_bsize
)
928 memset((char *)data
+ fs
->fs_cgsize
, 0,
929 fs
->fs_bsize
- fs
->fs_cgsize
);
930 numblks
= howmany(fs
->fs_size
, fs
->fs_frag
);
931 len
= howmany(fs
->fs_fpg
, fs
->fs_frag
);
932 base
= cg
* fs
->fs_fpg
/ fs
->fs_frag
;
933 if (base
+ len
>= numblks
)
934 len
= numblks
- base
- 1;
937 for ( ; loc
< NDADDR
; loc
++) {
938 if (ffs_isblock(fs
, cg_blksfree(cgp
, ns
), loc
))
939 db_assign(ip
, loc
, BLK_NOCOPY
);
940 else if (db_get(ip
, loc
) == BLK_NOCOPY
) {
942 db_assign(ip
, loc
, 0);
943 else if (passno
== 1)
944 panic("ffs_snapshot: lost direct block");
948 if ((error
= ffs_balloc(vp
, lblktosize(fs
, (off_t
)(base
+ loc
)),
949 fs
->fs_bsize
, l
->l_cred
, B_METAONLY
, &ibp
)) != 0)
951 indiroff
= (base
+ loc
- NDADDR
) % NINDIR(fs
);
952 for ( ; loc
< len
; loc
++, indiroff
++) {
953 if (indiroff
>= NINDIR(fs
)) {
955 if ((error
= ffs_balloc(vp
,
956 lblktosize(fs
, (off_t
)(base
+ loc
)),
957 fs
->fs_bsize
, l
->l_cred
, B_METAONLY
, &ibp
)) != 0)
961 if (ffs_isblock(fs
, cg_blksfree(cgp
, ns
), loc
))
962 idb_assign(ip
, ibp
->b_data
, indiroff
, BLK_NOCOPY
);
963 else if (idb_get(ip
, ibp
->b_data
, indiroff
) == BLK_NOCOPY
) {
965 idb_assign(ip
, ibp
->b_data
, indiroff
, 0);
966 else if (passno
== 1)
967 panic("ffs_snapshot: lost indirect block");
975 * Before expunging a snapshot inode, note all the
976 * blocks that it claims with BLK_SNAP so that fsck will
977 * be able to account for those blocks properly and so
978 * that this snapshot knows that it need not copy them
979 * if the other snapshot holding them is freed.
982 expunge(struct vnode
*snapvp
, struct inode
*cancelip
, struct fs
*fs
,
983 acctfunc_t acctfunc
, int expungetype
)
987 daddr_t len
, blkno
, numblks
, blksperindir
;
988 struct ufs1_dinode
*dip1
;
989 struct ufs2_dinode
*dip2
;
990 struct lwp
*l
= curlwp
;
994 ns
= UFS_FSNEEDSWAP(fs
);
996 * Prepare to expunge the inode. If its inode block has not
997 * yet been copied, then allocate and fill the copy.
999 lbn
= fragstoblks(fs
, ino_to_fsba(fs
, cancelip
->i_number
));
1000 error
= snapblkaddr(snapvp
, lbn
, &blkno
);
1004 error
= bread(snapvp
, lbn
, fs
->fs_bsize
, l
->l_cred
,
1007 error
= ffs_balloc(snapvp
, lblktosize(fs
, (off_t
)lbn
),
1008 fs
->fs_bsize
, l
->l_cred
, 0, &bp
);
1010 error
= rwfsblk(snapvp
, B_READ
, bp
->b_data
, lbn
);
1015 * Set a snapshot inode to be a zero length file, regular files
1016 * or unlinked snapshots to be completely unallocated.
1018 if (fs
->fs_magic
== FS_UFS1_MAGIC
) {
1019 dip1
= (struct ufs1_dinode
*)bp
->b_data
+
1020 ino_to_fsbo(fs
, cancelip
->i_number
);
1021 if (expungetype
== BLK_NOCOPY
|| cancelip
->i_nlink
== 0)
1024 dip1
->di_blocks
= 0;
1026 ufs_rw32(ufs_rw32(dip1
->di_flags
, ns
) & ~SF_SNAPSHOT
, ns
);
1027 memset(&dip1
->di_db
[0], 0, (NDADDR
+ NIADDR
) * sizeof(int32_t));
1029 dip2
= (struct ufs2_dinode
*)bp
->b_data
+
1030 ino_to_fsbo(fs
, cancelip
->i_number
);
1031 if (expungetype
== BLK_NOCOPY
|| cancelip
->i_nlink
== 0)
1034 dip2
->di_blocks
= 0;
1036 ufs_rw32(ufs_rw32(dip2
->di_flags
, ns
) & ~SF_SNAPSHOT
, ns
);
1037 memset(&dip2
->di_db
[0], 0, (NDADDR
+ NIADDR
) * sizeof(int64_t));
1041 * Now go through and expunge all the blocks in the file
1042 * using the function requested.
1044 numblks
= howmany(cancelip
->i_size
, fs
->fs_bsize
);
1045 if (fs
->fs_magic
== FS_UFS1_MAGIC
)
1046 bap
= &cancelip
->i_ffs1_db
[0];
1048 bap
= &cancelip
->i_ffs2_db
[0];
1049 if ((error
= (*acctfunc
)(snapvp
, bap
, 0, NDADDR
, fs
, 0, expungetype
)))
1051 if (fs
->fs_magic
== FS_UFS1_MAGIC
)
1052 bap
= &cancelip
->i_ffs1_ib
[0];
1054 bap
= &cancelip
->i_ffs2_ib
[0];
1055 if ((error
= (*acctfunc
)(snapvp
, bap
, 0, NIADDR
, fs
, -1, expungetype
)))
1059 len
= numblks
- NDADDR
;
1061 for (i
= 0; len
> 0 && i
< NIADDR
; i
++) {
1062 error
= indiracct(snapvp
, ITOV(cancelip
), i
,
1063 ib_get(cancelip
, i
), lbn
, rlbn
, len
,
1064 blksperindir
, fs
, acctfunc
, expungetype
);
1067 blksperindir
*= NINDIR(fs
);
1068 lbn
-= blksperindir
+ 1;
1069 len
-= blksperindir
;
1070 rlbn
+= blksperindir
;
1076 * Descend an indirect block chain for vnode cancelvp accounting for all
1077 * its indirect blocks in snapvp.
1080 indiracct(struct vnode
*snapvp
, struct vnode
*cancelvp
, int level
,
1081 daddr_t blkno
, daddr_t lbn
, daddr_t rlbn
, daddr_t remblks
,
1082 daddr_t blksperindir
, struct fs
*fs
, acctfunc_t acctfunc
, int expungetype
)
1085 daddr_t subblksperindir
;
1086 struct indir indirs
[NIADDR
+ 2];
1092 if (expungetype
== BLK_NOCOPY
)
1094 panic("indiracct: missing indir");
1096 if ((error
= ufs_getlbns(cancelvp
, rlbn
, indirs
, &num
)) != 0)
1098 if (lbn
!= indirs
[num
- 1 - level
].in_lbn
|| num
< 2)
1099 panic("indiracct: botched params");
1101 * We have to expand bread here since it will deadlock looking
1102 * up the block number for any blocks that are not in the cache.
1104 error
= ffs_getblk(cancelvp
, lbn
, fsbtodb(fs
, blkno
), fs
->fs_bsize
,
1108 if ((bp
->b_oflags
& (BO_DONE
| BO_DELWRI
)) == 0 && (error
=
1109 rwfsblk(bp
->b_vp
, B_READ
, bp
->b_data
, fragstoblks(fs
, blkno
)))) {
1114 * Account for the block pointers in this indirect block.
1116 last
= howmany(remblks
, blksperindir
);
1117 if (last
> NINDIR(fs
))
1119 bap
= malloc(fs
->fs_bsize
, M_DEVBUF
, M_WAITOK
| M_ZERO
);
1120 memcpy((void *)bap
, bp
->b_data
, fs
->fs_bsize
);
1122 error
= (*acctfunc
)(snapvp
, bap
, 0, last
,
1123 fs
, level
== 0 ? rlbn
: -1, expungetype
);
1124 if (error
|| level
== 0)
1127 * Account for the block pointers in each of the indirect blocks
1128 * in the levels below us.
1130 subblksperindir
= blksperindir
/ NINDIR(fs
);
1131 for (lbn
++, level
--, i
= 0; i
< last
; i
++) {
1132 error
= indiracct(snapvp
, cancelvp
, level
,
1133 idb_get(VTOI(snapvp
), bap
, i
), lbn
, rlbn
, remblks
,
1134 subblksperindir
, fs
, acctfunc
, expungetype
);
1137 rlbn
+= blksperindir
;
1138 lbn
-= blksperindir
;
1139 remblks
-= blksperindir
;
1142 free(bap
, M_DEVBUF
);
1147 * Do both snap accounting and map accounting.
1150 fullacct(struct vnode
*vp
, void *bap
, int oldblkp
, int lastblkp
,
1151 struct fs
*fs
, daddr_t lblkno
,
1152 int exptype
/* BLK_SNAP or BLK_NOCOPY */)
1156 if ((error
= snapacct(vp
, bap
, oldblkp
, lastblkp
, fs
, lblkno
, exptype
)))
1158 return (mapacct(vp
, bap
, oldblkp
, lastblkp
, fs
, lblkno
, exptype
));
1162 * Identify a set of blocks allocated in a snapshot inode.
1165 snapacct(struct vnode
*vp
, void *bap
, int oldblkp
, int lastblkp
,
1166 struct fs
*fs
, daddr_t lblkno
,
1167 int expungetype
/* BLK_SNAP or BLK_NOCOPY */)
1169 struct inode
*ip
= VTOI(vp
);
1170 struct lwp
*l
= curlwp
;
1176 for ( ; oldblkp
< lastblkp
; oldblkp
++) {
1177 blkno
= idb_get(ip
, bap
, oldblkp
);
1178 if (blkno
== 0 || blkno
== BLK_NOCOPY
|| blkno
== BLK_SNAP
)
1180 lbn
= fragstoblks(fs
, blkno
);
1182 blkno
= db_get(ip
, lbn
);
1183 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
1185 error
= ffs_balloc(vp
, lblktosize(fs
, (off_t
)lbn
),
1186 fs
->fs_bsize
, l
->l_cred
, B_METAONLY
, &ibp
);
1189 blkno
= idb_get(ip
, ibp
->b_data
,
1190 (lbn
- NDADDR
) % NINDIR(fs
));
1193 * If we are expunging a snapshot vnode and we
1194 * find a block marked BLK_NOCOPY, then it is
1195 * one that has been allocated to this snapshot after
1196 * we took our current snapshot and can be ignored.
1198 if (expungetype
== BLK_SNAP
&& blkno
== BLK_NOCOPY
) {
1203 panic("snapacct: bad block");
1205 db_assign(ip
, lbn
, expungetype
);
1207 idb_assign(ip
, ibp
->b_data
,
1208 (lbn
- NDADDR
) % NINDIR(fs
), expungetype
);
1217 * Account for a set of blocks allocated in a snapshot inode.
1220 mapacct(struct vnode
*vp
, void *bap
, int oldblkp
, int lastblkp
,
1221 struct fs
*fs
, daddr_t lblkno
, int expungetype
)
1229 inum
= ip
->i_number
;
1234 for ( ; oldblkp
< lastblkp
; oldblkp
++, lblkno
++) {
1235 blkno
= idb_get(ip
, bap
, oldblkp
);
1236 if (blkno
== 0 || blkno
== BLK_NOCOPY
)
1238 if (acctit
&& expungetype
== BLK_SNAP
&& blkno
!= BLK_SNAP
)
1239 *ip
->i_snapblklist
++ = lblkno
;
1240 if (blkno
== BLK_SNAP
)
1241 blkno
= blkstofrags(fs
, lblkno
);
1242 ffs_blkfree_snap(fs
, vp
, blkno
, fs
->fs_bsize
, inum
);
1246 #endif /* defined(FFS_NO_SNAPSHOT) */
1249 * Decrement extra reference on snapshot when last name is removed.
1250 * It will not be freed until the last open reference goes away.
1253 ffs_snapgone(struct inode
*ip
)
1255 struct mount
*mp
= ip
->i_devvp
->v_specmountpoint
;
1258 struct snap_info
*si
;
1261 si
= VFSTOUFS(mp
)->um_snapinfo
;
1264 * Find snapshot in incore list.
1266 mutex_enter(&si
->si_lock
);
1267 TAILQ_FOREACH(xp
, &si
->si_snapshots
, i_nextsnap
)
1270 mutex_exit(&si
->si_lock
);
1275 printf("ffs_snapgone: lost snapshot vnode %llu\n",
1276 (unsigned long long)ip
->i_number
);
1279 * Delete snapshot inode from superblock. Keep list dense.
1281 mutex_enter(&si
->si_lock
);
1283 for (snaploc
= 0; snaploc
< FSMAXSNAP
; snaploc
++)
1284 if (fs
->fs_snapinum
[snaploc
] == ip
->i_number
)
1286 if (snaploc
< FSMAXSNAP
) {
1287 for (snaploc
++; snaploc
< FSMAXSNAP
; snaploc
++) {
1288 if (fs
->fs_snapinum
[snaploc
] == 0)
1290 fs
->fs_snapinum
[snaploc
- 1] = fs
->fs_snapinum
[snaploc
];
1292 fs
->fs_snapinum
[snaploc
- 1] = 0;
1295 mutex_exit(&si
->si_lock
);
1299 * Prepare a snapshot file for being removed.
1302 ffs_snapremove(struct vnode
*vp
)
1304 struct inode
*ip
= VTOI(vp
), *xp
;
1305 struct vnode
*devvp
= ip
->i_devvp
;
1306 struct fs
*fs
= ip
->i_fs
;
1307 struct mount
*mp
= devvp
->v_specmountpoint
;
1309 struct snap_info
*si
;
1310 struct lwp
*l
= curlwp
;
1311 daddr_t numblks
, blkno
, dblk
;
1312 int error
, loc
, last
;
1314 si
= VFSTOUFS(mp
)->um_snapinfo
;
1316 * If active, delete from incore list (this snapshot may
1317 * already have been in the process of being deleted, so
1318 * would not have been active).
1320 * Clear copy-on-write flag if last snapshot.
1322 mutex_enter(&si
->si_lock
);
1323 if (is_active_snapshot(si
, ip
)) {
1324 TAILQ_REMOVE(&si
->si_snapshots
, ip
, i_nextsnap
);
1325 if (TAILQ_FIRST(&si
->si_snapshots
) != 0) {
1326 /* Roll back the list of preallocated blocks. */
1327 xp
= TAILQ_LAST(&si
->si_snapshots
, inodelst
);
1328 si
->si_snapblklist
= xp
->i_snapblklist
;
1330 mutex_exit(&si
->si_lock
);
1332 si
->si_snapblklist
= 0;
1334 mutex_exit(&si
->si_lock
);
1335 fscow_disestablish(mp
, ffs_copyonwrite
, devvp
);
1337 if (ip
->i_snapblklist
!= NULL
) {
1338 free(ip
->i_snapblklist
, M_UFSMNT
);
1339 ip
->i_snapblklist
= NULL
;
1342 mutex_exit(&si
->si_lock
);
1344 * Clear all BLK_NOCOPY fields. Pass any block claims to other
1345 * snapshots that want them (see ffs_snapblkfree below).
1347 for (blkno
= 1; blkno
< NDADDR
; blkno
++) {
1348 dblk
= db_get(ip
, blkno
);
1349 if (dblk
== BLK_NOCOPY
|| dblk
== BLK_SNAP
)
1350 db_assign(ip
, blkno
, 0);
1351 else if ((dblk
== blkstofrags(fs
, blkno
) &&
1352 ffs_snapblkfree(fs
, ip
->i_devvp
, dblk
, fs
->fs_bsize
,
1354 DIP_ADD(ip
, blocks
, -btodb(fs
->fs_bsize
));
1355 db_assign(ip
, blkno
, 0);
1358 numblks
= howmany(ip
->i_size
, fs
->fs_bsize
);
1359 for (blkno
= NDADDR
; blkno
< numblks
; blkno
+= NINDIR(fs
)) {
1360 error
= ffs_balloc(vp
, lblktosize(fs
, (off_t
)blkno
),
1361 fs
->fs_bsize
, l
->l_cred
, B_METAONLY
, &ibp
);
1364 if (fs
->fs_size
- blkno
> NINDIR(fs
))
1367 last
= fs
->fs_size
- blkno
;
1368 for (loc
= 0; loc
< last
; loc
++) {
1369 dblk
= idb_get(ip
, ibp
->b_data
, loc
);
1370 if (dblk
== BLK_NOCOPY
|| dblk
== BLK_SNAP
)
1371 idb_assign(ip
, ibp
->b_data
, loc
, 0);
1372 else if (dblk
== blkstofrags(fs
, blkno
) &&
1373 ffs_snapblkfree(fs
, ip
->i_devvp
, dblk
,
1374 fs
->fs_bsize
, ip
->i_number
)) {
1375 DIP_ADD(ip
, blocks
, -btodb(fs
->fs_bsize
));
1376 idb_assign(ip
, ibp
->b_data
, loc
, 0);
1382 * Clear snapshot flag and drop reference.
1384 ip
->i_flags
&= ~SF_SNAPSHOT
;
1385 DIP_ASSIGN(ip
, flags
, ip
->i_flags
);
1386 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
1390 * Notification that a block is being freed. Return zero if the free
1391 * should be allowed to proceed. Return non-zero if the snapshot file
1392 * wants to claim the block. The block will be claimed if it is an
1393 * uncopied part of one of the snapshots. It will be freed if it is
1394 * either a BLK_NOCOPY or has already been copied in all of the snapshots.
1395 * If a fragment is being freed, then all snapshots that care about
1396 * it must make a copy since a snapshot file can only claim full sized
1397 * blocks. Note that if more than one snapshot file maps the block,
1398 * we can pick one at random to claim it. Since none of the snapshots
1399 * can change, we are assurred that they will all see the same unmodified
1400 * image. When deleting a snapshot file (see ffs_snapremove above), we
1401 * must push any of these claimed blocks to one of the other snapshots
1402 * that maps it. These claimed blocks are easily identified as they will
1403 * have a block number equal to their logical block number within the
1404 * snapshot. A copied block can never have this property because they
1405 * must always have been allocated from a BLK_NOCOPY location.
1408 ffs_snapblkfree(struct fs
*fs
, struct vnode
*devvp
, daddr_t bno
,
1409 long size
, ino_t inum
)
1411 struct mount
*mp
= devvp
->v_specmountpoint
;
1414 struct vnode
*vp
= NULL
;
1415 struct snap_info
*si
;
1416 void *saved_data
= NULL
;
1420 int indiroff
= 0, snapshot_locked
= 0, error
= 0, claimedblk
= 0;
1422 si
= VFSTOUFS(mp
)->um_snapinfo
;
1423 lbn
= fragstoblks(fs
, bno
);
1424 mutex_enter(&si
->si_lock
);
1427 TAILQ_FOREACH(ip
, &si
->si_snapshots
, i_nextsnap
) {
1429 if (snapshot_locked
== 0) {
1430 if (!mutex_tryenter(&si
->si_snaplock
)) {
1431 mutex_exit(&si
->si_lock
);
1432 mutex_enter(&si
->si_snaplock
);
1433 mutex_enter(&si
->si_lock
);
1435 snapshot_locked
= 1;
1436 if (gen
!= si
->si_gen
)
1440 * Lookup block being written.
1443 blkno
= db_get(ip
, lbn
);
1445 mutex_exit(&si
->si_lock
);
1446 error
= ffs_balloc(vp
, lblktosize(fs
, (off_t
)lbn
),
1447 fs
->fs_bsize
, FSCRED
, B_METAONLY
, &ibp
);
1449 mutex_enter(&si
->si_lock
);
1452 indiroff
= (lbn
- NDADDR
) % NINDIR(fs
);
1453 blkno
= idb_get(ip
, ibp
->b_data
, indiroff
);
1454 mutex_enter(&si
->si_lock
);
1455 if (gen
!= si
->si_gen
) {
1461 * Check to see if block needs to be copied.
1465 * A block that we map is being freed. If it has not
1466 * been claimed yet, we will claim or copy it (below).
1469 } else if (blkno
== BLK_SNAP
) {
1471 * No previous snapshot claimed the block,
1472 * so it will be freed and become a BLK_NOCOPY
1473 * (don't care) for us.
1476 panic("snapblkfree: inconsistent block type");
1478 db_assign(ip
, lbn
, BLK_NOCOPY
);
1479 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
1481 idb_assign(ip
, ibp
->b_data
, indiroff
,
1483 mutex_exit(&si
->si_lock
);
1484 if (ip
->i_nlink
> 0)
1488 mutex_enter(&si
->si_lock
);
1489 if (gen
!= si
->si_gen
)
1493 } else /* BLK_NOCOPY or default */ {
1495 * If the snapshot has already copied the block
1496 * (default), or does not care about the block,
1504 * If this is a full size block, we will just grab it
1505 * and assign it to the snapshot inode. Otherwise we
1506 * will proceed to copy it. See explanation for this
1507 * routine as to why only a single snapshot needs to
1510 if (size
== fs
->fs_bsize
) {
1513 printf("%s %llu lbn %" PRId64
1515 "Grabonremove: snapino",
1516 (unsigned long long)ip
->i_number
,
1517 lbn
, (unsigned long long)inum
);
1519 mutex_exit(&si
->si_lock
);
1521 db_assign(ip
, lbn
, bno
);
1523 idb_assign(ip
, ibp
->b_data
, indiroff
, bno
);
1524 if (ip
->i_nlink
> 0)
1529 DIP_ADD(ip
, blocks
, btodb(size
));
1530 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
1531 if (ip
->i_nlink
> 0 && mp
->mnt_wapbl
)
1532 error
= syncsnap(vp
);
1535 mutex_exit(&si
->si_snaplock
);
1536 return (error
== 0);
1542 printf("%s%llu lbn %" PRId64
" %s %llu size %ld\n",
1543 "Copyonremove: snapino ",
1544 (unsigned long long)ip
->i_number
,
1545 lbn
, "for inum", (unsigned long long)inum
, size
);
1548 * If we have already read the old block contents, then
1549 * simply copy them to the new block. Note that we need
1550 * to synchronously write snapshots that have not been
1551 * unlinked, and hence will be visible after a crash,
1552 * to ensure their integrity.
1554 mutex_exit(&si
->si_lock
);
1555 if (saved_data
== NULL
) {
1556 saved_data
= malloc(fs
->fs_bsize
, M_UFSMNT
, M_WAITOK
);
1557 error
= rwfsblk(vp
, B_READ
, saved_data
, lbn
);
1559 free(saved_data
, M_UFSMNT
);
1561 mutex_enter(&si
->si_lock
);
1565 error
= wrsnapblk(vp
, saved_data
, lbn
);
1566 if (error
== 0 && ip
->i_nlink
> 0 && mp
->mnt_wapbl
)
1567 error
= syncsnap(vp
);
1568 mutex_enter(&si
->si_lock
);
1571 if (gen
!= si
->si_gen
)
1574 mutex_exit(&si
->si_lock
);
1576 free(saved_data
, M_UFSMNT
);
1578 * If we have been unable to allocate a block in which to do
1579 * the copy, then return non-zero so that the fragment will
1580 * not be freed. Although space will be lost, the snapshot
1581 * will stay consistent.
1583 if (snapshot_locked
)
1584 mutex_exit(&si
->si_snaplock
);
1589 * Associate snapshot files when mounting.
1592 ffs_snapshot_mount(struct mount
*mp
)
1594 struct vnode
*devvp
= VFSTOUFS(mp
)->um_devvp
;
1595 struct fs
*fs
= VFSTOUFS(mp
)->um_fs
;
1596 struct lwp
*l
= curlwp
;
1598 struct inode
*ip
, *xp
;
1599 struct snap_info
*si
;
1600 daddr_t snaplistsize
, *snapblklist
;
1601 int i
, error
, ns
, snaploc
, loc
;
1604 * No persistent snapshots on apple ufs file systems.
1606 if (UFS_MPISAPPLEUFS(VFSTOUFS(mp
)))
1609 si
= VFSTOUFS(mp
)->um_snapinfo
;
1610 ns
= UFS_FSNEEDSWAP(fs
);
1612 * XXX The following needs to be set before ffs_truncate or
1613 * VOP_READ can be called.
1615 mp
->mnt_stat
.f_iosize
= fs
->fs_bsize
;
1617 * Process each snapshot listed in the superblock.
1620 mutex_enter(&si
->si_lock
);
1621 for (snaploc
= 0; snaploc
< FSMAXSNAP
; snaploc
++) {
1622 if (fs
->fs_snapinum
[snaploc
] == 0)
1624 if ((error
= VFS_VGET(mp
, fs
->fs_snapinum
[snaploc
],
1626 printf("ffs_snapshot_mount: vget failed %d\n", error
);
1630 if ((ip
->i_flags
& SF_SNAPSHOT
) == 0) {
1631 printf("ffs_snapshot_mount: non-snapshot inode %d\n",
1632 fs
->fs_snapinum
[snaploc
]);
1635 for (loc
= snaploc
+ 1; loc
< FSMAXSNAP
; loc
++) {
1636 if (fs
->fs_snapinum
[loc
] == 0)
1638 fs
->fs_snapinum
[loc
- 1] = fs
->fs_snapinum
[loc
];
1640 fs
->fs_snapinum
[loc
- 1] = 0;
1646 * Read the block hints list. Use an empty list on
1649 error
= vn_rdwr(UIO_READ
, vp
,
1650 (void *)&snaplistsize
, sizeof(snaplistsize
),
1651 lblktosize(fs
, howmany(fs
->fs_size
, fs
->fs_frag
)),
1652 UIO_SYSSPACE
, IO_NODELOCKED
|IO_UNIT
|IO_ALTSEMANTICS
,
1653 l
->l_cred
, NULL
, NULL
);
1655 printf("ffs_snapshot_mount: read_1 failed %d\n", error
);
1658 snaplistsize
= ufs_rw64(snaplistsize
, ns
);
1659 snapblklist
= malloc(
1660 snaplistsize
* sizeof(daddr_t
), M_UFSMNT
, M_WAITOK
);
1664 error
= vn_rdwr(UIO_READ
, vp
, (void *)snapblklist
,
1665 snaplistsize
* sizeof(daddr_t
),
1666 lblktosize(fs
, howmany(fs
->fs_size
, fs
->fs_frag
)),
1667 UIO_SYSSPACE
, IO_NODELOCKED
|IO_UNIT
|IO_ALTSEMANTICS
,
1668 l
->l_cred
, NULL
, NULL
);
1669 for (i
= 0; i
< snaplistsize
; i
++)
1670 snapblklist
[i
] = ufs_rw64(snapblklist
[i
], ns
);
1672 printf("ffs_snapshot_mount: read_2 failed %d\n",
1677 ip
->i_snapblklist
= &snapblklist
[0];
1680 * Link it onto the active snapshot list.
1682 if (is_active_snapshot(si
, ip
))
1683 panic("ffs_snapshot_mount: %"PRIu64
" already on list",
1686 TAILQ_INSERT_TAIL(&si
->si_snapshots
, ip
, i_nextsnap
);
1687 vp
->v_vflag
|= VV_SYSTEM
;
1691 * No usable snapshots found.
1694 mutex_exit(&si
->si_lock
);
1698 * Attach the block hints list. We always want to
1699 * use the list from the newest snapshot.
1701 xp
= TAILQ_LAST(&si
->si_snapshots
, inodelst
);
1702 si
->si_snapblklist
= xp
->i_snapblklist
;
1703 fscow_establish(mp
, ffs_copyonwrite
, devvp
);
1705 mutex_exit(&si
->si_lock
);
1709 * Disassociate snapshot files when unmounting.
1712 ffs_snapshot_unmount(struct mount
*mp
)
1714 struct vnode
*devvp
= VFSTOUFS(mp
)->um_devvp
;
1716 struct vnode
*vp
= NULL
;
1717 struct snap_info
*si
;
1719 si
= VFSTOUFS(mp
)->um_snapinfo
;
1720 mutex_enter(&si
->si_lock
);
1721 while ((xp
= TAILQ_FIRST(&si
->si_snapshots
)) != 0) {
1723 TAILQ_REMOVE(&si
->si_snapshots
, xp
, i_nextsnap
);
1724 if (xp
->i_snapblklist
== si
->si_snapblklist
)
1725 si
->si_snapblklist
= NULL
;
1726 free(xp
->i_snapblklist
, M_UFSMNT
);
1727 if (xp
->i_nlink
> 0) {
1729 mutex_exit(&si
->si_lock
);
1731 mutex_enter(&si
->si_lock
);
1735 mutex_exit(&si
->si_lock
);
1737 fscow_disestablish(mp
, ffs_copyonwrite
, devvp
);
1741 * Check for need to copy block that is about to be written,
1742 * copying the block if necessary.
1745 ffs_copyonwrite(void *v
, struct buf
*bp
, bool data_valid
)
1749 struct vnode
*devvp
= v
, *vp
= NULL
;
1750 struct mount
*mp
= devvp
->v_specmountpoint
;
1751 struct snap_info
*si
;
1752 void *saved_data
= NULL
;
1753 daddr_t lbn
, blkno
, *snapblklist
;
1755 int lower
, upper
, mid
, snapshot_locked
= 0, error
= 0;
1758 * Check for valid snapshots.
1760 si
= VFSTOUFS(mp
)->um_snapinfo
;
1761 mutex_enter(&si
->si_lock
);
1762 ip
= TAILQ_FIRST(&si
->si_snapshots
);
1764 mutex_exit(&si
->si_lock
);
1768 * First check to see if it is after the file system or
1769 * in the preallocated list.
1770 * By doing this check we avoid several potential deadlocks.
1773 lbn
= fragstoblks(fs
, dbtofsb(fs
, bp
->b_blkno
));
1774 if (bp
->b_blkno
>= fsbtodb(fs
, fs
->fs_size
)) {
1775 mutex_exit(&si
->si_lock
);
1778 snapblklist
= si
->si_snapblklist
;
1779 upper
= (snapblklist
!= NULL
? snapblklist
[0] - 1 : 0);
1781 while (lower
<= upper
) {
1782 mid
= (lower
+ upper
) / 2;
1783 if (snapblklist
[mid
] == lbn
)
1785 if (snapblklist
[mid
] < lbn
)
1790 if (lower
<= upper
) {
1791 mutex_exit(&si
->si_lock
);
1795 * Not in the precomputed list, so check the snapshots.
1797 if (data_valid
&& bp
->b_bcount
== fs
->fs_bsize
)
1798 saved_data
= bp
->b_data
;
1801 TAILQ_FOREACH(ip
, &si
->si_snapshots
, i_nextsnap
) {
1804 * We ensure that everything of our own that needs to be
1805 * copied will be done at the time that ffs_snapshot is
1806 * called. Thus we can skip the check here which can
1807 * deadlock in doing the lookup in ffs_balloc.
1812 * Check to see if block needs to be copied.
1815 blkno
= db_get(ip
, lbn
);
1817 mutex_exit(&si
->si_lock
);
1818 if ((error
= snapblkaddr(vp
, lbn
, &blkno
)) != 0) {
1819 mutex_enter(&si
->si_lock
);
1822 mutex_enter(&si
->si_lock
);
1823 if (gen
!= si
->si_gen
)
1827 if (blkno
== BLK_SNAP
&& bp
->b_lblkno
>= 0)
1828 panic("ffs_copyonwrite: bad copy block");
1833 if (curlwp
== uvm
.pagedaemon_lwp
) {
1838 if (snapshot_locked
== 0) {
1839 if (!mutex_tryenter(&si
->si_snaplock
)) {
1840 mutex_exit(&si
->si_lock
);
1841 mutex_enter(&si
->si_snaplock
);
1842 mutex_enter(&si
->si_lock
);
1844 snapshot_locked
= 1;
1845 if (gen
!= si
->si_gen
)
1848 /* Check again if block still needs to be copied */
1850 blkno
= db_get(ip
, lbn
);
1852 mutex_exit(&si
->si_lock
);
1853 if ((error
= snapblkaddr(vp
, lbn
, &blkno
)) != 0) {
1854 mutex_enter(&si
->si_lock
);
1857 mutex_enter(&si
->si_lock
);
1858 if (gen
!= si
->si_gen
)
1866 * Allocate the block into which to do the copy. Since
1867 * multiple processes may all try to copy the same block,
1868 * we have to recheck our need to do a copy if we sleep
1869 * waiting for the lock.
1871 * Because all snapshots on a filesystem share a single
1872 * lock, we ensure that we will never be in competition
1873 * with another process to allocate a block.
1877 printf("Copyonwrite: snapino %llu lbn %" PRId64
" for ",
1878 (unsigned long long)ip
->i_number
, lbn
);
1879 if (bp
->b_vp
== devvp
)
1880 printf("fs metadata");
1882 printf("inum %llu", (unsigned long long)
1883 VTOI(bp
->b_vp
)->i_number
);
1884 printf(" lblkno %" PRId64
"\n", bp
->b_lblkno
);
1888 * If we have already read the old block contents, then
1889 * simply copy them to the new block. Note that we need
1890 * to synchronously write snapshots that have not been
1891 * unlinked, and hence will be visible after a crash,
1892 * to ensure their integrity.
1894 mutex_exit(&si
->si_lock
);
1895 if (saved_data
== NULL
) {
1896 saved_data
= malloc(fs
->fs_bsize
, M_UFSMNT
, M_WAITOK
);
1897 error
= rwfsblk(vp
, B_READ
, saved_data
, lbn
);
1899 free(saved_data
, M_UFSMNT
);
1901 mutex_enter(&si
->si_lock
);
1905 error
= wrsnapblk(vp
, saved_data
, lbn
);
1906 if (error
== 0 && ip
->i_nlink
> 0 && mp
->mnt_wapbl
)
1907 error
= syncsnap(vp
);
1908 mutex_enter(&si
->si_lock
);
1911 if (gen
!= si
->si_gen
)
1915 * Note that we need to synchronously write snapshots that
1916 * have not been unlinked, and hence will be visible after
1917 * a crash, to ensure their integrity.
1919 mutex_exit(&si
->si_lock
);
1920 if (saved_data
&& saved_data
!= bp
->b_data
)
1921 free(saved_data
, M_UFSMNT
);
1922 if (snapshot_locked
)
1923 mutex_exit(&si
->si_snaplock
);
1928 * Read from a snapshot.
1931 ffs_snapshot_read(struct vnode
*vp
, struct uio
*uio
, int ioflag
)
1933 struct inode
*ip
= VTOI(vp
);
1934 struct fs
*fs
= ip
->i_fs
;
1935 struct snap_info
*si
= VFSTOUFS(vp
->v_mount
)->um_snapinfo
;
1937 daddr_t lbn
, nextlbn
;
1938 off_t fsbytes
, bytesinfile
;
1939 long size
, xfersize
, blkoffset
;
1942 fstrans_start(vp
->v_mount
, FSTRANS_SHARED
);
1943 mutex_enter(&si
->si_snaplock
);
1945 if (ioflag
& IO_ALTSEMANTICS
)
1946 fsbytes
= ip
->i_size
;
1948 fsbytes
= lfragtosize(fs
, fs
->fs_size
);
1949 for (error
= 0, bp
= NULL
; uio
->uio_resid
> 0; bp
= NULL
) {
1950 bytesinfile
= fsbytes
- uio
->uio_offset
;
1951 if (bytesinfile
<= 0)
1953 lbn
= lblkno(fs
, uio
->uio_offset
);
1955 size
= fs
->fs_bsize
;
1956 blkoffset
= blkoff(fs
, uio
->uio_offset
);
1957 xfersize
= MIN(MIN(fs
->fs_bsize
- blkoffset
, uio
->uio_resid
),
1960 if (lblktosize(fs
, nextlbn
+ 1) >= fsbytes
) {
1961 if (lblktosize(fs
, lbn
) + size
> fsbytes
)
1962 size
= fragroundup(fs
,
1963 fsbytes
- lblktosize(fs
, lbn
));
1964 error
= bread(vp
, lbn
, size
, NOCRED
, 0, &bp
);
1966 int nextsize
= fs
->fs_bsize
;
1967 error
= breadn(vp
, lbn
,
1968 size
, &nextlbn
, &nextsize
, 1, NOCRED
, 0, &bp
);
1974 * We should only get non-zero b_resid when an I/O error
1975 * has occurred, which should cause us to break above.
1976 * However, if the short read did not cause an error,
1977 * then we want to ensure that we do not uiomove bad
1978 * or uninitialized data.
1980 size
-= bp
->b_resid
;
1981 if (size
< blkoffset
+ xfersize
) {
1982 xfersize
= size
- blkoffset
;
1986 error
= uiomove((char *)bp
->b_data
+ blkoffset
, xfersize
, uio
);
1994 mutex_exit(&si
->si_snaplock
);
1995 fstrans_done(vp
->v_mount
);
2000 * Lookup a snapshots data block address.
2001 * Simpler than UFS_BALLOC() as we know all metadata is already allocated
2002 * and safe even for the pagedaemon where we cannot bread().
2005 snapblkaddr(struct vnode
*vp
, daddr_t lbn
, daddr_t
*res
)
2007 struct indir indirs
[NIADDR
+ 2];
2008 struct inode
*ip
= VTOI(vp
);
2009 struct fs
*fs
= ip
->i_fs
;
2016 *res
= db_get(ip
, lbn
);
2019 if ((error
= ufs_getlbns(vp
, lbn
, indirs
, &num
)) != 0)
2021 if (curlwp
== uvm
.pagedaemon_lwp
) {
2022 mutex_enter(&bufcache_lock
);
2023 bp
= incore(vp
, indirs
[num
-1].in_lbn
);
2024 if (bp
&& (bp
->b_oflags
& (BO_DONE
| BO_DELWRI
))) {
2025 *res
= idb_get(ip
, bp
->b_data
, indirs
[num
-1].in_off
);
2029 mutex_exit(&bufcache_lock
);
2032 error
= bread(vp
, indirs
[num
-1].in_lbn
, fs
->fs_bsize
, NOCRED
, 0, &bp
);
2034 *res
= idb_get(ip
, bp
->b_data
, indirs
[num
-1].in_off
);
2041 * Read or write the specified block of the filesystem vp resides on
2042 * from or to the disk bypassing the buffer cache.
2045 rwfsblk(struct vnode
*vp
, int flags
, void *data
, daddr_t lbn
)
2048 struct inode
*ip
= VTOI(vp
);
2049 struct fs
*fs
= ip
->i_fs
;
2052 nbp
= getiobuf(NULL
, true);
2053 nbp
->b_flags
= flags
;
2054 nbp
->b_bcount
= nbp
->b_bufsize
= fs
->fs_bsize
;
2057 nbp
->b_blkno
= nbp
->b_rawblkno
= fsbtodb(fs
, blkstofrags(fs
, lbn
));
2059 nbp
->b_dev
= ip
->i_devvp
->v_rdev
;
2060 SET(nbp
->b_cflags
, BC_BUSY
); /* mark buffer busy */
2064 error
= biowait(nbp
);
2072 * Write all dirty buffers to disk and invalidate them.
2075 syncsnap(struct vnode
*vp
)
2079 struct fs
*fs
= VTOI(vp
)->i_fs
;
2081 mutex_enter(&bufcache_lock
);
2082 while ((bp
= LIST_FIRST(&vp
->v_dirtyblkhd
))) {
2083 KASSERT((bp
->b_cflags
& BC_BUSY
) == 0);
2084 KASSERT(bp
->b_bcount
== fs
->fs_bsize
);
2085 bp
->b_cflags
|= BC_BUSY
;
2086 mutex_exit(&bufcache_lock
);
2087 error
= rwfsblk(vp
, B_WRITE
, bp
->b_data
,
2088 fragstoblks(fs
, dbtofsb(fs
, bp
->b_blkno
)));
2089 brelse(bp
, BC_INVAL
| BC_VFLUSH
);
2092 mutex_enter(&bufcache_lock
);
2094 mutex_exit(&bufcache_lock
);
2100 * Write the specified block to a snapshot.
2103 wrsnapblk(struct vnode
*vp
, void *data
, daddr_t lbn
)
2105 struct inode
*ip
= VTOI(vp
);
2106 struct fs
*fs
= ip
->i_fs
;
2110 error
= ffs_balloc(vp
, lblktosize(fs
, (off_t
)lbn
), fs
->fs_bsize
,
2111 FSCRED
, (ip
->i_nlink
> 0 ? B_SYNC
: 0), &bp
);
2114 memcpy(bp
->b_data
, data
, fs
->fs_bsize
);
2115 if (ip
->i_nlink
> 0)
2124 * Check if this inode is present on the active snapshot list.
2125 * Must be called with snapinfo locked.
2128 is_active_snapshot(struct snap_info
*si
, struct inode
*ip
)
2132 KASSERT(mutex_owned(&si
->si_lock
));
2134 TAILQ_FOREACH(xp
, &si
->si_snapshots
, i_nextsnap
)
2141 * Get/Put direct block from inode or buffer containing disk addresses. Take
2142 * care for fs type (UFS1/UFS2) and byte swapping. These functions should go
2143 * into a global include.
2145 static inline daddr_t
2146 db_get(struct inode
*ip
, int loc
)
2148 if (ip
->i_ump
->um_fstype
== UFS1
)
2149 return ufs_rw32(ip
->i_ffs1_db
[loc
], UFS_IPNEEDSWAP(ip
));
2151 return ufs_rw64(ip
->i_ffs2_db
[loc
], UFS_IPNEEDSWAP(ip
));
2155 db_assign(struct inode
*ip
, int loc
, daddr_t val
)
2157 if (ip
->i_ump
->um_fstype
== UFS1
)
2158 ip
->i_ffs1_db
[loc
] = ufs_rw32(val
, UFS_IPNEEDSWAP(ip
));
2160 ip
->i_ffs2_db
[loc
] = ufs_rw64(val
, UFS_IPNEEDSWAP(ip
));
2163 static inline daddr_t
2164 ib_get(struct inode
*ip
, int loc
)
2166 if (ip
->i_ump
->um_fstype
== UFS1
)
2167 return ufs_rw32(ip
->i_ffs1_ib
[loc
], UFS_IPNEEDSWAP(ip
));
2169 return ufs_rw64(ip
->i_ffs2_ib
[loc
], UFS_IPNEEDSWAP(ip
));
2173 ib_assign(struct inode
*ip
, int loc
, daddr_t val
)
2175 if (ip
->i_ump
->um_fstype
== UFS1
)
2176 ip
->i_ffs1_ib
[loc
] = ufs_rw32(val
, UFS_IPNEEDSWAP(ip
));
2178 ip
->i_ffs2_ib
[loc
] = ufs_rw64(val
, UFS_IPNEEDSWAP(ip
));
2181 static inline daddr_t
2182 idb_get(struct inode
*ip
, void *bf
, int loc
)
2184 if (ip
->i_ump
->um_fstype
== UFS1
)
2185 return ufs_rw32(((int32_t *)(bf
))[loc
], UFS_IPNEEDSWAP(ip
));
2187 return ufs_rw64(((int64_t *)(bf
))[loc
], UFS_IPNEEDSWAP(ip
));
2191 idb_assign(struct inode
*ip
, void *bf
, int loc
, daddr_t val
)
2193 if (ip
->i_ump
->um_fstype
== UFS1
)
2194 ((int32_t *)(bf
))[loc
] = ufs_rw32(val
, UFS_IPNEEDSWAP(ip
));
2196 ((int64_t *)(bf
))[loc
] = ufs_rw64(val
, UFS_IPNEEDSWAP(ip
));