Merge from vendor branch NETGRAPH:
[dragonfly.git] / sys / vfs / hammer / hammer_vfsops.c
CommitLineData
427e5fc6 1/*
b84de5af 2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
427e5fc6
MD
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:
10 *
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.
20 *
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.
33 *
c82af904 34 * $DragonFly: src/sys/vfs/hammer/hammer_vfsops.c,v 1.54 2008/06/26 04:06:23 dillon Exp $
427e5fc6
MD
35 */
36
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>
b3deaf57 45#include <sys/sysctl.h>
427e5fc6
MD
46#include <sys/buf.h>
47#include <sys/buf2.h>
48#include "hammer.h"
49
2f85fa4d 50int hammer_debug_io;
d5ef456e 51int hammer_debug_general;
77062c8a 52int hammer_debug_debug;
e8599db1 53int hammer_debug_inode;
7d683b0f 54int hammer_debug_locks;
b3deaf57 55int hammer_debug_btree;
d113fda1 56int hammer_debug_tid;
cebe9493 57int hammer_debug_recover; /* -1 will disable, +1 will force */
46fe7ae1 58int hammer_debug_recover_faults;
a99b9ea2 59int hammer_debug_cluster_enable = 1; /* enable read clustering by default */
b3deaf57 60int hammer_count_inodes;
af209b0f 61int hammer_count_iqueued;
9f5097dc 62int hammer_count_reclaiming;
b3deaf57
MD
63int hammer_count_records;
64int hammer_count_record_datas;
65int hammer_count_volumes;
b3deaf57
MD
66int hammer_count_buffers;
67int hammer_count_nodes;
cb51be26
MD
68int64_t hammer_stats_btree_lookups;
69int64_t hammer_stats_btree_searches;
70int64_t hammer_stats_btree_inserts;
71int64_t hammer_stats_btree_deletes;
72int64_t hammer_stats_btree_elements;
73int64_t hammer_stats_btree_splits;
74int64_t hammer_stats_btree_iterations;
75int64_t hammer_stats_record_iterations;
9480ff55 76int hammer_count_dirtybufs; /* global */
a99b9ea2 77int hammer_count_refedbufs; /* global */
0832c9bb 78int hammer_count_reservations;
a99b9ea2
MD
79int hammer_count_io_running_read;
80int hammer_count_io_running_write;
81int hammer_count_io_locked;
9f5097dc 82int hammer_limit_dirtybufs; /* per-mount */
0832c9bb 83int hammer_limit_recs; /* as a whole XXX */
af209b0f 84int hammer_limit_iqueued; /* per-mount */
1f07f686 85int hammer_bio_count;
cb51be26
MD
86int hammer_verify_zone;
87int hammer_write_mode;
7d683b0f 88int64_t hammer_contention_count;
f03c9cf4 89int64_t hammer_zone_limit;
b3deaf57
MD
90
91SYSCTL_NODE(_vfs, OID_AUTO, hammer, CTLFLAG_RW, 0, "HAMMER filesystem");
d5ef456e
MD
92SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_general, CTLFLAG_RW,
93 &hammer_debug_general, 0, "");
2f85fa4d
MD
94SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_io, CTLFLAG_RW,
95 &hammer_debug_io, 0, "");
77062c8a
MD
96SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_debug, CTLFLAG_RW,
97 &hammer_debug_debug, 0, "");
e8599db1
MD
98SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_inode, CTLFLAG_RW,
99 &hammer_debug_inode, 0, "");
7d683b0f
MD
100SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_locks, CTLFLAG_RW,
101 &hammer_debug_locks, 0, "");
b3deaf57
MD
102SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_btree, CTLFLAG_RW,
103 &hammer_debug_btree, 0, "");
d113fda1
MD
104SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_tid, CTLFLAG_RW,
105 &hammer_debug_tid, 0, "");
b33e2cc0
MD
106SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover, CTLFLAG_RW,
107 &hammer_debug_recover, 0, "");
46fe7ae1
MD
108SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover_faults, CTLFLAG_RW,
109 &hammer_debug_recover_faults, 0, "");
a99b9ea2
MD
110SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_cluster_enable, CTLFLAG_RW,
111 &hammer_debug_cluster_enable, 0, "");
9480ff55
MD
112
113SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_dirtybufs, CTLFLAG_RW,
114 &hammer_limit_dirtybufs, 0, "");
47637bff
MD
115SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_recs, CTLFLAG_RW,
116 &hammer_limit_recs, 0, "");
af209b0f
MD
117SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_iqueued, CTLFLAG_RW,
118 &hammer_limit_iqueued, 0, "");
9480ff55 119
b3deaf57
MD
120SYSCTL_INT(_vfs_hammer, OID_AUTO, count_inodes, CTLFLAG_RD,
121 &hammer_count_inodes, 0, "");
af209b0f
MD
122SYSCTL_INT(_vfs_hammer, OID_AUTO, count_iqueued, CTLFLAG_RD,
123 &hammer_count_iqueued, 0, "");
9f5097dc
MD
124SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reclaiming, CTLFLAG_RD,
125 &hammer_count_reclaiming, 0, "");
b3deaf57
MD
126SYSCTL_INT(_vfs_hammer, OID_AUTO, count_records, CTLFLAG_RD,
127 &hammer_count_records, 0, "");
128SYSCTL_INT(_vfs_hammer, OID_AUTO, count_record_datas, CTLFLAG_RD,
129 &hammer_count_record_datas, 0, "");
130SYSCTL_INT(_vfs_hammer, OID_AUTO, count_volumes, CTLFLAG_RD,
131 &hammer_count_volumes, 0, "");
b3deaf57
MD
132SYSCTL_INT(_vfs_hammer, OID_AUTO, count_buffers, CTLFLAG_RD,
133 &hammer_count_buffers, 0, "");
134SYSCTL_INT(_vfs_hammer, OID_AUTO, count_nodes, CTLFLAG_RD,
135 &hammer_count_nodes, 0, "");
cb51be26
MD
136SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_searches, CTLFLAG_RD,
137 &hammer_stats_btree_searches, 0, "");
138SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_lookups, CTLFLAG_RD,
139 &hammer_stats_btree_lookups, 0, "");
140SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_inserts, CTLFLAG_RD,
141 &hammer_stats_btree_inserts, 0, "");
142SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_deletes, CTLFLAG_RD,
143 &hammer_stats_btree_deletes, 0, "");
144SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_elements, CTLFLAG_RD,
145 &hammer_stats_btree_elements, 0, "");
146SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_splits, CTLFLAG_RD,
147 &hammer_stats_btree_splits, 0, "");
148SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_iterations, CTLFLAG_RD,
149 &hammer_stats_btree_iterations, 0, "");
150SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_record_iterations, CTLFLAG_RD,
151 &hammer_stats_record_iterations, 0, "");
9480ff55
MD
152SYSCTL_INT(_vfs_hammer, OID_AUTO, count_dirtybufs, CTLFLAG_RD,
153 &hammer_count_dirtybufs, 0, "");
a99b9ea2
MD
154SYSCTL_INT(_vfs_hammer, OID_AUTO, count_refedbufs, CTLFLAG_RD,
155 &hammer_count_refedbufs, 0, "");
0832c9bb
MD
156SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reservations, CTLFLAG_RD,
157 &hammer_count_reservations, 0, "");
a99b9ea2
MD
158SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_running_read, CTLFLAG_RD,
159 &hammer_count_io_running_read, 0, "");
160SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_locked, CTLFLAG_RD,
161 &hammer_count_io_locked, 0, "");
162SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_running_write, CTLFLAG_RD,
163 &hammer_count_io_running_write, 0, "");
f03c9cf4
MD
164SYSCTL_QUAD(_vfs_hammer, OID_AUTO, zone_limit, CTLFLAG_RW,
165 &hammer_zone_limit, 0, "");
7d683b0f
MD
166SYSCTL_QUAD(_vfs_hammer, OID_AUTO, contention_count, CTLFLAG_RW,
167 &hammer_contention_count, 0, "");
cb51be26
MD
168SYSCTL_INT(_vfs_hammer, OID_AUTO, verify_zone, CTLFLAG_RW,
169 &hammer_verify_zone, 0, "");
170SYSCTL_INT(_vfs_hammer, OID_AUTO, write_mode, CTLFLAG_RW,
171 &hammer_write_mode, 0, "");
b3deaf57 172
bcac4bbb
MD
173KTR_INFO_MASTER(hammer);
174
427e5fc6
MD
175/*
176 * VFS ABI
177 */
178static void hammer_free_hmp(struct mount *mp);
179
180static int hammer_vfs_mount(struct mount *mp, char *path, caddr_t data,
181 struct ucred *cred);
182static int hammer_vfs_unmount(struct mount *mp, int mntflags);
183static int hammer_vfs_root(struct mount *mp, struct vnode **vpp);
184static int hammer_vfs_statfs(struct mount *mp, struct statfs *sbp,
185 struct ucred *cred);
6f97fce3
MD
186static int hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
187 struct ucred *cred);
427e5fc6 188static int hammer_vfs_sync(struct mount *mp, int waitfor);
513ca7d7
MD
189static int hammer_vfs_vget(struct mount *mp, ino_t ino,
190 struct vnode **vpp);
427e5fc6 191static int hammer_vfs_init(struct vfsconf *conf);
513ca7d7
MD
192static int hammer_vfs_fhtovp(struct mount *mp, struct fid *fhp,
193 struct vnode **vpp);
194static int hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp);
195static int hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
196 int *exflagsp, struct ucred **credanonp);
197
427e5fc6
MD
198
199static struct vfsops hammer_vfsops = {
200 .vfs_mount = hammer_vfs_mount,
201 .vfs_unmount = hammer_vfs_unmount,
202 .vfs_root = hammer_vfs_root,
203 .vfs_statfs = hammer_vfs_statfs,
6f97fce3 204 .vfs_statvfs = hammer_vfs_statvfs,
427e5fc6
MD
205 .vfs_sync = hammer_vfs_sync,
206 .vfs_vget = hammer_vfs_vget,
513ca7d7
MD
207 .vfs_init = hammer_vfs_init,
208 .vfs_vptofh = hammer_vfs_vptofh,
209 .vfs_fhtovp = hammer_vfs_fhtovp,
210 .vfs_checkexp = hammer_vfs_checkexp
427e5fc6
MD
211};
212
213MALLOC_DEFINE(M_HAMMER, "hammer-mount", "hammer mount");
214
215VFS_SET(hammer_vfsops, hammer, 0);
216MODULE_VERSION(hammer, 1);
217
218static int
219hammer_vfs_init(struct vfsconf *conf)
220{
0832c9bb 221 if (hammer_limit_recs == 0) /* XXX TODO */
a99b9ea2 222 hammer_limit_recs = nbuf * 25;
9f5097dc
MD
223 if (hammer_limit_dirtybufs == 0) {
224 hammer_limit_dirtybufs = hidirtybuffers / 2;
225 if (hammer_limit_dirtybufs < 100)
226 hammer_limit_dirtybufs = 100;
227 }
af209b0f
MD
228 if (hammer_limit_iqueued == 0)
229 hammer_limit_iqueued = desiredvnodes / 5;
427e5fc6
MD
230 return(0);
231}
232
233static int
234hammer_vfs_mount(struct mount *mp, char *mntpt, caddr_t data,
235 struct ucred *cred)
236{
237 struct hammer_mount_info info;
a89aec1b
MD
238 hammer_mount_t hmp;
239 hammer_volume_t rootvol;
27ea2398 240 struct vnode *rootvp;
427e5fc6
MD
241 const char *upath; /* volume name in userspace */
242 char *path; /* volume name in system space */
243 int error;
244 int i;
245
246 if ((error = copyin(data, &info, sizeof(info))) != 0)
247 return (error);
51c35492
MD
248 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
249 if (info.nvolumes <= 0 || info.nvolumes >= 32768)
250 return (EINVAL);
251 }
5de0c0e5
MD
252 if ((info.hflags & HMNT_MASTERID) &&
253 (info.masterid < -1 || info.masterid >= HAMMER_MAX_MASTERS)) {
254 return (EINVAL);
255 }
427e5fc6
MD
256
257 /*
258 * Interal mount data structure
259 */
195c19a1
MD
260 if (mp->mnt_flag & MNT_UPDATE) {
261 hmp = (void *)mp->mnt_data;
262 KKASSERT(hmp != NULL);
263 } else {
264 hmp = kmalloc(sizeof(*hmp), M_HAMMER, M_WAITOK | M_ZERO);
265 mp->mnt_data = (qaddr_t)hmp;
266 hmp->mp = mp;
195c19a1 267 hmp->namekey_iterator = mycpu->gd_time_seconds;
46fe7ae1 268 /*TAILQ_INIT(&hmp->recycle_list);*/
47197d71 269
dd94f1b1 270 hmp->root_btree_beg.localization = 0x00000000U;
47197d71
MD
271 hmp->root_btree_beg.obj_id = -0x8000000000000000LL;
272 hmp->root_btree_beg.key = -0x8000000000000000LL;
273 hmp->root_btree_beg.create_tid = 1;
274 hmp->root_btree_beg.delete_tid = 1;
275 hmp->root_btree_beg.rec_type = 0;
276 hmp->root_btree_beg.obj_type = 0;
277
dd94f1b1 278 hmp->root_btree_end.localization = 0xFFFFFFFFU;
47197d71
MD
279 hmp->root_btree_end.obj_id = 0x7FFFFFFFFFFFFFFFLL;
280 hmp->root_btree_end.key = 0x7FFFFFFFFFFFFFFFLL;
281 hmp->root_btree_end.create_tid = 0xFFFFFFFFFFFFFFFFULL;
282 hmp->root_btree_end.delete_tid = 0; /* special case */
283 hmp->root_btree_end.rec_type = 0xFFFFU;
284 hmp->root_btree_end.obj_type = 0;
f03c9cf4 285
9480ff55 286 hmp->sync_lock.refs = 1;
c9b9e29d 287 hmp->free_lock.refs = 1;
d99d6bf5
MD
288 hmp->undo_lock.refs = 1;
289 hmp->blkmap_lock.refs = 1;
9480ff55 290
059819e3 291 TAILQ_INIT(&hmp->flush_list);
cebe9493 292 TAILQ_INIT(&hmp->delay_list);
0729c8c8 293 TAILQ_INIT(&hmp->objid_cache_list);
e8599db1 294 TAILQ_INIT(&hmp->undo_lru_list);
7bc5b8c2 295 TAILQ_INIT(&hmp->reclaim_list);
195c19a1 296 }
51c35492
MD
297 hmp->hflags &= ~HMNT_USERFLAGS;
298 hmp->hflags |= info.hflags & HMNT_USERFLAGS;
5de0c0e5 299 if (info.hflags & HMNT_MASTERID)
5de0c0e5 300 hmp->masterid = info.masterid;
c82af904
MD
301 else
302 hmp->masterid = -1;
7f7c1f84 303 if (info.asof) {
51c35492 304 kprintf("ASOF\n");
7f7c1f84
MD
305 mp->mnt_flag |= MNT_RDONLY;
306 hmp->asof = info.asof;
307 } else {
308 hmp->asof = HAMMER_MAX_TID;
309 }
195c19a1
MD
310
311 /*
51c35492 312 * Re-open read-write if originally read-only, or vise-versa.
195c19a1
MD
313 */
314 if (mp->mnt_flag & MNT_UPDATE) {
51c35492
MD
315 error = 0;
316 if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
317 kprintf("HAMMER read-only -> read-write\n");
318 hmp->ronly = 0;
319 RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
320 hammer_adjust_volume_mode, NULL);
321 rootvol = hammer_get_root_volume(hmp, &error);
322 if (rootvol) {
323 hammer_recover_flush_buffers(hmp, rootvol);
9f5097dc
MD
324 bcopy(rootvol->ondisk->vol0_blockmap,
325 hmp->blockmap,
326 sizeof(hmp->blockmap));
51c35492
MD
327 hammer_rel_volume(rootvol, 0);
328 }
329 RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
330 hammer_reload_inode, NULL);
331 /* kernel clears MNT_RDONLY */
332 } else if (hmp->ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
333 kprintf("HAMMER read-write -> read-only\n");
334 hmp->ronly = 1; /* messy */
335 RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
336 hammer_reload_inode, NULL);
195c19a1 337 hmp->ronly = 0;
51c35492
MD
338 hammer_flusher_sync(hmp);
339 hammer_flusher_sync(hmp);
340 hammer_flusher_sync(hmp);
341 hmp->ronly = 1;
342 RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
343 hammer_adjust_volume_mode, NULL);
195c19a1 344 }
51c35492 345 return(error);
195c19a1
MD
346 }
347
427e5fc6
MD
348 RB_INIT(&hmp->rb_vols_root);
349 RB_INIT(&hmp->rb_inos_root);
40043e7f 350 RB_INIT(&hmp->rb_nods_root);
e8599db1 351 RB_INIT(&hmp->rb_undo_root);
0832c9bb
MD
352 RB_INIT(&hmp->rb_resv_root);
353 RB_INIT(&hmp->rb_bufs_root);
354
195c19a1 355 hmp->ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
427e5fc6 356
10a5d1ba
MD
357 TAILQ_INIT(&hmp->volu_list);
358 TAILQ_INIT(&hmp->undo_list);
359 TAILQ_INIT(&hmp->data_list);
360 TAILQ_INIT(&hmp->meta_list);
361 TAILQ_INIT(&hmp->lose_list);
362
427e5fc6
MD
363 /*
364 * Load volumes
365 */
366 path = objcache_get(namei_oc, M_WAITOK);
d26d0ae9 367 hmp->nvolumes = info.nvolumes;
427e5fc6
MD
368 for (i = 0; i < info.nvolumes; ++i) {
369 error = copyin(&info.volumes[i], &upath, sizeof(char *));
370 if (error == 0)
371 error = copyinstr(upath, path, MAXPATHLEN, NULL);
372 if (error == 0)
8cd0a023 373 error = hammer_install_volume(hmp, path);
427e5fc6
MD
374 if (error)
375 break;
376 }
377 objcache_put(namei_oc, path);
378
379 /*
380 * Make sure we found a root volume
381 */
382 if (error == 0 && hmp->rootvol == NULL) {
383 kprintf("hammer_mount: No root volume found!\n");
384 error = EINVAL;
385 }
427e5fc6
MD
386 if (error) {
387 hammer_free_hmp(mp);
388 return (error);
389 }
390
391 /*
27ea2398
MD
392 * No errors, setup enough of the mount point so we can lookup the
393 * root vnode.
427e5fc6 394 */
27ea2398
MD
395 mp->mnt_iosize_max = MAXPHYS;
396 mp->mnt_kern_flag |= MNTK_FSMID;
c0ade690
MD
397
398 /*
399 * note: f_iosize is used by vnode_pager_haspage() when constructing
400 * its VOP_BMAP call.
401 */
402 mp->mnt_stat.f_iosize = HAMMER_BUFSIZE;
fbc6e32a 403 mp->mnt_stat.f_bsize = HAMMER_BUFSIZE;
6f97fce3
MD
404
405 mp->mnt_vstat.f_frsize = HAMMER_BUFSIZE;
406 mp->mnt_vstat.f_bsize = HAMMER_BUFSIZE;
407
27ea2398
MD
408 mp->mnt_maxsymlinklen = 255;
409 mp->mnt_flag |= MNT_LOCAL;
410
411 vfs_add_vnodeops(mp, &hammer_vnode_vops, &mp->mnt_vn_norm_ops);
7a04d74f
MD
412 vfs_add_vnodeops(mp, &hammer_spec_vops, &mp->mnt_vn_spec_ops);
413 vfs_add_vnodeops(mp, &hammer_fifo_vops, &mp->mnt_vn_fifo_ops);
27ea2398 414
a89aec1b
MD
415 /*
416 * The root volume's ondisk pointer is only valid if we hold a
417 * reference to it.
418 */
419 rootvol = hammer_get_root_volume(hmp, &error);
420 if (error)
f90dde4c
MD
421 goto failed;
422
423 /*
9f5097dc 424 * Perform any necessary UNDO operations. The recovery code does
0729c8c8
MD
425 * call hammer_undo_lookup() so we have to pre-cache the blockmap,
426 * and then re-copy it again after recovery is complete.
c9b9e29d 427 *
51c35492
MD
428 * If this is a read-only mount the UNDO information is retained
429 * in memory in the form of dirty buffer cache buffers, and not
430 * written back to the media.
f90dde4c 431 */
0729c8c8
MD
432 bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
433 sizeof(hmp->blockmap));
c9b9e29d 434
f90dde4c
MD
435 error = hammer_recover(hmp, rootvol);
436 if (error) {
437 kprintf("Failed to recover HAMMER filesystem on mount\n");
a89aec1b 438 goto done;
f90dde4c
MD
439 }
440
441 /*
442 * Finish setup now that we have a good root volume
443 */
a89aec1b
MD
444 ksnprintf(mp->mnt_stat.f_mntfromname,
445 sizeof(mp->mnt_stat.f_mntfromname), "%s",
446 rootvol->ondisk->vol_name);
513ca7d7
MD
447 mp->mnt_stat.f_fsid.val[0] =
448 crc32((char *)&rootvol->ondisk->vol_fsid + 0, 8);
449 mp->mnt_stat.f_fsid.val[1] =
450 crc32((char *)&rootvol->ondisk->vol_fsid + 8, 8);
b84de5af 451
6f97fce3
MD
452 mp->mnt_vstat.f_fsid_uuid = rootvol->ondisk->vol_fsid;
453 mp->mnt_vstat.f_fsid = crc32(&mp->mnt_vstat.f_fsid_uuid,
454 sizeof(mp->mnt_vstat.f_fsid_uuid));
455
0729c8c8
MD
456 /*
457 * Certain often-modified fields in the root volume are cached in
458 * the hammer_mount structure so we do not have to generate lots
459 * of little UNDO structures for them.
c9b9e29d 460 *
9f5097dc
MD
461 * Recopy after recovery. This also has the side effect of
462 * setting our cached undo FIFO's first_offset, which serves to
463 * placemark the FIFO start for the NEXT flush cycle while the
464 * on-disk first_offset represents the LAST flush cycle.
0729c8c8 465 */
b84de5af 466 hmp->next_tid = rootvol->ondisk->vol0_next_tid;
0729c8c8
MD
467 bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
468 sizeof(hmp->blockmap));
e63644f0 469 hmp->copy_stat_freebigblocks = rootvol->ondisk->vol0_stat_freebigblocks;
0729c8c8 470
059819e3
MD
471 hammer_flusher_create(hmp);
472
27ea2398
MD
473 /*
474 * Locate the root directory using the root cluster's B-Tree as a
475 * starting point. The root directory uses an obj_id of 1.
476 *
477 * FUTURE: Leave the root directory cached referenced but unlocked
478 * in hmp->rootvp (need to flush it on unmount).
479 */
480 error = hammer_vfs_vget(mp, 1, &rootvp);
a89aec1b
MD
481 if (error)
482 goto done;
483 vput(rootvp);
27ea2398 484 /*vn_unlock(hmp->rootvp);*/
427e5fc6 485
a89aec1b 486done:
f90dde4c
MD
487 hammer_rel_volume(rootvol, 0);
488failed:
427e5fc6 489 /*
27ea2398 490 * Cleanup and return.
427e5fc6 491 */
27ea2398 492 if (error)
427e5fc6 493 hammer_free_hmp(mp);
427e5fc6
MD
494 return (error);
495}
496
497static int
498hammer_vfs_unmount(struct mount *mp, int mntflags)
499{
500#if 0
501 struct hammer_mount *hmp = (void *)mp->mnt_data;
502#endif
503 int flags;
66325755 504 int error;
27ea2398 505
427e5fc6
MD
506 /*
507 * Clean out the vnodes
508 */
66325755
MD
509 flags = 0;
510 if (mntflags & MNT_FORCE)
511 flags |= FORCECLOSE;
512 if ((error = vflush(mp, 0, flags)) != 0)
513 return (error);
427e5fc6
MD
514
515 /*
516 * Clean up the internal mount structure and related entities. This
517 * may issue I/O.
518 */
519 hammer_free_hmp(mp);
520 return(0);
521}
522
523/*
524 * Clean up the internal mount structure and disassociate it from the mount.
525 * This may issue I/O.
526 */
527static void
528hammer_free_hmp(struct mount *mp)
529{
530 struct hammer_mount *hmp = (void *)mp->mnt_data;
531
27ea2398 532#if 0
427e5fc6
MD
533 /*
534 * Clean up the root vnode
535 */
536 if (hmp->rootvp) {
537 vrele(hmp->rootvp);
538 hmp->rootvp = NULL;
539 }
27ea2398 540#endif
b84de5af 541 hammer_flusher_sync(hmp);
059819e3
MD
542 hammer_flusher_sync(hmp);
543 hammer_flusher_destroy(hmp);
427e5fc6 544
b84de5af
MD
545 KKASSERT(RB_EMPTY(&hmp->rb_inos_root));
546
547#if 0
427e5fc6
MD
548 /*
549 * Unload & flush inodes
b84de5af
MD
550 *
551 * XXX illegal to call this from here, it can only be done from
552 * the flusher.
427e5fc6
MD
553 */
554 RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
d113fda1 555 hammer_unload_inode, (void *)MNT_WAIT);
427e5fc6
MD
556
557 /*
558 * Unload & flush volumes
559 */
b84de5af
MD
560#endif
561 /*
0832c9bb 562 * Unload buffers and then volumes
b84de5af 563 */
0832c9bb
MD
564 RB_SCAN(hammer_buf_rb_tree, &hmp->rb_bufs_root, NULL,
565 hammer_unload_buffer, NULL);
427e5fc6
MD
566 RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
567 hammer_unload_volume, NULL);
568
569 mp->mnt_data = NULL;
66325755 570 mp->mnt_flag &= ~MNT_LOCAL;
427e5fc6 571 hmp->mp = NULL;
0729c8c8 572 hammer_destroy_objid_cache(hmp);
427e5fc6
MD
573 kfree(hmp, M_HAMMER);
574}
575
513ca7d7
MD
576/*
577 * Obtain a vnode for the specified inode number. An exclusively locked
578 * vnode is returned.
579 */
580int
581hammer_vfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
582{
36f82b23 583 struct hammer_transaction trans;
513ca7d7
MD
584 struct hammer_mount *hmp = (void *)mp->mnt_data;
585 struct hammer_inode *ip;
586 int error;
587
36f82b23
MD
588 hammer_simple_transaction(&trans, hmp);
589
513ca7d7 590 /*
36f82b23
MD
591 * Lookup the requested HAMMER inode. The structure must be
592 * left unlocked while we manipulate the related vnode to avoid
593 * a deadlock.
513ca7d7 594 */
ddfdf542
MD
595 ip = hammer_get_inode(&trans, NULL, ino,
596 hmp->asof, HAMMER_DEF_LOCALIZATION,
597 0, &error);
513ca7d7
MD
598 if (ip == NULL) {
599 *vpp = NULL;
600 return(error);
601 }
e8599db1 602 error = hammer_get_vnode(ip, vpp);
513ca7d7 603 hammer_rel_inode(ip, 0);
b84de5af 604 hammer_done_transaction(&trans);
513ca7d7
MD
605 return (error);
606}
607
427e5fc6
MD
608/*
609 * Return the root vnode for the filesystem.
610 *
611 * HAMMER stores the root vnode in the hammer_mount structure so
612 * getting it is easy.
613 */
614static int
615hammer_vfs_root(struct mount *mp, struct vnode **vpp)
616{
47197d71 617#if 0
427e5fc6 618 struct hammer_mount *hmp = (void *)mp->mnt_data;
47197d71 619#endif
27ea2398 620 int error;
427e5fc6 621
47197d71 622 error = hammer_vfs_vget(mp, 1, vpp);
27ea2398 623 return (error);
427e5fc6
MD
624}
625
626static int
627hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
628{
fbc6e32a
MD
629 struct hammer_mount *hmp = (void *)mp->mnt_data;
630 hammer_volume_t volume;
631 hammer_volume_ondisk_t ondisk;
632 int error;
47197d71 633 int64_t bfree;
fbc6e32a
MD
634
635 volume = hammer_get_root_volume(hmp, &error);
636 if (error)
637 return(error);
fbc6e32a
MD
638 ondisk = volume->ondisk;
639
47197d71
MD
640 /*
641 * Basic stats
642 */
fbc6e32a 643 mp->mnt_stat.f_files = ondisk->vol0_stat_inodes;
c3be93f2 644 bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE;
40043e7f 645 hammer_rel_volume(volume, 0);
47197d71 646
47197d71
MD
647 mp->mnt_stat.f_bfree = bfree / HAMMER_BUFSIZE;
648 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
fbc6e32a
MD
649 if (mp->mnt_stat.f_files < 0)
650 mp->mnt_stat.f_files = 0;
651
27ea2398
MD
652 *sbp = mp->mnt_stat;
653 return(0);
427e5fc6
MD
654}
655
6f97fce3
MD
656static int
657hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
658{
659 struct hammer_mount *hmp = (void *)mp->mnt_data;
660 hammer_volume_t volume;
661 hammer_volume_ondisk_t ondisk;
662 int error;
663 int64_t bfree;
664
665 volume = hammer_get_root_volume(hmp, &error);
666 if (error)
667 return(error);
668 ondisk = volume->ondisk;
669
670 /*
671 * Basic stats
672 */
673 mp->mnt_vstat.f_files = ondisk->vol0_stat_inodes;
674 bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE;
675 hammer_rel_volume(volume, 0);
676
677 mp->mnt_vstat.f_bfree = bfree / HAMMER_BUFSIZE;
678 mp->mnt_vstat.f_bavail = mp->mnt_stat.f_bfree;
679 if (mp->mnt_vstat.f_files < 0)
680 mp->mnt_vstat.f_files = 0;
681 *sbp = mp->mnt_vstat;
682 return(0);
683}
684
0729c8c8
MD
685/*
686 * Sync the filesystem. Currently we have to run it twice, the second
687 * one will advance the undo start index to the end index, so if a crash
688 * occurs no undos will be run on mount.
77062c8a
MD
689 *
690 * We do not sync the filesystem if we are called from a panic. If we did
691 * we might end up blowing up a sync that was already in progress.
0729c8c8 692 */
427e5fc6
MD
693static int
694hammer_vfs_sync(struct mount *mp, int waitfor)
695{
fbc6e32a 696 struct hammer_mount *hmp = (void *)mp->mnt_data;
0729c8c8
MD
697 int error;
698
77062c8a 699 if (panicstr == NULL) {
0729c8c8 700 error = hammer_sync_hmp(hmp, waitfor);
77062c8a
MD
701 if (error == 0)
702 error = hammer_sync_hmp(hmp, waitfor);
703 } else {
704 error = EIO;
77062c8a 705 }
0729c8c8 706 return (error);
427e5fc6
MD
707}
708
513ca7d7
MD
709/*
710 * Convert a vnode to a file handle.
711 */
712static int
713hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp)
714{
715 hammer_inode_t ip;
716
717 KKASSERT(MAXFIDSZ >= 16);
718 ip = VTOI(vp);
719 fhp->fid_len = offsetof(struct fid, fid_data[16]);
720 fhp->fid_reserved = 0;
721 bcopy(&ip->obj_id, fhp->fid_data + 0, sizeof(ip->obj_id));
722 bcopy(&ip->obj_asof, fhp->fid_data + 8, sizeof(ip->obj_asof));
723 return(0);
724}
725
726
727/*
728 * Convert a file handle back to a vnode.
729 */
730static int
731hammer_vfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
732{
36f82b23 733 struct hammer_transaction trans;
513ca7d7
MD
734 struct hammer_inode *ip;
735 struct hammer_inode_info info;
736 int error;
737
738 bcopy(fhp->fid_data + 0, &info.obj_id, sizeof(info.obj_id));
739 bcopy(fhp->fid_data + 8, &info.obj_asof, sizeof(info.obj_asof));
740
36f82b23
MD
741 hammer_simple_transaction(&trans, (void *)mp->mnt_data);
742
513ca7d7
MD
743 /*
744 * Get/allocate the hammer_inode structure. The structure must be
745 * unlocked while we manipulate the related vnode to avoid a
746 * deadlock.
747 */
ddfdf542
MD
748 ip = hammer_get_inode(&trans, NULL, info.obj_id,
749 info.obj_asof, HAMMER_DEF_LOCALIZATION,
36f82b23 750 0, &error);
513ca7d7
MD
751 if (ip == NULL) {
752 *vpp = NULL;
753 return(error);
754 }
e8599db1 755 error = hammer_get_vnode(ip, vpp);
513ca7d7 756 hammer_rel_inode(ip, 0);
b84de5af 757 hammer_done_transaction(&trans);
513ca7d7
MD
758 return (error);
759}
760
761static int
762hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
763 int *exflagsp, struct ucred **credanonp)
764{
765 hammer_mount_t hmp = (void *)mp->mnt_data;
766 struct netcred *np;
767 int error;
768
769 np = vfs_export_lookup(mp, &hmp->export, nam);
770 if (np) {
771 *exflagsp = np->netc_exflags;
772 *credanonp = &np->netc_anon;
773 error = 0;
774 } else {
775 error = EACCES;
776 }
777 return (error);
778
779}
780
781int
782hammer_vfs_export(struct mount *mp, int op, const struct export_args *export)
783{
784 hammer_mount_t hmp = (void *)mp->mnt_data;
785 int error;
786
787 switch(op) {
788 case MOUNTCTL_SET_EXPORT:
789 error = vfs_export(mp, &hmp->export, export);
790 break;
791 default:
792 error = EOPNOTSUPP;
793 break;
794 }
795 return(error);
796}
797