HAMMER - Rework write pipelining
[dragonfly.git] / sys / vfs / hammer / hammer_vfsops.c
blob331f43f91ab28431f0d04b19d087bf735f281bb5
1 /*
2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
16 * distribution.
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
32 * SUCH DAMAGE.
34 * $DragonFly: src/sys/vfs/hammer/hammer_vfsops.c,v 1.74 2008/11/13 02:18:43 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>
46 #include <sys/buf.h>
47 #include <sys/buf2.h>
48 #include "hammer.h"
50 int hammer_supported_version = HAMMER_VOL_VERSION_TWO;
51 int hammer_debug_io;
52 int hammer_debug_general;
53 int hammer_debug_debug = 1; /* medium-error panics */
54 int hammer_debug_inode;
55 int hammer_debug_locks;
56 int hammer_debug_btree;
57 int hammer_debug_tid;
58 int hammer_debug_recover; /* -1 will disable, +1 will force */
59 int hammer_debug_recover_faults;
60 int hammer_cluster_enable = 1; /* enable read clustering by default */
61 int hammer_count_fsyncs;
62 int hammer_count_inodes;
63 int hammer_count_iqueued;
64 int hammer_count_reclaiming;
65 int hammer_count_records;
66 int hammer_count_record_datas;
67 int hammer_count_volumes;
68 int hammer_count_buffers;
69 int hammer_count_nodes;
70 int64_t hammer_count_extra_space_used;
71 int64_t hammer_stats_btree_lookups;
72 int64_t hammer_stats_btree_searches;
73 int64_t hammer_stats_btree_inserts;
74 int64_t hammer_stats_btree_deletes;
75 int64_t hammer_stats_btree_elements;
76 int64_t hammer_stats_btree_splits;
77 int64_t hammer_stats_btree_iterations;
78 int64_t hammer_stats_btree_root_iterations;
79 int64_t hammer_stats_record_iterations;
81 int64_t hammer_stats_file_read;
82 int64_t hammer_stats_file_write;
83 int64_t hammer_stats_file_iopsr;
84 int64_t hammer_stats_file_iopsw;
85 int64_t hammer_stats_disk_read;
86 int64_t hammer_stats_disk_write;
87 int64_t hammer_stats_inode_flushes;
88 int64_t hammer_stats_commits;
90 int hammer_count_dirtybufspace; /* global */
91 int hammer_count_refedbufs; /* global */
92 int hammer_count_reservations;
93 int hammer_count_io_running_read;
94 int hammer_count_io_running_write;
95 int hammer_count_io_locked;
96 int hammer_limit_dirtybufspace; /* per-mount */
97 int hammer_limit_recs; /* as a whole XXX */
98 int hammer_limit_inode_recs = 1024; /* per inode */
99 int hammer_autoflush = 2000; /* auto flush */
100 int hammer_bio_count;
101 int hammer_verify_zone;
102 int hammer_verify_data = 1;
103 int hammer_write_mode;
104 int hammer_yield_check = 16;
105 int64_t hammer_contention_count;
106 int64_t hammer_zone_limit;
108 SYSCTL_NODE(_vfs, OID_AUTO, hammer, CTLFLAG_RW, 0, "HAMMER filesystem");
109 SYSCTL_INT(_vfs_hammer, OID_AUTO, supported_version, CTLFLAG_RD,
110 &hammer_supported_version, 0, "");
111 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_general, CTLFLAG_RW,
112 &hammer_debug_general, 0, "");
113 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_io, CTLFLAG_RW,
114 &hammer_debug_io, 0, "");
115 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_debug, CTLFLAG_RW,
116 &hammer_debug_debug, 0, "");
117 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_inode, CTLFLAG_RW,
118 &hammer_debug_inode, 0, "");
119 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_locks, CTLFLAG_RW,
120 &hammer_debug_locks, 0, "");
121 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_btree, CTLFLAG_RW,
122 &hammer_debug_btree, 0, "");
123 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_tid, CTLFLAG_RW,
124 &hammer_debug_tid, 0, "");
125 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover, CTLFLAG_RW,
126 &hammer_debug_recover, 0, "");
127 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover_faults, CTLFLAG_RW,
128 &hammer_debug_recover_faults, 0, "");
129 SYSCTL_INT(_vfs_hammer, OID_AUTO, cluster_enable, CTLFLAG_RW,
130 &hammer_cluster_enable, 0, "");
132 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_dirtybufspace, CTLFLAG_RW,
133 &hammer_limit_dirtybufspace, 0, "");
134 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_recs, CTLFLAG_RW,
135 &hammer_limit_recs, 0, "");
136 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_inode_recs, CTLFLAG_RW,
137 &hammer_limit_inode_recs, 0, "");
139 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_fsyncs, CTLFLAG_RD,
140 &hammer_count_fsyncs, 0, "");
141 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_inodes, CTLFLAG_RD,
142 &hammer_count_inodes, 0, "");
143 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_iqueued, CTLFLAG_RD,
144 &hammer_count_iqueued, 0, "");
145 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reclaiming, CTLFLAG_RD,
146 &hammer_count_reclaiming, 0, "");
147 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_records, CTLFLAG_RD,
148 &hammer_count_records, 0, "");
149 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_record_datas, CTLFLAG_RD,
150 &hammer_count_record_datas, 0, "");
151 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_volumes, CTLFLAG_RD,
152 &hammer_count_volumes, 0, "");
153 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_buffers, CTLFLAG_RD,
154 &hammer_count_buffers, 0, "");
155 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_nodes, CTLFLAG_RD,
156 &hammer_count_nodes, 0, "");
157 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, count_extra_space_used, CTLFLAG_RD,
158 &hammer_count_extra_space_used, 0, "");
160 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_searches, CTLFLAG_RD,
161 &hammer_stats_btree_searches, 0, "");
162 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_lookups, CTLFLAG_RD,
163 &hammer_stats_btree_lookups, 0, "");
164 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_inserts, CTLFLAG_RD,
165 &hammer_stats_btree_inserts, 0, "");
166 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_deletes, CTLFLAG_RD,
167 &hammer_stats_btree_deletes, 0, "");
168 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_elements, CTLFLAG_RD,
169 &hammer_stats_btree_elements, 0, "");
170 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_splits, CTLFLAG_RD,
171 &hammer_stats_btree_splits, 0, "");
172 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_iterations, CTLFLAG_RD,
173 &hammer_stats_btree_iterations, 0, "");
174 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_root_iterations, CTLFLAG_RD,
175 &hammer_stats_btree_root_iterations, 0, "");
176 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_record_iterations, CTLFLAG_RD,
177 &hammer_stats_record_iterations, 0, "");
179 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_read, CTLFLAG_RD,
180 &hammer_stats_file_read, 0, "");
181 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_write, CTLFLAG_RD,
182 &hammer_stats_file_write, 0, "");
183 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_iopsr, CTLFLAG_RD,
184 &hammer_stats_file_iopsr, 0, "");
185 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_file_iopsw, CTLFLAG_RD,
186 &hammer_stats_file_iopsw, 0, "");
187 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_disk_read, CTLFLAG_RD,
188 &hammer_stats_disk_read, 0, "");
189 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_disk_write, CTLFLAG_RD,
190 &hammer_stats_disk_write, 0, "");
191 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_inode_flushes, CTLFLAG_RD,
192 &hammer_stats_inode_flushes, 0, "");
193 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_commits, CTLFLAG_RD,
194 &hammer_stats_commits, 0, "");
196 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_dirtybufspace, CTLFLAG_RD,
197 &hammer_count_dirtybufspace, 0, "");
198 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_refedbufs, CTLFLAG_RD,
199 &hammer_count_refedbufs, 0, "");
200 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reservations, CTLFLAG_RD,
201 &hammer_count_reservations, 0, "");
202 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_running_read, CTLFLAG_RD,
203 &hammer_count_io_running_read, 0, "");
204 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_locked, CTLFLAG_RD,
205 &hammer_count_io_locked, 0, "");
206 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_running_write, CTLFLAG_RD,
207 &hammer_count_io_running_write, 0, "");
208 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, zone_limit, CTLFLAG_RW,
209 &hammer_zone_limit, 0, "");
210 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, contention_count, CTLFLAG_RW,
211 &hammer_contention_count, 0, "");
212 SYSCTL_INT(_vfs_hammer, OID_AUTO, autoflush, CTLFLAG_RW,
213 &hammer_autoflush, 0, "");
214 SYSCTL_INT(_vfs_hammer, OID_AUTO, verify_zone, CTLFLAG_RW,
215 &hammer_verify_zone, 0, "");
216 SYSCTL_INT(_vfs_hammer, OID_AUTO, verify_data, CTLFLAG_RW,
217 &hammer_verify_data, 0, "");
218 SYSCTL_INT(_vfs_hammer, OID_AUTO, write_mode, CTLFLAG_RW,
219 &hammer_write_mode, 0, "");
220 SYSCTL_INT(_vfs_hammer, OID_AUTO, yield_check, CTLFLAG_RW,
221 &hammer_yield_check, 0, "");
223 KTR_INFO_MASTER(hammer);
226 * VFS ABI
228 static void hammer_free_hmp(struct mount *mp);
230 static int hammer_vfs_mount(struct mount *mp, char *path, caddr_t data,
231 struct ucred *cred);
232 static int hammer_vfs_unmount(struct mount *mp, int mntflags);
233 static int hammer_vfs_root(struct mount *mp, struct vnode **vpp);
234 static int hammer_vfs_statfs(struct mount *mp, struct statfs *sbp,
235 struct ucred *cred);
236 static int hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
237 struct ucred *cred);
238 static int hammer_vfs_sync(struct mount *mp, int waitfor);
239 static int hammer_vfs_vget(struct mount *mp, struct vnode *dvp,
240 ino_t ino, struct vnode **vpp);
241 static int hammer_vfs_init(struct vfsconf *conf);
242 static int hammer_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
243 struct fid *fhp, struct vnode **vpp);
244 static int hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp);
245 static int hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
246 int *exflagsp, struct ucred **credanonp);
249 static struct vfsops hammer_vfsops = {
250 .vfs_mount = hammer_vfs_mount,
251 .vfs_unmount = hammer_vfs_unmount,
252 .vfs_root = hammer_vfs_root,
253 .vfs_statfs = hammer_vfs_statfs,
254 .vfs_statvfs = hammer_vfs_statvfs,
255 .vfs_sync = hammer_vfs_sync,
256 .vfs_vget = hammer_vfs_vget,
257 .vfs_init = hammer_vfs_init,
258 .vfs_vptofh = hammer_vfs_vptofh,
259 .vfs_fhtovp = hammer_vfs_fhtovp,
260 .vfs_checkexp = hammer_vfs_checkexp
263 MALLOC_DEFINE(M_HAMMER, "HAMMER-mount", "");
265 VFS_SET(hammer_vfsops, hammer, 0);
266 MODULE_VERSION(hammer, 1);
268 static int
269 hammer_vfs_init(struct vfsconf *conf)
271 int n;
273 if (hammer_limit_recs == 0) {
274 hammer_limit_recs = nbuf * 25;
275 n = kmalloc_limit(M_HAMMER) / 512;
276 if (hammer_limit_recs > n)
277 hammer_limit_recs = n;
279 if (hammer_limit_dirtybufspace == 0) {
280 hammer_limit_dirtybufspace = hidirtybufspace / 2;
281 if (hammer_limit_dirtybufspace < 100)
282 hammer_limit_dirtybufspace = 100;
284 return(0);
287 static int
288 hammer_vfs_mount(struct mount *mp, char *mntpt, caddr_t data,
289 struct ucred *cred)
291 struct hammer_mount_info info;
292 hammer_mount_t hmp;
293 hammer_volume_t rootvol;
294 struct vnode *rootvp;
295 struct vnode *devvp = NULL;
296 const char *upath; /* volume name in userspace */
297 char *path; /* volume name in system space */
298 int error;
299 int i;
300 int master_id;
301 int maxinodes;
304 * Accept hammer_mount_info. mntpt is NULL for root mounts at boot.
306 if (mntpt == NULL) {
307 if ((error = bdevvp(rootdev, &devvp))) {
308 kprintf("hammer_mountroot: can't find devvp\n");
309 return (error);
311 mp->mnt_flag &= ~MNT_RDONLY; /* mount R/W */
312 bzero(&info, sizeof(info));
313 info.asof = 0;
314 info.hflags = 0;
315 info.nvolumes = 1;
316 } else {
317 if ((error = copyin(data, &info, sizeof(info))) != 0)
318 return (error);
322 * updating or new mount
324 if (mp->mnt_flag & MNT_UPDATE) {
325 hmp = (void *)mp->mnt_data;
326 KKASSERT(hmp != NULL);
327 } else {
328 if (info.nvolumes <= 0 || info.nvolumes >= 32768)
329 return (EINVAL);
330 hmp = NULL;
334 * master-id validation. The master id may not be changed by a
335 * mount update.
337 if (info.hflags & HMNT_MASTERID) {
338 if (hmp && hmp->master_id != info.master_id) {
339 kprintf("hammer: cannot change master id "
340 "with mount update\n");
341 return(EINVAL);
343 master_id = info.master_id;
344 if (master_id < -1 || master_id >= HAMMER_MAX_MASTERS)
345 return (EINVAL);
346 } else {
347 if (hmp)
348 master_id = hmp->master_id;
349 else
350 master_id = 0;
354 * Interal mount data structure
356 if (hmp == NULL) {
357 hmp = kmalloc(sizeof(*hmp), M_HAMMER, M_WAITOK | M_ZERO);
358 mp->mnt_data = (qaddr_t)hmp;
359 hmp->mp = mp;
360 /*TAILQ_INIT(&hmp->recycle_list);*/
363 * Make sure kmalloc type limits are set appropriately. If root
364 * increases the vnode limit you may have to do a dummy remount
365 * to adjust the HAMMER inode limit.
367 kmalloc_create(&hmp->m_misc, "HAMMER-others");
368 kmalloc_create(&hmp->m_inodes, "HAMMER-inodes");
370 maxinodes = desiredvnodes + desiredvnodes / 5 +
371 HAMMER_RECLAIM_WAIT;
372 kmalloc_raise_limit(hmp->m_inodes,
373 maxinodes * sizeof(struct hammer_inode));
375 hmp->root_btree_beg.localization = 0x00000000U;
376 hmp->root_btree_beg.obj_id = -0x8000000000000000LL;
377 hmp->root_btree_beg.key = -0x8000000000000000LL;
378 hmp->root_btree_beg.create_tid = 1;
379 hmp->root_btree_beg.delete_tid = 1;
380 hmp->root_btree_beg.rec_type = 0;
381 hmp->root_btree_beg.obj_type = 0;
383 hmp->root_btree_end.localization = 0xFFFFFFFFU;
384 hmp->root_btree_end.obj_id = 0x7FFFFFFFFFFFFFFFLL;
385 hmp->root_btree_end.key = 0x7FFFFFFFFFFFFFFFLL;
386 hmp->root_btree_end.create_tid = 0xFFFFFFFFFFFFFFFFULL;
387 hmp->root_btree_end.delete_tid = 0; /* special case */
388 hmp->root_btree_end.rec_type = 0xFFFFU;
389 hmp->root_btree_end.obj_type = 0;
391 hmp->krate.freq = 1; /* maximum reporting rate (hz) */
392 hmp->krate.count = -16; /* initial burst */
394 hmp->sync_lock.refs = 1;
395 hmp->free_lock.refs = 1;
396 hmp->undo_lock.refs = 1;
397 hmp->blkmap_lock.refs = 1;
399 TAILQ_INIT(&hmp->delay_list);
400 TAILQ_INIT(&hmp->flush_group_list);
401 TAILQ_INIT(&hmp->objid_cache_list);
402 TAILQ_INIT(&hmp->undo_lru_list);
403 TAILQ_INIT(&hmp->reclaim_list);
405 hmp->hflags &= ~HMNT_USERFLAGS;
406 hmp->hflags |= info.hflags & HMNT_USERFLAGS;
408 hmp->master_id = master_id;
410 if (info.asof) {
411 mp->mnt_flag |= MNT_RDONLY;
412 hmp->asof = info.asof;
413 } else {
414 hmp->asof = HAMMER_MAX_TID;
418 * Re-open read-write if originally read-only, or vise-versa.
420 if (mp->mnt_flag & MNT_UPDATE) {
421 error = 0;
422 if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
423 kprintf("HAMMER read-only -> read-write\n");
424 hmp->ronly = 0;
425 RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
426 hammer_adjust_volume_mode, NULL);
427 rootvol = hammer_get_root_volume(hmp, &error);
428 if (rootvol) {
429 hammer_recover_flush_buffers(hmp, rootvol, 1);
430 bcopy(rootvol->ondisk->vol0_blockmap,
431 hmp->blockmap,
432 sizeof(hmp->blockmap));
433 hammer_rel_volume(rootvol, 0);
435 RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
436 hammer_reload_inode, NULL);
437 /* kernel clears MNT_RDONLY */
438 } else if (hmp->ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
439 kprintf("HAMMER read-write -> read-only\n");
440 hmp->ronly = 1; /* messy */
441 RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
442 hammer_reload_inode, NULL);
443 hmp->ronly = 0;
444 hammer_flusher_sync(hmp);
445 hammer_flusher_sync(hmp);
446 hammer_flusher_sync(hmp);
447 hmp->ronly = 1;
448 RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
449 hammer_adjust_volume_mode, NULL);
451 return(error);
454 RB_INIT(&hmp->rb_vols_root);
455 RB_INIT(&hmp->rb_inos_root);
456 RB_INIT(&hmp->rb_nods_root);
457 RB_INIT(&hmp->rb_undo_root);
458 RB_INIT(&hmp->rb_resv_root);
459 RB_INIT(&hmp->rb_bufs_root);
460 RB_INIT(&hmp->rb_pfsm_root);
462 hmp->ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
464 TAILQ_INIT(&hmp->volu_list);
465 TAILQ_INIT(&hmp->undo_list);
466 TAILQ_INIT(&hmp->data_list);
467 TAILQ_INIT(&hmp->meta_list);
468 TAILQ_INIT(&hmp->lose_list);
471 * Load volumes
473 path = objcache_get(namei_oc, M_WAITOK);
474 hmp->nvolumes = -1;
475 for (i = 0; i < info.nvolumes; ++i) {
476 if (mntpt == NULL) {
478 * Root mount.
479 * Only one volume; and no need for copyin.
481 KKASSERT(info.nvolumes == 1);
482 ksnprintf(path, MAXPATHLEN, "/dev/%s",
483 mp->mnt_stat.f_mntfromname);
484 error = 0;
485 } else {
486 error = copyin(&info.volumes[i], &upath,
487 sizeof(char *));
488 if (error == 0)
489 error = copyinstr(upath, path,
490 MAXPATHLEN, NULL);
492 if (error == 0)
493 error = hammer_install_volume(hmp, path, devvp);
494 if (error)
495 break;
497 objcache_put(namei_oc, path);
500 * Make sure we found a root volume
502 if (error == 0 && hmp->rootvol == NULL) {
503 kprintf("hammer_mount: No root volume found!\n");
504 error = EINVAL;
508 * Check that all required volumes are available
510 if (error == 0 && hammer_mountcheck_volumes(hmp)) {
511 kprintf("hammer_mount: Missing volumes, cannot mount!\n");
512 error = EINVAL;
515 if (error) {
516 hammer_free_hmp(mp);
517 return (error);
521 * No errors, setup enough of the mount point so we can lookup the
522 * root vnode.
524 mp->mnt_iosize_max = MAXPHYS;
525 mp->mnt_kern_flag |= MNTK_FSMID;
528 * note: f_iosize is used by vnode_pager_haspage() when constructing
529 * its VOP_BMAP call.
531 mp->mnt_stat.f_iosize = HAMMER_BUFSIZE;
532 mp->mnt_stat.f_bsize = HAMMER_BUFSIZE;
534 mp->mnt_vstat.f_frsize = HAMMER_BUFSIZE;
535 mp->mnt_vstat.f_bsize = HAMMER_BUFSIZE;
537 mp->mnt_maxsymlinklen = 255;
538 mp->mnt_flag |= MNT_LOCAL;
540 vfs_add_vnodeops(mp, &hammer_vnode_vops, &mp->mnt_vn_norm_ops);
541 vfs_add_vnodeops(mp, &hammer_spec_vops, &mp->mnt_vn_spec_ops);
542 vfs_add_vnodeops(mp, &hammer_fifo_vops, &mp->mnt_vn_fifo_ops);
545 * The root volume's ondisk pointer is only valid if we hold a
546 * reference to it.
548 rootvol = hammer_get_root_volume(hmp, &error);
549 if (error)
550 goto failed;
553 * Perform any necessary UNDO operations. The recovery code does
554 * call hammer_undo_lookup() so we have to pre-cache the blockmap,
555 * and then re-copy it again after recovery is complete.
557 * If this is a read-only mount the UNDO information is retained
558 * in memory in the form of dirty buffer cache buffers, and not
559 * written back to the media.
561 bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
562 sizeof(hmp->blockmap));
565 * Check filesystem version
567 hmp->version = rootvol->ondisk->vol_version;
568 if (hmp->version < HAMMER_VOL_VERSION_MIN ||
569 hmp->version > HAMMER_VOL_VERSION_MAX) {
570 kprintf("HAMMER: mount unsupported fs version %d\n",
571 hmp->version);
572 error = ERANGE;
573 goto done;
577 * The undo_rec_limit limits the size of flush groups to avoid
578 * blowing out the UNDO FIFO. This calculation is typically in
579 * the tens of thousands and is designed primarily when small
580 * HAMMER filesystems are created.
582 hmp->undo_rec_limit = hammer_undo_max(hmp) / 8192 + 100;
583 if (hammer_debug_general & 0x0001)
584 kprintf("HAMMER: undo_rec_limit %d\n", hmp->undo_rec_limit);
586 error = hammer_recover(hmp, rootvol);
587 if (error) {
588 kprintf("Failed to recover HAMMER filesystem on mount\n");
589 goto done;
593 * Finish setup now that we have a good root volume.
595 * The top 16 bits of fsid.val[1] is a pfs id.
597 ksnprintf(mp->mnt_stat.f_mntfromname,
598 sizeof(mp->mnt_stat.f_mntfromname), "%s",
599 rootvol->ondisk->vol_name);
600 mp->mnt_stat.f_fsid.val[0] =
601 crc32((char *)&rootvol->ondisk->vol_fsid + 0, 8);
602 mp->mnt_stat.f_fsid.val[1] =
603 crc32((char *)&rootvol->ondisk->vol_fsid + 8, 8);
604 mp->mnt_stat.f_fsid.val[1] &= 0x0000FFFF;
606 mp->mnt_vstat.f_fsid_uuid = rootvol->ondisk->vol_fsid;
607 mp->mnt_vstat.f_fsid = crc32(&mp->mnt_vstat.f_fsid_uuid,
608 sizeof(mp->mnt_vstat.f_fsid_uuid));
611 * Certain often-modified fields in the root volume are cached in
612 * the hammer_mount structure so we do not have to generate lots
613 * of little UNDO structures for them.
615 * Recopy after recovery. This also has the side effect of
616 * setting our cached undo FIFO's first_offset, which serves to
617 * placemark the FIFO start for the NEXT flush cycle while the
618 * on-disk first_offset represents the LAST flush cycle.
620 hmp->next_tid = rootvol->ondisk->vol0_next_tid;
621 hmp->flush_tid1 = hmp->next_tid;
622 hmp->flush_tid2 = hmp->next_tid;
623 bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
624 sizeof(hmp->blockmap));
625 hmp->copy_stat_freebigblocks = rootvol->ondisk->vol0_stat_freebigblocks;
627 hammer_flusher_create(hmp);
630 * Locate the root directory using the root cluster's B-Tree as a
631 * starting point. The root directory uses an obj_id of 1.
633 * FUTURE: Leave the root directory cached referenced but unlocked
634 * in hmp->rootvp (need to flush it on unmount).
636 error = hammer_vfs_vget(mp, NULL, 1, &rootvp);
637 if (error)
638 goto done;
639 vput(rootvp);
640 /*vn_unlock(hmp->rootvp);*/
642 done:
643 hammer_rel_volume(rootvol, 0);
644 failed:
646 * Cleanup and return.
648 if (error)
649 hammer_free_hmp(mp);
650 return (error);
653 static int
654 hammer_vfs_unmount(struct mount *mp, int mntflags)
656 #if 0
657 struct hammer_mount *hmp = (void *)mp->mnt_data;
658 #endif
659 int flags;
660 int error;
663 * Clean out the vnodes
665 flags = 0;
666 if (mntflags & MNT_FORCE)
667 flags |= FORCECLOSE;
668 if ((error = vflush(mp, 0, flags)) != 0)
669 return (error);
672 * Clean up the internal mount structure and related entities. This
673 * may issue I/O.
675 hammer_free_hmp(mp);
676 return(0);
680 * Clean up the internal mount structure and disassociate it from the mount.
681 * This may issue I/O.
683 static void
684 hammer_free_hmp(struct mount *mp)
686 struct hammer_mount *hmp = (void *)mp->mnt_data;
687 hammer_flush_group_t flg;
688 int count;
689 int dummy;
692 * Flush anything dirty. This won't even run if the
693 * filesystem errored-out.
695 count = 0;
696 while (hammer_flusher_haswork(hmp)) {
697 hammer_flusher_sync(hmp);
698 ++count;
699 if (count >= 5) {
700 if (count == 5)
701 kprintf("HAMMER: umount flushing.");
702 else
703 kprintf(".");
704 tsleep(&dummy, 0, "hmrufl", hz);
706 if (count == 30) {
707 kprintf("giving up\n");
708 break;
711 if (count >= 5 && count < 30)
712 kprintf("\n");
715 * If the mount had a critical error we have to destroy any
716 * remaining inodes before we can finish cleaning up the flusher.
718 if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) {
719 RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
720 hammer_destroy_inode_callback, NULL);
724 * There shouldn't be any inodes left now and any left over
725 * flush groups should now be empty.
727 KKASSERT(RB_EMPTY(&hmp->rb_inos_root));
728 while ((flg = TAILQ_FIRST(&hmp->flush_group_list)) != NULL) {
729 TAILQ_REMOVE(&hmp->flush_group_list, flg, flush_entry);
730 KKASSERT(TAILQ_EMPTY(&flg->flush_list));
731 if (flg->refs) {
732 kprintf("HAMMER: Warning, flush_group %p was "
733 "not empty on umount!\n", flg);
735 kfree(flg, hmp->m_misc);
739 * We can finally destroy the flusher
741 hammer_flusher_destroy(hmp);
744 * We may have held recovered buffers due to a read-only mount.
745 * These must be discarded.
747 if (hmp->ronly)
748 hammer_recover_flush_buffers(hmp, NULL, -1);
751 * Unload buffers and then volumes
753 RB_SCAN(hammer_buf_rb_tree, &hmp->rb_bufs_root, NULL,
754 hammer_unload_buffer, NULL);
755 RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
756 hammer_unload_volume, NULL);
758 mp->mnt_data = NULL;
759 mp->mnt_flag &= ~MNT_LOCAL;
760 hmp->mp = NULL;
761 hammer_destroy_objid_cache(hmp);
762 kmalloc_destroy(&hmp->m_misc);
763 kmalloc_destroy(&hmp->m_inodes);
764 kfree(hmp, M_HAMMER);
768 * Report critical errors. ip may be NULL.
770 void
771 hammer_critical_error(hammer_mount_t hmp, hammer_inode_t ip,
772 int error, const char *msg)
774 hmp->flags |= HAMMER_MOUNT_CRITICAL_ERROR;
775 krateprintf(&hmp->krate,
776 "HAMMER(%s): Critical error inode=%lld %s\n",
777 hmp->mp->mnt_stat.f_mntfromname,
778 (long long)(ip ? ip->obj_id : -1), msg);
779 if (hmp->ronly == 0) {
780 hmp->ronly = 2; /* special errored read-only mode */
781 hmp->mp->mnt_flag |= MNT_RDONLY;
782 kprintf("HAMMER(%s): Forcing read-only mode\n",
783 hmp->mp->mnt_stat.f_mntfromname);
785 hmp->error = error;
790 * Obtain a vnode for the specified inode number. An exclusively locked
791 * vnode is returned.
794 hammer_vfs_vget(struct mount *mp, struct vnode *dvp,
795 ino_t ino, struct vnode **vpp)
797 struct hammer_transaction trans;
798 struct hammer_mount *hmp = (void *)mp->mnt_data;
799 struct hammer_inode *ip;
800 int error;
801 u_int32_t localization;
803 hammer_simple_transaction(&trans, hmp);
806 * If a directory vnode is supplied (mainly NFS) then we can acquire
807 * the PFS domain from it. Otherwise we would only be able to vget
808 * inodes in the root PFS.
810 if (dvp) {
811 localization = HAMMER_DEF_LOCALIZATION +
812 VTOI(dvp)->obj_localization;
813 } else {
814 localization = HAMMER_DEF_LOCALIZATION;
818 * Lookup the requested HAMMER inode. The structure must be
819 * left unlocked while we manipulate the related vnode to avoid
820 * a deadlock.
822 ip = hammer_get_inode(&trans, NULL, ino,
823 hmp->asof, localization,
824 0, &error);
825 if (ip == NULL) {
826 *vpp = NULL;
827 hammer_done_transaction(&trans);
828 return(error);
830 error = hammer_get_vnode(ip, vpp);
831 hammer_rel_inode(ip, 0);
832 hammer_done_transaction(&trans);
833 return (error);
837 * Return the root vnode for the filesystem.
839 * HAMMER stores the root vnode in the hammer_mount structure so
840 * getting it is easy.
842 static int
843 hammer_vfs_root(struct mount *mp, struct vnode **vpp)
845 #if 0
846 struct hammer_mount *hmp = (void *)mp->mnt_data;
847 #endif
848 int error;
850 error = hammer_vfs_vget(mp, NULL, 1, vpp);
851 return (error);
854 static int
855 hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
857 struct hammer_mount *hmp = (void *)mp->mnt_data;
858 hammer_volume_t volume;
859 hammer_volume_ondisk_t ondisk;
860 int error;
861 int64_t bfree;
862 int64_t breserved;
864 volume = hammer_get_root_volume(hmp, &error);
865 if (error)
866 return(error);
867 ondisk = volume->ondisk;
870 * Basic stats
872 _hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE, &breserved);
873 mp->mnt_stat.f_files = ondisk->vol0_stat_inodes;
874 bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE;
875 hammer_rel_volume(volume, 0);
877 mp->mnt_stat.f_bfree = (bfree - breserved) / HAMMER_BUFSIZE;
878 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
879 if (mp->mnt_stat.f_files < 0)
880 mp->mnt_stat.f_files = 0;
882 *sbp = mp->mnt_stat;
883 return(0);
886 static int
887 hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
889 struct hammer_mount *hmp = (void *)mp->mnt_data;
890 hammer_volume_t volume;
891 hammer_volume_ondisk_t ondisk;
892 int error;
893 int64_t bfree;
894 int64_t breserved;
896 volume = hammer_get_root_volume(hmp, &error);
897 if (error)
898 return(error);
899 ondisk = volume->ondisk;
902 * Basic stats
904 _hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE, &breserved);
905 mp->mnt_vstat.f_files = ondisk->vol0_stat_inodes;
906 bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE;
907 hammer_rel_volume(volume, 0);
909 mp->mnt_vstat.f_bfree = (bfree - breserved) / HAMMER_BUFSIZE;
910 mp->mnt_vstat.f_bavail = mp->mnt_stat.f_bfree;
911 if (mp->mnt_vstat.f_files < 0)
912 mp->mnt_vstat.f_files = 0;
913 *sbp = mp->mnt_vstat;
914 return(0);
918 * Sync the filesystem. Currently we have to run it twice, the second
919 * one will advance the undo start index to the end index, so if a crash
920 * occurs no undos will be run on mount.
922 * We do not sync the filesystem if we are called from a panic. If we did
923 * we might end up blowing up a sync that was already in progress.
925 static int
926 hammer_vfs_sync(struct mount *mp, int waitfor)
928 struct hammer_mount *hmp = (void *)mp->mnt_data;
929 int error;
931 if (panicstr == NULL) {
932 error = hammer_sync_hmp(hmp, waitfor);
933 } else {
934 error = EIO;
936 return (error);
940 * Convert a vnode to a file handle.
942 static int
943 hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp)
945 hammer_inode_t ip;
947 KKASSERT(MAXFIDSZ >= 16);
948 ip = VTOI(vp);
949 fhp->fid_len = offsetof(struct fid, fid_data[16]);
950 fhp->fid_ext = ip->obj_localization >> 16;
951 bcopy(&ip->obj_id, fhp->fid_data + 0, sizeof(ip->obj_id));
952 bcopy(&ip->obj_asof, fhp->fid_data + 8, sizeof(ip->obj_asof));
953 return(0);
958 * Convert a file handle back to a vnode.
960 * Use rootvp to enforce PFS isolation when a PFS is exported via a
961 * null mount.
963 static int
964 hammer_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
965 struct fid *fhp, struct vnode **vpp)
967 struct hammer_transaction trans;
968 struct hammer_inode *ip;
969 struct hammer_inode_info info;
970 int error;
971 u_int32_t localization;
973 bcopy(fhp->fid_data + 0, &info.obj_id, sizeof(info.obj_id));
974 bcopy(fhp->fid_data + 8, &info.obj_asof, sizeof(info.obj_asof));
975 if (rootvp)
976 localization = VTOI(rootvp)->obj_localization;
977 else
978 localization = (u_int32_t)fhp->fid_ext << 16;
980 hammer_simple_transaction(&trans, (void *)mp->mnt_data);
983 * Get/allocate the hammer_inode structure. The structure must be
984 * unlocked while we manipulate the related vnode to avoid a
985 * deadlock.
987 ip = hammer_get_inode(&trans, NULL, info.obj_id,
988 info.obj_asof, localization, 0, &error);
989 if (ip == NULL) {
990 *vpp = NULL;
991 return(error);
993 error = hammer_get_vnode(ip, vpp);
994 hammer_rel_inode(ip, 0);
995 hammer_done_transaction(&trans);
996 return (error);
999 static int
1000 hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
1001 int *exflagsp, struct ucred **credanonp)
1003 hammer_mount_t hmp = (void *)mp->mnt_data;
1004 struct netcred *np;
1005 int error;
1007 np = vfs_export_lookup(mp, &hmp->export, nam);
1008 if (np) {
1009 *exflagsp = np->netc_exflags;
1010 *credanonp = &np->netc_anon;
1011 error = 0;
1012 } else {
1013 error = EACCES;
1015 return (error);
1020 hammer_vfs_export(struct mount *mp, int op, const struct export_args *export)
1022 hammer_mount_t hmp = (void *)mp->mnt_data;
1023 int error;
1025 switch(op) {
1026 case MOUNTCTL_SET_EXPORT:
1027 error = vfs_export(mp, &hmp->export, export);
1028 break;
1029 default:
1030 error = EOPNOTSUPP;
1031 break;
1033 return(error);