HAMMER 46/Many: Performance pass, media changes, bug fixes.
[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 *
2f85fa4d 34 * $DragonFly: src/sys/vfs/hammer/hammer_vfsops.c,v 1.35 2008/05/18 01:48:50 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;
46fe7ae1
MD
57int hammer_debug_recover; /* -1 will disable, +1 will force */
58int hammer_debug_recover_faults;
b3deaf57
MD
59int hammer_count_inodes;
60int hammer_count_records;
61int hammer_count_record_datas;
62int hammer_count_volumes;
b3deaf57
MD
63int hammer_count_buffers;
64int hammer_count_nodes;
9480ff55
MD
65int hammer_count_dirtybufs; /* global */
66int hammer_limit_dirtybufs = 100; /* per-mount */
1f07f686 67int hammer_bio_count;
7d683b0f 68int64_t hammer_contention_count;
f03c9cf4 69int64_t hammer_zone_limit;
b3deaf57
MD
70
71SYSCTL_NODE(_vfs, OID_AUTO, hammer, CTLFLAG_RW, 0, "HAMMER filesystem");
d5ef456e
MD
72SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_general, CTLFLAG_RW,
73 &hammer_debug_general, 0, "");
2f85fa4d
MD
74SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_io, CTLFLAG_RW,
75 &hammer_debug_io, 0, "");
77062c8a
MD
76SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_debug, CTLFLAG_RW,
77 &hammer_debug_debug, 0, "");
e8599db1
MD
78SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_inode, CTLFLAG_RW,
79 &hammer_debug_inode, 0, "");
7d683b0f
MD
80SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_locks, CTLFLAG_RW,
81 &hammer_debug_locks, 0, "");
b3deaf57
MD
82SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_btree, CTLFLAG_RW,
83 &hammer_debug_btree, 0, "");
d113fda1
MD
84SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_tid, CTLFLAG_RW,
85 &hammer_debug_tid, 0, "");
b33e2cc0
MD
86SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover, CTLFLAG_RW,
87 &hammer_debug_recover, 0, "");
46fe7ae1
MD
88SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover_faults, CTLFLAG_RW,
89 &hammer_debug_recover_faults, 0, "");
9480ff55
MD
90
91SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_dirtybufs, CTLFLAG_RW,
92 &hammer_limit_dirtybufs, 0, "");
93
b3deaf57
MD
94SYSCTL_INT(_vfs_hammer, OID_AUTO, count_inodes, CTLFLAG_RD,
95 &hammer_count_inodes, 0, "");
96SYSCTL_INT(_vfs_hammer, OID_AUTO, count_records, CTLFLAG_RD,
97 &hammer_count_records, 0, "");
98SYSCTL_INT(_vfs_hammer, OID_AUTO, count_record_datas, CTLFLAG_RD,
99 &hammer_count_record_datas, 0, "");
100SYSCTL_INT(_vfs_hammer, OID_AUTO, count_volumes, CTLFLAG_RD,
101 &hammer_count_volumes, 0, "");
b3deaf57
MD
102SYSCTL_INT(_vfs_hammer, OID_AUTO, count_buffers, CTLFLAG_RD,
103 &hammer_count_buffers, 0, "");
104SYSCTL_INT(_vfs_hammer, OID_AUTO, count_nodes, CTLFLAG_RD,
105 &hammer_count_nodes, 0, "");
9480ff55
MD
106SYSCTL_INT(_vfs_hammer, OID_AUTO, count_dirtybufs, CTLFLAG_RD,
107 &hammer_count_dirtybufs, 0, "");
f03c9cf4
MD
108SYSCTL_QUAD(_vfs_hammer, OID_AUTO, zone_limit, CTLFLAG_RW,
109 &hammer_zone_limit, 0, "");
7d683b0f
MD
110SYSCTL_QUAD(_vfs_hammer, OID_AUTO, contention_count, CTLFLAG_RW,
111 &hammer_contention_count, 0, "");
b3deaf57 112
427e5fc6
MD
113/*
114 * VFS ABI
115 */
116static void hammer_free_hmp(struct mount *mp);
117
118static int hammer_vfs_mount(struct mount *mp, char *path, caddr_t data,
119 struct ucred *cred);
120static int hammer_vfs_unmount(struct mount *mp, int mntflags);
121static int hammer_vfs_root(struct mount *mp, struct vnode **vpp);
122static int hammer_vfs_statfs(struct mount *mp, struct statfs *sbp,
123 struct ucred *cred);
124static int hammer_vfs_sync(struct mount *mp, int waitfor);
513ca7d7
MD
125static int hammer_vfs_vget(struct mount *mp, ino_t ino,
126 struct vnode **vpp);
427e5fc6 127static int hammer_vfs_init(struct vfsconf *conf);
513ca7d7
MD
128static int hammer_vfs_fhtovp(struct mount *mp, struct fid *fhp,
129 struct vnode **vpp);
130static int hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp);
131static int hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
132 int *exflagsp, struct ucred **credanonp);
133
427e5fc6
MD
134
135static struct vfsops hammer_vfsops = {
136 .vfs_mount = hammer_vfs_mount,
137 .vfs_unmount = hammer_vfs_unmount,
138 .vfs_root = hammer_vfs_root,
139 .vfs_statfs = hammer_vfs_statfs,
140 .vfs_sync = hammer_vfs_sync,
141 .vfs_vget = hammer_vfs_vget,
513ca7d7
MD
142 .vfs_init = hammer_vfs_init,
143 .vfs_vptofh = hammer_vfs_vptofh,
144 .vfs_fhtovp = hammer_vfs_fhtovp,
145 .vfs_checkexp = hammer_vfs_checkexp
427e5fc6
MD
146};
147
148MALLOC_DEFINE(M_HAMMER, "hammer-mount", "hammer mount");
149
150VFS_SET(hammer_vfsops, hammer, 0);
151MODULE_VERSION(hammer, 1);
152
153static int
154hammer_vfs_init(struct vfsconf *conf)
155{
47197d71 156 /*hammer_init_alist_config();*/
427e5fc6
MD
157 return(0);
158}
159
160static int
161hammer_vfs_mount(struct mount *mp, char *mntpt, caddr_t data,
162 struct ucred *cred)
163{
164 struct hammer_mount_info info;
a89aec1b
MD
165 hammer_mount_t hmp;
166 hammer_volume_t rootvol;
27ea2398 167 struct vnode *rootvp;
427e5fc6
MD
168 const char *upath; /* volume name in userspace */
169 char *path; /* volume name in system space */
170 int error;
171 int i;
172
173 if ((error = copyin(data, &info, sizeof(info))) != 0)
174 return (error);
175 if (info.nvolumes <= 0 || info.nvolumes >= 32768)
176 return (EINVAL);
177
178 /*
179 * Interal mount data structure
180 */
195c19a1
MD
181 if (mp->mnt_flag & MNT_UPDATE) {
182 hmp = (void *)mp->mnt_data;
183 KKASSERT(hmp != NULL);
184 } else {
185 hmp = kmalloc(sizeof(*hmp), M_HAMMER, M_WAITOK | M_ZERO);
186 mp->mnt_data = (qaddr_t)hmp;
187 hmp->mp = mp;
188 hmp->zbuf = kmalloc(HAMMER_BUFSIZE, M_HAMMER, M_WAITOK|M_ZERO);
189 hmp->namekey_iterator = mycpu->gd_time_seconds;
46fe7ae1 190 /*TAILQ_INIT(&hmp->recycle_list);*/
47197d71 191
2f85fa4d 192 hmp->root_btree_beg.localization = HAMMER_MIN_LOCALIZATION;
47197d71
MD
193 hmp->root_btree_beg.obj_id = -0x8000000000000000LL;
194 hmp->root_btree_beg.key = -0x8000000000000000LL;
195 hmp->root_btree_beg.create_tid = 1;
196 hmp->root_btree_beg.delete_tid = 1;
197 hmp->root_btree_beg.rec_type = 0;
198 hmp->root_btree_beg.obj_type = 0;
199
2f85fa4d 200 hmp->root_btree_end.localization = HAMMER_MAX_LOCALIZATION;
47197d71
MD
201 hmp->root_btree_end.obj_id = 0x7FFFFFFFFFFFFFFFLL;
202 hmp->root_btree_end.key = 0x7FFFFFFFFFFFFFFFLL;
203 hmp->root_btree_end.create_tid = 0xFFFFFFFFFFFFFFFFULL;
204 hmp->root_btree_end.delete_tid = 0; /* special case */
205 hmp->root_btree_end.rec_type = 0xFFFFU;
206 hmp->root_btree_end.obj_type = 0;
40043e7f 207 lockinit(&hmp->blockmap_lock, "blkmap", 0, 0);
f03c9cf4 208
9480ff55 209 hmp->sync_lock.refs = 1;
c9b9e29d 210 hmp->free_lock.refs = 1;
9480ff55 211
059819e3 212 TAILQ_INIT(&hmp->flush_list);
0729c8c8 213 TAILQ_INIT(&hmp->objid_cache_list);
e8599db1 214 TAILQ_INIT(&hmp->undo_lru_list);
0729c8c8
MD
215
216 /*
217 * Set default zone limits. This value can be reduced
218 * further by the zone limit specified in the root volume.
219 *
220 * The sysctl can force a small zone limit for debugging
221 * purposes.
222 */
f03c9cf4
MD
223 for (i = 0; i < HAMMER_MAX_ZONES; ++i) {
224 hmp->zone_limits[i] =
225 HAMMER_ZONE_ENCODE(i, HAMMER_ZONE_LIMIT);
0729c8c8 226
f03c9cf4
MD
227 if (hammer_zone_limit) {
228 hmp->zone_limits[i] =
229 HAMMER_ZONE_ENCODE(i, hammer_zone_limit);
230 }
bf686dbe 231 hammer_init_holes(hmp, &hmp->holes[i]);
f03c9cf4 232 }
195c19a1
MD
233 }
234 hmp->hflags = info.hflags;
7f7c1f84
MD
235 if (info.asof) {
236 mp->mnt_flag |= MNT_RDONLY;
237 hmp->asof = info.asof;
238 } else {
239 hmp->asof = HAMMER_MAX_TID;
240 }
195c19a1
MD
241
242 /*
243 * Re-open read-write if originally read-only, or vise-versa XXX
244 */
245 if (mp->mnt_flag & MNT_UPDATE) {
246 if (hmp->ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
247 kprintf("HAMMER read-write -> read-only XXX\n");
248 hmp->ronly = 1;
249 } else if (hmp->ronly && (mp->mnt_flag & MNT_RDONLY) == 0) {
250 kprintf("HAMMER read-only -> read-write XXX\n");
251 hmp->ronly = 0;
252 }
253 return(0);
254 }
255
427e5fc6
MD
256 RB_INIT(&hmp->rb_vols_root);
257 RB_INIT(&hmp->rb_inos_root);
40043e7f 258 RB_INIT(&hmp->rb_nods_root);
e8599db1 259 RB_INIT(&hmp->rb_undo_root);
195c19a1 260 hmp->ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
427e5fc6 261
10a5d1ba
MD
262 TAILQ_INIT(&hmp->volu_list);
263 TAILQ_INIT(&hmp->undo_list);
264 TAILQ_INIT(&hmp->data_list);
265 TAILQ_INIT(&hmp->meta_list);
266 TAILQ_INIT(&hmp->lose_list);
267
427e5fc6
MD
268 /*
269 * Load volumes
270 */
271 path = objcache_get(namei_oc, M_WAITOK);
d26d0ae9 272 hmp->nvolumes = info.nvolumes;
427e5fc6
MD
273 for (i = 0; i < info.nvolumes; ++i) {
274 error = copyin(&info.volumes[i], &upath, sizeof(char *));
275 if (error == 0)
276 error = copyinstr(upath, path, MAXPATHLEN, NULL);
277 if (error == 0)
8cd0a023 278 error = hammer_install_volume(hmp, path);
427e5fc6
MD
279 if (error)
280 break;
281 }
282 objcache_put(namei_oc, path);
283
284 /*
285 * Make sure we found a root volume
286 */
287 if (error == 0 && hmp->rootvol == NULL) {
288 kprintf("hammer_mount: No root volume found!\n");
289 error = EINVAL;
290 }
427e5fc6
MD
291 if (error) {
292 hammer_free_hmp(mp);
293 return (error);
294 }
295
296 /*
27ea2398
MD
297 * No errors, setup enough of the mount point so we can lookup the
298 * root vnode.
427e5fc6 299 */
27ea2398
MD
300 mp->mnt_iosize_max = MAXPHYS;
301 mp->mnt_kern_flag |= MNTK_FSMID;
c0ade690
MD
302
303 /*
304 * note: f_iosize is used by vnode_pager_haspage() when constructing
305 * its VOP_BMAP call.
306 */
307 mp->mnt_stat.f_iosize = HAMMER_BUFSIZE;
fbc6e32a 308 mp->mnt_stat.f_bsize = HAMMER_BUFSIZE;
27ea2398
MD
309 mp->mnt_maxsymlinklen = 255;
310 mp->mnt_flag |= MNT_LOCAL;
311
312 vfs_add_vnodeops(mp, &hammer_vnode_vops, &mp->mnt_vn_norm_ops);
7a04d74f
MD
313 vfs_add_vnodeops(mp, &hammer_spec_vops, &mp->mnt_vn_spec_ops);
314 vfs_add_vnodeops(mp, &hammer_fifo_vops, &mp->mnt_vn_fifo_ops);
27ea2398 315
a89aec1b
MD
316 /*
317 * The root volume's ondisk pointer is only valid if we hold a
318 * reference to it.
319 */
320 rootvol = hammer_get_root_volume(hmp, &error);
321 if (error)
f90dde4c
MD
322 goto failed;
323
324 /*
0729c8c8
MD
325 * Perform any necessary UNDO operations. The recover code does
326 * call hammer_undo_lookup() so we have to pre-cache the blockmap,
327 * and then re-copy it again after recovery is complete.
c9b9e29d
MD
328 *
329 * The recover code will load hmp->flusher_undo_start.
f90dde4c 330 */
0729c8c8
MD
331 bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
332 sizeof(hmp->blockmap));
c9b9e29d 333
f90dde4c
MD
334 error = hammer_recover(hmp, rootvol);
335 if (error) {
336 kprintf("Failed to recover HAMMER filesystem on mount\n");
a89aec1b 337 goto done;
f90dde4c
MD
338 }
339
340 /*
341 * Finish setup now that we have a good root volume
342 */
a89aec1b
MD
343 ksnprintf(mp->mnt_stat.f_mntfromname,
344 sizeof(mp->mnt_stat.f_mntfromname), "%s",
345 rootvol->ondisk->vol_name);
513ca7d7
MD
346 mp->mnt_stat.f_fsid.val[0] =
347 crc32((char *)&rootvol->ondisk->vol_fsid + 0, 8);
348 mp->mnt_stat.f_fsid.val[1] =
349 crc32((char *)&rootvol->ondisk->vol_fsid + 8, 8);
b84de5af 350
0729c8c8
MD
351 /*
352 * Certain often-modified fields in the root volume are cached in
353 * the hammer_mount structure so we do not have to generate lots
354 * of little UNDO structures for them.
c9b9e29d
MD
355 *
356 * Recopy after recovery.
0729c8c8 357 */
b84de5af 358 hmp->next_tid = rootvol->ondisk->vol0_next_tid;
0729c8c8
MD
359 bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
360 sizeof(hmp->blockmap));
361
362 /*
363 * Use the zone limit set by newfs_hammer, or the zone limit set by
364 * sysctl (for debugging), whichever is smaller.
365 */
366 if (rootvol->ondisk->vol0_zone_limit) {
367 hammer_off_t vol0_zone_limit;
368
369 vol0_zone_limit = rootvol->ondisk->vol0_zone_limit;
370 for (i = 0; i < HAMMER_MAX_ZONES; ++i) {
371 if (hmp->zone_limits[i] > vol0_zone_limit)
372 hmp->zone_limits[i] = vol0_zone_limit;
373 }
374 }
b84de5af 375
059819e3
MD
376 hammer_flusher_create(hmp);
377
27ea2398
MD
378 /*
379 * Locate the root directory using the root cluster's B-Tree as a
380 * starting point. The root directory uses an obj_id of 1.
381 *
382 * FUTURE: Leave the root directory cached referenced but unlocked
383 * in hmp->rootvp (need to flush it on unmount).
384 */
385 error = hammer_vfs_vget(mp, 1, &rootvp);
a89aec1b
MD
386 if (error)
387 goto done;
388 vput(rootvp);
27ea2398 389 /*vn_unlock(hmp->rootvp);*/
427e5fc6 390
a89aec1b 391done:
f90dde4c
MD
392 hammer_rel_volume(rootvol, 0);
393failed:
427e5fc6 394 /*
27ea2398 395 * Cleanup and return.
427e5fc6 396 */
27ea2398 397 if (error)
427e5fc6 398 hammer_free_hmp(mp);
427e5fc6
MD
399 return (error);
400}
401
402static int
403hammer_vfs_unmount(struct mount *mp, int mntflags)
404{
405#if 0
406 struct hammer_mount *hmp = (void *)mp->mnt_data;
407#endif
408 int flags;
66325755 409 int error;
27ea2398 410
427e5fc6
MD
411 /*
412 * Clean out the vnodes
413 */
66325755
MD
414 flags = 0;
415 if (mntflags & MNT_FORCE)
416 flags |= FORCECLOSE;
417 if ((error = vflush(mp, 0, flags)) != 0)
418 return (error);
427e5fc6
MD
419
420 /*
421 * Clean up the internal mount structure and related entities. This
422 * may issue I/O.
423 */
424 hammer_free_hmp(mp);
425 return(0);
426}
427
428/*
429 * Clean up the internal mount structure and disassociate it from the mount.
430 * This may issue I/O.
431 */
432static void
433hammer_free_hmp(struct mount *mp)
434{
435 struct hammer_mount *hmp = (void *)mp->mnt_data;
bf686dbe 436 int i;
427e5fc6 437
27ea2398 438#if 0
427e5fc6
MD
439 /*
440 * Clean up the root vnode
441 */
442 if (hmp->rootvp) {
443 vrele(hmp->rootvp);
444 hmp->rootvp = NULL;
445 }
27ea2398 446#endif
b84de5af 447 hammer_flusher_sync(hmp);
059819e3
MD
448 hammer_flusher_sync(hmp);
449 hammer_flusher_destroy(hmp);
427e5fc6 450
b84de5af
MD
451 KKASSERT(RB_EMPTY(&hmp->rb_inos_root));
452
453#if 0
427e5fc6
MD
454 /*
455 * Unload & flush inodes
b84de5af
MD
456 *
457 * XXX illegal to call this from here, it can only be done from
458 * the flusher.
427e5fc6
MD
459 */
460 RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
d113fda1 461 hammer_unload_inode, (void *)MNT_WAIT);
427e5fc6
MD
462
463 /*
464 * Unload & flush volumes
465 */
b84de5af
MD
466#endif
467 /*
468 * Unload the volumes
469 */
427e5fc6
MD
470 RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
471 hammer_unload_volume, NULL);
472
473 mp->mnt_data = NULL;
66325755 474 mp->mnt_flag &= ~MNT_LOCAL;
427e5fc6 475 hmp->mp = NULL;
0729c8c8 476 hammer_destroy_objid_cache(hmp);
66325755 477 kfree(hmp->zbuf, M_HAMMER);
40043e7f 478 lockuninit(&hmp->blockmap_lock);
bf686dbe
MD
479
480 for (i = 0; i < HAMMER_MAX_ZONES; ++i)
481 hammer_free_holes(hmp, &hmp->holes[i]);
482
427e5fc6
MD
483 kfree(hmp, M_HAMMER);
484}
485
513ca7d7
MD
486/*
487 * Obtain a vnode for the specified inode number. An exclusively locked
488 * vnode is returned.
489 */
490int
491hammer_vfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
492{
36f82b23 493 struct hammer_transaction trans;
513ca7d7
MD
494 struct hammer_mount *hmp = (void *)mp->mnt_data;
495 struct hammer_inode *ip;
496 int error;
497
36f82b23
MD
498 hammer_simple_transaction(&trans, hmp);
499
513ca7d7 500 /*
36f82b23
MD
501 * Lookup the requested HAMMER inode. The structure must be
502 * left unlocked while we manipulate the related vnode to avoid
503 * a deadlock.
513ca7d7 504 */
36f82b23 505 ip = hammer_get_inode(&trans, NULL, ino, hmp->asof, 0, &error);
513ca7d7
MD
506 if (ip == NULL) {
507 *vpp = NULL;
508 return(error);
509 }
e8599db1 510 error = hammer_get_vnode(ip, vpp);
513ca7d7 511 hammer_rel_inode(ip, 0);
b84de5af 512 hammer_done_transaction(&trans);
513ca7d7
MD
513 return (error);
514}
515
427e5fc6
MD
516/*
517 * Return the root vnode for the filesystem.
518 *
519 * HAMMER stores the root vnode in the hammer_mount structure so
520 * getting it is easy.
521 */
522static int
523hammer_vfs_root(struct mount *mp, struct vnode **vpp)
524{
47197d71 525#if 0
427e5fc6 526 struct hammer_mount *hmp = (void *)mp->mnt_data;
47197d71 527#endif
27ea2398 528 int error;
427e5fc6 529
47197d71 530 error = hammer_vfs_vget(mp, 1, vpp);
27ea2398 531 return (error);
427e5fc6
MD
532}
533
534static int
535hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
536{
fbc6e32a
MD
537 struct hammer_mount *hmp = (void *)mp->mnt_data;
538 hammer_volume_t volume;
539 hammer_volume_ondisk_t ondisk;
540 int error;
47197d71 541 int64_t bfree;
fbc6e32a
MD
542
543 volume = hammer_get_root_volume(hmp, &error);
544 if (error)
545 return(error);
fbc6e32a
MD
546 ondisk = volume->ondisk;
547
47197d71
MD
548 /*
549 * Basic stats
550 */
fbc6e32a 551 mp->mnt_stat.f_files = ondisk->vol0_stat_inodes;
c3be93f2 552 bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE;
40043e7f 553 hammer_rel_volume(volume, 0);
47197d71 554
47197d71
MD
555 mp->mnt_stat.f_bfree = bfree / HAMMER_BUFSIZE;
556 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
fbc6e32a
MD
557 if (mp->mnt_stat.f_files < 0)
558 mp->mnt_stat.f_files = 0;
559
27ea2398
MD
560 *sbp = mp->mnt_stat;
561 return(0);
427e5fc6
MD
562}
563
0729c8c8
MD
564/*
565 * Sync the filesystem. Currently we have to run it twice, the second
566 * one will advance the undo start index to the end index, so if a crash
567 * occurs no undos will be run on mount.
77062c8a
MD
568 *
569 * We do not sync the filesystem if we are called from a panic. If we did
570 * we might end up blowing up a sync that was already in progress.
0729c8c8 571 */
427e5fc6
MD
572static int
573hammer_vfs_sync(struct mount *mp, int waitfor)
574{
fbc6e32a 575 struct hammer_mount *hmp = (void *)mp->mnt_data;
0729c8c8
MD
576 int error;
577
77062c8a 578 if (panicstr == NULL) {
0729c8c8 579 error = hammer_sync_hmp(hmp, waitfor);
77062c8a
MD
580 if (error == 0)
581 error = hammer_sync_hmp(hmp, waitfor);
582 } else {
583 error = EIO;
584 hkprintf("S");
585 }
0729c8c8 586 return (error);
427e5fc6
MD
587}
588
513ca7d7
MD
589/*
590 * Convert a vnode to a file handle.
591 */
592static int
593hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp)
594{
595 hammer_inode_t ip;
596
597 KKASSERT(MAXFIDSZ >= 16);
598 ip = VTOI(vp);
599 fhp->fid_len = offsetof(struct fid, fid_data[16]);
600 fhp->fid_reserved = 0;
601 bcopy(&ip->obj_id, fhp->fid_data + 0, sizeof(ip->obj_id));
602 bcopy(&ip->obj_asof, fhp->fid_data + 8, sizeof(ip->obj_asof));
603 return(0);
604}
605
606
607/*
608 * Convert a file handle back to a vnode.
609 */
610static int
611hammer_vfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
612{
36f82b23 613 struct hammer_transaction trans;
513ca7d7
MD
614 struct hammer_inode *ip;
615 struct hammer_inode_info info;
616 int error;
617
618 bcopy(fhp->fid_data + 0, &info.obj_id, sizeof(info.obj_id));
619 bcopy(fhp->fid_data + 8, &info.obj_asof, sizeof(info.obj_asof));
620
36f82b23
MD
621 hammer_simple_transaction(&trans, (void *)mp->mnt_data);
622
513ca7d7
MD
623 /*
624 * Get/allocate the hammer_inode structure. The structure must be
625 * unlocked while we manipulate the related vnode to avoid a
626 * deadlock.
627 */
36f82b23
MD
628 ip = hammer_get_inode(&trans, NULL, info.obj_id, info.obj_asof,
629 0, &error);
513ca7d7
MD
630 if (ip == NULL) {
631 *vpp = NULL;
632 return(error);
633 }
e8599db1 634 error = hammer_get_vnode(ip, vpp);
513ca7d7 635 hammer_rel_inode(ip, 0);
b84de5af 636 hammer_done_transaction(&trans);
513ca7d7
MD
637 return (error);
638}
639
640static int
641hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
642 int *exflagsp, struct ucred **credanonp)
643{
644 hammer_mount_t hmp = (void *)mp->mnt_data;
645 struct netcred *np;
646 int error;
647
648 np = vfs_export_lookup(mp, &hmp->export, nam);
649 if (np) {
650 *exflagsp = np->netc_exflags;
651 *credanonp = &np->netc_anon;
652 error = 0;
653 } else {
654 error = EACCES;
655 }
656 return (error);
657
658}
659
660int
661hammer_vfs_export(struct mount *mp, int op, const struct export_args *export)
662{
663 hammer_mount_t hmp = (void *)mp->mnt_data;
664 int error;
665
666 switch(op) {
667 case MOUNTCTL_SET_EXPORT:
668 error = vfs_export(mp, &hmp->export, export);
669 break;
670 default:
671 error = EOPNOTSUPP;
672 break;
673 }
674 return(error);
675}
676