2 * Copyright (c) 2007-2008 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>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/vfs/hammer/hammer_vfsops.c,v 1.48 2008/06/17 04:02:38 dillon Exp $
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/vnode.h>
41 #include <sys/mount.h>
42 #include <sys/malloc.h>
43 #include <sys/nlookup.h>
44 #include <sys/fcntl.h>
45 #include <sys/sysctl.h>
51 int hammer_debug_general
;
52 int hammer_debug_debug
;
53 int hammer_debug_inode
;
54 int hammer_debug_locks
;
55 int hammer_debug_btree
;
57 int hammer_debug_recover
; /* -1 will disable, +1 will force */
58 int hammer_debug_recover_faults
;
59 int hammer_debug_cluster_enable
= 1; /* enable read clustering by default */
60 int hammer_count_inodes
;
61 int hammer_count_iqueued
;
62 int hammer_count_reclaiming
;
63 int hammer_count_records
;
64 int hammer_count_record_datas
;
65 int hammer_count_volumes
;
66 int hammer_count_buffers
;
67 int hammer_count_nodes
;
68 int64_t hammer_stats_btree_lookups
;
69 int64_t hammer_stats_btree_searches
;
70 int64_t hammer_stats_btree_inserts
;
71 int64_t hammer_stats_btree_deletes
;
72 int64_t hammer_stats_btree_elements
;
73 int64_t hammer_stats_btree_splits
;
74 int64_t hammer_stats_btree_iterations
;
75 int64_t hammer_stats_record_iterations
;
76 int hammer_count_dirtybufs
; /* global */
77 int hammer_count_refedbufs
; /* global */
78 int hammer_count_reservations
;
79 int hammer_count_io_running_read
;
80 int hammer_count_io_running_write
;
81 int hammer_count_io_locked
;
82 int hammer_limit_dirtybufs
; /* per-mount */
83 int hammer_limit_irecs
; /* per-inode */
84 int hammer_limit_recs
; /* as a whole XXX */
85 int hammer_limit_iqueued
; /* per-mount */
87 int hammer_verify_zone
;
88 int hammer_write_mode
;
89 int64_t hammer_contention_count
;
90 int64_t hammer_zone_limit
;
92 SYSCTL_NODE(_vfs
, OID_AUTO
, hammer
, CTLFLAG_RW
, 0, "HAMMER filesystem");
93 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, debug_general
, CTLFLAG_RW
,
94 &hammer_debug_general
, 0, "");
95 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, debug_io
, CTLFLAG_RW
,
96 &hammer_debug_io
, 0, "");
97 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, debug_debug
, CTLFLAG_RW
,
98 &hammer_debug_debug
, 0, "");
99 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, debug_inode
, CTLFLAG_RW
,
100 &hammer_debug_inode
, 0, "");
101 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, debug_locks
, CTLFLAG_RW
,
102 &hammer_debug_locks
, 0, "");
103 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, debug_btree
, CTLFLAG_RW
,
104 &hammer_debug_btree
, 0, "");
105 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, debug_tid
, CTLFLAG_RW
,
106 &hammer_debug_tid
, 0, "");
107 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, debug_recover
, CTLFLAG_RW
,
108 &hammer_debug_recover
, 0, "");
109 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, debug_recover_faults
, CTLFLAG_RW
,
110 &hammer_debug_recover_faults
, 0, "");
111 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, debug_cluster_enable
, CTLFLAG_RW
,
112 &hammer_debug_cluster_enable
, 0, "");
114 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, limit_dirtybufs
, CTLFLAG_RW
,
115 &hammer_limit_dirtybufs
, 0, "");
116 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, limit_irecs
, CTLFLAG_RW
,
117 &hammer_limit_irecs
, 0, "");
118 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, limit_recs
, CTLFLAG_RW
,
119 &hammer_limit_recs
, 0, "");
120 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, limit_iqueued
, CTLFLAG_RW
,
121 &hammer_limit_iqueued
, 0, "");
123 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, count_inodes
, CTLFLAG_RD
,
124 &hammer_count_inodes
, 0, "");
125 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, count_iqueued
, CTLFLAG_RD
,
126 &hammer_count_iqueued
, 0, "");
127 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, count_reclaiming
, CTLFLAG_RD
,
128 &hammer_count_reclaiming
, 0, "");
129 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, count_records
, CTLFLAG_RD
,
130 &hammer_count_records
, 0, "");
131 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, count_record_datas
, CTLFLAG_RD
,
132 &hammer_count_record_datas
, 0, "");
133 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, count_volumes
, CTLFLAG_RD
,
134 &hammer_count_volumes
, 0, "");
135 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, count_buffers
, CTLFLAG_RD
,
136 &hammer_count_buffers
, 0, "");
137 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, count_nodes
, CTLFLAG_RD
,
138 &hammer_count_nodes
, 0, "");
139 SYSCTL_QUAD(_vfs_hammer
, OID_AUTO
, stats_btree_searches
, CTLFLAG_RD
,
140 &hammer_stats_btree_searches
, 0, "");
141 SYSCTL_QUAD(_vfs_hammer
, OID_AUTO
, stats_btree_lookups
, CTLFLAG_RD
,
142 &hammer_stats_btree_lookups
, 0, "");
143 SYSCTL_QUAD(_vfs_hammer
, OID_AUTO
, stats_btree_inserts
, CTLFLAG_RD
,
144 &hammer_stats_btree_inserts
, 0, "");
145 SYSCTL_QUAD(_vfs_hammer
, OID_AUTO
, stats_btree_deletes
, CTLFLAG_RD
,
146 &hammer_stats_btree_deletes
, 0, "");
147 SYSCTL_QUAD(_vfs_hammer
, OID_AUTO
, stats_btree_elements
, CTLFLAG_RD
,
148 &hammer_stats_btree_elements
, 0, "");
149 SYSCTL_QUAD(_vfs_hammer
, OID_AUTO
, stats_btree_splits
, CTLFLAG_RD
,
150 &hammer_stats_btree_splits
, 0, "");
151 SYSCTL_QUAD(_vfs_hammer
, OID_AUTO
, stats_btree_iterations
, CTLFLAG_RD
,
152 &hammer_stats_btree_iterations
, 0, "");
153 SYSCTL_QUAD(_vfs_hammer
, OID_AUTO
, stats_record_iterations
, CTLFLAG_RD
,
154 &hammer_stats_record_iterations
, 0, "");
155 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, count_dirtybufs
, CTLFLAG_RD
,
156 &hammer_count_dirtybufs
, 0, "");
157 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, count_refedbufs
, CTLFLAG_RD
,
158 &hammer_count_refedbufs
, 0, "");
159 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, count_reservations
, CTLFLAG_RD
,
160 &hammer_count_reservations
, 0, "");
161 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, count_io_running_read
, CTLFLAG_RD
,
162 &hammer_count_io_running_read
, 0, "");
163 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, count_io_locked
, CTLFLAG_RD
,
164 &hammer_count_io_locked
, 0, "");
165 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, count_io_running_write
, CTLFLAG_RD
,
166 &hammer_count_io_running_write
, 0, "");
167 SYSCTL_QUAD(_vfs_hammer
, OID_AUTO
, zone_limit
, CTLFLAG_RW
,
168 &hammer_zone_limit
, 0, "");
169 SYSCTL_QUAD(_vfs_hammer
, OID_AUTO
, contention_count
, CTLFLAG_RW
,
170 &hammer_contention_count
, 0, "");
171 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, verify_zone
, CTLFLAG_RW
,
172 &hammer_verify_zone
, 0, "");
173 SYSCTL_INT(_vfs_hammer
, OID_AUTO
, write_mode
, CTLFLAG_RW
,
174 &hammer_write_mode
, 0, "");
179 static void hammer_free_hmp(struct mount
*mp
);
181 static int hammer_vfs_mount(struct mount
*mp
, char *path
, caddr_t data
,
183 static int hammer_vfs_unmount(struct mount
*mp
, int mntflags
);
184 static int hammer_vfs_root(struct mount
*mp
, struct vnode
**vpp
);
185 static int hammer_vfs_statfs(struct mount
*mp
, struct statfs
*sbp
,
187 static int hammer_vfs_statvfs(struct mount
*mp
, struct statvfs
*sbp
,
189 static int hammer_vfs_sync(struct mount
*mp
, int waitfor
);
190 static int hammer_vfs_vget(struct mount
*mp
, ino_t ino
,
192 static int hammer_vfs_init(struct vfsconf
*conf
);
193 static int hammer_vfs_fhtovp(struct mount
*mp
, struct fid
*fhp
,
195 static int hammer_vfs_vptofh(struct vnode
*vp
, struct fid
*fhp
);
196 static int hammer_vfs_checkexp(struct mount
*mp
, struct sockaddr
*nam
,
197 int *exflagsp
, struct ucred
**credanonp
);
200 static struct vfsops hammer_vfsops
= {
201 .vfs_mount
= hammer_vfs_mount
,
202 .vfs_unmount
= hammer_vfs_unmount
,
203 .vfs_root
= hammer_vfs_root
,
204 .vfs_statfs
= hammer_vfs_statfs
,
205 .vfs_statvfs
= hammer_vfs_statvfs
,
206 .vfs_sync
= hammer_vfs_sync
,
207 .vfs_vget
= hammer_vfs_vget
,
208 .vfs_init
= hammer_vfs_init
,
209 .vfs_vptofh
= hammer_vfs_vptofh
,
210 .vfs_fhtovp
= hammer_vfs_fhtovp
,
211 .vfs_checkexp
= hammer_vfs_checkexp
214 MALLOC_DEFINE(M_HAMMER
, "hammer-mount", "hammer mount");
216 VFS_SET(hammer_vfsops
, hammer
, 0);
217 MODULE_VERSION(hammer
, 1);
220 hammer_vfs_init(struct vfsconf
*conf
)
222 if (hammer_limit_irecs
== 0)
223 hammer_limit_irecs
= nbuf
* 8;
224 if (hammer_limit_recs
== 0) /* XXX TODO */
225 hammer_limit_recs
= nbuf
* 25;
226 if (hammer_limit_dirtybufs
== 0) {
227 hammer_limit_dirtybufs
= hidirtybuffers
/ 2;
228 if (hammer_limit_dirtybufs
< 100)
229 hammer_limit_dirtybufs
= 100;
231 if (hammer_limit_iqueued
== 0)
232 hammer_limit_iqueued
= desiredvnodes
/ 5;
237 hammer_vfs_mount(struct mount
*mp
, char *mntpt
, caddr_t data
,
240 struct hammer_mount_info info
;
242 hammer_volume_t rootvol
;
243 struct vnode
*rootvp
;
244 const char *upath
; /* volume name in userspace */
245 char *path
; /* volume name in system space */
249 if ((error
= copyin(data
, &info
, sizeof(info
))) != 0)
251 if ((mp
->mnt_flag
& MNT_UPDATE
) == 0) {
252 if (info
.nvolumes
<= 0 || info
.nvolumes
>= 32768)
257 * Interal mount data structure
259 if (mp
->mnt_flag
& MNT_UPDATE
) {
260 hmp
= (void *)mp
->mnt_data
;
261 KKASSERT(hmp
!= NULL
);
263 hmp
= kmalloc(sizeof(*hmp
), M_HAMMER
, M_WAITOK
| M_ZERO
);
264 mp
->mnt_data
= (qaddr_t
)hmp
;
266 hmp
->namekey_iterator
= mycpu
->gd_time_seconds
;
267 /*TAILQ_INIT(&hmp->recycle_list);*/
269 hmp
->root_btree_beg
.localization
= HAMMER_MIN_LOCALIZATION
;
270 hmp
->root_btree_beg
.obj_id
= -0x8000000000000000LL
;
271 hmp
->root_btree_beg
.key
= -0x8000000000000000LL
;
272 hmp
->root_btree_beg
.create_tid
= 1;
273 hmp
->root_btree_beg
.delete_tid
= 1;
274 hmp
->root_btree_beg
.rec_type
= 0;
275 hmp
->root_btree_beg
.obj_type
= 0;
277 hmp
->root_btree_end
.localization
= HAMMER_MAX_LOCALIZATION
;
278 hmp
->root_btree_end
.obj_id
= 0x7FFFFFFFFFFFFFFFLL
;
279 hmp
->root_btree_end
.key
= 0x7FFFFFFFFFFFFFFFLL
;
280 hmp
->root_btree_end
.create_tid
= 0xFFFFFFFFFFFFFFFFULL
;
281 hmp
->root_btree_end
.delete_tid
= 0; /* special case */
282 hmp
->root_btree_end
.rec_type
= 0xFFFFU
;
283 hmp
->root_btree_end
.obj_type
= 0;
285 hmp
->sync_lock
.refs
= 1;
286 hmp
->free_lock
.refs
= 1;
287 hmp
->undo_lock
.refs
= 1;
288 hmp
->blkmap_lock
.refs
= 1;
290 TAILQ_INIT(&hmp
->flush_list
);
291 TAILQ_INIT(&hmp
->delay_list
);
292 TAILQ_INIT(&hmp
->objid_cache_list
);
293 TAILQ_INIT(&hmp
->undo_lru_list
);
294 TAILQ_INIT(&hmp
->reclaim_list
);
296 hmp
->hflags
&= ~HMNT_USERFLAGS
;
297 hmp
->hflags
|= info
.hflags
& HMNT_USERFLAGS
;
300 mp
->mnt_flag
|= MNT_RDONLY
;
301 hmp
->asof
= info
.asof
;
303 hmp
->asof
= HAMMER_MAX_TID
;
307 * Re-open read-write if originally read-only, or vise-versa.
309 if (mp
->mnt_flag
& MNT_UPDATE
) {
311 if (hmp
->ronly
&& (mp
->mnt_kern_flag
& MNTK_WANTRDWR
)) {
312 kprintf("HAMMER read-only -> read-write\n");
314 RB_SCAN(hammer_vol_rb_tree
, &hmp
->rb_vols_root
, NULL
,
315 hammer_adjust_volume_mode
, NULL
);
316 rootvol
= hammer_get_root_volume(hmp
, &error
);
318 hammer_recover_flush_buffers(hmp
, rootvol
);
319 bcopy(rootvol
->ondisk
->vol0_blockmap
,
321 sizeof(hmp
->blockmap
));
322 hammer_rel_volume(rootvol
, 0);
324 RB_SCAN(hammer_ino_rb_tree
, &hmp
->rb_inos_root
, NULL
,
325 hammer_reload_inode
, NULL
);
326 /* kernel clears MNT_RDONLY */
327 } else if (hmp
->ronly
== 0 && (mp
->mnt_flag
& MNT_RDONLY
)) {
328 kprintf("HAMMER read-write -> read-only\n");
329 hmp
->ronly
= 1; /* messy */
330 RB_SCAN(hammer_ino_rb_tree
, &hmp
->rb_inos_root
, NULL
,
331 hammer_reload_inode
, NULL
);
333 hammer_flusher_sync(hmp
);
334 hammer_flusher_sync(hmp
);
335 hammer_flusher_sync(hmp
);
337 RB_SCAN(hammer_vol_rb_tree
, &hmp
->rb_vols_root
, NULL
,
338 hammer_adjust_volume_mode
, NULL
);
343 RB_INIT(&hmp
->rb_vols_root
);
344 RB_INIT(&hmp
->rb_inos_root
);
345 RB_INIT(&hmp
->rb_nods_root
);
346 RB_INIT(&hmp
->rb_undo_root
);
347 RB_INIT(&hmp
->rb_resv_root
);
348 RB_INIT(&hmp
->rb_bufs_root
);
350 hmp
->ronly
= ((mp
->mnt_flag
& MNT_RDONLY
) != 0);
352 TAILQ_INIT(&hmp
->volu_list
);
353 TAILQ_INIT(&hmp
->undo_list
);
354 TAILQ_INIT(&hmp
->data_list
);
355 TAILQ_INIT(&hmp
->meta_list
);
356 TAILQ_INIT(&hmp
->lose_list
);
361 path
= objcache_get(namei_oc
, M_WAITOK
);
362 hmp
->nvolumes
= info
.nvolumes
;
363 for (i
= 0; i
< info
.nvolumes
; ++i
) {
364 error
= copyin(&info
.volumes
[i
], &upath
, sizeof(char *));
366 error
= copyinstr(upath
, path
, MAXPATHLEN
, NULL
);
368 error
= hammer_install_volume(hmp
, path
);
372 objcache_put(namei_oc
, path
);
375 * Make sure we found a root volume
377 if (error
== 0 && hmp
->rootvol
== NULL
) {
378 kprintf("hammer_mount: No root volume found!\n");
387 * No errors, setup enough of the mount point so we can lookup the
390 mp
->mnt_iosize_max
= MAXPHYS
;
391 mp
->mnt_kern_flag
|= MNTK_FSMID
;
394 * note: f_iosize is used by vnode_pager_haspage() when constructing
397 mp
->mnt_stat
.f_iosize
= HAMMER_BUFSIZE
;
398 mp
->mnt_stat
.f_bsize
= HAMMER_BUFSIZE
;
400 mp
->mnt_vstat
.f_frsize
= HAMMER_BUFSIZE
;
401 mp
->mnt_vstat
.f_bsize
= HAMMER_BUFSIZE
;
403 mp
->mnt_maxsymlinklen
= 255;
404 mp
->mnt_flag
|= MNT_LOCAL
;
406 vfs_add_vnodeops(mp
, &hammer_vnode_vops
, &mp
->mnt_vn_norm_ops
);
407 vfs_add_vnodeops(mp
, &hammer_spec_vops
, &mp
->mnt_vn_spec_ops
);
408 vfs_add_vnodeops(mp
, &hammer_fifo_vops
, &mp
->mnt_vn_fifo_ops
);
411 * The root volume's ondisk pointer is only valid if we hold a
414 rootvol
= hammer_get_root_volume(hmp
, &error
);
419 * Perform any necessary UNDO operations. The recovery code does
420 * call hammer_undo_lookup() so we have to pre-cache the blockmap,
421 * and then re-copy it again after recovery is complete.
423 * If this is a read-only mount the UNDO information is retained
424 * in memory in the form of dirty buffer cache buffers, and not
425 * written back to the media.
427 bcopy(rootvol
->ondisk
->vol0_blockmap
, hmp
->blockmap
,
428 sizeof(hmp
->blockmap
));
430 error
= hammer_recover(hmp
, rootvol
);
432 kprintf("Failed to recover HAMMER filesystem on mount\n");
437 * Finish setup now that we have a good root volume
439 ksnprintf(mp
->mnt_stat
.f_mntfromname
,
440 sizeof(mp
->mnt_stat
.f_mntfromname
), "%s",
441 rootvol
->ondisk
->vol_name
);
442 mp
->mnt_stat
.f_fsid
.val
[0] =
443 crc32((char *)&rootvol
->ondisk
->vol_fsid
+ 0, 8);
444 mp
->mnt_stat
.f_fsid
.val
[1] =
445 crc32((char *)&rootvol
->ondisk
->vol_fsid
+ 8, 8);
447 mp
->mnt_vstat
.f_fsid_uuid
= rootvol
->ondisk
->vol_fsid
;
448 mp
->mnt_vstat
.f_fsid
= crc32(&mp
->mnt_vstat
.f_fsid_uuid
,
449 sizeof(mp
->mnt_vstat
.f_fsid_uuid
));
452 * Certain often-modified fields in the root volume are cached in
453 * the hammer_mount structure so we do not have to generate lots
454 * of little UNDO structures for them.
456 * Recopy after recovery. This also has the side effect of
457 * setting our cached undo FIFO's first_offset, which serves to
458 * placemark the FIFO start for the NEXT flush cycle while the
459 * on-disk first_offset represents the LAST flush cycle.
461 hmp
->next_tid
= rootvol
->ondisk
->vol0_next_tid
;
462 bcopy(rootvol
->ondisk
->vol0_blockmap
, hmp
->blockmap
,
463 sizeof(hmp
->blockmap
));
464 hmp
->copy_stat_freebigblocks
= rootvol
->ondisk
->vol0_stat_freebigblocks
;
466 hammer_flusher_create(hmp
);
469 * Locate the root directory using the root cluster's B-Tree as a
470 * starting point. The root directory uses an obj_id of 1.
472 * FUTURE: Leave the root directory cached referenced but unlocked
473 * in hmp->rootvp (need to flush it on unmount).
475 error
= hammer_vfs_vget(mp
, 1, &rootvp
);
479 /*vn_unlock(hmp->rootvp);*/
482 hammer_rel_volume(rootvol
, 0);
485 * Cleanup and return.
493 hammer_vfs_unmount(struct mount
*mp
, int mntflags
)
496 struct hammer_mount
*hmp
= (void *)mp
->mnt_data
;
502 * Clean out the vnodes
505 if (mntflags
& MNT_FORCE
)
507 if ((error
= vflush(mp
, 0, flags
)) != 0)
511 * Clean up the internal mount structure and related entities. This
519 * Clean up the internal mount structure and disassociate it from the mount.
520 * This may issue I/O.
523 hammer_free_hmp(struct mount
*mp
)
525 struct hammer_mount
*hmp
= (void *)mp
->mnt_data
;
529 * Clean up the root vnode
536 hammer_flusher_sync(hmp
);
537 hammer_flusher_sync(hmp
);
538 hammer_flusher_destroy(hmp
);
540 KKASSERT(RB_EMPTY(&hmp
->rb_inos_root
));
544 * Unload & flush inodes
546 * XXX illegal to call this from here, it can only be done from
549 RB_SCAN(hammer_ino_rb_tree
, &hmp
->rb_inos_root
, NULL
,
550 hammer_unload_inode
, (void *)MNT_WAIT
);
553 * Unload & flush volumes
557 * Unload buffers and then volumes
559 RB_SCAN(hammer_buf_rb_tree
, &hmp
->rb_bufs_root
, NULL
,
560 hammer_unload_buffer
, NULL
);
561 RB_SCAN(hammer_vol_rb_tree
, &hmp
->rb_vols_root
, NULL
,
562 hammer_unload_volume
, NULL
);
565 mp
->mnt_flag
&= ~MNT_LOCAL
;
567 hammer_destroy_objid_cache(hmp
);
568 kfree(hmp
, M_HAMMER
);
572 * Obtain a vnode for the specified inode number. An exclusively locked
576 hammer_vfs_vget(struct mount
*mp
, ino_t ino
, struct vnode
**vpp
)
578 struct hammer_transaction trans
;
579 struct hammer_mount
*hmp
= (void *)mp
->mnt_data
;
580 struct hammer_inode
*ip
;
583 hammer_simple_transaction(&trans
, hmp
);
586 * Lookup the requested HAMMER inode. The structure must be
587 * left unlocked while we manipulate the related vnode to avoid
590 ip
= hammer_get_inode(&trans
, NULL
, ino
, hmp
->asof
, 0, &error
);
595 error
= hammer_get_vnode(ip
, vpp
);
596 hammer_rel_inode(ip
, 0);
597 hammer_done_transaction(&trans
);
602 * Return the root vnode for the filesystem.
604 * HAMMER stores the root vnode in the hammer_mount structure so
605 * getting it is easy.
608 hammer_vfs_root(struct mount
*mp
, struct vnode
**vpp
)
611 struct hammer_mount
*hmp
= (void *)mp
->mnt_data
;
615 error
= hammer_vfs_vget(mp
, 1, vpp
);
620 hammer_vfs_statfs(struct mount
*mp
, struct statfs
*sbp
, struct ucred
*cred
)
622 struct hammer_mount
*hmp
= (void *)mp
->mnt_data
;
623 hammer_volume_t volume
;
624 hammer_volume_ondisk_t ondisk
;
628 volume
= hammer_get_root_volume(hmp
, &error
);
631 ondisk
= volume
->ondisk
;
636 mp
->mnt_stat
.f_files
= ondisk
->vol0_stat_inodes
;
637 bfree
= ondisk
->vol0_stat_freebigblocks
* HAMMER_LARGEBLOCK_SIZE
;
638 hammer_rel_volume(volume
, 0);
640 mp
->mnt_stat
.f_bfree
= bfree
/ HAMMER_BUFSIZE
;
641 mp
->mnt_stat
.f_bavail
= mp
->mnt_stat
.f_bfree
;
642 if (mp
->mnt_stat
.f_files
< 0)
643 mp
->mnt_stat
.f_files
= 0;
650 hammer_vfs_statvfs(struct mount
*mp
, struct statvfs
*sbp
, struct ucred
*cred
)
652 struct hammer_mount
*hmp
= (void *)mp
->mnt_data
;
653 hammer_volume_t volume
;
654 hammer_volume_ondisk_t ondisk
;
658 volume
= hammer_get_root_volume(hmp
, &error
);
661 ondisk
= volume
->ondisk
;
666 mp
->mnt_vstat
.f_files
= ondisk
->vol0_stat_inodes
;
667 bfree
= ondisk
->vol0_stat_freebigblocks
* HAMMER_LARGEBLOCK_SIZE
;
668 hammer_rel_volume(volume
, 0);
670 mp
->mnt_vstat
.f_bfree
= bfree
/ HAMMER_BUFSIZE
;
671 mp
->mnt_vstat
.f_bavail
= mp
->mnt_stat
.f_bfree
;
672 if (mp
->mnt_vstat
.f_files
< 0)
673 mp
->mnt_vstat
.f_files
= 0;
674 *sbp
= mp
->mnt_vstat
;
679 * Sync the filesystem. Currently we have to run it twice, the second
680 * one will advance the undo start index to the end index, so if a crash
681 * occurs no undos will be run on mount.
683 * We do not sync the filesystem if we are called from a panic. If we did
684 * we might end up blowing up a sync that was already in progress.
687 hammer_vfs_sync(struct mount
*mp
, int waitfor
)
689 struct hammer_mount
*hmp
= (void *)mp
->mnt_data
;
692 if (panicstr
== NULL
) {
693 error
= hammer_sync_hmp(hmp
, waitfor
);
695 error
= hammer_sync_hmp(hmp
, waitfor
);
703 * Convert a vnode to a file handle.
706 hammer_vfs_vptofh(struct vnode
*vp
, struct fid
*fhp
)
710 KKASSERT(MAXFIDSZ
>= 16);
712 fhp
->fid_len
= offsetof(struct fid
, fid_data
[16]);
713 fhp
->fid_reserved
= 0;
714 bcopy(&ip
->obj_id
, fhp
->fid_data
+ 0, sizeof(ip
->obj_id
));
715 bcopy(&ip
->obj_asof
, fhp
->fid_data
+ 8, sizeof(ip
->obj_asof
));
721 * Convert a file handle back to a vnode.
724 hammer_vfs_fhtovp(struct mount
*mp
, struct fid
*fhp
, struct vnode
**vpp
)
726 struct hammer_transaction trans
;
727 struct hammer_inode
*ip
;
728 struct hammer_inode_info info
;
731 bcopy(fhp
->fid_data
+ 0, &info
.obj_id
, sizeof(info
.obj_id
));
732 bcopy(fhp
->fid_data
+ 8, &info
.obj_asof
, sizeof(info
.obj_asof
));
734 hammer_simple_transaction(&trans
, (void *)mp
->mnt_data
);
737 * Get/allocate the hammer_inode structure. The structure must be
738 * unlocked while we manipulate the related vnode to avoid a
741 ip
= hammer_get_inode(&trans
, NULL
, info
.obj_id
, info
.obj_asof
,
747 error
= hammer_get_vnode(ip
, vpp
);
748 hammer_rel_inode(ip
, 0);
749 hammer_done_transaction(&trans
);
754 hammer_vfs_checkexp(struct mount
*mp
, struct sockaddr
*nam
,
755 int *exflagsp
, struct ucred
**credanonp
)
757 hammer_mount_t hmp
= (void *)mp
->mnt_data
;
761 np
= vfs_export_lookup(mp
, &hmp
->export
, nam
);
763 *exflagsp
= np
->netc_exflags
;
764 *credanonp
= &np
->netc_anon
;
774 hammer_vfs_export(struct mount
*mp
, int op
, const struct export_args
*export
)
776 hammer_mount_t hmp
= (void *)mp
->mnt_data
;
780 case MOUNTCTL_SET_EXPORT
:
781 error
= vfs_export(mp
, &hmp
->export
, export
);