2 * Copyright (c) 2011-2015 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 * by Daniel Flores (GSOC 2013 - mentored by Matthew Dillon, compression)
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/nlookup.h>
39 #include <sys/vnode.h>
40 #include <sys/mount.h>
41 #include <sys/fcntl.h>
44 #include <sys/vfsops.h>
45 #include <sys/sysctl.h>
46 #include <sys/socket.h>
47 #include <sys/objcache.h>
50 #include <sys/namei.h>
51 #include <sys/mountctl.h>
52 #include <sys/dirent.h>
55 #include <sys/mutex.h>
56 #include <sys/mutex2.h>
59 #include "hammer2_disk.h"
60 #include "hammer2_mount.h"
61 #include "hammer2_lz4.h"
63 #include "zlib/hammer2_zlib.h"
65 #define REPORT_REFS_ERRORS 1 /* XXX remove me */
67 MALLOC_DEFINE(M_OBJCACHE
, "objcache", "Object Cache");
69 struct hammer2_sync_info
{
70 hammer2_trans_t trans
;
75 TAILQ_HEAD(hammer2_mntlist
, hammer2_mount
);
76 TAILQ_HEAD(hammer2_pfslist
, hammer2_pfsmount
);
77 static struct hammer2_mntlist hammer2_mntlist
;
78 static struct hammer2_pfslist hammer2_pfslist
;
79 static struct lock hammer2_mntlk
;
82 int hammer2_cluster_enable
= 1;
83 int hammer2_hardlink_enable
= 1;
84 int hammer2_flush_pipe
= 100;
85 int hammer2_synchronous_flush
= 1;
86 int hammer2_dio_count
;
87 long hammer2_limit_dirty_chains
;
88 long hammer2_iod_file_read
;
89 long hammer2_iod_meta_read
;
90 long hammer2_iod_indr_read
;
91 long hammer2_iod_fmap_read
;
92 long hammer2_iod_volu_read
;
93 long hammer2_iod_file_write
;
94 long hammer2_iod_meta_write
;
95 long hammer2_iod_indr_write
;
96 long hammer2_iod_fmap_write
;
97 long hammer2_iod_volu_write
;
98 long hammer2_ioa_file_read
;
99 long hammer2_ioa_meta_read
;
100 long hammer2_ioa_indr_read
;
101 long hammer2_ioa_fmap_read
;
102 long hammer2_ioa_volu_read
;
103 long hammer2_ioa_fmap_write
;
104 long hammer2_ioa_file_write
;
105 long hammer2_ioa_meta_write
;
106 long hammer2_ioa_indr_write
;
107 long hammer2_ioa_volu_write
;
109 MALLOC_DECLARE(C_BUFFER
);
110 MALLOC_DEFINE(C_BUFFER
, "compbuffer", "Buffer used for compression.");
112 MALLOC_DECLARE(D_BUFFER
);
113 MALLOC_DEFINE(D_BUFFER
, "decompbuffer", "Buffer used for decompression.");
115 SYSCTL_NODE(_vfs
, OID_AUTO
, hammer2
, CTLFLAG_RW
, 0, "HAMMER2 filesystem");
117 SYSCTL_INT(_vfs_hammer2
, OID_AUTO
, debug
, CTLFLAG_RW
,
118 &hammer2_debug
, 0, "");
119 SYSCTL_INT(_vfs_hammer2
, OID_AUTO
, cluster_enable
, CTLFLAG_RW
,
120 &hammer2_cluster_enable
, 0, "");
121 SYSCTL_INT(_vfs_hammer2
, OID_AUTO
, hardlink_enable
, CTLFLAG_RW
,
122 &hammer2_hardlink_enable
, 0, "");
123 SYSCTL_INT(_vfs_hammer2
, OID_AUTO
, flush_pipe
, CTLFLAG_RW
,
124 &hammer2_flush_pipe
, 0, "");
125 SYSCTL_INT(_vfs_hammer2
, OID_AUTO
, synchronous_flush
, CTLFLAG_RW
,
126 &hammer2_synchronous_flush
, 0, "");
127 SYSCTL_LONG(_vfs_hammer2
, OID_AUTO
, limit_dirty_chains
, CTLFLAG_RW
,
128 &hammer2_limit_dirty_chains
, 0, "");
129 SYSCTL_INT(_vfs_hammer2
, OID_AUTO
, dio_count
, CTLFLAG_RD
,
130 &hammer2_dio_count
, 0, "");
132 SYSCTL_LONG(_vfs_hammer2
, OID_AUTO
, iod_file_read
, CTLFLAG_RW
,
133 &hammer2_iod_file_read
, 0, "");
134 SYSCTL_LONG(_vfs_hammer2
, OID_AUTO
, iod_meta_read
, CTLFLAG_RW
,
135 &hammer2_iod_meta_read
, 0, "");
136 SYSCTL_LONG(_vfs_hammer2
, OID_AUTO
, iod_indr_read
, CTLFLAG_RW
,
137 &hammer2_iod_indr_read
, 0, "");
138 SYSCTL_LONG(_vfs_hammer2
, OID_AUTO
, iod_fmap_read
, CTLFLAG_RW
,
139 &hammer2_iod_fmap_read
, 0, "");
140 SYSCTL_LONG(_vfs_hammer2
, OID_AUTO
, iod_volu_read
, CTLFLAG_RW
,
141 &hammer2_iod_volu_read
, 0, "");
143 SYSCTL_LONG(_vfs_hammer2
, OID_AUTO
, iod_file_write
, CTLFLAG_RW
,
144 &hammer2_iod_file_write
, 0, "");
145 SYSCTL_LONG(_vfs_hammer2
, OID_AUTO
, iod_meta_write
, CTLFLAG_RW
,
146 &hammer2_iod_meta_write
, 0, "");
147 SYSCTL_LONG(_vfs_hammer2
, OID_AUTO
, iod_indr_write
, CTLFLAG_RW
,
148 &hammer2_iod_indr_write
, 0, "");
149 SYSCTL_LONG(_vfs_hammer2
, OID_AUTO
, iod_fmap_write
, CTLFLAG_RW
,
150 &hammer2_iod_fmap_write
, 0, "");
151 SYSCTL_LONG(_vfs_hammer2
, OID_AUTO
, iod_volu_write
, CTLFLAG_RW
,
152 &hammer2_iod_volu_write
, 0, "");
154 SYSCTL_LONG(_vfs_hammer2
, OID_AUTO
, ioa_file_read
, CTLFLAG_RW
,
155 &hammer2_ioa_file_read
, 0, "");
156 SYSCTL_LONG(_vfs_hammer2
, OID_AUTO
, ioa_meta_read
, CTLFLAG_RW
,
157 &hammer2_ioa_meta_read
, 0, "");
158 SYSCTL_LONG(_vfs_hammer2
, OID_AUTO
, ioa_indr_read
, CTLFLAG_RW
,
159 &hammer2_ioa_indr_read
, 0, "");
160 SYSCTL_LONG(_vfs_hammer2
, OID_AUTO
, ioa_fmap_read
, CTLFLAG_RW
,
161 &hammer2_ioa_fmap_read
, 0, "");
162 SYSCTL_LONG(_vfs_hammer2
, OID_AUTO
, ioa_volu_read
, CTLFLAG_RW
,
163 &hammer2_ioa_volu_read
, 0, "");
165 SYSCTL_LONG(_vfs_hammer2
, OID_AUTO
, ioa_file_write
, CTLFLAG_RW
,
166 &hammer2_ioa_file_write
, 0, "");
167 SYSCTL_LONG(_vfs_hammer2
, OID_AUTO
, ioa_meta_write
, CTLFLAG_RW
,
168 &hammer2_ioa_meta_write
, 0, "");
169 SYSCTL_LONG(_vfs_hammer2
, OID_AUTO
, ioa_indr_write
, CTLFLAG_RW
,
170 &hammer2_ioa_indr_write
, 0, "");
171 SYSCTL_LONG(_vfs_hammer2
, OID_AUTO
, ioa_fmap_write
, CTLFLAG_RW
,
172 &hammer2_ioa_fmap_write
, 0, "");
173 SYSCTL_LONG(_vfs_hammer2
, OID_AUTO
, ioa_volu_write
, CTLFLAG_RW
,
174 &hammer2_ioa_volu_write
, 0, "");
176 static int hammer2_vfs_init(struct vfsconf
*conf
);
177 static int hammer2_vfs_uninit(struct vfsconf
*vfsp
);
178 static int hammer2_vfs_mount(struct mount
*mp
, char *path
, caddr_t data
,
180 static int hammer2_remount(hammer2_mount_t
*, struct mount
*, char *,
181 struct vnode
*, struct ucred
*);
182 static int hammer2_recovery(hammer2_mount_t
*hmp
);
183 static int hammer2_vfs_unmount(struct mount
*mp
, int mntflags
);
184 static int hammer2_vfs_root(struct mount
*mp
, struct vnode
**vpp
);
185 static int hammer2_vfs_statfs(struct mount
*mp
, struct statfs
*sbp
,
187 static int hammer2_vfs_statvfs(struct mount
*mp
, struct statvfs
*sbp
,
189 static int hammer2_vfs_vget(struct mount
*mp
, struct vnode
*dvp
,
190 ino_t ino
, struct vnode
**vpp
);
191 static int hammer2_vfs_fhtovp(struct mount
*mp
, struct vnode
*rootvp
,
192 struct fid
*fhp
, struct vnode
**vpp
);
193 static int hammer2_vfs_vptofh(struct vnode
*vp
, struct fid
*fhp
);
194 static int hammer2_vfs_checkexp(struct mount
*mp
, struct sockaddr
*nam
,
195 int *exflagsp
, struct ucred
**credanonp
);
197 static int hammer2_install_volume_header(hammer2_mount_t
*hmp
);
198 static int hammer2_sync_scan2(struct mount
*mp
, struct vnode
*vp
, void *data
);
200 static void hammer2_write_thread(void *arg
);
202 static void hammer2_vfs_unmount_hmp1(struct mount
*mp
, hammer2_mount_t
*hmp
);
203 static void hammer2_vfs_unmount_hmp2(struct mount
*mp
, hammer2_mount_t
*hmp
);
206 * Functions for compression in threads,
207 * from hammer2_vnops.c
209 static void hammer2_write_file_core(struct buf
*bp
, hammer2_trans_t
*trans
,
211 const hammer2_inode_data_t
*ripdata
,
212 hammer2_cluster_t
*cparent
,
213 hammer2_key_t lbase
, int ioflag
, int pblksize
,
215 static void hammer2_compress_and_write(struct buf
*bp
, hammer2_trans_t
*trans
,
217 const hammer2_inode_data_t
*ripdata
,
218 hammer2_cluster_t
*cparent
,
219 hammer2_key_t lbase
, int ioflag
,
220 int pblksize
, int *errorp
,
221 int comp_algo
, int check_algo
);
222 static void hammer2_zero_check_and_write(struct buf
*bp
,
223 hammer2_trans_t
*trans
, hammer2_inode_t
*ip
,
224 const hammer2_inode_data_t
*ripdata
,
225 hammer2_cluster_t
*cparent
,
227 int ioflag
, int pblksize
, int *errorp
,
229 static int test_block_zeros(const char *buf
, size_t bytes
);
230 static void zero_write(struct buf
*bp
, hammer2_trans_t
*trans
,
232 const hammer2_inode_data_t
*ripdata
,
233 hammer2_cluster_t
*cparent
,
236 static void hammer2_write_bp(hammer2_cluster_t
*cluster
, struct buf
*bp
,
237 int ioflag
, int pblksize
, int *errorp
,
241 * HAMMER2 vfs operations.
243 static struct vfsops hammer2_vfsops
= {
244 .vfs_init
= hammer2_vfs_init
,
245 .vfs_uninit
= hammer2_vfs_uninit
,
246 .vfs_sync
= hammer2_vfs_sync
,
247 .vfs_mount
= hammer2_vfs_mount
,
248 .vfs_unmount
= hammer2_vfs_unmount
,
249 .vfs_root
= hammer2_vfs_root
,
250 .vfs_statfs
= hammer2_vfs_statfs
,
251 .vfs_statvfs
= hammer2_vfs_statvfs
,
252 .vfs_vget
= hammer2_vfs_vget
,
253 .vfs_vptofh
= hammer2_vfs_vptofh
,
254 .vfs_fhtovp
= hammer2_vfs_fhtovp
,
255 .vfs_checkexp
= hammer2_vfs_checkexp
258 MALLOC_DEFINE(M_HAMMER2
, "HAMMER2-mount", "");
260 VFS_SET(hammer2_vfsops
, hammer2
, 0);
261 MODULE_VERSION(hammer2
, 1);
265 hammer2_vfs_init(struct vfsconf
*conf
)
267 static struct objcache_malloc_args margs_read
;
268 static struct objcache_malloc_args margs_write
;
274 if (HAMMER2_BLOCKREF_BYTES
!= sizeof(struct hammer2_blockref
))
276 if (HAMMER2_INODE_BYTES
!= sizeof(struct hammer2_inode_data
))
278 if (HAMMER2_VOLUME_BYTES
!= sizeof(struct hammer2_volume_data
))
282 kprintf("HAMMER2 structure size mismatch; cannot continue.\n");
284 margs_read
.objsize
= 65536;
285 margs_read
.mtype
= D_BUFFER
;
287 margs_write
.objsize
= 32768;
288 margs_write
.mtype
= C_BUFFER
;
290 cache_buffer_read
= objcache_create(margs_read
.mtype
->ks_shortdesc
,
291 0, 1, NULL
, NULL
, NULL
, objcache_malloc_alloc
,
292 objcache_malloc_free
, &margs_read
);
293 cache_buffer_write
= objcache_create(margs_write
.mtype
->ks_shortdesc
,
294 0, 1, NULL
, NULL
, NULL
, objcache_malloc_alloc
,
295 objcache_malloc_free
, &margs_write
);
297 lockinit(&hammer2_mntlk
, "mntlk", 0, 0);
298 TAILQ_INIT(&hammer2_mntlist
);
299 TAILQ_INIT(&hammer2_pfslist
);
301 hammer2_limit_dirty_chains
= desiredvnodes
/ 10;
303 hammer2_trans_manage_init();
310 hammer2_vfs_uninit(struct vfsconf
*vfsp __unused
)
312 objcache_destroy(cache_buffer_read
);
313 objcache_destroy(cache_buffer_write
);
318 * Core PFS allocator. Used to allocate the pmp structure for PFS cluster
319 * mounts and the spmp structure for media (hmp) structures.
321 static hammer2_pfsmount_t
*
322 hammer2_pfsalloc(const hammer2_inode_data_t
*ripdata
, hammer2_tid_t alloc_tid
)
324 hammer2_pfsmount_t
*pmp
;
326 pmp
= kmalloc(sizeof(*pmp
), M_HAMMER2
, M_WAITOK
| M_ZERO
);
327 kmalloc_create(&pmp
->minode
, "HAMMER2-inodes");
328 kmalloc_create(&pmp
->mmsg
, "HAMMER2-pfsmsg");
329 lockinit(&pmp
->lock
, "pfslk", 0, 0);
330 spin_init(&pmp
->inum_spin
, "hm2pfsalloc_inum");
331 RB_INIT(&pmp
->inum_tree
);
332 TAILQ_INIT(&pmp
->unlinkq
);
333 spin_init(&pmp
->list_spin
, "hm2pfsalloc_list");
335 pmp
->alloc_tid
= alloc_tid
+ 1; /* our first media transaction id */
336 pmp
->flush_tid
= pmp
->alloc_tid
;
338 pmp
->inode_tid
= ripdata
->pfs_inum
+ 1;
339 pmp
->pfs_clid
= ripdata
->pfs_clid
;
341 mtx_init(&pmp
->wthread_mtx
);
342 bioq_init(&pmp
->wthread_bioq
);
348 * Mount or remount HAMMER2 fileystem from physical media
351 * mp mount point structure
357 * mp mount point structure
358 * path path to mount point
359 * data pointer to argument structure in user space
360 * volume volume path (device@LABEL form)
361 * hflags user mount flags
362 * cred user credentials
369 hammer2_vfs_mount(struct mount
*mp
, char *path
, caddr_t data
,
372 struct hammer2_mount_info info
;
373 hammer2_pfsmount_t
*pmp
;
374 hammer2_pfsmount_t
*spmp
;
375 hammer2_mount_t
*hmp
;
376 hammer2_key_t key_next
;
377 hammer2_key_t key_dummy
;
380 struct nlookupdata nd
;
381 hammer2_chain_t
*parent
;
382 hammer2_chain_t
*rchain
;
383 hammer2_cluster_t
*cluster
;
384 hammer2_cluster_t
*cparent
;
385 const hammer2_inode_data_t
*ripdata
;
386 hammer2_blockref_t bref
;
388 char devstr
[MNAMELEN
];
406 kprintf("hammer2_mount\n");
412 bzero(&info
, sizeof(info
));
413 info
.cluster_fd
= -1;
417 * Non-root mount or updating a mount
419 error
= copyin(data
, &info
, sizeof(info
));
423 error
= copyinstr(info
.volume
, devstr
, MNAMELEN
- 1, &done
);
427 /* Extract device and label */
429 label
= strchr(devstr
, '@');
431 ((label
+ 1) - dev
) > done
) {
439 if (mp
->mnt_flag
& MNT_UPDATE
) {
441 /* HAMMER2 implements NFS export via mountctl */
443 for (i
= 0; i
< pmp
->iroot
->cluster
.nchains
; ++i
) {
444 hmp
= pmp
->iroot
->cluster
.array
[i
]->hmp
;
446 error
= hammer2_remount(hmp
, mp
, path
,
451 /*hammer2_inode_install_hidden(pmp);*/
460 * Lookup name and verify it refers to a block device.
462 error
= nlookup_init(&nd
, dev
, UIO_SYSSPACE
, NLC_FOLLOW
);
464 error
= nlookup(&nd
);
466 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &devvp
);
470 if (vn_isdisk(devvp
, &error
))
471 error
= vfs_mountedon(devvp
);
475 * Determine if the device has already been mounted. After this
476 * check hmp will be non-NULL if we are doing the second or more
477 * hammer2 mounts from the same device.
479 lockmgr(&hammer2_mntlk
, LK_EXCLUSIVE
);
480 TAILQ_FOREACH(hmp
, &hammer2_mntlist
, mntentry
) {
481 if (hmp
->devvp
== devvp
)
486 * Open the device if this isn't a secondary mount and construct
487 * the H2 device mount (hmp).
490 hammer2_chain_t
*schain
;
493 if (error
== 0 && vcount(devvp
) > 0)
497 * Now open the device
500 ronly
= ((mp
->mnt_flag
& MNT_RDONLY
) != 0);
501 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
);
502 error
= vinvalbuf(devvp
, V_SAVE
, 0, 0);
504 error
= VOP_OPEN(devvp
,
505 ronly
? FREAD
: FREAD
| FWRITE
,
510 if (error
&& devvp
) {
515 lockmgr(&hammer2_mntlk
, LK_RELEASE
);
518 hmp
= kmalloc(sizeof(*hmp
), M_HAMMER2
, M_WAITOK
| M_ZERO
);
521 kmalloc_create(&hmp
->mchain
, "HAMMER2-chains");
522 TAILQ_INSERT_TAIL(&hammer2_mntlist
, hmp
, mntentry
);
523 RB_INIT(&hmp
->iotree
);
524 spin_init(&hmp
->io_spin
, "hm2mount_io");
525 spin_init(&hmp
->list_spin
, "hm2mount_list");
526 TAILQ_INIT(&hmp
->flushq
);
528 lockinit(&hmp
->vollk
, "h2vol", 0, 0);
531 * vchain setup. vchain.data is embedded.
532 * vchain.refs is initialized and will never drop to 0.
534 * NOTE! voldata is not yet loaded.
536 hmp
->vchain
.hmp
= hmp
;
537 hmp
->vchain
.refs
= 1;
538 hmp
->vchain
.data
= (void *)&hmp
->voldata
;
539 hmp
->vchain
.bref
.type
= HAMMER2_BREF_TYPE_VOLUME
;
540 hmp
->vchain
.bref
.data_off
= 0 | HAMMER2_PBUFRADIX
;
541 hmp
->vchain
.bref
.mirror_tid
= hmp
->voldata
.mirror_tid
;
543 hammer2_chain_core_alloc(NULL
, &hmp
->vchain
);
544 /* hmp->vchain.u.xxx is left NULL */
547 * fchain setup. fchain.data is embedded.
548 * fchain.refs is initialized and will never drop to 0.
550 * The data is not used but needs to be initialized to
551 * pass assertion muster. We use this chain primarily
552 * as a placeholder for the freemap's top-level RBTREE
553 * so it does not interfere with the volume's topology
556 hmp
->fchain
.hmp
= hmp
;
557 hmp
->fchain
.refs
= 1;
558 hmp
->fchain
.data
= (void *)&hmp
->voldata
.freemap_blockset
;
559 hmp
->fchain
.bref
.type
= HAMMER2_BREF_TYPE_FREEMAP
;
560 hmp
->fchain
.bref
.data_off
= 0 | HAMMER2_PBUFRADIX
;
561 hmp
->fchain
.bref
.mirror_tid
= hmp
->voldata
.freemap_tid
;
562 hmp
->fchain
.bref
.methods
=
563 HAMMER2_ENC_CHECK(HAMMER2_CHECK_FREEMAP
) |
564 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE
);
566 hammer2_chain_core_alloc(NULL
, &hmp
->fchain
);
567 /* hmp->fchain.u.xxx is left NULL */
570 * Install the volume header and initialize fields from
573 error
= hammer2_install_volume_header(hmp
);
576 hammer2_vfs_unmount_hmp1(mp
, hmp
);
577 hammer2_vfs_unmount_hmp2(mp
, hmp
);
578 lockmgr(&hammer2_mntlk
, LK_RELEASE
);
579 hammer2_vfs_unmount(mp
, MNT_FORCE
);
584 * Really important to get these right or flush will get
587 hmp
->spmp
= hammer2_pfsalloc(NULL
, hmp
->voldata
.mirror_tid
);
588 kprintf("alloc spmp %p tid %016jx\n",
589 hmp
->spmp
, hmp
->voldata
.mirror_tid
);
594 hmp
->vchain
.bref
.mirror_tid
= hmp
->voldata
.mirror_tid
;
595 hmp
->vchain
.bref
.modify_tid
= hmp
->vchain
.bref
.mirror_tid
;
596 hmp
->vchain
.pmp
= spmp
;
597 hmp
->fchain
.bref
.mirror_tid
= hmp
->voldata
.freemap_tid
;
598 hmp
->fchain
.bref
.modify_tid
= hmp
->fchain
.bref
.mirror_tid
;
599 hmp
->fchain
.pmp
= spmp
;
602 * First locate the super-root inode, which is key 0
603 * relative to the volume header's blockset.
605 * Then locate the root inode by scanning the directory keyspace
606 * represented by the label.
608 parent
= hammer2_chain_lookup_init(&hmp
->vchain
, 0);
609 schain
= hammer2_chain_lookup(&parent
, &key_dummy
,
610 HAMMER2_SROOT_KEY
, HAMMER2_SROOT_KEY
,
611 &cache_index
, 0, &ddflag
);
612 hammer2_chain_lookup_done(parent
);
613 if (schain
== NULL
) {
614 kprintf("hammer2_mount: invalid super-root\n");
616 hammer2_vfs_unmount_hmp1(mp
, hmp
);
617 hammer2_vfs_unmount_hmp2(mp
, hmp
);
618 lockmgr(&hammer2_mntlk
, LK_RELEASE
);
619 hammer2_vfs_unmount(mp
, MNT_FORCE
);
624 * Sanity-check schain's pmp, finish initializing spmp.
626 ripdata
= &hammer2_chain_rdata(schain
)->ipdata
;
627 KKASSERT(schain
->pmp
== spmp
);
628 spmp
->pfs_clid
= ripdata
->pfs_clid
;
631 * NOTE: inode_get sucks up schain's lock.
633 cluster
= hammer2_cluster_from_chain(schain
);
634 spmp
->iroot
= hammer2_inode_get(spmp
, NULL
, cluster
);
635 spmp
->spmp_hmp
= hmp
;
636 hammer2_inode_ref(spmp
->iroot
);
637 hammer2_inode_unlock_ex(spmp
->iroot
, cluster
);
639 /* leave spmp->iroot with one ref */
641 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0) {
642 error
= hammer2_recovery(hmp
);
643 /* XXX do something with error */
647 hammer2_iocom_init(hmp
);
650 * Ref the cluster management messaging descriptor. The mount
651 * program deals with the other end of the communications pipe.
653 fp
= holdfp(curproc
->p_fd
, info
.cluster_fd
, -1);
655 hammer2_cluster_reconnect(hmp
, fp
);
657 kprintf("hammer2_mount: bad cluster_fd!\n");
665 * Lookup mount point under the media-localized super-root.
667 * cluster->pmp will incorrectly point to spmp and must be fixed
670 cparent
= hammer2_inode_lock_ex(spmp
->iroot
);
671 lhc
= hammer2_dirhash(label
, strlen(label
));
672 cluster
= hammer2_cluster_lookup(cparent
, &key_next
,
673 lhc
, lhc
+ HAMMER2_DIRHASH_LOMASK
,
676 if (hammer2_cluster_type(cluster
) == HAMMER2_BREF_TYPE_INODE
&&
678 hammer2_cluster_rdata(cluster
)->ipdata
.filename
) == 0) {
681 cluster
= hammer2_cluster_next(cparent
, cluster
, &key_next
,
683 lhc
+ HAMMER2_DIRHASH_LOMASK
, 0);
685 hammer2_inode_unlock_ex(spmp
->iroot
, cparent
);
687 if (cluster
== NULL
) {
688 kprintf("hammer2_mount: PFS label not found\n");
689 hammer2_vfs_unmount_hmp1(mp
, hmp
);
690 hammer2_vfs_unmount_hmp2(mp
, hmp
);
691 lockmgr(&hammer2_mntlk
, LK_RELEASE
);
692 hammer2_vfs_unmount(mp
, MNT_FORCE
);
696 for (i
= 0; i
< cluster
->nchains
; ++i
) {
697 rchain
= cluster
->array
[i
];
698 if (rchain
->flags
& HAMMER2_CHAIN_MOUNTED
) {
699 kprintf("hammer2_mount: PFS label already mounted!\n");
700 hammer2_cluster_unlock(cluster
);
701 hammer2_vfs_unmount_hmp1(mp
, hmp
);
702 hammer2_vfs_unmount_hmp2(mp
, hmp
);
703 lockmgr(&hammer2_mntlk
, LK_RELEASE
);
704 hammer2_vfs_unmount(mp
, MNT_FORCE
);
707 KKASSERT(rchain
->pmp
== NULL
);
709 if (rchain
->flags
& HAMMER2_CHAIN_RECYCLE
) {
710 kprintf("hammer2_mount: PFS label is recycling\n");
711 hammer2_cluster_unlock(cluster
);
712 hammer2_vfs_unmount_hmp1(mp
, hmp
);
713 hammer2_vfs_unmount_hmp2(mp
, hmp
);
714 lockmgr(&hammer2_mntlk
, LK_RELEASE
);
715 hammer2_vfs_unmount(mp
, MNT_FORCE
);
722 * Check to see if the cluster id is already mounted at the mount
723 * point. If it is, add us to the cluster.
725 ripdata
= &hammer2_cluster_rdata(cluster
)->ipdata
;
726 hammer2_cluster_bref(cluster
, &bref
);
727 TAILQ_FOREACH(pmp
, &hammer2_pfslist
, mntentry
) {
728 if (pmp
->spmp_hmp
== NULL
&&
729 bcmp(&pmp
->pfs_clid
, &ripdata
->pfs_clid
,
730 sizeof(pmp
->pfs_clid
)) == 0) {
739 hammer2_inode_ref(pmp
->iroot
);
740 ccms_thread_lock(&pmp
->iroot
->topo_cst
, CCMS_STATE_EXCLUSIVE
);
742 if (pmp
->iroot
->cluster
.nchains
+ cluster
->nchains
>
743 HAMMER2_MAXCLUSTER
) {
744 kprintf("hammer2_mount: cluster full!\n");
746 ccms_thread_unlock(&pmp
->iroot
->topo_cst
);
747 hammer2_inode_drop(pmp
->iroot
);
749 hammer2_cluster_unlock(cluster
);
750 hammer2_vfs_unmount_hmp1(mp
, hmp
);
751 hammer2_vfs_unmount_hmp2(mp
, hmp
);
752 lockmgr(&hammer2_mntlk
, LK_RELEASE
);
753 hammer2_vfs_unmount(mp
, MNT_FORCE
);
756 kprintf("hammer2_vfs_mount: Adding pfs to existing cluster\n");
757 j
= pmp
->iroot
->cluster
.nchains
;
758 for (i
= 0; i
< cluster
->nchains
; ++i
) {
759 rchain
= cluster
->array
[i
];
760 KKASSERT(rchain
->pmp
== NULL
);
762 hammer2_chain_ref(cluster
->array
[i
]);
763 pmp
->iroot
->cluster
.array
[j
] = cluster
->array
[i
];
766 pmp
->iroot
->cluster
.nchains
= j
;
767 ccms_thread_unlock(&pmp
->iroot
->topo_cst
);
768 hammer2_inode_drop(pmp
->iroot
);
769 hammer2_cluster_unlock(cluster
);
770 lockmgr(&hammer2_mntlk
, LK_RELEASE
);
773 hammer2_inode_install_hidden(pmp
);
779 * Block device opened successfully, finish initializing the
782 * From this point on we have to call hammer2_unmount() on failure.
784 pmp
= hammer2_pfsalloc(ripdata
, bref
.mirror_tid
);
785 kprintf("PMP mirror_tid is %016jx\n", bref
.mirror_tid
);
786 for (i
= 0; i
< cluster
->nchains
; ++i
) {
787 rchain
= cluster
->array
[i
];
788 KKASSERT(rchain
->pmp
== NULL
);
790 atomic_set_int(&rchain
->flags
, HAMMER2_CHAIN_MOUNTED
);
794 ccms_domain_init(&pmp
->ccms_dom
);
795 TAILQ_INSERT_TAIL(&hammer2_pfslist
, pmp
, mntentry
);
796 lockmgr(&hammer2_mntlk
, LK_RELEASE
);
798 kprintf("hammer2_mount hmp=%p pmp=%p pmpcnt=%d\n",
799 hmp
, pmp
, hmp
->pmp_count
);
801 mp
->mnt_flag
= MNT_LOCAL
;
802 mp
->mnt_kern_flag
|= MNTK_ALL_MPSAFE
; /* all entry pts are SMP */
803 mp
->mnt_kern_flag
|= MNTK_THR_SYNC
; /* new vsyncscan semantics */
806 * required mount structure initializations
808 mp
->mnt_stat
.f_iosize
= HAMMER2_PBUFSIZE
;
809 mp
->mnt_stat
.f_bsize
= HAMMER2_PBUFSIZE
;
811 mp
->mnt_vstat
.f_frsize
= HAMMER2_PBUFSIZE
;
812 mp
->mnt_vstat
.f_bsize
= HAMMER2_PBUFSIZE
;
817 mp
->mnt_iosize_max
= MAXPHYS
;
818 mp
->mnt_data
= (qaddr_t
)pmp
;
822 * After this point hammer2_vfs_unmount() has visibility on hmp
823 * and manual hmp1/hmp2 calls are not needed on fatal errors.
825 pmp
->iroot
= hammer2_inode_get(pmp
, NULL
, cluster
);
826 hammer2_inode_ref(pmp
->iroot
); /* ref for pmp->iroot */
827 hammer2_inode_unlock_ex(pmp
->iroot
, cluster
);
830 * The logical file buffer bio write thread handles things
831 * like physical block assignment and compression.
833 * (only applicable to pfs mounts, not applicable to spmp)
835 pmp
->wthread_destroy
= 0;
836 lwkt_create(hammer2_write_thread
, pmp
,
837 &pmp
->wthread_td
, NULL
, 0, -1, "hwrite-%s", label
);
840 * With the cluster operational install ihidden.
841 * (only applicable to pfs mounts, not applicable to spmp)
843 hammer2_inode_install_hidden(pmp
);
849 vfs_add_vnodeops(mp
, &hammer2_vnode_vops
, &mp
->mnt_vn_norm_ops
);
850 vfs_add_vnodeops(mp
, &hammer2_spec_vops
, &mp
->mnt_vn_spec_ops
);
851 vfs_add_vnodeops(mp
, &hammer2_fifo_vops
, &mp
->mnt_vn_fifo_ops
);
853 copyinstr(info
.volume
, mp
->mnt_stat
.f_mntfromname
, MNAMELEN
- 1, &size
);
854 bzero(mp
->mnt_stat
.f_mntfromname
+ size
, MNAMELEN
- size
);
855 bzero(mp
->mnt_stat
.f_mntonname
, sizeof(mp
->mnt_stat
.f_mntonname
));
856 copyinstr(path
, mp
->mnt_stat
.f_mntonname
,
857 sizeof(mp
->mnt_stat
.f_mntonname
) - 1,
861 * Initial statfs to prime mnt_stat.
863 hammer2_vfs_statfs(mp
, &mp
->mnt_stat
, cred
);
869 * Handle bioq for strategy write
873 hammer2_write_thread(void *arg
)
875 hammer2_pfsmount_t
*pmp
;
878 hammer2_trans_t trans
;
881 hammer2_cluster_t
*cparent
;
882 hammer2_inode_data_t
*wipdata
;
890 mtx_lock(&pmp
->wthread_mtx
);
891 while (pmp
->wthread_destroy
== 0) {
892 if (bioq_first(&pmp
->wthread_bioq
) == NULL
) {
893 mtxsleep(&pmp
->wthread_bioq
, &pmp
->wthread_mtx
,
898 hammer2_trans_init(&trans
, pmp
, HAMMER2_TRANS_BUFCACHE
);
900 while ((bio
= bioq_takefirst(&pmp
->wthread_bioq
)) != NULL
) {
902 * dummy bio for synchronization. The transaction
903 * must be reinitialized.
905 if (bio
->bio_buf
== NULL
) {
906 bio
->bio_flags
|= BIO_DONE
;
908 hammer2_trans_done(&trans
);
909 hammer2_trans_init(&trans
, pmp
,
910 HAMMER2_TRANS_BUFCACHE
);
915 * else normal bio processing
917 mtx_unlock(&pmp
->wthread_mtx
);
919 hammer2_lwinprog_drop(pmp
);
927 * Inode is modified, flush size and mtime changes
928 * to ensure that the file size remains consistent
929 * with the buffers being flushed.
931 * NOTE: The inode_fsync() call only flushes the
932 * inode's meta-data state, it doesn't try
933 * to flush underlying buffers or chains.
935 cparent
= hammer2_inode_lock_ex(ip
);
936 if (ip
->flags
& (HAMMER2_INODE_RESIZED
|
937 HAMMER2_INODE_MTIME
)) {
938 hammer2_inode_fsync(&trans
, ip
, cparent
);
940 wipdata
= hammer2_cluster_modify_ip(&trans
, ip
,
942 lblksize
= hammer2_calc_logical(ip
, bio
->bio_offset
,
944 pblksize
= hammer2_calc_physical(ip
, wipdata
, lbase
);
945 hammer2_write_file_core(bp
, &trans
, ip
, wipdata
,
949 hammer2_cluster_modsync(cparent
);
950 hammer2_inode_unlock_ex(ip
, cparent
);
952 kprintf("hammer2: error in buffer write\n");
953 bp
->b_flags
|= B_ERROR
;
957 mtx_lock(&pmp
->wthread_mtx
);
959 hammer2_trans_done(&trans
);
961 pmp
->wthread_destroy
= -1;
962 wakeup(&pmp
->wthread_destroy
);
964 mtx_unlock(&pmp
->wthread_mtx
);
968 hammer2_bioq_sync(hammer2_pfsmount_t
*pmp
)
972 bzero(&sync_bio
, sizeof(sync_bio
)); /* dummy with no bio_buf */
973 mtx_lock(&pmp
->wthread_mtx
);
974 if (pmp
->wthread_destroy
== 0 &&
975 TAILQ_FIRST(&pmp
->wthread_bioq
.queue
)) {
976 bioq_insert_tail(&pmp
->wthread_bioq
, &sync_bio
);
977 while ((sync_bio
.bio_flags
& BIO_DONE
) == 0)
978 mtxsleep(&sync_bio
, &pmp
->wthread_mtx
, 0, "h2bioq", 0);
980 mtx_unlock(&pmp
->wthread_mtx
);
984 * Return a chain suitable for I/O, creating the chain if necessary
985 * and assigning its physical block.
989 hammer2_assign_physical(hammer2_trans_t
*trans
,
990 hammer2_inode_t
*ip
, hammer2_cluster_t
*cparent
,
991 hammer2_key_t lbase
, int pblksize
, int *errorp
)
993 hammer2_cluster_t
*cluster
;
994 hammer2_cluster_t
*dparent
;
995 hammer2_key_t key_dummy
;
996 int pradix
= hammer2_getradix(pblksize
);
1000 * Locate the chain associated with lbase, return a locked chain.
1001 * However, do not instantiate any data reference (which utilizes a
1002 * device buffer) because we will be using direct IO via the
1003 * logical buffer cache buffer.
1006 KKASSERT(pblksize
>= HAMMER2_ALLOC_MIN
);
1008 dparent
= hammer2_cluster_lookup_init(cparent
, 0);
1009 cluster
= hammer2_cluster_lookup(dparent
, &key_dummy
,
1011 HAMMER2_LOOKUP_NODATA
, &ddflag
);
1013 if (cluster
== NULL
) {
1015 * We found a hole, create a new chain entry.
1017 * NOTE: DATA chains are created without device backing
1018 * store (nor do we want any).
1020 *errorp
= hammer2_cluster_create(trans
, dparent
, &cluster
,
1021 lbase
, HAMMER2_PBUFRADIX
,
1022 HAMMER2_BREF_TYPE_DATA
,
1024 if (cluster
== NULL
) {
1025 hammer2_cluster_lookup_done(dparent
);
1026 panic("hammer2_cluster_create: par=%p error=%d\n",
1027 dparent
->focus
, *errorp
);
1030 /*ip->delta_dcount += pblksize;*/
1032 switch (hammer2_cluster_type(cluster
)) {
1033 case HAMMER2_BREF_TYPE_INODE
:
1035 * The data is embedded in the inode. The
1036 * caller is responsible for marking the inode
1037 * modified and copying the data to the embedded
1041 case HAMMER2_BREF_TYPE_DATA
:
1042 if (hammer2_cluster_need_resize(cluster
, pblksize
)) {
1043 hammer2_cluster_resize(trans
, ip
,
1046 HAMMER2_MODIFY_OPTDATA
);
1050 * DATA buffers must be marked modified whether the
1051 * data is in a logical buffer or not. We also have
1052 * to make this call to fixup the chain data pointers
1053 * after resizing in case this is an encrypted or
1054 * compressed buffer.
1056 hammer2_cluster_modify(trans
, cluster
,
1057 HAMMER2_MODIFY_OPTDATA
);
1060 panic("hammer2_assign_physical: bad type");
1067 * Cleanup. If cluster wound up being the inode itself, i.e.
1068 * the DIRECTDATA case for offset 0, then we need to update cparent.
1069 * The caller expects cparent to not become stale.
1071 hammer2_cluster_lookup_done(dparent
);
1072 /* dparent = NULL; safety */
1073 if (cluster
&& ddflag
)
1074 hammer2_cluster_replace_locked(cparent
, cluster
);
1079 * bio queued from hammer2_vnops.c.
1081 * The core write function which determines which path to take
1082 * depending on compression settings. We also have to locate the
1083 * related clusters so we can calculate and set the check data for
1088 hammer2_write_file_core(struct buf
*bp
, hammer2_trans_t
*trans
,
1089 hammer2_inode_t
*ip
,
1090 const hammer2_inode_data_t
*ripdata
,
1091 hammer2_cluster_t
*cparent
,
1092 hammer2_key_t lbase
, int ioflag
, int pblksize
,
1095 hammer2_cluster_t
*cluster
;
1097 switch(HAMMER2_DEC_ALGO(ripdata
->comp_algo
)) {
1098 case HAMMER2_COMP_NONE
:
1100 * We have to assign physical storage to the buffer
1101 * we intend to dirty or write now to avoid deadlocks
1102 * in the strategy code later.
1104 * This can return NOOFFSET for inode-embedded data.
1105 * The strategy code will take care of it in that case.
1107 cluster
= hammer2_assign_physical(trans
, ip
, cparent
,
1110 hammer2_write_bp(cluster
, bp
, ioflag
, pblksize
, errorp
,
1111 ripdata
->check_algo
);
1113 hammer2_cluster_unlock(cluster
);
1115 case HAMMER2_COMP_AUTOZERO
:
1117 * Check for zero-fill only
1119 hammer2_zero_check_and_write(bp
, trans
, ip
,
1120 ripdata
, cparent
, lbase
,
1121 ioflag
, pblksize
, errorp
,
1122 ripdata
->check_algo
);
1124 case HAMMER2_COMP_LZ4
:
1125 case HAMMER2_COMP_ZLIB
:
1128 * Check for zero-fill and attempt compression.
1130 hammer2_compress_and_write(bp
, trans
, ip
,
1135 ripdata
->check_algo
);
1141 * Generic function that will perform the compression in compression
1142 * write path. The compression algorithm is determined by the settings
1143 * obtained from inode.
1147 hammer2_compress_and_write(struct buf
*bp
, hammer2_trans_t
*trans
,
1148 hammer2_inode_t
*ip
, const hammer2_inode_data_t
*ripdata
,
1149 hammer2_cluster_t
*cparent
,
1150 hammer2_key_t lbase
, int ioflag
, int pblksize
,
1151 int *errorp
, int comp_algo
, int check_algo
)
1153 hammer2_cluster_t
*cluster
;
1154 hammer2_chain_t
*chain
;
1156 int comp_block_size
;
1160 if (test_block_zeros(bp
->b_data
, pblksize
)) {
1161 zero_write(bp
, trans
, ip
, ripdata
, cparent
, lbase
, errorp
);
1168 KKASSERT(pblksize
/ 2 <= 32768);
1170 if (ip
->comp_heuristic
< 8 || (ip
->comp_heuristic
& 7) == 0) {
1171 z_stream strm_compress
;
1175 switch(HAMMER2_DEC_ALGO(comp_algo
)) {
1176 case HAMMER2_COMP_LZ4
:
1177 comp_buffer
= objcache_get(cache_buffer_write
,
1179 comp_size
= LZ4_compress_limitedOutput(
1181 &comp_buffer
[sizeof(int)],
1183 pblksize
/ 2 - sizeof(int));
1185 * We need to prefix with the size, LZ4
1186 * doesn't do it for us. Add the related
1189 *(int *)comp_buffer
= comp_size
;
1191 comp_size
+= sizeof(int);
1193 case HAMMER2_COMP_ZLIB
:
1194 comp_level
= HAMMER2_DEC_LEVEL(comp_algo
);
1195 if (comp_level
== 0)
1196 comp_level
= 6; /* default zlib compression */
1197 else if (comp_level
< 6)
1199 else if (comp_level
> 9)
1201 ret
= deflateInit(&strm_compress
, comp_level
);
1203 kprintf("HAMMER2 ZLIB: fatal error "
1204 "on deflateInit.\n");
1207 comp_buffer
= objcache_get(cache_buffer_write
,
1209 strm_compress
.next_in
= bp
->b_data
;
1210 strm_compress
.avail_in
= pblksize
;
1211 strm_compress
.next_out
= comp_buffer
;
1212 strm_compress
.avail_out
= pblksize
/ 2;
1213 ret
= deflate(&strm_compress
, Z_FINISH
);
1214 if (ret
== Z_STREAM_END
) {
1215 comp_size
= pblksize
/ 2 -
1216 strm_compress
.avail_out
;
1220 ret
= deflateEnd(&strm_compress
);
1223 kprintf("Error: Unknown compression method.\n");
1224 kprintf("Comp_method = %d.\n", comp_algo
);
1229 if (comp_size
== 0) {
1231 * compression failed or turned off
1233 comp_block_size
= pblksize
; /* safety */
1234 if (++ip
->comp_heuristic
> 128)
1235 ip
->comp_heuristic
= 8;
1238 * compression succeeded
1240 ip
->comp_heuristic
= 0;
1241 if (comp_size
<= 1024) {
1242 comp_block_size
= 1024;
1243 } else if (comp_size
<= 2048) {
1244 comp_block_size
= 2048;
1245 } else if (comp_size
<= 4096) {
1246 comp_block_size
= 4096;
1247 } else if (comp_size
<= 8192) {
1248 comp_block_size
= 8192;
1249 } else if (comp_size
<= 16384) {
1250 comp_block_size
= 16384;
1251 } else if (comp_size
<= 32768) {
1252 comp_block_size
= 32768;
1254 panic("hammer2: WRITE PATH: "
1255 "Weird comp_size value.");
1257 comp_block_size
= pblksize
;
1261 cluster
= hammer2_assign_physical(trans
, ip
, cparent
,
1262 lbase
, comp_block_size
,
1267 kprintf("WRITE PATH: An error occurred while "
1268 "assigning physical space.\n");
1269 KKASSERT(cluster
== NULL
);
1273 for (i
= 0; i
< cluster
->nchains
; ++i
) {
1274 hammer2_inode_data_t
*wipdata
;
1278 chain
= cluster
->array
[i
]; /* XXX */
1279 KKASSERT(chain
->flags
& HAMMER2_CHAIN_MODIFIED
);
1281 switch(chain
->bref
.type
) {
1282 case HAMMER2_BREF_TYPE_INODE
:
1283 wipdata
= &hammer2_chain_wdata(chain
)->ipdata
;
1284 KKASSERT(wipdata
->op_flags
& HAMMER2_OPFLAG_DIRECTDATA
);
1285 KKASSERT(bp
->b_loffset
== 0);
1286 bcopy(bp
->b_data
, wipdata
->u
.data
,
1287 HAMMER2_EMBEDDED_BYTES
);
1289 case HAMMER2_BREF_TYPE_DATA
:
1291 * Optimize out the read-before-write
1294 *errorp
= hammer2_io_newnz(chain
->hmp
,
1295 chain
->bref
.data_off
,
1299 hammer2_io_brelse(&dio
);
1300 kprintf("hammer2: WRITE PATH: "
1301 "dbp bread error\n");
1304 bdata
= hammer2_io_data(dio
, chain
->bref
.data_off
);
1307 * When loading the block make sure we don't
1308 * leave garbage after the compressed data.
1311 chain
->bref
.methods
=
1312 HAMMER2_ENC_COMP(comp_algo
) +
1313 HAMMER2_ENC_CHECK(check_algo
);
1314 bcopy(comp_buffer
, bdata
, comp_size
);
1315 if (comp_size
!= comp_block_size
) {
1316 bzero(bdata
+ comp_size
,
1317 comp_block_size
- comp_size
);
1320 chain
->bref
.methods
=
1322 HAMMER2_COMP_NONE
) +
1323 HAMMER2_ENC_CHECK(check_algo
);
1324 bcopy(bp
->b_data
, bdata
, pblksize
);
1328 * The flush code doesn't calculate check codes for
1329 * file data (doing so can result in excessive I/O),
1332 hammer2_chain_setcheck(chain
, bdata
);
1335 * Device buffer is now valid, chain is no longer in
1336 * the initial state.
1338 * (No blockref table worries with file data)
1340 atomic_clear_int(&chain
->flags
, HAMMER2_CHAIN_INITIAL
);
1342 /* Now write the related bdp. */
1343 if (ioflag
& IO_SYNC
) {
1345 * Synchronous I/O requested.
1347 hammer2_io_bwrite(&dio
);
1349 } else if ((ioflag & IO_DIRECT) &&
1350 loff + n == pblksize) {
1351 hammer2_io_bdwrite(&dio);
1353 } else if (ioflag
& IO_ASYNC
) {
1354 hammer2_io_bawrite(&dio
);
1356 hammer2_io_bdwrite(&dio
);
1360 panic("hammer2_write_bp: bad chain type %d\n",
1368 hammer2_cluster_unlock(cluster
);
1370 objcache_put(cache_buffer_write
, comp_buffer
);
1374 * Function that performs zero-checking and writing without compression,
1375 * it corresponds to default zero-checking path.
1379 hammer2_zero_check_and_write(struct buf
*bp
, hammer2_trans_t
*trans
,
1380 hammer2_inode_t
*ip
, const hammer2_inode_data_t
*ripdata
,
1381 hammer2_cluster_t
*cparent
,
1382 hammer2_key_t lbase
, int ioflag
, int pblksize
, int *errorp
,
1385 hammer2_cluster_t
*cluster
;
1387 if (test_block_zeros(bp
->b_data
, pblksize
)) {
1388 zero_write(bp
, trans
, ip
, ripdata
, cparent
, lbase
, errorp
);
1390 cluster
= hammer2_assign_physical(trans
, ip
, cparent
,
1391 lbase
, pblksize
, errorp
);
1392 hammer2_write_bp(cluster
, bp
, ioflag
, pblksize
, errorp
,
1395 hammer2_cluster_unlock(cluster
);
1400 * A function to test whether a block of data contains only zeros,
1401 * returns TRUE (non-zero) if the block is all zeros.
1405 test_block_zeros(const char *buf
, size_t bytes
)
1409 for (i
= 0; i
< bytes
; i
+= sizeof(long)) {
1410 if (*(const long *)(buf
+ i
) != 0)
1417 * Function to "write" a block that contains only zeros.
1421 zero_write(struct buf
*bp
, hammer2_trans_t
*trans
,
1422 hammer2_inode_t
*ip
, const hammer2_inode_data_t
*ripdata
,
1423 hammer2_cluster_t
*cparent
,
1424 hammer2_key_t lbase
, int *errorp __unused
)
1426 hammer2_cluster_t
*cluster
;
1427 hammer2_media_data_t
*data
;
1428 hammer2_key_t key_dummy
;
1431 cparent
= hammer2_cluster_lookup_init(cparent
, 0);
1432 cluster
= hammer2_cluster_lookup(cparent
, &key_dummy
, lbase
, lbase
,
1433 HAMMER2_LOOKUP_NODATA
, &ddflag
);
1435 data
= hammer2_cluster_wdata(cluster
);
1438 KKASSERT(cluster
->focus
->flags
&
1439 HAMMER2_CHAIN_MODIFIED
);
1440 bzero(data
->ipdata
.u
.data
, HAMMER2_EMBEDDED_BYTES
);
1441 hammer2_cluster_modsync(cluster
);
1443 hammer2_cluster_delete(trans
, cparent
, cluster
,
1444 HAMMER2_DELETE_PERMANENT
);
1446 hammer2_cluster_unlock(cluster
);
1448 hammer2_cluster_lookup_done(cparent
);
1452 * Function to write the data as it is, without performing any sort of
1453 * compression. This function is used in path without compression and
1454 * default zero-checking path.
1458 hammer2_write_bp(hammer2_cluster_t
*cluster
, struct buf
*bp
, int ioflag
,
1459 int pblksize
, int *errorp
, int check_algo
)
1461 hammer2_chain_t
*chain
;
1462 hammer2_inode_data_t
*wipdata
;
1468 error
= 0; /* XXX TODO below */
1470 for (i
= 0; i
< cluster
->nchains
; ++i
) {
1471 chain
= cluster
->array
[i
]; /* XXX */
1472 KKASSERT(chain
->flags
& HAMMER2_CHAIN_MODIFIED
);
1474 switch(chain
->bref
.type
) {
1475 case HAMMER2_BREF_TYPE_INODE
:
1476 wipdata
= &hammer2_chain_wdata(chain
)->ipdata
;
1477 KKASSERT(wipdata
->op_flags
& HAMMER2_OPFLAG_DIRECTDATA
);
1478 KKASSERT(bp
->b_loffset
== 0);
1479 bcopy(bp
->b_data
, wipdata
->u
.data
,
1480 HAMMER2_EMBEDDED_BYTES
);
1483 case HAMMER2_BREF_TYPE_DATA
:
1484 error
= hammer2_io_newnz(chain
->hmp
,
1485 chain
->bref
.data_off
,
1486 chain
->bytes
, &dio
);
1488 hammer2_io_bqrelse(&dio
);
1489 kprintf("hammer2: WRITE PATH: "
1490 "dbp bread error\n");
1493 bdata
= hammer2_io_data(dio
, chain
->bref
.data_off
);
1495 chain
->bref
.methods
= HAMMER2_ENC_COMP(
1496 HAMMER2_COMP_NONE
) +
1497 HAMMER2_ENC_CHECK(check_algo
);
1498 bcopy(bp
->b_data
, bdata
, chain
->bytes
);
1501 * The flush code doesn't calculate check codes for
1502 * file data (doing so can result in excessive I/O),
1505 hammer2_chain_setcheck(chain
, bdata
);
1508 * Device buffer is now valid, chain is no longer in
1509 * the initial state.
1511 * (No blockref table worries with file data)
1513 atomic_clear_int(&chain
->flags
, HAMMER2_CHAIN_INITIAL
);
1515 if (ioflag
& IO_SYNC
) {
1517 * Synchronous I/O requested.
1519 hammer2_io_bwrite(&dio
);
1521 } else if ((ioflag & IO_DIRECT) &&
1522 loff + n == pblksize) {
1523 hammer2_io_bdwrite(&dio);
1525 } else if (ioflag
& IO_ASYNC
) {
1526 hammer2_io_bawrite(&dio
);
1528 hammer2_io_bdwrite(&dio
);
1532 panic("hammer2_write_bp: bad chain type %d\n",
1538 KKASSERT(error
== 0); /* XXX TODO */
1545 hammer2_remount(hammer2_mount_t
*hmp
, struct mount
*mp
, char *path
,
1546 struct vnode
*devvp
, struct ucred
*cred
)
1550 if (hmp
->ronly
&& (mp
->mnt_kern_flag
& MNTK_WANTRDWR
)) {
1551 error
= hammer2_recovery(hmp
);
1560 hammer2_vfs_unmount(struct mount
*mp
, int mntflags
)
1562 hammer2_pfsmount_t
*pmp
;
1563 hammer2_mount_t
*hmp
;
1564 hammer2_chain_t
*rchain
;
1565 hammer2_cluster_t
*cluster
;
1575 lockmgr(&hammer2_mntlk
, LK_EXCLUSIVE
);
1576 TAILQ_REMOVE(&hammer2_pfslist
, pmp
, mntentry
);
1579 * If mount initialization proceeded far enough we must flush
1582 if (mntflags
& MNT_FORCE
)
1587 error
= vflush(mp
, 0, flags
);
1592 ccms_domain_uninit(&pmp
->ccms_dom
);
1594 if (pmp
->wthread_td
) {
1595 mtx_lock(&pmp
->wthread_mtx
);
1596 pmp
->wthread_destroy
= 1;
1597 wakeup(&pmp
->wthread_bioq
);
1598 while (pmp
->wthread_destroy
!= -1) {
1599 mtxsleep(&pmp
->wthread_destroy
,
1600 &pmp
->wthread_mtx
, 0,
1603 mtx_unlock(&pmp
->wthread_mtx
);
1604 pmp
->wthread_td
= NULL
;
1608 * Cleanup our reference on ihidden.
1611 hammer2_inode_drop(pmp
->ihidden
);
1612 pmp
->ihidden
= NULL
;
1616 * Cleanup our reference on iroot. iroot is (should) not be needed
1617 * by the flush code.
1620 cluster
= &pmp
->iroot
->cluster
;
1621 for (i
= 0; i
< pmp
->iroot
->cluster
.nchains
; ++i
) {
1622 rchain
= pmp
->iroot
->cluster
.array
[i
];
1626 hammer2_vfs_unmount_hmp1(mp
, hmp
);
1628 atomic_clear_int(&rchain
->flags
, HAMMER2_CHAIN_MOUNTED
);
1629 #if REPORT_REFS_ERRORS
1630 if (rchain
->refs
!= 1)
1631 kprintf("PMP->RCHAIN %p REFS WRONG %d\n",
1632 rchain
, rchain
->refs
);
1634 KKASSERT(rchain
->refs
== 1);
1636 hammer2_chain_drop(rchain
);
1637 cluster
->array
[i
] = NULL
;
1638 hammer2_vfs_unmount_hmp2(mp
, hmp
);
1640 cluster
->focus
= NULL
;
1642 #if REPORT_REFS_ERRORS
1643 if (pmp
->iroot
->refs
!= 1)
1644 kprintf("PMP->IROOT %p REFS WRONG %d\n",
1645 pmp
->iroot
, pmp
->iroot
->refs
);
1647 KKASSERT(pmp
->iroot
->refs
== 1);
1649 /* ref for pmp->iroot */
1650 hammer2_inode_drop(pmp
->iroot
);
1655 mp
->mnt_data
= NULL
;
1657 kmalloc_destroy(&pmp
->mmsg
);
1658 kmalloc_destroy(&pmp
->minode
);
1660 kfree(pmp
, M_HAMMER2
);
1664 lockmgr(&hammer2_mntlk
, LK_RELEASE
);
1671 hammer2_vfs_unmount_hmp1(struct mount
*mp
, hammer2_mount_t
*hmp
)
1673 hammer2_mount_exlock(hmp
);
1676 kprintf("hammer2_unmount hmp=%p pmpcnt=%d\n", hmp
, hmp
->pmp_count
);
1679 * Cycle the volume data lock as a safety (probably not needed any
1680 * more). To ensure everything is out we need to flush at least
1681 * three times. (1) The running of the unlinkq can dirty the
1682 * filesystem, (2) A normal flush can dirty the freemap, and
1683 * (3) ensure that the freemap is fully synchronized.
1685 * The next mount's recovery scan can clean everything up but we want
1686 * to leave the filesystem in a 100% clean state on a normal unmount.
1688 hammer2_voldata_lock(hmp
);
1689 hammer2_voldata_unlock(hmp
);
1691 hammer2_vfs_sync(mp
, MNT_WAIT
);
1692 hammer2_vfs_sync(mp
, MNT_WAIT
);
1693 hammer2_vfs_sync(mp
, MNT_WAIT
);
1697 * XXX chain depend deadlock?
1699 hammer2_iocom_uninit(hmp
);
1701 if (hmp
->pmp_count
== 0) {
1702 if ((hmp
->vchain
.flags
| hmp
->fchain
.flags
) &
1703 HAMMER2_CHAIN_FLUSH_MASK
) {
1704 kprintf("hammer2_unmount: chains left over "
1705 "after final sync\n");
1706 kprintf(" vchain %08x\n", hmp
->vchain
.flags
);
1707 kprintf(" fchain %08x\n", hmp
->fchain
.flags
);
1709 if (hammer2_debug
& 0x0010)
1710 Debugger("entered debugger");
1717 hammer2_vfs_unmount_hmp2(struct mount
*mp
, hammer2_mount_t
*hmp
)
1719 hammer2_pfsmount_t
*spmp
;
1720 struct vnode
*devvp
;
1722 int ronly
= ((mp
->mnt_flag
& MNT_RDONLY
) != 0);
1725 * If no PFS's left drop the master hammer2_mount for the
1728 if (hmp
->pmp_count
== 0) {
1730 * Clean up SPMP and the super-root inode
1735 hammer2_inode_drop(spmp
->iroot
);
1739 kmalloc_destroy(&spmp
->mmsg
);
1740 kmalloc_destroy(&spmp
->minode
);
1741 kfree(spmp
, M_HAMMER2
);
1745 * Finish up with the device vnode
1747 if ((devvp
= hmp
->devvp
) != NULL
) {
1748 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
);
1749 vinvalbuf(devvp
, (ronly
? 0 : V_SAVE
), 0, 0);
1751 VOP_CLOSE(devvp
, (ronly
? FREAD
: FREAD
|FWRITE
), NULL
);
1758 * Clear vchain/fchain flags that might prevent final cleanup
1761 if (hmp
->vchain
.flags
& HAMMER2_CHAIN_MODIFIED
) {
1762 atomic_clear_int(&hmp
->vchain
.flags
,
1763 HAMMER2_CHAIN_MODIFIED
);
1764 hammer2_pfs_memory_wakeup(hmp
->vchain
.pmp
);
1765 hammer2_chain_drop(&hmp
->vchain
);
1767 if (hmp
->vchain
.flags
& HAMMER2_CHAIN_UPDATE
) {
1768 atomic_clear_int(&hmp
->vchain
.flags
,
1769 HAMMER2_CHAIN_UPDATE
);
1770 hammer2_chain_drop(&hmp
->vchain
);
1773 if (hmp
->fchain
.flags
& HAMMER2_CHAIN_MODIFIED
) {
1774 atomic_clear_int(&hmp
->fchain
.flags
,
1775 HAMMER2_CHAIN_MODIFIED
);
1776 hammer2_pfs_memory_wakeup(hmp
->fchain
.pmp
);
1777 hammer2_chain_drop(&hmp
->fchain
);
1779 if (hmp
->fchain
.flags
& HAMMER2_CHAIN_UPDATE
) {
1780 atomic_clear_int(&hmp
->fchain
.flags
,
1781 HAMMER2_CHAIN_UPDATE
);
1782 hammer2_chain_drop(&hmp
->fchain
);
1786 * Final drop of embedded freemap root chain to
1787 * clean up fchain.core (fchain structure is not
1788 * flagged ALLOCATED so it is cleaned out and then
1791 hammer2_chain_drop(&hmp
->fchain
);
1794 * Final drop of embedded volume root chain to clean
1795 * up vchain.core (vchain structure is not flagged
1796 * ALLOCATED so it is cleaned out and then left to
1800 hammer2_dump_chain(&hmp
->vchain
, 0, &dumpcnt
, 'v');
1802 hammer2_dump_chain(&hmp
->fchain
, 0, &dumpcnt
, 'f');
1803 hammer2_mount_unlock(hmp
);
1804 hammer2_chain_drop(&hmp
->vchain
);
1806 hammer2_io_cleanup(hmp
, &hmp
->iotree
);
1807 if (hmp
->iofree_count
) {
1808 kprintf("io_cleanup: %d I/O's left hanging\n",
1812 TAILQ_REMOVE(&hammer2_mntlist
, hmp
, mntentry
);
1813 kmalloc_destroy(&hmp
->mchain
);
1814 kfree(hmp
, M_HAMMER2
);
1816 hammer2_mount_unlock(hmp
);
1822 hammer2_vfs_vget(struct mount
*mp
, struct vnode
*dvp
,
1823 ino_t ino
, struct vnode
**vpp
)
1825 kprintf("hammer2_vget\n");
1826 return (EOPNOTSUPP
);
1831 hammer2_vfs_root(struct mount
*mp
, struct vnode
**vpp
)
1833 hammer2_pfsmount_t
*pmp
;
1834 hammer2_cluster_t
*cparent
;
1839 if (pmp
->iroot
== NULL
) {
1843 cparent
= hammer2_inode_lock_sh(pmp
->iroot
);
1844 vp
= hammer2_igetv(pmp
->iroot
, cparent
, &error
);
1845 hammer2_inode_unlock_sh(pmp
->iroot
, cparent
);
1848 kprintf("vnodefail\n");
1857 * XXX incorporate ipdata->inode_quota and data_quota
1861 hammer2_vfs_statfs(struct mount
*mp
, struct statfs
*sbp
, struct ucred
*cred
)
1863 hammer2_pfsmount_t
*pmp
;
1864 hammer2_mount_t
*hmp
;
1867 KKASSERT(pmp
->iroot
->cluster
.nchains
>= 1);
1868 hmp
= pmp
->iroot
->cluster
.focus
->hmp
; /* XXX */
1870 mp
->mnt_stat
.f_files
= pmp
->inode_count
;
1871 mp
->mnt_stat
.f_ffree
= 0;
1872 mp
->mnt_stat
.f_blocks
= hmp
->voldata
.allocator_size
/ HAMMER2_PBUFSIZE
;
1873 mp
->mnt_stat
.f_bfree
= hmp
->voldata
.allocator_free
/ HAMMER2_PBUFSIZE
;
1874 mp
->mnt_stat
.f_bavail
= mp
->mnt_stat
.f_bfree
;
1876 *sbp
= mp
->mnt_stat
;
1882 hammer2_vfs_statvfs(struct mount
*mp
, struct statvfs
*sbp
, struct ucred
*cred
)
1884 hammer2_pfsmount_t
*pmp
;
1885 hammer2_mount_t
*hmp
;
1888 KKASSERT(pmp
->iroot
->cluster
.nchains
>= 1);
1889 hmp
= pmp
->iroot
->cluster
.focus
->hmp
; /* XXX */
1891 mp
->mnt_vstat
.f_bsize
= HAMMER2_PBUFSIZE
;
1892 mp
->mnt_vstat
.f_files
= pmp
->inode_count
;
1893 mp
->mnt_vstat
.f_ffree
= 0;
1894 mp
->mnt_vstat
.f_blocks
= hmp
->voldata
.allocator_size
/ HAMMER2_PBUFSIZE
;
1895 mp
->mnt_vstat
.f_bfree
= hmp
->voldata
.allocator_free
/ HAMMER2_PBUFSIZE
;
1896 mp
->mnt_vstat
.f_bavail
= mp
->mnt_vstat
.f_bfree
;
1898 *sbp
= mp
->mnt_vstat
;
1903 * Mount-time recovery (RW mounts)
1905 * Updates to the free block table are allowed to lag flushes by one
1906 * transaction. In case of a crash, then on a fresh mount we must do an
1907 * incremental scan of the last committed transaction id and make sure that
1908 * all related blocks have been marked allocated.
1910 * The super-root topology and each PFS has its own transaction id domain,
1911 * so we must track PFS boundary transitions.
1913 struct hammer2_recovery_elm
{
1914 TAILQ_ENTRY(hammer2_recovery_elm
) entry
;
1915 hammer2_chain_t
*chain
;
1916 hammer2_tid_t sync_tid
;
1919 TAILQ_HEAD(hammer2_recovery_list
, hammer2_recovery_elm
);
1921 struct hammer2_recovery_info
{
1922 struct hammer2_recovery_list list
;
1926 static int hammer2_recovery_scan(hammer2_trans_t
*trans
, hammer2_mount_t
*hmp
,
1927 hammer2_chain_t
*parent
,
1928 struct hammer2_recovery_info
*info
,
1929 hammer2_tid_t sync_tid
);
1931 #define HAMMER2_RECOVERY_MAXDEPTH 10
1935 hammer2_recovery(hammer2_mount_t
*hmp
)
1937 hammer2_trans_t trans
;
1938 struct hammer2_recovery_info info
;
1939 struct hammer2_recovery_elm
*elm
;
1940 hammer2_chain_t
*parent
;
1941 hammer2_tid_t sync_tid
;
1943 int cumulative_error
= 0;
1945 hammer2_trans_init(&trans
, hmp
->spmp
, 0);
1948 TAILQ_INIT(&info
.list
);
1950 parent
= hammer2_chain_lookup_init(&hmp
->vchain
, 0);
1951 cumulative_error
= hammer2_recovery_scan(&trans
, hmp
, parent
,
1953 hammer2_chain_lookup_done(parent
);
1955 while ((elm
= TAILQ_FIRST(&info
.list
)) != NULL
) {
1956 TAILQ_REMOVE(&info
.list
, elm
, entry
);
1957 parent
= elm
->chain
;
1958 sync_tid
= elm
->sync_tid
;
1959 kfree(elm
, M_HAMMER2
);
1961 hammer2_chain_lock(parent
, HAMMER2_RESOLVE_ALWAYS
|
1962 HAMMER2_RESOLVE_NOREF
);
1963 error
= hammer2_recovery_scan(&trans
, hmp
, parent
,
1965 hammer2_chain_unlock(parent
);
1967 cumulative_error
= error
;
1969 hammer2_trans_done(&trans
);
1971 return cumulative_error
;
1976 hammer2_recovery_scan(hammer2_trans_t
*trans
, hammer2_mount_t
*hmp
,
1977 hammer2_chain_t
*parent
,
1978 struct hammer2_recovery_info
*info
,
1979 hammer2_tid_t sync_tid
)
1981 const hammer2_inode_data_t
*ripdata
;
1982 hammer2_chain_t
*chain
;
1984 int cumulative_error
= 0;
1985 int pfs_boundary
= 0;
1989 * Adjust freemap to ensure that the block(s) are marked allocated.
1991 if (parent
->bref
.type
!= HAMMER2_BREF_TYPE_VOLUME
) {
1992 hammer2_freemap_adjust(trans
, hmp
, &parent
->bref
,
1993 HAMMER2_FREEMAP_DORECOVER
);
1997 * Check type for recursive scan
1999 switch(parent
->bref
.type
) {
2000 case HAMMER2_BREF_TYPE_VOLUME
:
2001 /* data already instantiated */
2003 case HAMMER2_BREF_TYPE_INODE
:
2005 * Must instantiate data for DIRECTDATA test and also
2008 hammer2_chain_lock(parent
, HAMMER2_RESOLVE_ALWAYS
);
2009 ripdata
= &hammer2_chain_rdata(parent
)->ipdata
;
2010 if (ripdata
->op_flags
& HAMMER2_OPFLAG_DIRECTDATA
) {
2011 /* not applicable to recovery scan */
2012 hammer2_chain_unlock(parent
);
2015 if ((ripdata
->op_flags
& HAMMER2_OPFLAG_PFSROOT
) &&
2018 sync_tid
= parent
->bref
.mirror_tid
- 1;
2020 hammer2_chain_unlock(parent
);
2022 case HAMMER2_BREF_TYPE_INDIRECT
:
2024 * Must instantiate data for recursion
2026 hammer2_chain_lock(parent
, HAMMER2_RESOLVE_ALWAYS
);
2027 hammer2_chain_unlock(parent
);
2029 case HAMMER2_BREF_TYPE_DATA
:
2030 case HAMMER2_BREF_TYPE_FREEMAP
:
2031 case HAMMER2_BREF_TYPE_FREEMAP_NODE
:
2032 case HAMMER2_BREF_TYPE_FREEMAP_LEAF
:
2033 /* not applicable to recovery scan */
2041 * Defer operation if depth limit reached or if we are crossing a
2044 if (info
->depth
>= HAMMER2_RECOVERY_MAXDEPTH
|| pfs_boundary
) {
2045 struct hammer2_recovery_elm
*elm
;
2047 elm
= kmalloc(sizeof(*elm
), M_HAMMER2
, M_ZERO
| M_WAITOK
);
2048 elm
->chain
= parent
;
2049 elm
->sync_tid
= sync_tid
;
2050 hammer2_chain_ref(parent
);
2051 TAILQ_INSERT_TAIL(&info
->list
, elm
, entry
);
2052 /* unlocked by caller */
2059 * Recursive scan of the last flushed transaction only. We are
2060 * doing this without pmp assignments so don't leave the chains
2061 * hanging around after we are done with them.
2064 chain
= hammer2_chain_scan(parent
, NULL
, &cache_index
,
2065 HAMMER2_LOOKUP_NODATA
);
2067 atomic_set_int(&chain
->flags
, HAMMER2_CHAIN_RELEASE
);
2068 if (chain
->bref
.mirror_tid
>= sync_tid
) {
2070 error
= hammer2_recovery_scan(trans
, hmp
, chain
,
2074 cumulative_error
= error
;
2076 chain
= hammer2_chain_scan(parent
, chain
, &cache_index
,
2077 HAMMER2_LOOKUP_NODATA
);
2080 return cumulative_error
;
2084 * Sync the entire filesystem; this is called from the filesystem syncer
2085 * process periodically and whenever a user calls sync(1) on the hammer
2088 * Currently is actually called from the syncer! \o/
2090 * This task will have to snapshot the state of the dirty inode chain.
2091 * From that, it will have to make sure all of the inodes on the dirty
2092 * chain have IO initiated. We make sure that io is initiated for the root
2095 * If waitfor is set, we wait for media to acknowledge the new rootblock.
2097 * THINKS: side A vs side B, to have sync not stall all I/O?
2100 hammer2_vfs_sync(struct mount
*mp
, int waitfor
)
2102 struct hammer2_sync_info info
;
2103 hammer2_inode_t
*iroot
;
2104 hammer2_chain_t
*chain
;
2105 hammer2_chain_t
*parent
;
2106 hammer2_pfsmount_t
*pmp
;
2107 hammer2_mount_t
*hmp
;
2118 KKASSERT(iroot
->pmp
== pmp
);
2121 * We can't acquire locks on existing vnodes while in a transaction
2122 * without risking a deadlock. This assumes that vfsync() can be
2123 * called without the vnode locked (which it can in DragonFly).
2124 * Otherwise we'd have to implement a multi-pass or flag the lock
2125 * failures and retry.
2127 * The reclamation code interlocks with the sync list's token
2128 * (by removing the vnode from the scan list) before unlocking
2129 * the inode, giving us time to ref the inode.
2131 /*flags = VMSC_GETVP;*/
2133 if (waitfor
& MNT_LAZY
)
2134 flags
|= VMSC_ONEPASS
;
2137 * Start our flush transaction. This does not return until all
2138 * concurrent transactions have completed and will prevent any
2139 * new transactions from running concurrently, except for the
2140 * buffer cache transactions.
2142 * For efficiency do an async pass before making sure with a
2143 * synchronous pass on all related buffer cache buffers. It
2144 * should theoretically not be possible for any new file buffers
2145 * to be instantiated during this sequence.
2147 hammer2_trans_init(&info
.trans
, pmp
, HAMMER2_TRANS_ISFLUSH
|
2148 HAMMER2_TRANS_PREFLUSH
);
2149 hammer2_run_unlinkq(&info
.trans
, pmp
);
2152 info
.waitfor
= MNT_NOWAIT
;
2153 vsyncscan(mp
, flags
| VMSC_NOWAIT
, hammer2_sync_scan2
, &info
);
2154 info
.waitfor
= MNT_WAIT
;
2155 vsyncscan(mp
, flags
, hammer2_sync_scan2
, &info
);
2158 * Clear PREFLUSH. This prevents (or asserts on) any new logical
2159 * buffer cache flushes which occur during the flush. Device buffers
2164 if (info
.error
== 0 && (waitfor
& MNT_WAIT
)) {
2165 info
.waitfor
= waitfor
;
2166 vsyncscan(mp
, flags
, hammer2_sync_scan2
, &info
);
2170 hammer2_bioq_sync(info
.trans
.pmp
);
2171 atomic_clear_int(&info
.trans
.flags
, HAMMER2_TRANS_PREFLUSH
);
2176 * Flush all storage elements making up the cluster
2178 * We must also flush any deleted siblings because the super-root
2179 * flush won't do it for us. They all must be staged or the
2180 * super-root flush will not be able to update its block table
2183 * XXX currently done serially instead of concurrently
2185 for (i
= 0; iroot
&& i
< iroot
->cluster
.nchains
; ++i
) {
2186 chain
= iroot
->cluster
.array
[i
];
2188 hammer2_chain_lock(chain
, HAMMER2_RESOLVE_ALWAYS
);
2189 hammer2_flush(&info
.trans
, chain
);
2190 hammer2_chain_unlock(chain
);
2194 hammer2_trans_done(&info
.trans
);
2198 * Flush all volume roots to synchronize PFS flushes with the
2199 * storage media. Use a super-root transaction for each one.
2201 * The flush code will detect super-root -> pfs-root chain
2202 * transitions using the last pfs-root flush.
2204 for (i
= 0; iroot
&& i
< iroot
->cluster
.nchains
; ++i
) {
2205 chain
= iroot
->cluster
.array
[i
];
2212 * We only have to flush each hmp once
2214 for (j
= i
- 1; j
>= 0; --j
) {
2215 if (iroot
->cluster
.array
[j
] &&
2216 iroot
->cluster
.array
[j
]->hmp
== hmp
)
2221 hammer2_trans_spmp(&info
.trans
, hmp
->spmp
);
2224 * Force an update of the XID from the PFS root to the
2225 * topology root. We couldn't do this from the PFS
2226 * transaction because a SPMP transaction is needed.
2227 * This does not modify blocks, instead what it does is
2228 * allow the flush code to find the transition point and
2229 * then update on the way back up.
2231 parent
= chain
->parent
;
2232 KKASSERT(chain
->pmp
!= parent
->pmp
);
2233 hammer2_chain_setflush(&info
.trans
, parent
);
2236 * Media mounts have two 'roots', vchain for the topology
2237 * and fchain for the free block table. Flush both.
2239 * Note that the topology and free block table are handled
2240 * independently, so the free block table can wind up being
2241 * ahead of the topology. We depend on the bulk free scan
2242 * code to deal with any loose ends.
2244 hammer2_chain_lock(&hmp
->vchain
, HAMMER2_RESOLVE_ALWAYS
);
2245 hammer2_chain_lock(&hmp
->fchain
, HAMMER2_RESOLVE_ALWAYS
);
2246 if (hmp
->fchain
.flags
& HAMMER2_CHAIN_FLUSH_MASK
) {
2248 * This will also modify vchain as a side effect,
2249 * mark vchain as modified now.
2251 hammer2_voldata_modify(hmp
);
2252 chain
= &hmp
->fchain
;
2253 hammer2_flush(&info
.trans
, chain
);
2254 KKASSERT(chain
== &hmp
->fchain
);
2256 hammer2_chain_unlock(&hmp
->fchain
);
2257 hammer2_chain_unlock(&hmp
->vchain
);
2259 hammer2_chain_lock(&hmp
->vchain
, HAMMER2_RESOLVE_ALWAYS
);
2260 if (hmp
->vchain
.flags
& HAMMER2_CHAIN_FLUSH_MASK
) {
2261 chain
= &hmp
->vchain
;
2262 hammer2_flush(&info
.trans
, chain
);
2263 KKASSERT(chain
== &hmp
->vchain
);
2268 hammer2_chain_unlock(&hmp
->vchain
);
2271 hammer2_chain_lock(&hmp
->fchain
, HAMMER2_RESOLVE_ALWAYS
);
2272 if ((hmp
->fchain
.flags
& HAMMER2_CHAIN_FLUSH_MASK
) ||
2274 /* this will also modify vchain as a side effect */
2275 chain
= &hmp
->fchain
;
2276 hammer2_flush(&info
.trans
, chain
);
2277 KKASSERT(chain
== &hmp
->fchain
);
2279 hammer2_chain_unlock(&hmp
->fchain
);
2285 * We can't safely flush the volume header until we have
2286 * flushed any device buffers which have built up.
2288 * XXX this isn't being incremental
2290 vn_lock(hmp
->devvp
, LK_EXCLUSIVE
| LK_RETRY
);
2291 error
= VOP_FSYNC(hmp
->devvp
, MNT_WAIT
, 0);
2292 vn_unlock(hmp
->devvp
);
2295 * The flush code sets CHAIN_VOLUMESYNC to indicate that the
2296 * volume header needs synchronization via hmp->volsync.
2298 * XXX synchronize the flag & data with only this flush XXX
2301 (hmp
->vchain
.flags
& HAMMER2_CHAIN_VOLUMESYNC
)) {
2305 * Synchronize the disk before flushing the volume
2309 bp
->b_bio1
.bio_offset
= 0;
2312 bp
->b_cmd
= BUF_CMD_FLUSH
;
2313 bp
->b_bio1
.bio_done
= biodone_sync
;
2314 bp
->b_bio1
.bio_flags
|= BIO_SYNC
;
2315 vn_strategy(hmp
->devvp
, &bp
->b_bio1
);
2316 biowait(&bp
->b_bio1
, "h2vol");
2320 * Then we can safely flush the version of the
2321 * volume header synchronized by the flush code.
2323 i
= hmp
->volhdrno
+ 1;
2324 if (i
>= HAMMER2_NUM_VOLHDRS
)
2326 if (i
* HAMMER2_ZONE_BYTES64
+ HAMMER2_SEGSIZE
>
2327 hmp
->volsync
.volu_size
) {
2330 kprintf("sync volhdr %d %jd\n",
2331 i
, (intmax_t)hmp
->volsync
.volu_size
);
2332 bp
= getblk(hmp
->devvp
, i
* HAMMER2_ZONE_BYTES64
,
2333 HAMMER2_PBUFSIZE
, 0, 0);
2334 atomic_clear_int(&hmp
->vchain
.flags
,
2335 HAMMER2_CHAIN_VOLUMESYNC
);
2336 bcopy(&hmp
->volsync
, bp
->b_data
, HAMMER2_PBUFSIZE
);
2341 total_error
= error
;
2344 hammer2_trans_done(&info
.trans
);
2347 hammer2_trans_done(&info
.trans
);
2349 return (total_error
);
2356 hammer2_sync_scan2(struct mount
*mp
, struct vnode
*vp
, void *data
)
2358 struct hammer2_sync_info
*info
= data
;
2359 hammer2_inode_t
*ip
;
2368 if (vp
->v_type
== VNON
|| vp
->v_type
== VBAD
) {
2372 if ((ip
->flags
& HAMMER2_INODE_MODIFIED
) == 0 &&
2373 RB_EMPTY(&vp
->v_rbdirty_tree
)) {
2379 * VOP_FSYNC will start a new transaction so replicate some code
2380 * here to do it inline (see hammer2_vop_fsync()).
2382 * WARNING: The vfsync interacts with the buffer cache and might
2383 * block, we can't hold the inode lock at that time.
2384 * However, we MUST ref ip before blocking to ensure that
2385 * it isn't ripped out from under us (since we do not
2386 * hold a lock on the vnode).
2388 hammer2_inode_ref(ip
);
2389 atomic_clear_int(&ip
->flags
, HAMMER2_INODE_MODIFIED
);
2391 vfsync(vp
, MNT_NOWAIT
, 1, NULL
, NULL
);
2393 hammer2_inode_drop(ip
);
2397 info
->error
= error
;
2404 hammer2_vfs_vptofh(struct vnode
*vp
, struct fid
*fhp
)
2411 hammer2_vfs_fhtovp(struct mount
*mp
, struct vnode
*rootvp
,
2412 struct fid
*fhp
, struct vnode
**vpp
)
2419 hammer2_vfs_checkexp(struct mount
*mp
, struct sockaddr
*nam
,
2420 int *exflagsp
, struct ucred
**credanonp
)
2426 * Support code for hammer2_vfs_mount(). Read, verify, and install the volume
2427 * header into the HMP
2429 * XXX read four volhdrs and use the one with the highest TID whos CRC
2434 * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
2435 * nonexistant locations.
2437 * XXX Record selected volhdr and ring updates to each of 4 volhdrs
2441 hammer2_install_volume_header(hammer2_mount_t
*hmp
)
2443 hammer2_volume_data_t
*vd
;
2445 hammer2_crc32_t crc0
, crc
, bcrc0
, bcrc
;
2457 * There are up to 4 copies of the volume header (syncs iterate
2458 * between them so there is no single master). We don't trust the
2459 * volu_size field so we don't know precisely how large the filesystem
2460 * is, so depend on the OS to return an error if we go beyond the
2461 * block device's EOF.
2463 for (i
= 0; i
< HAMMER2_NUM_VOLHDRS
; i
++) {
2464 error
= bread(hmp
->devvp
, i
* HAMMER2_ZONE_BYTES64
,
2465 HAMMER2_VOLUME_BYTES
, &bp
);
2472 vd
= (struct hammer2_volume_data
*) bp
->b_data
;
2473 if ((vd
->magic
!= HAMMER2_VOLUME_ID_HBO
) &&
2474 (vd
->magic
!= HAMMER2_VOLUME_ID_ABO
)) {
2480 if (vd
->magic
== HAMMER2_VOLUME_ID_ABO
) {
2481 /* XXX: Reversed-endianness filesystem */
2482 kprintf("hammer2: reverse-endian filesystem detected");
2488 crc
= vd
->icrc_sects
[HAMMER2_VOL_ICRC_SECT0
];
2489 crc0
= hammer2_icrc32(bp
->b_data
+ HAMMER2_VOLUME_ICRC0_OFF
,
2490 HAMMER2_VOLUME_ICRC0_SIZE
);
2491 bcrc
= vd
->icrc_sects
[HAMMER2_VOL_ICRC_SECT1
];
2492 bcrc0
= hammer2_icrc32(bp
->b_data
+ HAMMER2_VOLUME_ICRC1_OFF
,
2493 HAMMER2_VOLUME_ICRC1_SIZE
);
2494 if ((crc0
!= crc
) || (bcrc0
!= bcrc
)) {
2495 kprintf("hammer2 volume header crc "
2496 "mismatch copy #%d %08x/%08x\n",
2503 if (valid
== 0 || hmp
->voldata
.mirror_tid
< vd
->mirror_tid
) {
2512 hmp
->volsync
= hmp
->voldata
;
2514 if (error_reported
|| bootverbose
|| 1) { /* 1/DEBUG */
2515 kprintf("hammer2: using volume header #%d\n",
2520 kprintf("hammer2: no valid volume headers found!\n");
2526 * This handles hysteresis on regular file flushes. Because the BIOs are
2527 * routed to a thread it is possible for an excessive number to build up
2528 * and cause long front-end stalls long before the runningbuffspace limit
2529 * is hit, so we implement hammer2_flush_pipe to control the
2532 * This is a particular problem when compression is used.
2535 hammer2_lwinprog_ref(hammer2_pfsmount_t
*pmp
)
2537 atomic_add_int(&pmp
->count_lwinprog
, 1);
2541 hammer2_lwinprog_drop(hammer2_pfsmount_t
*pmp
)
2545 lwinprog
= atomic_fetchadd_int(&pmp
->count_lwinprog
, -1);
2546 if ((lwinprog
& HAMMER2_LWINPROG_WAITING
) &&
2547 (lwinprog
& HAMMER2_LWINPROG_MASK
) <= hammer2_flush_pipe
* 2 / 3) {
2548 atomic_clear_int(&pmp
->count_lwinprog
,
2549 HAMMER2_LWINPROG_WAITING
);
2550 wakeup(&pmp
->count_lwinprog
);
2555 hammer2_lwinprog_wait(hammer2_pfsmount_t
*pmp
)
2560 lwinprog
= pmp
->count_lwinprog
;
2562 if ((lwinprog
& HAMMER2_LWINPROG_MASK
) < hammer2_flush_pipe
)
2564 tsleep_interlock(&pmp
->count_lwinprog
, 0);
2565 atomic_set_int(&pmp
->count_lwinprog
, HAMMER2_LWINPROG_WAITING
);
2566 lwinprog
= pmp
->count_lwinprog
;
2567 if ((lwinprog
& HAMMER2_LWINPROG_MASK
) < hammer2_flush_pipe
)
2569 tsleep(&pmp
->count_lwinprog
, PINTERLOCKED
, "h2wpipe", hz
);
2574 * Manage excessive memory resource use for chain and related
2578 hammer2_pfs_memory_wait(hammer2_pfsmount_t
*pmp
)
2588 * Atomic check condition and wait. Also do an early speedup of
2589 * the syncer to try to avoid hitting the wait.
2592 waiting
= pmp
->inmem_dirty_chains
;
2594 count
= waiting
& HAMMER2_DIRTYCHAIN_MASK
;
2596 limit
= pmp
->mp
->mnt_nvnodelistsize
/ 10;
2597 if (limit
< hammer2_limit_dirty_chains
)
2598 limit
= hammer2_limit_dirty_chains
;
2603 if ((int)(ticks
- zzticks
) > hz
) {
2605 kprintf("count %ld %ld\n", count
, limit
);
2610 * Block if there are too many dirty chains present, wait
2611 * for the flush to clean some out.
2613 if (count
> limit
) {
2614 tsleep_interlock(&pmp
->inmem_dirty_chains
, 0);
2615 if (atomic_cmpset_int(&pmp
->inmem_dirty_chains
,
2617 waiting
| HAMMER2_DIRTYCHAIN_WAITING
)) {
2618 speedup_syncer(pmp
->mp
);
2619 tsleep(&pmp
->inmem_dirty_chains
, PINTERLOCKED
,
2622 continue; /* loop on success or fail */
2626 * Try to start an early flush before we are forced to block.
2628 if (count
> limit
* 7 / 10)
2629 speedup_syncer(pmp
->mp
);
2635 hammer2_pfs_memory_inc(hammer2_pfsmount_t
*pmp
)
2638 atomic_add_int(&pmp
->inmem_dirty_chains
, 1);
2643 hammer2_pfs_memory_wakeup(hammer2_pfsmount_t
*pmp
)
2651 waiting
= pmp
->inmem_dirty_chains
;
2653 if (atomic_cmpset_int(&pmp
->inmem_dirty_chains
,
2656 ~HAMMER2_DIRTYCHAIN_WAITING
)) {
2661 if (waiting
& HAMMER2_DIRTYCHAIN_WAITING
)
2662 wakeup(&pmp
->inmem_dirty_chains
);
2669 hammer2_dump_chain(hammer2_chain_t
*chain
, int tab
, int *countp
, char pfx
)
2671 hammer2_chain_t
*scan
;
2672 hammer2_chain_t
*parent
;
2676 kprintf("%*.*s...\n", tab
, tab
, "");
2681 kprintf("%*.*s%c-chain %p.%d %016jx/%d mir=%016jx\n",
2683 chain
, chain
->bref
.type
,
2684 chain
->bref
.key
, chain
->bref
.keybits
,
2685 chain
->bref
.mirror_tid
);
2687 kprintf("%*.*s [%08x] (%s) refs=%d\n",
2690 ((chain
->bref
.type
== HAMMER2_BREF_TYPE_INODE
&&
2691 chain
->data
) ? (char *)chain
->data
->ipdata
.filename
: "?"),
2694 kprintf("%*.*s core [%08x]",
2698 parent
= chain
->parent
;
2700 kprintf("\n%*.*s p=%p [pflags %08x prefs %d",
2702 parent
, parent
->flags
, parent
->refs
);
2703 if (RB_EMPTY(&chain
->core
.rbtree
)) {
2707 RB_FOREACH(scan
, hammer2_chain_tree
, &chain
->core
.rbtree
)
2708 hammer2_dump_chain(scan
, tab
+ 4, countp
, 'a');
2709 if (chain
->bref
.type
== HAMMER2_BREF_TYPE_INODE
&& chain
->data
)
2710 kprintf("%*.*s}(%s)\n", tab
, tab
, "",
2711 chain
->data
->ipdata
.filename
);
2713 kprintf("%*.*s}\n", tab
, tab
, "");